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

import cn.com.taiji.sdk.model.comm.protocol.eoms.company.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jkcredit.invoice.mapper.customer.CustomerMapper;
import com.jkcredit.invoice.mapper.customer.CustomerRechargeMapper;
import com.jkcredit.invoice.mapper.customer.CustomerRecMapper;
import com.jkcredit.invoice.model.entity.customer.Customer;
import com.jkcredit.invoice.model.entity.CustomerRecharge;
import com.jkcredit.invoice.model.entity.customer.CustomerRec;
import com.jkcredit.invoice.service.customer.CustomerService;
import com.jkcredit.invoice.service.upService.CommInterFace;
import com.jkcredit.invoice.util.DateUtil;
import com.jkcredit.invoice.util.MathUtil;
import com.jkcredit.invoice.util.RespR;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("customerService")
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper,Customer> implements CustomerService{
    @Autowired
    CustomerMapper customerMapper;

    @Autowired
    CustomerRechargeMapper customerRechargeMapper;

    @Autowired
    CommInterFace commInterFace;

    @Autowired
    CustomerRecMapper customerRecMapper;
    @Override
    public IPage findAllCustomers(Page page, Customer customer) {
        return baseMapper.selectAllByPage(page,customer);
    }

    @Override
    public Boolean addCustomers(Customer customer) {
        customer.setFirstSign(DateUtil.getCurrentDateStr());
        if(customerMapper.selectByCustomerName(customer.getCustomerName())!=null){
            return false;
        };
        return customerMapper.insert(customer)>0;
    }

    @Override
    public Boolean updateCustomer(Customer customer) {
       return  customerMapper.updateByPrimaryKeySelective(customer)>0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean customRecharge(CustomerRecharge customerRecharge) {
        Customer customer = customerMapper.selectByCustomerName(customerRecharge.getCustomerName());
        customer.setAccountBalance(MathUtil.add(customer.getAccountBalance(),customerRecharge.getRechargeMony()));
        customerMapper.updateByPrimaryKeySelective(customer);
        customerRecharge.setRechargeTime(DateUtil.getCurrentDateStr());
        customerRechargeMapper.insert(customerRecharge);
        return true;
    }
    public RespR contractAdd(CustomerRec customerRec){
        customerRec = customerRecMapper.selectByCustomerNameAndCompanyConcat(customerRec);
        if(customerRec.getRecStatus().intValue() == 2){
            return new RespR(false,"无法上传协议，未注册");
        }
        /*if(customerRec.getContractSuc().intValue() == 1){
            return new RespR(false,"协议已经上传");
        }*/
        B2bContractAddRequest b2bContractAddRequest = new B2bContractAddRequest();
        b2bContractAddRequest.setCompanyNum(customerRec.getCompanyNum());
        b2bContractAddRequest.setServiceStartTime(DateUtil.dateToTime(customerRec.getServiceStartTime()));
        b2bContractAddRequest.setServiceEndTime(DateUtil.dateToTime(customerRec.getServiceEndTime()));
        b2bContractAddRequest.setServiceType(customerRec.getServiceType());
        b2bContractAddRequest.setContractFileName(customerRec.getContractFileName());
        b2bContractAddRequest.setBase64Str(customerRec.getBase64Str());
        RespR<B2bContractAddResponse> responseRespR = commInterFace.b2bContractAdd(b2bContractAddRequest);
        if(responseRespR.getCode() == 1){
            return new RespR(false,responseRespR.getMsg());
        }else{
            customerRec.setContractSuc(1);
            customerRec.setContrantNum(responseRespR.getData().getContrantNum());
            customerRecMapper.updateByPrimaryKeySelective(customerRec);
            return new RespR("协议上传成功");
        }

    }

    @Override
    public RespR contractDownload(String id) {
        CustomerRec customerRec = customerRecMapper.selectByPrimaryKeyHasBase64(Integer.valueOf(id));
        return new RespR(customerRec);
    }

    public RespR customeRec(CustomerRec customerRec){
        customerRec = customerRecMapper.selectByCustomerNameAndCompanyConcat(customerRec);
        if(customerRec.getRecStatus().intValue() == 1 && customerRec.getContractSuc() == 1){
            return new RespR(false,"已经注册过了");
        }
        /*if(customerRec.getRecStatus().intValue() == 1){
            return contractAdd(customerRec);
        }*/
        CompanyAddRequest companyAddRequest = buildCompany(customerRec);
        //注册企业
        RespR<CompanyAddResponse> respR = commInterFace.addCompany(companyAddRequest);
        if(respR.getCode() == 1){
            customerRec.setRecStatus(2);
            customerRecMapper.updateByPrimaryKeySelective(customerRec);
            return new RespR(false,respR.getMsg());
        }
        CompanyAddResponse response = respR.getData();
        //注册协议
        String companyNum = response.getCompanyNum();

        customerRec.setRecStatus(1);
        customerRec.setCompanyNum(companyNum);
        customerRecMapper.updateByPrimaryKeySelective(customerRec);
        //contractAdd(customerRec);
        return new RespR(true);
    };

    private CompanyAddRequest buildCompany(CustomerRec customerRec){
        CompanyAddRequest companyAddRequest = new CompanyAddRequest();
        companyAddRequest.setName(customerRec.getCompanyName());
        companyAddRequest.setTaxpayerCode(customerRec.getCompanyReferencenum());
        companyAddRequest.setCompanyType(customerRec.getCompanyType());
        companyAddRequest.setOperatingRangeType(customerRec.getOperatingRangeType());
        companyAddRequest.setContact(customerRec.getCompanyLeader());
        companyAddRequest.setTel(customerRec.getCompanyLeaderPhone());
        companyAddRequest.setBuyerName(customerRec.getCompanyName());
        companyAddRequest.setBuyerTaxpayerCode(customerRec.getCompanyReferencenum());
        companyAddRequest.setBuyerAddr(customerRec.getCompanyAdress());
        companyAddRequest.setBuyerTel(customerRec.getCompanyPhone());
        companyAddRequest.setBuyerBank(customerRec.getCompanyOpenbank());
        companyAddRequest.setBuyerBankAccount(customerRec.getCompanyOpenbankAcc());
        companyAddRequest.setEmergencyTel(customerRec.getCompanyLeaderPhone());
        companyAddRequest.setEmergencyContact(customerRec.getCompanyLeader());
        companyAddRequest.setCompanyType(customerRec.getCompanyType());
        return  companyAddRequest;
    };

    @Override
    public RespR customerRecQuery(CustomerRec customerRec) {
        CompanyQueryRequest companyQueryRequest = new CompanyQueryRequest();
        companyQueryRequest.setCompanyName(customerRec.getCompanyName());
        companyQueryRequest.setTaxpaterCode(customerRec.getCompanyReferencenum());
        RespR<CompanyQueryResponse> responseRespR = commInterFace.companyQuery(companyQueryRequest);
        if(responseRespR.getCode() == 1){
            return new RespR(false,responseRespR.getMsg());
        }else{
            return new RespR(responseRespR.getData().getItems());
        }
    }

    @Override
    public RespR updateCustomerRecStatus(CustomerRec customerRec) {
        CustomerRec customerRec1 = customerRecMapper.selectByCustomerNameAndCompany(customerRec);
        customerRec1.setRecStatus(customerRec.getRecStatus());
        int i = customerRecMapper.updateByPrimaryKey(customerRec1);
        if(i<=0){
            return new RespR(false);
        }
        return new RespR(true);
    }
}
