package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.domain.BasParam;
import cn.cecep.talroad.mapper.BasParamMapper;
import cn.cecep.talroad.service.IBasParamService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 监测参数汇总(有组织)Service业务层处理
 *
 * @author szhpt
 * @date 2023-01-10*/
@Service
public class BasParamServiceImpl extends ServiceImpl<BasParamMapper,BasParam> implements IBasParamService
{
    @Autowired
    private BasParamMapper basParamMapper;

    /**
     * 查询监测参数汇总(有组织)列表
     *
     * @param basParam 监测参数汇总(有组织)
     * @return 监测参数汇总(有组织)
     */
    @Override
    public List<BasParam> selectBasParamList(BasParam basParam)
    {
        return basParamMapper.selectBasParamList(basParam);
    }

    @Override
    public Map<String, Object> getMonitroingParam() {
        Map<String, Object> result = new HashMap<>();

        //查询行业类别
        List<BasParam> basParams = baseMapper.selectList(new QueryWrapper<BasParam>().eq("status", 1).orderByAsc("show_order"));

        Map<String, List<BasParam>> data = basParams.stream().collect(Collectors.groupingBy(item -> item.getIndustry() + "_" + item.getDeviceType()));
        for (String key : data.keySet()) {
            List<BasParam> list = data.get(key);
            List<MonitoringParamVo> monitoringParamVoList = Lists.newArrayList();
            Map<String, List<BasParam>> collect = list.stream().collect(Collectors.groupingBy(BasParam::getFacilityType));

            for (String facilityType : collect.keySet()) {
                List<BasParam> facilityTypeList = collect.get(facilityType);
                MonitoringParamVo monitoringParamVo = new MonitoringParamVo();
                monitoringParamVo.setId(facilityType);
                monitoringParamVo.setLabel(facilityTypeList.get(0).getFacilityName());
                monitoringParamVo.setOrder(facilityTypeList.get(0).getShowOrder());
                List<SeriesObj> seriesObjList = Lists.newArrayList();
                for (BasParam basParamPO : facilityTypeList) {
                    SeriesObj seriesObj = new SeriesObj();
                    seriesObj.setId(UUID.randomUUID().toString());
                    seriesObj.setParamId(underscoreName(basParamPO.getParamCodeSvc()));
                    seriesObj.setLabel(basParamPO.getName());
                    seriesObj.setValue(basParamPO.getParamCodeSvc());
                    seriesObj.setUnit(basParamPO.getUnitReal());
                    seriesObj.setMinWidth(basParamPO.getMinWidth().toString());
                    seriesObjList.add(seriesObj);
                }
                monitoringParamVo.setChildren(seriesObjList);
                monitoringParamVoList.add(monitoringParamVo);
            }
            // 排序
            monitoringParamVoList = monitoringParamVoList.stream().sorted(Comparator.comparing(MonitoringParamVo::getOrder)).collect(Collectors.toList());
            result.put(key, monitoringParamVoList);
        }

        return result;

    }

    public static String underscoreName(String name) {
        StringBuilder result = new StringBuilder();
        if (name != null && name.length() > 0) {
            // 将第一个字符处理成小写
            result.append(name.substring(0, 1).toLowerCase());
            // 循环处理其余字符
            for (int i = 1; i < name.length(); i++) {
                String s = name.substring(i, i + 1);
                // 在大写字母前添加下划线
                if (s.equals(s.toUpperCase()) && !Character.isDigit(s.charAt(0))) {
                    result.append("_");
                }
                // 其他字符直接转成小写
                result.append(s.toLowerCase());
            }
        }
        return result.toString();
    }

    @Data
    public static class MonitoringParamVo {
        @ApiModelProperty("facility_type")
        private String id;
        @ApiModelProperty("facility_name")
        private String label;
        @ApiModelProperty("参数")
        private List<SeriesObj> children;

        /**
         * 用于排序设施
         */
        private Integer order;
    }

    @Data
    public static class SeriesObj {
        @ApiModelProperty("id")
        private String id;
        @ApiModelProperty("param_code_svc")
        private String value;
        @ApiModelProperty("name")
        private String label;
        @ApiModelProperty("unit_real")
        private String unit;
        @ApiModelProperty("param_id")
        private String paramId;
        @ApiModelProperty("min_width")
        private String minWidth;

        public void setId(String id) {
            this.id = id.replaceAll("-","");
        }
    }

}
