package com.ningdatech.carapi.industry.manage;

import com.alibaba.fastjson.JSONObject;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.carapi.car.datascreen.model.dto.DataScreenParam;
import com.ningdatech.carapi.car.datascreen.model.vo.RegionDistributionStatisticsVO;
import com.ningdatech.carapi.car.datascreen.service.VehicleStatisticsService;
import com.ningdatech.carapi.industry.model.po.CeriticatePO;
import com.ningdatech.carapi.industry.service.DataScreenService;
import com.ningdatech.carapi.sys.entity.dto.RegionDTO;
import com.ningdatech.carapi.sys.helper.RegionsCacheHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author PoffyZhang
 * @Classname DataScreenManage
 * @Description
 * @Date 2022/12/15 19:06
 */
@Component
@RequiredArgsConstructor
public class DataScreenManage {

    private final VehicleStatisticsService vehicleStatisticsService;

    private final DataScreenService dataScreenService;

    private final RegionsCacheHelper regionsCacheHelper;

    public List<RegionDistributionStatisticsVO> regionDistribution(DataScreenParam param) {
        return vehicleStatisticsService.getRegionDistribution(param);
    }

    public Map<String, Object> tainCertificate(DataScreenParam param) {
        Map<String, Object> res = new LinkedHashMap<>();
        List<CeriticatePO> list = dataScreenService.tainCertificate(param);
        complementaryRegion(list, param.getRegionId());
        for (CeriticatePO ceriticate : list) {
            res.put(ceriticate.getRegionName(), ceriticate.getCount());
        }

        return res;
    }

    public JSONObject laboratoryQualificationCertificate(DataScreenParam param) {
        JSONObject res = new JSONObject(true);
        List<CeriticatePO> list = dataScreenService.laboratoryQualificationCertificate(param);
        complementaryRegion(list, param.getRegionId());
        for (CeriticatePO ceriticate : list) {
            res.put(ceriticate.getRegionName(), ceriticate.getCount());
        }
        return res;
    }

    public JSONObject laboratoryAcceptanceCertificate(DataScreenParam param) {
        JSONObject res = new JSONObject(true);
        List<CeriticatePO> list = dataScreenService.laboratoryAcceptanceCertificate(param);
        complementaryRegion(list, param.getRegionId());
        for (CeriticatePO ceriticate : list) {
            res.put(ceriticate.getRegionName(), ceriticate.getCount());
        }
        return res;
    }

    public JSONObject recordCertificateMortarEnterprise(DataScreenParam param) {
        JSONObject res = new JSONObject(true);
        List<CeriticatePO> list = dataScreenService.recordCertificateMortarEnterprise(param);
        complementaryRegion(list, param.getRegionId());
        for (CeriticatePO ceriticate : list) {
            res.put(ceriticate.getRegionName(), ceriticate.getCount());
        }
        return res;
    }

    private void complementaryRegion(List<CeriticatePO> list, Long regionId) {
        List<RegionDTO> regions = regionsCacheHelper.listChildOnly(regionId, true);
        Map<String, CeriticatePO> regionMap = CollUtils.listToMap(list, CeriticatePO::getRegionName);
        List<CeriticatePO> tmpList = new ArrayList<>();
        regions.forEach(w -> {
            CeriticatePO ceriticate = regionMap.get(w.getRegionName());
            if (ceriticate == null) {
                ceriticate = new CeriticatePO();
                ceriticate.setRegionId(w.getId());
                ceriticate.setRegionName(w.getRegionName());
                ceriticate.setCount(0);
            }
            tmpList.add(ceriticate);
        });
        list.clear();
        list.addAll(tmpList);
    }
}
