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

import cn.com.taiji.sdk.model.comm.protocol.tts.card.server.CardBindQueryListToBModel;
import cn.com.taiji.sdk.model.comm.protocol.tts.card.server.CardBindQueryListToBRequest;
import cn.com.taiji.sdk.model.comm.protocol.tts.card.server.CardBindQueryListToBResponse;
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.CustomerCarRecMapper;
import com.jkcredit.invoice.mapper.customer.CustomerRecMapper;
import com.jkcredit.invoice.model.entity.customer.CustomerCarRec;
import com.jkcredit.invoice.model.entity.customer.CustomerRec;
import com.jkcredit.invoice.service.nocar.NoCarRecService;
import com.jkcredit.invoice.service.upService.SelfCarInterface;
import com.jkcredit.invoice.util.DateUtil;
import com.jkcredit.invoice.util.RespR;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
@Service
public class NoCarRecServiceImpl extends ServiceImpl<CustomerCarRecMapper,CustomerCarRec> implements NoCarRecService {
    @Autowired
    CustomerCarRecMapper customerCarRecMapper;
    @Autowired
    CustomerRecMapper customerRecMapper;
    @Autowired
    SelfCarInterface selfCarInterface;
    @Override
    public IPage<List<CustomerCarRec>> findByPageAndCarRec(Page page, CustomerCarRec customerCarRec) {
        if("0".equals(customerCarRec.getBusinessType()) && !StringUtils.isEmpty(customerCarRec.getCarNum())){
            CustomerRec customerRec = new CustomerRec();
            customerRec.setCompanyName(customerCarRec.getCompanyName());
            customerRec.setCustomerName(customerCarRec.getCustomerName());
            customerRec = customerRecMapper.selectByCustomerNameAndCompany(customerRec);
            if(customerRec != null){
                List<CustomerCarRec> customerCarRecs = customerCarRecMapper.selectAllEtcBySelfCarNum(customerCarRec.getCarNum());
                CardBindQueryListToBRequest cardBindQueryListToBRequest = new CardBindQueryListToBRequest();
                cardBindQueryListToBRequest.setPlateNum(customerCarRec.getCarNum());
                cardBindQueryListToBRequest.setCompanyNum(customerRec.getCompanyNum());
                RespR<CardBindQueryListToBResponse> responseRespR = selfCarInterface.cardBindQueryListToB(cardBindQueryListToBRequest);
                if(responseRespR.getCode() !=1){
                    List<CardBindQueryListToBModel> cardBindQueryListToBResponses = responseRespR.getData().getResult();
                    if(cardBindQueryListToBResponses!=null && cardBindQueryListToBResponses.size()>0){
                        if(customerCarRecs!=null && customerCarRecs.size()>0){
                            customerCarRecs.forEach(customerCarRec1 -> {
                                boolean has = false;
                                for(CardBindQueryListToBModel cardBindQueryListToBModel:cardBindQueryListToBResponses){
                                    if(cardBindQueryListToBModel.getCardId().equals(customerCarRec1.getEtcNum())){
                                        has = true;
                                    }
                                }
                                if(!has){
                                    customerCarRec1.setRecStatus(2);
                                    customerCarRecMapper.updateETCByPrimaryKeySelective(customerCarRec1);
                                }else{
                                    customerCarRec1.setRecStatus(1);
                                    customerCarRecMapper.updateETCByPrimaryKeySelective(customerCarRec1);
                                }
                            });
                        }

                        for(CardBindQueryListToBModel cardBindQueryListToBModel:cardBindQueryListToBResponses){
                            boolean has = false;
                           for(CustomerCarRec customerCarRec1:customerCarRecs){

                               if(cardBindQueryListToBModel.getCardId().equals(customerCarRec1.getEtcNum())){
                                   has = true;
                               }
                           }
                           if(!has){
                               CustomerCarRec customerCarRecDb = new CustomerCarRec();
                               customerCarRecDb.setInterType(1);
                               customerCarRecDb.setCompanyNum(cardBindQueryListToBModel.getCompanyNum());
                               customerCarRecDb.setCarNum(cardBindQueryListToBModel.getPlateNum());
                               customerCarRecDb.setBusinessType("0");
                               customerCarRecDb.setCustomerName(customerCarRec.getCustomerName());
                               customerCarRecDb.setSuccTime(DateUtil.getCurrentDateStr());
                               customerCarRecDb.setCompanyName(customerRec.getCompanyName());
                               customerCarRecDb.setEtcNum(cardBindQueryListToBModel.getCardId());
                               customerCarRecDb.setCarColor(cardBindQueryListToBModel.getPlateColor()+"");
                               customerCarRecDb.setRecStatus(1);
                               customerCarRecMapper.insert(customerCarRecDb);
                           }

                        }
                    }

            }

            }
        }
        return customerCarRecMapper.selectAllByPage(page,customerCarRec);
    }
}
