package com.jkcredit.invoice.service.lowerservice.impl;

import com.jkcredit.invoice.annotation.annotationdes.AuthenticationInterceptor;
import com.jkcredit.invoice.mapper.customer.CustomerRecMapper;
import com.jkcredit.invoice.model.entity.customer.CustomerRec;
import com.jkcredit.invoice.model.entity.user.User;
import com.jkcredit.invoice.service.lowerservice.CheckHasAuthRole;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 校验接口实现类
 * @author mumuxigua
 */
@Service("checkHasAuthRole")
public class CheckHasAuthRoleImpl implements CheckHasAuthRole {
    @Autowired
    CustomerRecMapper customerRecMapper;

    @Override
    public void checkCompanyRole(User user, String companyName) {
        if (AuthenticationInterceptor.AUTH_ADMIN.equals(user.getRoleId())) {
            return;
        }
        CustomerRec customerRecPa = new CustomerRec();
        customerRecPa.setCustomerName(user.getUserName());
        customerRecPa.setCompanyName(companyName);
        CustomerRec customerRec = customerRecMapper.selectByCustomerNameAndCompany(customerRecPa);
        if (customerRec == null) {
            throw new RuntimeException("非法的资源请求");
        }
    }

    @Override
    public void checkCustomerRole(User currentUser, String customerName) {
        if (AuthenticationInterceptor.AUTH_ADMIN.equals(currentUser.getRoleId())) {
            return;
        }
        if (!currentUser.getUserName().equals(customerName)) {
            throw new RuntimeException("非法的资源请求");
        }
    }

    @Override
    public boolean checkMessage(String... args) {
        for (String arg : args) {
            if (StringUtils.isBlank(arg)){
                return false;
            }
        }
        return true;
    }
}
