package com.ruoyi.dk.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.dk.domain.Borrow;
import com.ruoyi.dk.domain.Customer;
import com.ruoyi.dk.domain.CustomerInfo;
import com.ruoyi.dk.mapper.CustomerInfoMapper;
import com.ruoyi.dk.service.BorrowService;
import com.ruoyi.dk.service.CustomerInfoService;
import com.ruoyi.dk.service.CustomerService;
import com.ruoyi.component.core.exception.CustomException;
import com.ruoyi.component.core.util.MessageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfo> implements CustomerInfoService {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private BorrowService borrowService;
    @Override
    public void updateCustomerInfo(CustomerInfo customerInfo) {
        Long customerId = customerInfo.getCustomerId();
        long count = borrowService.count(Wrappers.lambdaQuery(Borrow.class).eq(Borrow::getCustomerId, customerId));
        if(count > 0){
            throw new CustomException(MessageUtils.message("dk.customer.update.having.loans"));
        }
        this.update(customerInfo,Wrappers.lambdaQuery(CustomerInfo.class)
                .eq(CustomerInfo::getCustomerId, customerId));
        CustomerInfo one = this.getOne(Wrappers.lambdaQuery(CustomerInfo.class)
                .eq(CustomerInfo::getCustomerId, customerId));
        checkCustomerInfoFlag(one);
    }

    private void checkCustomerInfoFlag(CustomerInfo one){
        Long customerId = one.getCustomerId();
        int realNameAuth = 0;
        boolean infoFlag = false;
        boolean cardFlag = false;
        boolean bankFlag = false;
        boolean signFlag = false;
        // 身份信息
        if(StringUtils.isNotEmpty(one.getRealName()) && StringUtils.isNotEmpty(one.getCardNum()) &&
                StringUtils.isNotEmpty(one.getCardFrontPicture()) && StringUtils.isNotEmpty(one.getCardBackPicture()) &&
                StringUtils.isNotEmpty(one.getHandCardPicture())){
            cardFlag = true;
        }
        // 资料信息
        if(StringUtils.isNotEmpty(one.getCompanyName()) && StringUtils.isNotEmpty(one.getCompanyTitle()) &&
                StringUtils.isNotEmpty(one.getCompanyPhone()) && StringUtils.isNotEmpty(one.getCompanyYear()) &&
                StringUtils.isNotEmpty(one.getCompanyAddress()) && StringUtils.isNotEmpty(one.getCompanyAddressInfo()) &&
                StringUtils.isNotEmpty(one.getKinsfolkName()) && StringUtils.isNotEmpty(one.getKinsfolkRef()) &&
                StringUtils.isNotEmpty(one.getKinsfolkPhone()) && one.getIncomeWan() != null){
            infoFlag = true;
        }
        // 收款银行卡
        if(StringUtils.isNotEmpty(one.getBankType()) && StringUtils.isNotEmpty(one.getBackCardNum())){
            bankFlag = true;
        }
        if(!one.getAllowSignature()){
            signFlag = true;
        } else {
            signFlag = StringUtils.isNotEmpty(one.getSignature());
        }
        if(infoFlag && cardFlag && bankFlag && signFlag){
            realNameAuth = 1;
        }
        this.update(Wrappers.lambdaUpdate(CustomerInfo.class)
                .eq(CustomerInfo::getId, customerId)
                .set(CustomerInfo::getInfoFlag,infoFlag)
                .set(CustomerInfo::getCardFlag,cardFlag)
                .set(CustomerInfo::getBankFlag,bankFlag));
        customerService.update(Wrappers.lambdaUpdate(Customer.class)
                .eq(Customer::getId, customerId)
                .set(Customer::getRealNameAuth,realNameAuth));
    }

    @Override
    public CustomerInfo getByCustomerId(Long customerId) {
        return this.getOne(Wrappers.lambdaQuery(CustomerInfo.class)
                .eq(CustomerInfo::getCustomerId,customerId));
    }

    @Override
    public boolean updateAllowSignature(CustomerInfo bo) {
        this.update(Wrappers.lambdaUpdate(CustomerInfo.class)
                .eq(CustomerInfo::getCustomerId,bo.getCustomerId())
                .set(CustomerInfo::getAllowSignature,bo.getAllowSignature()));
        CustomerInfo customerInfo = this.getByCustomerId(bo.getCustomerId());
        checkCustomerInfoFlag(customerInfo);
        return false;
    }
}
