package com.yeejoin.amos.boot.module.statistcs.biz.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yeejoin.amos.boot.biz.common.annotation.TechnicalParameter;
import com.yeejoin.amos.boot.biz.common.entity.DataDictionary;
import com.yeejoin.amos.boot.biz.common.service.impl.DataDictionaryServiceImpl;
import com.yeejoin.amos.boot.module.common.api.dto.TechParamItem;
import com.yeejoin.amos.boot.module.common.biz.utils.TechParamUtil;
import com.yeejoin.amos.boot.module.statistcs.biz.utils.JsonUtils;
import com.yeejoin.amos.boot.module.statistics.api.enums.AdvanceSearchEnum;
import com.yeejoin.amos.boot.module.statistics.api.enums.ConditionEnum;
import com.yeejoin.amos.boot.module.statistics.api.mapper.TzsCustomFilterMapper;
import com.yeejoin.amos.boot.module.ymt.api.enums.EquipmentClassifityEnum;
import com.yeejoin.amos.component.feign.utils.FeignUtil;
import com.yeejoin.amos.feign.systemctl.Systemctl;
import com.yeejoin.amos.feign.systemctl.model.DictionarieModel;
import com.yeejoin.amos.feign.systemctl.model.DictionarieValueModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.typroject.tyboot.core.foundation.utils.ValidationUtil;

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

/**
 * 综合统计分析服务实现类
 *
 * @author system_generator
 * @date 2025-06-24
 */
@Service
public class ComprehensiveStatisticalAnalysisServiceImpl {

    @Value("classpath:/json/equipCategory.json")
    private Resource equipCategory;

    @Autowired
    private TzsCustomFilterMapper tzsCustomFilterMapper;
    @Autowired
    private DataDictionaryServiceImpl dataDictionaryService;

    public List<Map<String, Object>> queryEquipmentCategory(String key) {
        List<Map<String, Object>> list;
        Map<String, List<Map<String, Object>>> resourceJson = JsonUtils.getResourceJson(equipCategory);
        if (key == null) {
            list = resourceJson.get(EquipmentClassifityEnum.ZHTJFX.getCode());
        } else {
            list = tzsCustomFilterMapper.selectEquipmentCategoryByParentId(key);
        }

        return list;
    }

    public JSONObject queryEquipmentSearchData(String value) {
        List<TechParamItem> paramMetaList = TechParamUtil.getParamMetaList(value);
        ArrayList<JSONObject> list = new ArrayList<>();
        for(int i= 0;i< paramMetaList.size();i++){
            JSONObject object = new JSONObject();
            object.put("key",paramMetaList.get(i).getParamKey());
            object.put("value",paramMetaList.get(i).getParamKey());
            object.put("label",paramMetaList.get(i).getParamLabel());
            object.put("unit",paramMetaList.get(i).getUnit());
            JSONObject skillConfig = new JSONObject();
            skillConfig.put("conditions",getConditionList(paramMetaList.get(i).getParamType(),paramMetaList.get(i).getDictCode()));
            if(!ValidationUtil.isEmpty(paramMetaList.get(i).getDictCode())){
                skillConfig.put("type","select");
                skillConfig.put("data",getData(paramMetaList.get(i).getDictCode()));
            }else if(TechnicalParameter.ParamType.BIG_DECIMAL.equals(paramMetaList.get(i).getParamType())){
                skillConfig.put("type","inputNumber");
            }else if(TechnicalParameter.ParamType.STRING.equals(paramMetaList.get(i).getParamType())){
                skillConfig.put("type","input");
            }else if(TechnicalParameter.ParamType.DATE.equals(paramMetaList.get(i).getParamType())){
                skillConfig.put("type","date");
            }
            skillConfig.put("isMulti",false);
            object.put("skillConfig",skillConfig);
            list.add(object);
        }
        JSONObject result = new JSONObject();
        result.put("techParam",list);
        return result;
    }

    private JSONArray getData(String dictCode) {
        JSONArray jsonArray = new JSONArray();
        //根据dictCode查询数据字典
        List<DataDictionary> dictionaries = dataDictionaryService.getByType(dictCode);
        for (DataDictionary dictionary : dictionaries) {
            JSONObject object = new JSONObject();
            object.put("label", dictionary.getName());
            object.put("value", dictionary.getCode());
            jsonArray.add(object);
         }
        return jsonArray;
    }

    private JSONArray getConditionList(TechnicalParameter.ParamType value,String dictCode) {
        return ConditionEnum.getByCode(ValidationUtil.isEmpty(dictCode) ? value : null);
    }
    public JSONArray queryAdvancedSearch() {
        JSONArray all = AdvanceSearchEnum.getAll();
        return all;
    }
}