package com.project.zcustom.service.build.impl;

import com.project.common.core.domain.search.PageParamVo;
import com.project.zcustom.domain.build.PlatBuildParams;
import com.project.zcustom.domain.build.PlatBuildYearTrend;
import com.project.zcustom.mapper.build.PlatBuildParamsMapper;
import com.project.zcustom.service.build.IPlatBuildParamsService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.common.utils.StringUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * 楼宇数据Service业务层处理
 *
 * @author change
 * @date 2024-11-01
 */
@Service
public class PlatBuildParamsServiceImpl extends ServiceImpl<PlatBuildParamsMapper, PlatBuildParams> implements IPlatBuildParamsService {

    /**
     * 组装查询条件
     */
    private LambdaQueryWrapper<PlatBuildParams> getLambdaQueryWrapper(PlatBuildParams platBuildParams) {
        LambdaQueryWrapper<PlatBuildParams> lqw = new LambdaQueryWrapper<PlatBuildParams>();
        if (StringUtils.isNotBlank(platBuildParams.getAppOrg())) {
            lqw.eq(PlatBuildParams::getAppOrg, platBuildParams.getAppOrg());
        }
        if (StringUtils.isNotNull(platBuildParams.getType())) {
            lqw.eq(PlatBuildParams::getType, platBuildParams.getType());
        }
        if (StringUtils.isNotBlank(platBuildParams.getParamName())) {
            lqw.like(PlatBuildParams::getParamName, platBuildParams.getParamName());
        }
        if (StringUtils.isNotBlank(platBuildParams.getParamKey())) {
            lqw.eq(PlatBuildParams::getParamKey, platBuildParams.getParamKey());
        }
        return lqw;
    }

    /**
     * 分页查询楼宇数据列表
     *
     * @param platBuildParams 楼宇数据
     * @return 楼宇数据集合
     */
    @Override
    public IPage<PlatBuildParams> pagingPlatBuildParamsList(PlatBuildParams platBuildParams) {
        IPage<PlatBuildParams> page = new Page<>(platBuildParams.getPageNum(), platBuildParams.getPageSize());
        LambdaQueryWrapper<PlatBuildParams> lqw = getLambdaQueryWrapper(platBuildParams);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatBuildParams::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 分页查询楼宇数据列表
     *
     * @param lqw
     * @return 楼宇数据集合
     */
    @Override
    public IPage<PlatBuildParams> pagingPlatBuildParamsList(PageParamVo pageParamVo, LambdaQueryWrapper<PlatBuildParams> lqw) {
        IPage<PlatBuildParams> page = new Page<>(pageParamVo.getPageNum(), pageParamVo.getPageSize());
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatBuildParams::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 查询楼宇数据列表
     *
     * @param platBuildParams 楼宇数据
     * @return 楼宇数据
     */
    @Override
    public List<PlatBuildParams> selectPlatBuildParamsList(PlatBuildParams platBuildParams) {
        LambdaQueryWrapper<PlatBuildParams> lqw = getLambdaQueryWrapper(platBuildParams);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatBuildParams::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询楼宇数据列表
     *
     * @param platBuildParams 楼宇数据
     * @return 楼宇数据
     */
    @Override
    public List<PlatBuildParams> selectPlatBuildParamsList(PlatBuildParams platBuildParams, Long limitNum) {
        LambdaQueryWrapper<PlatBuildParams> lqw = getLambdaQueryWrapper(platBuildParams);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatBuildParams::getCreateTime);
        lqw.last("limit " + limitNum);
        return baseMapper.selectList(lqw);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 楼宇数据
     */
    @Override
    public PlatBuildParams getPlatBuildParamsByField(String field, String filedValue) {
        QueryWrapper<PlatBuildParams> lqw = new QueryWrapper<PlatBuildParams>();
        lqw.eq(field, filedValue);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 楼宇数据
     */
    @Override
    public List<PlatBuildParams> selectPlatBuildParamsListByField(String field, String filedValue, Long limitNum) {
        QueryWrapper<PlatBuildParams> lqw = new QueryWrapper<PlatBuildParams>();
        lqw.eq(field, filedValue);
        lqw.last("limit " + limitNum);
        return this.list(lqw);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 楼宇数据
     */
    @Override
    public List<PlatBuildParams> selectPlatBuildParamsListByField(String field, String filedValue) {
        QueryWrapper<PlatBuildParams> lqw = new QueryWrapper<PlatBuildParams>();
        lqw.eq(field, filedValue);
        if (field.equals("type") && filedValue.equals("12")){
            lqw.orderByDesc("params_value");
        }
        return this.list(lqw);
    }

    /**
     * 判断指定字段值是否存在
     *
     * @param field      字段名称
     * @param filedValue 字段值
     */
    @Override
    public void checkFieldExist(String field, String filedValue, String tips, Long id) {
        QueryWrapper<PlatBuildParams> lqw = new QueryWrapper<PlatBuildParams>();
        lqw.eq(field, filedValue);
        lqw.ne("id", id);
        if (this.getOne(lqw, false) != null) {
            throw new RuntimeException(tips + "已存在");
        }
    }

    /**
     * 真删接口
     */
    @Override
    public void reallyDeletePlatBuildParams(LambdaQueryWrapper<PlatBuildParams> lqw) {
        baseMapper.reallyDeleteByWrapper(lqw);
    }

    /**
     * 设置添加趋势数据
     */
    @Override
    public void setBuildParamsData(PlatBuildParams platBuildParams, List<String> keyList, Map<String, Object> mapData, Integer type, String appOrg){
        platBuildParams.setType(type);
        platBuildParams.setAppOrg(appOrg);
        StringUtils.setFieldValue(platBuildParams, mapData, keyList, 0, PlatBuildParams::setParamName, String.class);
        StringUtils.setFieldValue(platBuildParams, mapData, keyList, 1, PlatBuildParams::setParamKey, String.class);
        StringUtils.setFieldValue(platBuildParams, mapData, keyList, 1, PlatBuildParams::setParamsValue, BigDecimal.class);
        StringUtils.setFieldValue(platBuildParams, mapData, keyList, 2, PlatBuildParams::setParamsValue2, BigDecimal.class);
        StringUtils.setFieldValue(platBuildParams, mapData, keyList, 2, PlatBuildParams::setParamsValue3, BigDecimal.class);
        StringUtils.setFieldValue(platBuildParams, mapData, keyList, 2, PlatBuildParams::setParamsValue4, BigDecimal.class);
    }


    /**
     * 添加楼宇数据
     */
    @Override
    public void addPlatBuildParams(PlatBuildParams platBuildParams,String appOrg){
        LambdaQueryWrapper<PlatBuildParams> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PlatBuildParams::getType, platBuildParams.getType());
        if(StringUtils.isNotBlank(appOrg)){
            lqw.eq(PlatBuildParams::getAppOrg, appOrg);
        }

        List<Integer> validTypes = Arrays.asList(4, 5, 6, 7, 8, 9, 10, 12); // 4月用电量信息 5 楼宇月用水电热信息 6 数据中心机房信息 7 供暖能耗信息 8 燃气用量信息 9 资源信息 10 可再生电力信息 12 各单位年度累计办公碳排放量
        if(validTypes.contains(platBuildParams.getType())){//根据key值唯一
            lqw.eq(PlatBuildParams::getParamKey, platBuildParams.getParamKey());
        }
        lqw.last("limit 1");
        PlatBuildParams paramsInfo = this.getOne(lqw);
        if(paramsInfo!=null){
            paramsInfo.setParamsValue(platBuildParams.getParamsValue());
            paramsInfo.setParamsValue2(platBuildParams.getParamsValue2());
            paramsInfo.setParamsValue3(platBuildParams.getParamsValue3());
            paramsInfo.setParamsValue4(platBuildParams.getParamsValue4());
            this.updateById(paramsInfo);
        }else{
            this.save(platBuildParams);
        }
    }

    /**
     * 月用电量信息
     * @return
     */
    @Override
    public Map<String, Object> powerForMonth(){
        List<String> xList = new ArrayList<>();
        List<BigDecimal> valueList = new ArrayList<>();
        List<PlatBuildParams> list = this.selectPlatBuildParamsListByField("type", "4");
        if(!list.isEmpty()){
            for (PlatBuildParams item : list){
                xList.add(item.getParamName());
                valueList.add(item.getParamsValue());
            }
        }else{
            xList = Arrays.asList("照明", "电梯", "办公", "空调","食堂","其他");
            valueList = StringUtils.getDefaultListBigDecimal(6);
        }
        Map<String,Object> tempMap = new HashMap<>();
        StringUtils.createdTableData(xList,valueList,"#ff9d00","月用电量",tempMap);
        return tempMap;
    }

    /**
     * 年度累计综合能耗
     * @return
     */
    @Override
    public Map<String, Object> energyForYear(){
        PlatBuildParams platBuildParams = this.getPlatBuildParamsByField("type", "0");
        Map<String, Object> tempMap = new HashMap<>();
        if (StringUtils.isNull(platBuildParams)) {
            StringUtils.getMapData("年度累计综合能耗", "energyNum", 0, tempMap);
            StringUtils.getMapData("同比减少", "reduceNum", 0, tempMap);
        } else {
            StringUtils.getMapData("年度累计综合能耗", "energyNum", platBuildParams.getParamsValue(), tempMap);
            StringUtils.getMapData("同比减少", "reduceNum", platBuildParams.getParamsValue2(), tempMap);
        }
        return tempMap;
    }

    /**
     * 楼宇月用水电热信息
     */
    @Override
    public Map<String, Object> useHeatGas(String appOrg){
        LambdaQueryWrapper<PlatBuildParams> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PlatBuildParams::getType, 5);
        if(StringUtils.isNotBlank(appOrg) && !appOrg.equals("0000")){
            lqw.eq(PlatBuildParams::getAppOrg, appOrg);
        }
        List<PlatBuildParams> paramList = this.list(lqw);
        Map<String, Object> tempMap = new HashMap<>();
        if (paramList.isEmpty()) {
            StringUtils.getMapData("楼宇月用水量", "water", 0, tempMap);
            StringUtils.getMapData("楼宇月用热量", "hot", 0, tempMap);
            StringUtils.getMapData("楼宇月用气量", "gas", 0, tempMap);
        } else {
            for (PlatBuildParams item : paramList){
                if(item.getParamKey().equals("water")){
                    StringUtils.getMapData("楼宇月用水量", "water", item.getParamsValue(), tempMap);
                }else if(item.getParamKey().equals("re")){
                    StringUtils.getMapData("楼宇月用热量", "hot", item.getParamsValue(), tempMap);
                }else if(item.getParamKey().equals("qi")){
                    StringUtils.getMapData("楼宇月用气量", "gas", item.getParamsValue(), tempMap);
                }
            }
        }
        return tempMap;
    }

    /**
     * 数据中心机房信息接口
     */
    @Override
    public Map<String, Object> dataCenterRoom(String appOrg){
        LambdaQueryWrapper<PlatBuildParams> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PlatBuildParams::getType, 6);
        if(StringUtils.isNotBlank(appOrg) && !appOrg.equals("0000")){
            lqw.eq(PlatBuildParams::getAppOrg, appOrg);
        }
        List<PlatBuildParams> paramList = this.list(lqw);
        Map<String, Object> tempMap = new HashMap<>();
        if (paramList.isEmpty()) {
            StringUtils.getMapData("建筑面积", "acreage", 0, tempMap);
            StringUtils.getMapData("用电量", "electric", 0, tempMap);
        } else {
            for (PlatBuildParams item : paramList){
                if(item.getParamKey().equals("jzmj")){
                    StringUtils.getMapData("建筑面积", "acreage", item.getParamsValue(), tempMap);
                }else if(item.getParamKey().equals("ydl")){
                    StringUtils.getMapData("用电量", "electric", item.getParamsValue(), tempMap);
                }
            }
        }
        return tempMap;
    }

    /**
     * 年度累计电耗
     */
    @Override
    public Map<String, Object> powerForYear(){
        PlatBuildParams platBuildParams = this.getPlatBuildParamsByField("type", "1");
        Map<String, Object> tempMap = new HashMap<>();
        if (StringUtils.isNull(platBuildParams)) {
            StringUtils.getMapData("年度累计电耗", "energyYear", 0, tempMap);
        } else {
            StringUtils.getMapData("年度累计电耗", "energyYear", platBuildParams.getParamsValue(), tempMap);
        }
        return tempMap;
    }

    /**
     * 供暖能耗信息
     */
    @Override
    public Map<String, Object> heatingEnergy(String appOrg){
        LambdaQueryWrapper<PlatBuildParams> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PlatBuildParams::getType, 7);
        if(StringUtils.isNotBlank(appOrg) && !appOrg.equals("0000")){
            lqw.eq(PlatBuildParams::getAppOrg, appOrg);
        }
        List<PlatBuildParams> paramList = this.list(lqw);
        Map<String, Object> tempMap = new HashMap<>();
        if (paramList.isEmpty()) {
            StringUtils.getMapData("用暖人数", "energyUser", 0, tempMap);
            StringUtils.getMapData("供暖能耗", "heating", 0, tempMap);
            StringUtils.getMapData("非供暖能耗", "noHeating", 0, tempMap);
        } else {
            for (PlatBuildParams item : paramList){
                if(item.getParamKey().equals("ynrs")){
                    StringUtils.getMapData("用暖人数", "energyUser", item.getParamsValue(), tempMap);
                }else if(item.getParamKey().equals("gnnh")){
                    StringUtils.getMapData("供暖能耗", "heating", item.getParamsValue(), tempMap);
                }else if(item.getParamKey().equals("fgnnh")){
                    StringUtils.getMapData("非供暖能耗", "noHeating", item.getParamsValue(), tempMap);
                }
            }
        }
        return tempMap;
    }


    /**
     * 燃气用量信息
     */
    @Override
    public Map<String, Object> gasInfo(String appOrg){
        LambdaQueryWrapper<PlatBuildParams> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PlatBuildParams::getType, 8);
        if(StringUtils.isNotBlank(appOrg) && !appOrg.equals("0000")){
            lqw.eq(PlatBuildParams::getAppOrg, appOrg);
        }
        List<PlatBuildParams> paramList = this.list(lqw);
        Map<String, Object> tempMap = new HashMap<>();
        if (paramList.isEmpty()) {
            StringUtils.getMapData("餐厨燃气用气量", "gas", 0, tempMap);
            StringUtils.getMapData("供冷热燃气用量", "hotGas", 0, tempMap);
        } else {
            for (PlatBuildParams item : paramList){
                if(item.getParamKey().equals("caic")){
                    StringUtils.getMapData("餐厨燃气用气量", "gas", item.getParamsValue(), tempMap);
                }else if(item.getParamKey().equals("gongl")){
                    StringUtils.getMapData("供冷热燃气用量", "hotGas", item.getParamsValue(), tempMap);
                }
            }
        }
        return tempMap;
    }

    /**
     * 资源信息
     */
    @Override
    public List<Map<String, Object>> resourceInfo(String appOrg){
        LambdaQueryWrapper<PlatBuildParams> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PlatBuildParams::getType, 9);
        if(StringUtils.isNotBlank(appOrg) && !appOrg.equals("0000")){
            lqw.eq(PlatBuildParams::getAppOrg, appOrg);
        }
        List<PlatBuildParams> paramList = this.list(lqw);
        List<Map<String, Object>> tempMap = new ArrayList<>();
        if (paramList.isEmpty()) {
           return new ArrayList<>();
        } else {
            for (PlatBuildParams item : paramList){
                Map<String, Object> mapInfo = new HashMap<>();
                mapInfo.put("name", item.getParamName());
                mapInfo.put("value", item.getParamsValue());
                tempMap.add(mapInfo);
            }
        }
        return tempMap;
    }

    /**
     * 可再生电力信息
     */
    @Override
    public Map<String, Object> greenPowerInfo(String appOrg){
        LambdaQueryWrapper<PlatBuildParams> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PlatBuildParams::getType, 10);
        if(StringUtils.isNotBlank(appOrg) && !appOrg.equals("0000")){
            lqw.eq(PlatBuildParams::getAppOrg, appOrg);
        }
        List<PlatBuildParams> paramList = this.list(lqw);
        Map<String, Object> tempMap = new HashMap<>();
        if (paramList.isEmpty()) {
            StringUtils.getMapData("可再生电力发电量", "green", 0, tempMap);
            StringUtils.getMapData("可再生电力送电量", "greenSend", 0, tempMap);
        } else {
            for (PlatBuildParams item : paramList){
                if(item.getParamKey().equals("green")){
                    StringUtils.getMapData("可再生电力发电量", "green", item.getParamsValue(), tempMap);
                }else if(item.getParamKey().equals("sdl")){
                    StringUtils.getMapData("可再生电力送电量", "greenSend", item.getParamsValue(), tempMap);
                }
            }
        }
        return tempMap;
    }

    /**
     * 年度累计水耗
     */
    @Override
    public Map<String, Object> waterForYear(){
        PlatBuildParams platBuildParams = this.getPlatBuildParamsByField("type", "2");
        Map<String, Object> tempMap = new HashMap<>();
        if (StringUtils.isNull(platBuildParams)) {
            StringUtils.getMapData("年度累计水耗", "waterYear", 0, tempMap);
        } else {
            StringUtils.getMapData("年度累计水耗", "waterYear", platBuildParams.getParamsValue(), tempMap);
        }
        return tempMap;
    }

    /**
     * 办公碳排放情况
     */
    @Override
    public Map<String, Object> officeCarbonForYear(){
        PlatBuildParams platBuildParams = this.getPlatBuildParamsByField("type", "11");
        Map<String, Object> tempMap = new HashMap<>();
        if (StringUtils.isNull(platBuildParams)) {
            StringUtils.getMapData("年度累计碳排放总量", "carbonTotal", 0, tempMap);
            StringUtils.getMapData("排放强度", "strength", 0, tempMap);
            StringUtils.getMapData("排放强度下降值", "strengthLow", 0, tempMap);
            StringUtils.getMapData("办公用碳排放值", "carbonOffice", 0, tempMap);
        } else {
            StringUtils.getMapData("年度累计碳排放总量", "carbonTotal", platBuildParams.getParamsValue(), tempMap);
            StringUtils.getMapData("排放强度", "strength", platBuildParams.getParamsValue2(), tempMap);
            StringUtils.getMapData("排放强度下降值", "strengthLow", platBuildParams.getParamsValue3(), tempMap);
            StringUtils.getMapData("办公用碳排放值", "carbonOffice", platBuildParams.getParamsValue4(), tempMap);
        }
        return tempMap;
    }

    /**
     * 年度累计非供暖能耗
     */
    @Override
    public Map<String, Object> nonHeatingEnergyForYear(){
        PlatBuildParams platBuildParams = this.getPlatBuildParamsByField("type", "3");
        Map<String, Object> tempMap = new HashMap<>();
        if (StringUtils.isNull(platBuildParams)) {
            StringUtils.getMapData("年度累计非供暖能耗", "nonHeatingEnergy", 0, tempMap);
        } else {
            StringUtils.getMapData("年度累计非供暖能耗", "nonHeatingEnergy", platBuildParams.getParamsValue(), tempMap);
        }
        return tempMap;
    }

}
