package com.yeejoin.amos.boot.module.tcm.biz.service.impl.statistics;

import com.alibaba.fastjson.JSONArray;
import com.yeejoin.amos.boot.module.tcm.api.dto.ScreenDto;
import com.yeejoin.amos.boot.module.tcm.api.mapper.ScreenMapper;
import com.yeejoin.amos.boot.module.tcm.api.service.statistics.IScreenService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.typroject.tyboot.core.restful.utils.ResponseModel;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ScreenImpl implements IScreenService {

    @Resource
    private ScreenMapper screenMapper;

    @Override
    public List<Map<String,Object>> companyInfo(ScreenDto screenDto) {

        List<Map<String,Object>> list = screenMapper.getCompanyInfo(screenDto);
        if(CollectionUtils.isEmpty(list)){
            return list;
        }
        //数据处理后Map，key为企业类型+设备，value为计数
        Map<String, Integer> dataMap = new HashMap<>();
        for(Map<String,Object> map : list){
            JSONArray equipCategoryArray = (JSONArray) JSONArray.parse((String) map.get("equipCategory"));
            String[] UnitTypeArray = ((String) map.get("unitType")).split("#");
            for(int i=0;i<UnitTypeArray.length;i++){
                String unitTypeName = UnitTypeArray[i];
                for(int j=0;j<equipCategoryArray.size();j++){
                    if(dataMap.containsKey(unitTypeName+equipCategoryArray.get(j))){
                        dataMap.put(unitTypeName+equipCategoryArray.get(j),dataMap.get(unitTypeName+equipCategoryArray.get(j))+1);
                    }else{
                        dataMap.put(unitTypeName+equipCategoryArray.get(j),1);
                    }
                }
            }
        }
        /**
         * 4000	起重机械
         * 3000	电梯
         * 5000	场（厂）内机动车辆
         * 1000	锅炉
         * 2000	压力容器
         * 6000	大型游乐设施（B类）
         * 9000	客运索道
         *企业数量：按单位类型及单位管理的设备进行统计（按设备类型收集制造单位数量，省、市-李静）：八大类制造单位（不要气瓶）、电梯维保单位、八大类使用单位（不要气瓶）、气瓶充装单位、八大类安改维单位（不要气瓶）、八大类设计单位（不要气瓶）
         * 1、设计单位只有压力容器设计和压力管道设计两类；(没有数据显示0)
         * 3、安改维有锅炉、压力管道、电梯、起重机械、客运索道、场（厂）内专用机动车辆、大型游乐设施7类；
         * 4、充装单位有移动式压力容器、气瓶两类（所有充装企业都统计为气瓶类型）
         */
        //制造Map
        Map<String,Object> zhizaoMap = new HashMap<>();
        List<Map<String,Object>> zhizaoCompanyList = new ArrayList<>();
        zhizaoMap.put("key","zhizaoCompany");
        zhizaoMap.put("value","制造单位");
        //制造电梯
        Map<String,Object> zhizaodiantiMap = new HashMap<>();
        zhizaodiantiMap.put("key","key01");
        zhizaodiantiMap.put("name","电梯");
        zhizaodiantiMap.put("value",dataMap.getOrDefault("制造单位3000",0));
        zhizaoCompanyList.add(zhizaodiantiMap);
        //制造锅炉
        Map<String,Object> zhizaoguoluMap = new HashMap<>();
        zhizaoguoluMap.put("key","key02");
        zhizaoguoluMap.put("name","锅炉");
        zhizaoguoluMap.put("value",dataMap.getOrDefault("制造单位1000",0));
        zhizaoCompanyList.add(zhizaoguoluMap);
        //制造锅炉
        Map<String,Object> zhizaojidongcheMap = new HashMap<>();
        zhizaojidongcheMap.put("key","key03");
        zhizaojidongcheMap.put("name","场内机动车");
        zhizaojidongcheMap.put("value",dataMap.getOrDefault("制造单位5000",0));
        zhizaoCompanyList.add(zhizaojidongcheMap);
        //制造压力容器
        Map<String,Object> zhizaoyalirongqiMap = new HashMap<>();
        zhizaoyalirongqiMap.put("key","key04");
        zhizaoyalirongqiMap.put("name","压力容器");
        zhizaoyalirongqiMap.put("value",dataMap.getOrDefault("制造单位2000",0));
        zhizaoCompanyList.add(zhizaoyalirongqiMap);
        //制造起重机械
        Map<String,Object> zhizaoqizhongjixieMap = new HashMap<>();
        zhizaoqizhongjixieMap.put("key","key06");
        zhizaoqizhongjixieMap.put("name","起重机械");
        zhizaoqizhongjixieMap.put("value",dataMap.getOrDefault("制造单位4000",0));
        zhizaoCompanyList.add(zhizaoqizhongjixieMap);
        //制造游乐设施
        Map<String,Object> zhizaoyoulesheshiMap = new HashMap<>();
        zhizaoyoulesheshiMap.put("key","key07");
        zhizaoyoulesheshiMap.put("name","大型游乐设施");
        zhizaoyoulesheshiMap.put("value",dataMap.getOrDefault("制造单位6000",0));
        zhizaoCompanyList.add(zhizaoyoulesheshiMap);
        //制造客运索道
        Map<String,Object> zhizaokeyunsuodaoMap = new HashMap<>();
        zhizaokeyunsuodaoMap.put("key","key08");
        zhizaokeyunsuodaoMap.put("name","客运索道");
        zhizaokeyunsuodaoMap.put("value",dataMap.getOrDefault("制造单位9000",0));
        zhizaoCompanyList.add(zhizaokeyunsuodaoMap);
        zhizaoMap.put("data",zhizaoCompanyList);
        return null;
    }

}
