package com.cusc.nirvana.user.rnr.project.handler.impl;

import com.cusc.nirvana.common.result.Response;
import com.cusc.nirvana.user.rnr.fp.dto.VinCardDTO;
import com.cusc.nirvana.user.rnr.fp.dto.VinCardInfoDTO;
import com.cusc.nirvana.user.rnr.fp.dto.VinCardVerityResult;
import com.cusc.nirvana.user.rnr.fp.dto.VinIccidDTO;
import com.cusc.nirvana.user.rnr.fp.service.ISimVehicleRelService;
import com.cusc.nirvana.user.rnr.project.constant.ProjectConstant;
import com.cusc.nirvana.user.rnr.project.dto.ProjectCardExistResponseDTO;
import com.cusc.nirvana.user.rnr.project.handler.CuscVinCardHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author stayAnd
 * @date 2022/4/8
 */
@Service
@Slf4j
public class InternalVinCardHandler extends CuscVinCardHandler {


    @Autowired
    private ISimVehicleRelService simVehicleRelService;

    @Override
    public VinCardInfoDTO queryCardByVin(String vin) {
        VinCardInfoDTO vinCardInfoDTO = new VinCardInfoDTO();
        List<String> iccids = new ArrayList<>();
        vinCardInfoDTO.setVin(vin);
        Response<List<VinIccidDTO>> iccidByVin = this.simVehicleRelService.getIccidByVin(vin);
        if(iccidByVin.isSuccess()){
            List<VinIccidDTO> list = iccidByVin.getData();
            iccids = list.stream().filter(dto -> dto.getVin().equals(vin)).map(dto -> dto.getIccid()).collect(Collectors.toList());
        }
        log.info("通过vin查询iccid,{},{}", vin, iccids);
        vinCardInfoDTO.setIccidList(iccids);
        return vinCardInfoDTO;
    }

    @Override
    public List<VinCardInfoDTO> queryCardByVinList(List<String> vinList) {
        List<VinCardInfoDTO> list = new ArrayList<>();
        if(!vinList.isEmpty()){
            List<String> collect = vinList.stream().distinct().collect(Collectors.toList());
            for (String vin : collect) {
                VinCardInfoDTO vinCardInfoDTO = this.queryCardByVin(vin);
                list.add(vinCardInfoDTO);
            }
        }
        return list;
    }

    @Override
    public Response<ProjectCardExistResponseDTO> isCardExist(String iccid) {
        return isCardExist(ProjectConstant.PROJECT_SERVICE_URL + "/project/api/v1/sim/is-exist", iccid);
    }
}
