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

import cn.com.taiji.sdk.model.comm.protocol.tts.card.server.*;
import cn.com.taiji.sdk.model.comm.protocol.tts.invoice.server.*;
import cn.com.taiji.sdk.model.comm.protocol.tts.trade.service.CardTradeModel;
import cn.com.taiji.sdk.model.comm.protocol.tts.trade.service.CardTradeRequest;
import cn.com.taiji.sdk.model.comm.protocol.tts.trade.service.CardTradeResponse;
import com.jkcredit.invoice.common.CommonConstants;
import com.jkcredit.invoice.mapper.Binvoce.SelfCarApplMapper;
import com.jkcredit.invoice.mapper.Binvoce.SelfCarInvoiceMapper;
import com.jkcredit.invoice.mapper.calculateInfor.SelfCarCalculateInforMapper;
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.mapper.waybill.SellCarTradeMapper;
import com.jkcredit.invoice.model.entity.Calculate.SelfCarCalculateInfor;
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.model.entity.invoice.SelfCarAppl;
import com.jkcredit.invoice.model.entity.invoice.SelfCarInvoice;
import com.jkcredit.invoice.model.entity.waybill.SelfCarTrade;
import com.jkcredit.invoice.service.lowerService.SelfCarServiceL;
import com.jkcredit.invoice.service.lowerService.vo.*;
import com.jkcredit.invoice.service.upService.SelfCarInterface;
import com.jkcredit.invoice.util.DateUtil;
import com.jkcredit.invoice.util.RespR;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
public class SelfCarServiceLImpl implements SelfCarServiceL {
    @Autowired
    SelfCarInterface selfCarInterface;
    @Autowired
    CustomerCarRecMapper customerCarRecMapper;
    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    CustomerRecMapper customerRecMapper;
    @Autowired
    SellCarTradeMapper sellCarTradeMapper;
    @Autowired
    SelfCarApplMapper selfCarApplMapper;
    @Autowired
    SelfCarInvoiceMapper selfCarInvoiceMapper;
    @Autowired
    SelfCarCalculateInforMapper calculateInforMapper;
    @Override
    public RespR<List<CardTradeModel>> getTradeList(TradeRequestVo tradeRequestVo) {
        Customer customer = customerMapper.selectByCustomerName(tradeRequestVo.getCustomerName());
        if(customer == null || customer.getAccstatus().intValue() == 1){
            return new RespR(false,"客户已经停用");
        }
        if(StringUtils.isEmpty(tradeRequestVo.getStartTime()) || StringUtils.isEmpty(tradeRequestVo.getEndTime())){
            return new RespR(false,"时间为空");
        }
        if(DateUtil.getDistanceDays(tradeRequestVo.getEndTime(),tradeRequestVo.getStartTime())>90){
            return new RespR(false,"不能查询大于90天的交易");
        }
        CustomerRec customerRec = new CustomerRec();
        customerRec.setCustomerName(tradeRequestVo.getCustomerName());
        customerRec.setCompanyName(tradeRequestVo.getCompanyName());
        customerRec = customerRecMapper.selectByCustomerNameAndCompany(customerRec);
        if(customerRec == null || customerRec.getRecStatus().intValue() !=1 ){
            return new RespR(false,"客户未备案");
        }
        CustomerCarRec customerCarRec = customerCarRecMapper.selectByETC(tradeRequestVo.getEtcId());
        if(customerCarRec == null || customerCarRec.getRecStatus() != 1){
            return new RespR(false,"该etc卡未绑定或绑定类型错误");
        }
        CardTradeRequest cardTradeRequest = new CardTradeRequest();
        cardTradeRequest.setCardId(tradeRequestVo.getEtcId());
        cardTradeRequest.setCompanyNum(customerRec.getCompanyNum());
        cardTradeRequest.setStartExTime(DateUtil.dateFormate(tradeRequestVo.getStartTime()));
        cardTradeRequest.setEndExTime(DateUtil.dateFormate(tradeRequestVo.getEndTime()));
        cardTradeRequest.setTradeStatus(tradeRequestVo.getTradeStatus());
        RespR<CardTradeResponse> resp = selfCarInterface.cardTrade(cardTradeRequest);
        if(resp.getCode() == 1){
            return new RespR(false,"请求失败："+resp.getMsg());
        }else{
            CardTradeResponse cardTradeResponse = resp.getData();
            List<CardTradeModel> result  = cardTradeResponse.getResult();
            //对不存在的tradeId入表
            if(result!=null && result.size()>0){
                for(CardTradeModel cardTradeModel:result){
                    SelfCarTrade selfCarTrade =  sellCarTradeMapper.selectByTradeId(cardTradeModel.getTradeId());
                    if(selfCarTrade == null){
                        selfCarTrade = new SelfCarTrade();
                        selfCarTrade.setCustomId(customerRec.getCustomerName());
                        selfCarTrade.setCompanyName(customerRec.getCompanyName());
                        selfCarTrade.setCompanyReferencenum(customerRec.getCompanyReferencenum());
                        selfCarTrade.setCompanyNum(customerRec.getCompanyNum());
                        selfCarTrade.setCardId(cardTradeModel.getCardId());
                        selfCarTrade.setTradeId(cardTradeModel.getTradeId());
                        selfCarTrade.setExTime(DateUtil.tTimeToDate(cardTradeModel.getExTime()));
                        selfCarTrade.setFee(cardTradeModel.getFee());
                        selfCarTrade.setStatus(tradeRequestVo.getTradeStatus());
                        sellCarTradeMapper.insert(selfCarTrade);
                    }else{
                        if(selfCarTrade.getStatus()!=tradeRequestVo.getTradeStatus()){
                            selfCarTrade.setStatus(tradeRequestVo.getTradeStatus());
                            sellCarTradeMapper.updateByTradeId(selfCarTrade);
                        }
                    }
                };
            }
            return new RespR(result);
        }
    }

    @Override
    public RespR<String> applInvoice(InvoiceApplVo invoiceApplVo) {
        Customer customer = customerMapper.selectByCustomerName(invoiceApplVo.getCustomerName());
        if(customer == null || customer.getAccstatus().intValue() == 1){
            return new RespR(false,"客户已经停用");
        }
        CustomerRec customerRec = new CustomerRec();
        customerRec.setCustomerName(invoiceApplVo.getCustomerName());
        customerRec.setCompanyName(invoiceApplVo.getCompanyName());
        customerRec = customerRecMapper.selectByCustomerNameAndCompany(customerRec);
        if(customerRec == null || customerRec.getRecStatus().intValue() !=1 ){
            return new RespR(false,"客户未备案");
        }
        CustomerCarRec customerCarRec = customerCarRecMapper.selectByETC(invoiceApplVo.getCardId());
        if(customerCarRec == null || customerCarRec.getRecStatus() != 1){
            return new RespR(false,"该etc卡未绑定或绑定类型错误");
        }
        B2BInvoiceApplyRequest b2BInvoiceApplyRequest = new B2BInvoiceApplyRequest();
        b2BInvoiceApplyRequest.setCardId(invoiceApplVo.getCardId());
        b2BInvoiceApplyRequest.setCompanyNum(customerRec.getCompanyNum());
        List<B2BInvoiceApplyModel> b2BInvoiceApplyModels = new ArrayList<>();
        List<String> tradeIds = invoiceApplVo.getTradeIds();
        if(tradeIds == null || tradeIds.size()<=0){
            return new RespR(false,"没有交易Id");
        }
        for(String tradeId:tradeIds){
            B2BInvoiceApplyModel b2BInvoiceApplyModel = new B2BInvoiceApplyModel();
            b2BInvoiceApplyModel.setTradeId(tradeId);
            b2BInvoiceApplyModels.add(b2BInvoiceApplyModel);
        }
        b2BInvoiceApplyRequest.setTradeIdModel(b2BInvoiceApplyModels);
        RespR<B2BInvoiceApplyResponse> respR = selfCarInterface.b2BInvoiceApply(b2BInvoiceApplyRequest);
        if(respR.getCode()==1){
            return new RespR(false,"申请失败");
        }else{
            B2BInvoiceApplyResponse b2BInvoiceApplyResponse = respR.getData();
            SelfCarAppl selfCarAppl = new SelfCarAppl();
            selfCarAppl.setApplId(b2BInvoiceApplyResponse.getApplyId());
            selfCarAppl.setApplTime(b2BInvoiceApplyResponse.getReceiveTime());
            selfCarAppl.setCompanyNum(b2BInvoiceApplyRequest.getCompanyNum());
            selfCarAppl.setCustomId(customer.getCustomerName());
            selfCarAppl.setCardId(invoiceApplVo.getCardId());
            selfCarAppl.setInvoiceAmount(b2BInvoiceApplyResponse.getInvoiceAmount());
            selfCarAppl.setInvoiceCount(b2BInvoiceApplyResponse.getInvoiceCount());
            selfCarAppl.setInvoiceTaxAmount(b2BInvoiceApplyResponse.getInvoiceTaxAmount());
            selfCarApplMapper.insert(selfCarAppl);

            //更新交易id申请开票时间，并将转态改为开票中
            tradeIds.forEach(tradeId->{
                SelfCarTrade selfCarTrade =  sellCarTradeMapper.selectByTradeId(tradeId);
                selfCarTrade.setStatus(2);
                selfCarTrade.setAclTime(DateUtil.getCurrentDateStr());
                selfCarTrade.setApplId(b2BInvoiceApplyResponse.getApplyId());
                sellCarTradeMapper.updateByTradeId(selfCarTrade);
            });
            //SelfCarTrade selfCarTrade =  sellCarTradeMapper.selectByTradeId(cardTradeModel.getTradeId());
            return new RespR<>(b2BInvoiceApplyResponse.getApplyId());
        }

    }

    @Override
    @Transactional
    public RespR<List<SelfCarInvoice>> getSelfCarInvoicesByApplyIds(ApplQueryInvVo applQueryInvVo) {
        B2BInvoiceQueryByApplyRequest b2BInvoiceQueryRequest = new B2BInvoiceQueryByApplyRequest();
        Customer customer = customerMapper.selectByCustomerName(applQueryInvVo.getCustomerName());
        CustomerRec customerRec = new CustomerRec();
        customerRec.setCustomerName(applQueryInvVo.getCustomerName());
        customerRec.setCompanyName(applQueryInvVo.getCompanyName());
        customerRec = customerRecMapper.selectByCustomerNameAndCompany(customerRec);
        if(customerRec == null || customerRec.getRecStatus().intValue() !=1 ){
            return new RespR(false,"客户未备案");
        }
        b2BInvoiceQueryRequest.setCardId(applQueryInvVo.getEtcNum());
        b2BInvoiceQueryRequest.setCompanyNum(customerRec.getCompanyNum());
        b2BInvoiceQueryRequest.setApplyId(applQueryInvVo.getApplId());
        RespR<B2BInvoiceQueryByApplyResponse> responseRespR = selfCarInterface.b2bContractQuery(b2BInvoiceQueryRequest);
        if(responseRespR.getCode() == 1){
            return new RespR(false,responseRespR.getMsg());
        }else{
            B2BInvoiceQueryByApplyResponse response = responseRespR.getData();
            List<B2BInvoiceQueryByApplyModel> b2BInvoiceQueryByApplyModels = response.getResult();
            for(B2BInvoiceQueryByApplyModel b2BInvoiceQueryByApplyModel:b2BInvoiceQueryByApplyModels){
                SelfCarInvoice selfCarInvoice = new SelfCarInvoice();
                BeanUtils.copyProperties(b2BInvoiceQueryByApplyModel,selfCarInvoice);
                selfCarInvoice.setCompanyName(customerRec.getCompanyName());
                selfCarInvoice.setCompanyNum(customerRec.getCompanyNum());
                selfCarInvoice.setCustomerName(customerRec.getCustomerName());
                selfCarInvoice.setBuyerTaxpayerCode(customerRec.getCompanyReferencenum());
                selfCarInvoice.setExStation(b2BInvoiceQueryByApplyModel.getExStationName());
                selfCarInvoice.setEnStation(b2BInvoiceQueryByApplyModel.getEnStationName());
                selfCarInvoice.setInvoiceMakeTime(DateUtil.tTimeToDate(selfCarInvoice.getInvoiceMakeTime()));
                selfCarInvoice.setExTime(DateUtil.tTimeToDate(selfCarInvoice.getExTime()));
                if(selfCarInvoiceMapper.selectByTradeAndInvoiceMakeTime(selfCarInvoice)==null || selfCarInvoiceMapper.selectByTradeAndInvoiceMakeTime(selfCarInvoice).size()==0){
                    selfCarInvoiceMapper.insert(selfCarInvoice);
                }
                //计费
                SelfCarCalculateInfor carCalculateInfor = new SelfCarCalculateInfor();
                carCalculateInfor.setCustomId(customerRec.getCustomerName());
                carCalculateInfor.setCompanyNum(customerRec.getCompanyName());
                carCalculateInfor.setCalTime(StringUtils.substring(b2BInvoiceQueryByApplyModel.getExTime(),0,7));
                carCalculateInfor.setFee(customer.getFee());
                carCalculateInfor.setEtcNum(b2BInvoiceQueryByApplyModel.getCardId());
                if(calculateInforMapper.selectByEtcAndTime(carCalculateInfor) == null){
                    calculateInforMapper.insert(carCalculateInfor);
                }
            }
            //返回相应的发票
            return new RespR(b2BInvoiceQueryByApplyModels);
        }
    }

    @Override
    public RespR<List<SelfCarInvoice>> getSelfCarInvoicesByTime(SelfCarDueQueryVo selfCarDueQueryVo) {
        Customer customer = customerMapper.selectByCustomerName(selfCarDueQueryVo.getCustomername());
        if(customer == null || customer.getAccstatus().intValue() == 1){
            return new RespR(false,"客户已经停用");
        }
        CustomerRec customerRec = new CustomerRec();
        customerRec.setCustomerName(selfCarDueQueryVo.getCustomername());
        customerRec.setCompanyName(selfCarDueQueryVo.getCompanyName());
        customerRec = customerRecMapper.selectByCustomerNameAndCompany(customerRec);
        if(customerRec == null || customerRec.getRecStatus().intValue() !=1 ){
            return new RespR(false,"客户未备案");
        }
        CustomerCarRec customerCarRec = customerCarRecMapper.selectByETC(selfCarDueQueryVo.getCardId());
        if(customerCarRec == null  || customerCarRec.getRecStatus() != 1){
            return new RespR(false,"该etc卡未绑定或绑定类型错误");
        }
        B2BInvoiceQueryRequest b2BInvoiceQueryRequest = new B2BInvoiceQueryRequest();
        b2BInvoiceQueryRequest.setCompanyNum(customerRec.getCompanyNum());
        b2BInvoiceQueryRequest.setCardId(selfCarDueQueryVo.getCardId());
        b2BInvoiceQueryRequest.setStartInvoiceMakeTime(DateUtil.dateFormate(selfCarDueQueryVo.getStartTime()));
        b2BInvoiceQueryRequest.setEndInvoiceMakeTime(DateUtil.dateFormate(selfCarDueQueryVo.getEndTime()));

        RespR<B2BInvoiceQueryResponse> responseRespR = selfCarInterface.b2BInvoiceQuery(b2BInvoiceQueryRequest);
        if(responseRespR.getCode() == 1){
            return new RespR(false,responseRespR.getMsg());
        }else{
            B2BInvoiceQueryResponse b2BInvoiceQueryResponse = responseRespR.getData();
            List<B2BInvoiceListModel> b2BInvoiceListModels = b2BInvoiceQueryResponse.getResult();
            List<SelfCarInvoice> selfCarInvoices = new ArrayList<>();
            if(b2BInvoiceListModels!=null && b2BInvoiceListModels.size()>0){
                for(B2BInvoiceListModel b2BInvoiceListModel:b2BInvoiceListModels){
                    SelfCarInvoice selfCarInvoice = new SelfCarInvoice();
                    BeanUtils.copyProperties(b2BInvoiceListModel,selfCarInvoice);
                    selfCarInvoice.setInvoiceMakeTime(DateUtil.dateFormate(selfCarInvoice.getInvoiceMakeTime()));
                    selfCarInvoice.setExTime(DateUtil.dateFormate(selfCarInvoice.getExTime()));
                    if(selfCarInvoiceMapper.selectByTradeAndInvoiceMakeTime(selfCarInvoice)==null || selfCarInvoiceMapper.selectByTradeAndInvoiceMakeTime(selfCarInvoice).size()==0){
                        selfCarInvoiceMapper.insert(selfCarInvoice);
                    }

                    //计费
                    SelfCarCalculateInfor carCalculateInfor = new SelfCarCalculateInfor();
                    carCalculateInfor.setCustomId(customerRec.getCustomerName());
                    carCalculateInfor.setCompanyNum(customerRec.getCompanyName());
                    carCalculateInfor.setCalTime(StringUtils.substring(b2BInvoiceListModel.getExTime(),0,7));
                    carCalculateInfor.setFee(customer.getFee());
                    carCalculateInfor.setEtcNum(b2BInvoiceListModel.getCardId());
                    if(calculateInforMapper.selectByEtcAndTime(carCalculateInfor) == null){
                        //todo 扣减客户金额
                        calculateInforMapper.insert(carCalculateInfor);
                    }
                    selfCarInvoices.add(selfCarInvoice);
                }
            }
            return new RespR(b2BInvoiceListModels);
        }
    }

    @Override
    public RespR getEtcInfo(EtcQueryVo etcQueryVo) {
        if(etcQueryVo.getCarList()==null || etcQueryVo.getCarList().size()<=0){
            return new RespR(false,"车辆信息为空");
        }
        Customer customer = customerMapper.selectByCustomerName(etcQueryVo.getCustomerName());
        if(customer == null || customer.getAccstatus().intValue() == 1){
            return new RespR(false,"客户已经停用");
        }
        CustomerRec customerRec = new CustomerRec();
        customerRec.setCustomerName(etcQueryVo.getCustomerName());
        customerRec.setCompanyName(etcQueryVo.getCompanyName());
        customerRec = customerRecMapper.selectByCustomerNameAndCompany(customerRec);
        if(customerRec == null || customerRec.getRecStatus().intValue() !=1 ){
            return new RespR(false,"客户未备案");
        }
        CardQueryCardToBRequest cardQueryCardToBRequest = new CardQueryCardToBRequest();
        cardQueryCardToBRequest.setCompanyNum(customerRec.getCompanyNum());
        List<CardQueryCardToBModel> cardQueryCardToBModels = new ArrayList<>();
        etcQueryVo.getCarList().forEach(carVo -> {
            CardQueryCardToBModel cardQueryCardToBModel = new CardQueryCardToBModel();
            cardQueryCardToBModel.setPlateNum(carVo.getNum());
            cardQueryCardToBModel.setPlateColor(carVo.getColor());
            cardQueryCardToBModels.add(cardQueryCardToBModel);
        });
        cardQueryCardToBRequest.setVehicleList(cardQueryCardToBModels);
        RespR<CardQueryCardToBResponse> respR = selfCarInterface.cardQueryCardToB(cardQueryCardToBRequest);
        if(respR.getCode() == 1){
            return new RespR(false,respR.getMsg());
        }
        return new RespR(respR.getData().getResult());
    }

    @Override
    public RespR customerEtcRec(EtcBindVo etcBindVo) {
        if(etcBindVo== null || etcBindVo.getCards() ==null || etcBindVo.getCards().size()<=0){
            return new RespR(false,"无备案Etc");
        }
        StringBuffer res = new StringBuffer();
        Customer customer = customerMapper.selectByCustomerName(etcBindVo.getCustomerName());
        if(customer == null || customer.getAccstatus()!=0){
            res.append("#客户："+etcBindVo.getCustomerName()+"不存在或状态异常");
            return new RespR(false,res.toString());
        }
        CustomerRec customerRecP = new CustomerRec();
        customerRecP.setCustomerName(etcBindVo.getCustomerName());
        customerRecP.setCompanyName(etcBindVo.getCompanyName());
        CustomerRec customerRec = customerRecMapper.selectByCustomerNameAndCompany(customerRecP);
        if(customerRec == null || customerRec.getRecStatus()!=1){
            res.append("#客户："+etcBindVo.getCustomerName()+"企业:"+etcBindVo.getCompanyName()+"请先备案");
            return new RespR(false,res.toString());
        }
        return recEtc(etcBindVo,customerRec);
    }
    public RespR recEtc(EtcBindVo etcBindVo,CustomerRec customerRec){
        CardQueryCardToBRequest cardQueryCardToBRequest = new CardQueryCardToBRequest();
        cardQueryCardToBRequest.setCompanyNum(customerRec.getCompanyNum());
        List<CardQueryCardToBModel> cardQueryCardToBModels = new ArrayList<>();
        etcBindVo.getCards().forEach(carVo -> {
            CardQueryCardToBModel cardQueryCardToBModel = new CardQueryCardToBModel();
            cardQueryCardToBModel.setPlateNum(carVo.getNum());
            cardQueryCardToBModel.setPlateColor(carVo.getColor());
            cardQueryCardToBModels.add(cardQueryCardToBModel);
        });
        cardQueryCardToBRequest.setVehicleList(cardQueryCardToBModels);
        RespR<CardQueryCardToBResponse> respR = selfCarInterface.cardQueryCardToB(cardQueryCardToBRequest);
        if(respR.getCode() == 1){
            return new RespR(false,respR.getMsg());
        }
        if(respR.getData().getResult() == null){
            return new RespR("成功");
        }
        List<CustomerCarRec> customerCarRecs = new ArrayList<>();
        List<CardBindingToBModel> cardBindingToBModels = new ArrayList<>();
        for(int i=0;i<respR.getData().getResult().size();i++){
            CardQueryCardResultToBModel cardQueryCardResultToBModel = respR.getData().getResult().get(i);
            CarVo carVo = etcBindVo.getCards().get(i);
            CustomerCarRec customerCarRec = new CustomerCarRec();
            customerCarRec.setBusinessType(CommonConstants.BUSSINESS_TYPE_SELFCAR);
            customerCarRec.setCompanyName(customerRec.getCompanyName());
            customerCarRec.setCustomerName(customerRec.getCustomerName());
            customerCarRec.setCompanyNum(customerRec.getCompanyNum());
            customerCarRec.setCustPhone(etcBindVo.getMobile());
            customerCarRec.setRecStatus(CommonConstants.REC_STATUS_WAIT);
            customerCarRec.setCarNum(carVo.getNum());
            customerCarRec.setEtcNum(carVo.getEtcNum());
            customerCarRec.setCarColor(carVo.getColor()+"");
            customerCarRecs.add(customerCarRec);
            CardBindingToBModel cardBindingToBModel = new CardBindingToBModel();
            cardBindingToBModel.setCardId(cardQueryCardResultToBModel.getId());
            cardBindingToBModels.add(cardBindingToBModel);
        }
        CardBindingToBRequest cardBindingToBRequest = new CardBindingToBRequest();
        cardBindingToBRequest.setMobile(etcBindVo.getMobile());
        cardBindingToBRequest.setCompanyNum(customerRec.getCompanyNum());
        cardBindingToBRequest.setCardIdList(cardBindingToBModels);
        RespR<CardBindingToBResponse> respR1 = selfCarInterface.cardBindingToB(cardBindingToBRequest);
        if(respR1.getCode() == 1){
            /**
             * etc入库
             */
            customerCarRecs.forEach(customerCarRec -> {
                customerCarRec.setRecStatus(CommonConstants.REC_STATUS_FAIL);
                customerCarRec.setFailTime(DateUtil.getCurrentDateStr());
                customerCarRec.setFailReason(respR1.getMsg());
                updateETCRec(customerCarRec);
            });
            return new RespR(false,"注册失败："+respR.getMsg());
        }else{
            /**
             * etc入库
             */
            customerCarRecs.forEach(customerCarRec -> {
                customerCarRec.setRecStatus(CommonConstants.REC_STATUS_WAIT);
                updateETCRec(customerCarRec);
            });
            return new RespR("已经发送绑定请求，请发送收到的验证码");
        }
    }

    public void updateETCRec(CustomerCarRec customerCarRec){
        //根据车牌号查询是新增还是更新
        CustomerCarRec customerCarRec1 = customerCarRecMapper.selectByETC(customerCarRec.getEtcNum());
        if (customerCarRec1 == null) {
            customerCarRecMapper.insert(customerCarRec);
        } else {
            customerCarRecMapper.updateETCByPrimaryKey(customerCarRec);
        }
    }
    @Override
    public RespR customerEtcRecValid(EtcValidVo etcValidVo) {
        StringBuffer res = new StringBuffer();
        Customer customer = customerMapper.selectByCustomerName(etcValidVo.getCustomerName());
        if(customer == null || customer.getAccstatus()!=0){
            res.append("#客户："+etcValidVo.getCustomerName()+"不存在或状态异常");
            return new RespR(false,res.toString());
        }
        CustomerRec customerRecP = new CustomerRec();
        customerRecP.setCustomerName(etcValidVo.getCustomerName());
        customerRecP.setCompanyName(etcValidVo.getCompanyName());
        CustomerRec customerRec = customerRecMapper.selectByCustomerNameAndCompany(customerRecP);
        if(customerRec == null || customerRec.getRecStatus()!=1){
            res.append("#客户："+etcValidVo.getCustomerName()+"企业:"+etcValidVo.getCompanyName()+"请先备案");
            return new RespR(false,res.toString());
        }
        CardValidCodeToBRequest cardValidCodeToBRequest = new CardValidCodeToBRequest();
        cardValidCodeToBRequest.setCompanyNum(customerRec.getCompanyNum());
        cardValidCodeToBRequest.setMobile(etcValidVo.getMobile());
        cardValidCodeToBRequest.setValidCode(etcValidVo.getValidCode());
        RespR<CardValidCodeToBResponse> respR = selfCarInterface.cardValidCodeToB(cardValidCodeToBRequest);
        CustomerCarRec customerCarRec;
        if(respR.getCode() == 1){
            customerCarRec = new CustomerCarRec();
            customerCarRec.setRecStatus(CommonConstants.REC_STATUS_FAIL);
            customerCarRec.setFailReason(respR.getMsg());
            customerCarRec.setFailTime(DateUtil.getCurrentDateStr());
            customerCarRec.setCompanyNum(customerRec.getCompanyNum());
            customerCarRec.setCustPhone(etcValidVo.getMobile());
            customerCarRecMapper.updateWaitETCStatus(customerCarRec);
            return new RespR(false,"验证失败");
        }else{
            customerCarRec = new CustomerCarRec();
            customerCarRec.setRecStatus(CommonConstants.REC_STATUS_SUC);
            customerCarRec.setSuccTime(DateUtil.getCurrentDateStr());
            customerCarRec.setCompanyNum(customerRec.getCompanyNum());
            customerCarRec.setCustPhone(etcValidVo.getMobile());
            customerCarRecMapper.updateWaitETCStatus(customerCarRec);
            return new RespR(false,"校验成功");
        }
    }
    @Override
    public RespR customerCarUnRec(List<CustomerCarRec> customerCarRecs) {
        if(customerCarRecs==null || customerCarRecs.size()<=0){
            return new RespR(false,"无解绑信息");
        }

        StringBuffer res = new StringBuffer();
        try {
            for(CustomerCarRec customerCarRec:customerCarRecs){
                CustomerRec customerRecP = new CustomerRec();
                customerRecP.setCustomerName(customerCarRec.getCustomerName());
                customerRecP.setCompanyName(customerCarRec.getCompanyName());
                CustomerRec customerRec = customerRecMapper.selectByCustomerNameAndCompany(customerRecP);
                if("0".equals(customerRec.getBussinessType())){
                    CardUnbindToBRequest cardUnbindToBRequest = new CardUnbindToBRequest();
                    cardUnbindToBRequest.setCardId(customerCarRec.getEtcNum());
                    cardUnbindToBRequest.setCompanyNum(customerRec.getCompanyNum());
                    RespR<CardUnbindToBResponse> respR = selfCarInterface.cardUnbindToB(cardUnbindToBRequest);
                    if(respR.getCode() == 1){
                        res.append(customerCarRec.getEtcNum()+","+customerCarRec.getCarNum()+"解绑失败;"+"#");
                    }else{
                        customerCarRec.setRecStatus(2);
                        customerCarRecMapper.updateByPrimaryKeySelective(customerCarRec);
                    }
                }
            }
            if(org.springframework.util.StringUtils.isEmpty(res.toString())){
                return new RespR("解绑成功");
            }else{
                return new RespR(false,res.toString());
            }

        }catch (Exception e){
            return new RespR(false,e.getMessage());
        }

    }
}
