package com.cusc.nirvana.user.rnr.fp.service.impl;

import com.cusc.nirvana.common.result.Response;
import com.cusc.nirvana.user.rnr.fp.dto.*;
import com.cusc.nirvana.user.rnr.fp.service.ISimVehicleRelService;
import com.cusc.nirvana.user.rnr.fp.service.IVinCardService;
import com.cusc.nirvana.user.rnr.project.context.VinCardContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author stayAnd
 * @date 2022/4/11
 */
@Slf4j
@Service
public class VinCardServiceImpl implements IVinCardService {

    @Resource
    private VinCardContext vinCardContext;

    @Autowired
    private ISimVehicleRelService simVehicleRelService;

    @Override
    public VinCardInfoDTO queryVinCard(String vin) {
        return vinCardContext.getVinCardHandler().queryCardByVin(vin);
    }

    @Override
    public List<VinCardInfoDTO> queryVinCardList(List<String> vinList) {
        return vinCardContext.getVinCardHandler().queryCardByVinList(vinList);
    }

    /**
     * 车卡关系和卡校验
     *
     * @param verifyDTOs
     * @return
     */
    @Override
    public Response verifyVinCard(List<VinCardDTO> verifyDTOs) {
        List<VinCardVerityResult> iccidVerifyResults = new ArrayList<>();
        verifyDTOs.forEach(verifyDTO -> {
            VinCardVerityResult vinCardVerityResult = new VinCardVerityResult();
            vinCardVerityResult.setVin(verifyDTO.getVin());
            vinCardVerityResult.setIccid(verifyDTO.getIccid());
            vinCardVerityResult.setExist(false);
			VinIccidDTO data = simVehicleRelService.getVinByIccid(verifyDTO.getIccid()).getData();
			if (null != data && data.getBindStatus() == 0) {
				vinCardVerityResult.setExist(org.apache.commons.lang3.StringUtils.isBlank(data.getVin()) || verifyDTO.getVin().equals(data.getVin()));
            }
            iccidVerifyResults.add(vinCardVerityResult);
        });

        VerifyVinCardResponseDTO verifyVinCardResponseDTO = new VerifyVinCardResponseDTO();
        verifyVinCardResponseDTO.setIccidVerifyResults(iccidVerifyResults);
        return Response.createSuccess(verifyVinCardResponseDTO);
    }

	//------------------------------ 私有方法去
    private String toKey(String vin, String iccid) {
        return vin + "-" + iccid;
    }
}
