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 com.jkcredit.invoice.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

import static com.jkcredit.invoice.common.CommonConst.SELF_CAR;

/**
 * 无车操作接口实现类
 * @author mumuxigua
 */
@Service
@Slf4j
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 (SELF_CAR.equals(customerCarRec.getBusinessType()) && !StringUtils.isEmpty(customerCarRec.getCarNum())) {
            CustomerRec customerRec = new CustomerRec();
            customerRec.setCompanyName(customerCarRec.getCompanyName());
            customerRec.setCustomerName(customerCarRec.getCustomerName());
            customerRec = customerRecMapper.selectByCustomerNameAndCompany(customerRec);
            String carNums = customerCarRec.getCarNum();
            if (customerRec != null) {
                if(!StringUtil.isEmpty(carNums)){
                    String [] carNumArr = carNums.split(",");
                    for(int i =0;i<carNumArr.length;i++){
                        customerCarRec.setCarNum(carNumArr[i]);
                        try {
                            updateCustomerCar(customerCarRec,customerRec);
                        }catch (Exception e){
                            e.printStackTrace();
                            log.error("更新ETC失败：msg[{}]",e.getMessage());
                        }

                    }
                }
            }
            customerCarRec.setCarNumArr(carNums.split(","));
        }
        return customerCarRecMapper.selectAllByPage(page, customerCarRec);
    }
    public void updateCustomerCar(CustomerCarRec customerCarRec,CustomerRec customerRec){
        List<CustomerCarRec> customerCarRecs = customerCarRecMapper.selectAllEtcBySelfCarNum(customerCarRec.getCarNum());
        CardBindQueryListToBRequest bRequest = new CardBindQueryListToBRequest();
        bRequest.setPlateNum(customerCarRec.getCarNum());
        bRequest.setCompanyNum(customerRec.getCompanyNum());
        RespR<CardBindQueryListToBResponse> responseRespR = selfCarInterface.cardBindQueryListToB(bRequest);
        if (responseRespR.getCode() != 1) {
            List<CardBindQueryListToBModel> bModels = responseRespR.getData().getResult();
            if (bModels != null && bModels.size() > 0) {
                if (customerCarRecs != null && customerCarRecs.size() > 0) {
                    customerCarRecs.forEach(customerCarRec1 -> {
                        boolean has = false;
                        for (CardBindQueryListToBModel bModel : bModels) {
                            if (bModel.getCardId().equals(customerCarRec1.getEtcNum())) {
                                has = true;
                                customerCarRec1.setCompanyName(customerCarRec.getCompanyName());
                                if (customerCarRec1.getCompanyNum() != null && !customerCarRec1.getCompanyNum().equals(bModel.getCompanyNum())) {
                                    customerCarRec1.setSuccTime(DateUtil.getCurrentDateStr());
                                }
                                customerCarRec1.setCompanyNum(bModel.getCompanyNum());
                                customerCarRec1.setCarNum(customerCarRec.getCarNum());
                                customerCarRec1.setCarColor(bModel.getPlateColor() + "");
                                customerCarRec1.setRecStatus(1);
                                customerCarRecMapper.updateEtcByPrimaryKeySelective(customerCarRec1);
                            }
                        }
                        if (!has) {
                            customerCarRec1.setRecStatus(2);
                            customerCarRecMapper.updateEtcByPrimaryKeySelective(customerCarRec1);
                        }
                    });
                }
                for (CardBindQueryListToBModel bModel : bModels) {
                    boolean has = false;
                    if (customerCarRecs != null && customerCarRecs.size() > 0) {
                        for (CustomerCarRec customerCarRec1 : customerCarRecs) {

                            if (bModel.getCardId().equals(customerCarRec1.getEtcNum())) {
                                has = true;
                            }
                        }
                    }
                    if (!has) {
                        CustomerCarRec customerCarRecDb = new CustomerCarRec();
                        customerCarRecDb.setInterType(1);
                        customerCarRecDb.setCompanyNum(bModel.getCompanyNum());
                        customerCarRecDb.setCarNum(bModel.getPlateNum());
                        customerCarRecDb.setBusinessType("0");
                        customerCarRecDb.setCustomerName(customerCarRec.getCustomerName());
                        customerCarRecDb.setSuccTime(DateUtil.getCurrentDateStr());
                        customerCarRecDb.setCompanyName(customerRec.getCompanyName());
                        customerCarRecDb.setEtcNum(bModel.getCardId());
                        customerCarRecDb.setCarColor(bModel.getPlateColor() + "");
                        customerCarRecDb.setRecStatus(1);
                        customerCarRecMapper.insert(customerCarRecDb);
                    }
                }
            } else {
                //车牌下的卡都更新为注销
                if (customerCarRecs != null && customerCarRecs.size() > 0) {
                    customerCarRecs.forEach(customerCarRec1 -> {
                        customerCarRec1.setRecStatus(2);
                        customerCarRecMapper.updateEtcByPrimaryKeySelective(customerCarRec1);
                    });
                }
            }
        }
    }
}
