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

import cn.com.taiji.sdk.model.comm.protocol.eoms.company.B2bCompanyModel;
import cn.com.taiji.sdk.model.comm.protocol.tts.card.server.*;
import cn.com.taiji.sdk.model.comm.protocol.tts.vehicle.VehicleRegisterRequest;
import cn.com.taiji.sdk.model.comm.protocol.tts.vehicle.VehicleRegisterResponse;
import com.jkcredit.invoice.common.CommonConstants;
import com.jkcredit.invoice.component.SendMailService;
import com.jkcredit.invoice.mapper.customer.CustomerCarRecMapper;
import com.jkcredit.invoice.mapper.customer.CustomerMapper;
import com.jkcredit.invoice.mapper.customer.CustomerRecMapper;
import com.jkcredit.invoice.model.entity.customer.Customer;
import com.jkcredit.invoice.model.entity.customer.CustomerCarRec;
import com.jkcredit.invoice.model.entity.customer.CustomerRec;
import com.jkcredit.invoice.service.customer.CustomerService;
import com.jkcredit.invoice.service.lowerService.CustomeLowerService;
import com.jkcredit.invoice.service.lowerService.vo.EtcBindVo;
import com.jkcredit.invoice.service.lowerService.vo.EtcValidVo;
import com.jkcredit.invoice.service.manager.ParamService;
import com.jkcredit.invoice.service.upService.NoCarInterface;
import com.jkcredit.invoice.service.upService.SelfCarInterface;
import com.jkcredit.invoice.util.DateUtil;
import com.jkcredit.invoice.util.RespR;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service("lowerService")
public class CustomerLowerServiceImpl implements CustomeLowerService {
    @Autowired
    CustomerMapper customerMapper;

    @Autowired
    CustomerRecMapper customerRecMapper;

    @Autowired
    CustomerCarRecMapper customerCarRecMapper;

    @Autowired
    NoCarInterface noCarInterface;

    @Autowired
    SelfCarInterface selfCarInterface;
    @Autowired
    CustomerService customerService;

    @Autowired
    SendMailService sendMailService;

    @Autowired
    ParamService paramService;

    private String mailTemp = "<style class=\"fox_global_style\">div.fox_html_content { line-height: 1.5; }div.fox_html_content { font-size: 10.5pt; font-family: 'Microsoft YaHei UI'; color: rgb(0, 0, 0); line-height: 1.5; }</style> <div><span id=\"_FoxCURSOR\"></span><br></div> <div>您好</div><div>&nbsp;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 当前有客户 ${a},主体 ${b} ,录入了${c}的企业注册信息，请尽快处理</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 谢谢</div><div><br></div><div><br></div><div><br></div><div><span id=\"_FoxFROMNAME\"><div style=\"MARGIN: 10px; FONT-FAMILY: verdana; FONT-SIZE: 10pt\"><br></div></span></div>";
    @Override
    public RespR customeRec(List<CustomerRec> customerRecs) {
        log.info("客户备案信息：CustomerLowerServiceImpl.customeRec{}",customerRecs);
        if(customerRecs==null || customerRecs.size()<=0){return new RespR(false,"备案信息为空");};
        StringBuffer res = new StringBuffer();
        customerRecs.stream().forEach(customerRec -> {
            Customer customer = customerMapper.selectByCustomerName(customerRec.getCustomerName());
            if(customer == null || customer.getAccstatus()!=0){
                res.append("#"+customerRec.getCustomerName()+"不存在或已经停用;#");
                return;
            }
            if(!customer.getBussinessType().equals(Integer.parseInt(customerRec.getBussinessType()))){
                res.append("#"+customerRec.getCompanyName()+"业务类型有误;#");
                return;
            }
            if(StringUtils.isEmpty(customerRec.getCompanyName()) || StringUtils.isEmpty(customerRec.getCompanyReferencenum())){
                res.append("企业备案名称或税号为空");
                return;
            }
            //默认备案状态为备案中 2-失败 1-成功 3-等待协议上传 4-停用 0-备案中
            customerRec.setRecStatus(0);
            //主动查询上游接口，如果已经备案则更新
            RespR respR = customerService.customerRecQuery(customerRec);
            if(respR.getCode() == 0){
               List<B2bCompanyModel> b2bCompanyModels = (List<B2bCompanyModel>)respR.getData();
               if(b2bCompanyModels.size()>=1){
                   b2bCompanyModels.forEach(b2bCompanyModel->{
                       if(b2bCompanyModel.getCompanyType().equals(customerRec.getCompanyType())){
                           customerRec.setCompanyNum(b2bCompanyModel.getCompanyNum());
                           customerRec.setCompanyType(b2bCompanyModel.getCompanyType());
                           customerRec.setRecStatus(1);
                       }
                   });
               }else{
                   log.info("AAAAAAAAAAA"+customerRec.getCompanyName()+b2bCompanyModels.size());
               }
            }
            //客户备案信息入表
            CustomerRec customerRec1 = customerRecMapper.selectByCustomerNameAndCompany(customerRec);
            customerRec.setCreatetime(DateUtil.getCurrentDateStr());
            customerRec.setCompanyBelongName(customer.getCompany());
            if(customerRec1 == null){
                customerRecMapper.insert(customerRec);
            }else{
                customerRec.setId(customerRec1.getId());
                customerRecMapper.updateByPrimaryKey(customerRec);
            }


            try{
                String mail = paramService.getParamsByParamName("MAILTO").getParamValue();
                sendMailService.sendMail(mail,
                        mailTemp.replace("${a}",customer.getCustomerName())
                                .replace("${b}",customer.getCompany())
                                 .replace("${c}",customerRec.getCompanyName()),customerRec.getCompanyName());
            }catch (Exception e){
                e.printStackTrace();
            }

        });
        if(StringUtils.isEmpty(res.toString())){
            log.info("客户备案成功：CustomerLowerServiceImpl.customeRec{}",customerRecs);
            return new RespR(true,"成功");
        }else{
            log.info("客户备案失败信息：CustomerLowerServiceImpl.customeRec{},msg{}",customerRecs,res.toString());
            return new RespR(false,res.toString());
        }

    }

    @Override
    public RespR customeRecQueryList(CustomerRec customerRec) {
       /* List<CustomerRec> customerRecs = customerRecMapper.selectByCustomerNameAll(customerRec.getCustomerName());
        if(customerRec.getBussinessType()!=null){
            List<CustomerRec> customerRecsN = new ArrayList<>();
            if(customerRecs!=null && customerRecs.size()>0){
                customerRecs.forEach(customerRec1 -> {
                    if(customerRec.getBussinessType().equals(customerRec1.getBussinessType())){
                        if(customerRec1.getRecStatus() == 1)
                        customerRecsN.add(customerRec1);
                    }
                });
            }
            return new RespR(customerRecsN); TODO
        }*/

        return new RespR(customerRecMapper.selectByCustomerNameCpAll(customerRec));
    }
    @Override
    public RespR customeRecQuery(CustomerRec customerRec) {

        return new RespR(customerRecMapper.selectByCustomerNameAndCompany(customerRec));
    }





    @Override
    public RespR customeRecUpload(List<CustomerRec> customerRecs) {
        log.info("客户协议上传信息：CustomerLowerServiceImpl.customeRecUpload{}",customerRecs);
        if(customerRecs==null || customerRecs.size()<=0){return new RespR(false,"协议上传信息为空");};
        StringBuffer res = new StringBuffer();
        customerRecs.stream().forEach(customerRec -> {
            Customer customer = customerMapper.selectByCustomerName(customerRec.getCustomerName());
            if(customer == null || customer.getAccstatus()!=0){
                res.append("#"+customerRec.getCustomerName()+"不存在或已经停用;#");
                return;
            }

            //客户备案信息入表
            CustomerRec customerRec1 = customerRecMapper.selectByCustomerNameAndCompanyNum(customerRec);
            customerRec.setId(customerRec1.getId());
            customerRecMapper.updateByPrimaryKeySelective(customerRec);

        });
        if(StringUtils.isEmpty(res.toString())){
            log.info("客户协议上传成功信息" + "：CustomerLowerServiceImpl.customeRecUpload{}",customerRecs);
            return new RespR(true,"协议上传成功");
        }else{
            log.info("客户协议上传失败信息：CustomerLowerServiceImpl.customeRecUpload{},msg{}",customerRecs,res.toString());
            return new RespR(false,res.toString());
        }

    }
}
