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

import com.project.common.core.domain.search.PageParamVo;
import com.project.common.utils.DateUtils;
import com.project.zcustom.domain.build.PlatBuildYearTrend;
import com.project.zcustom.mapper.build.PlatBuildYearTrendMapper;
import com.project.zcustom.service.build.IPlatBuildYearTrendService;
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.time.LocalDate;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 年度趋势变化数据Service业务层处理
 *
 * @author change
 * @date 2024-11-01
 */
@Service
public class PlatBuildYearTrendServiceImpl extends ServiceImpl<PlatBuildYearTrendMapper, PlatBuildYearTrend> implements IPlatBuildYearTrendService {

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

    /**
     * 分页查询年度趋势变化数据列表
     *
     * @param platBuildYearTrend 年度趋势变化数据
     * @return 年度趋势变化数据集合
     */
    @Override
    public IPage<PlatBuildYearTrend> pagingPlatBuildYearTrendList(PlatBuildYearTrend platBuildYearTrend) {
        IPage<PlatBuildYearTrend> page = new Page<>(platBuildYearTrend.getPageNum(), platBuildYearTrend.getPageSize());
        LambdaQueryWrapper<PlatBuildYearTrend> lqw = getLambdaQueryWrapper(platBuildYearTrend);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatBuildYearTrend::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

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

    /**
     * 查询年度趋势变化数据列表
     *
     * @param platBuildYearTrend 年度趋势变化数据
     * @return 年度趋势变化数据
     */
    @Override
    public List<PlatBuildYearTrend> selectPlatBuildYearTrendList(PlatBuildYearTrend platBuildYearTrend) {
        LambdaQueryWrapper<PlatBuildYearTrend> lqw = getLambdaQueryWrapper(platBuildYearTrend);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatBuildYearTrend::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询年度趋势变化数据列表
     *
     * @param platBuildYearTrend 年度趋势变化数据
     * @return 年度趋势变化数据
     */
    @Override
    public List<PlatBuildYearTrend> selectPlatBuildYearTrendList(PlatBuildYearTrend platBuildYearTrend, Long limitNum) {
        LambdaQueryWrapper<PlatBuildYearTrend> lqw = getLambdaQueryWrapper(platBuildYearTrend);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatBuildYearTrend::getCreateTime);
        lqw.last("limit " + limitNum);
        return baseMapper.selectList(lqw);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 年度趋势变化数据
     */
    @Override
    public PlatBuildYearTrend getPlatBuildYearTrendByField(String field, String filedValue) {
        QueryWrapper<PlatBuildYearTrend> lqw = new QueryWrapper<PlatBuildYearTrend>();
        lqw.eq(field, filedValue);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

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

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 年度趋势变化数据
     */
    @Override
    public List<PlatBuildYearTrend> selectPlatBuildYearTrendListByField(String field, String filedValue) {
        QueryWrapper<PlatBuildYearTrend> lqw = new QueryWrapper<PlatBuildYearTrend>();
        lqw.eq(field, filedValue);
        return this.list(lqw);
    }

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

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


    /**
     * 设置添加趋势数据
     * type取值
     * 0 碳排放量月度变化趋势
     * 1 年度综合能耗变化趋势图
     * 2 年度电耗变化趋势图
     * 3 年度水耗趋势接口
     * 4 年度非供暖能耗趋势
     */
    @Override
    public void setBuildYearTrendData(PlatBuildYearTrend platBuildYearTrend, List<String> keyList, Map<String, Object> mapData, Integer type, String appOrg){
        platBuildYearTrend.setType(type);
        platBuildYearTrend.setAppOrg(appOrg);
        StringUtils.setFieldValue(platBuildYearTrend, mapData, keyList, 0, PlatBuildYearTrend::setMonthString, String.class);
        StringUtils.setFieldValue(platBuildYearTrend, mapData, keyList, 1, PlatBuildYearTrend::setCurrentValue, BigDecimal.class);
        StringUtils.setFieldValue(platBuildYearTrend, mapData, keyList, 2, PlatBuildYearTrend::setLastYearValue, BigDecimal.class);
        StringUtils.setFieldValue(platBuildYearTrend, mapData, keyList, 3, PlatBuildYearTrend::setCurrentYear, String.class);
        StringUtils.setFieldValue(platBuildYearTrend, mapData, keyList, 4, PlatBuildYearTrend::setLastYear, String.class);
    }

    /**
     * 添加趋势数据
     */
    @Override
    public void addPlatBuildYearTrend(PlatBuildYearTrend platBuildYearTrend,String appOrg){
        LambdaQueryWrapper<PlatBuildYearTrend> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PlatBuildYearTrend::getMonthString, platBuildYearTrend.getMonthString());
        lqw.eq(PlatBuildYearTrend::getCurrentYear, platBuildYearTrend.getCurrentYear());
        lqw.eq(PlatBuildYearTrend::getLastYear, platBuildYearTrend.getLastYear());
        lqw.eq(PlatBuildYearTrend::getType, platBuildYearTrend.getType());
        if(StringUtils.isNotBlank(appOrg)){
            lqw.eq(PlatBuildYearTrend::getAppOrg, appOrg);
        }
        lqw.last("limit 1");
        PlatBuildYearTrend trendInfo = this.getOne(lqw);
        if(StringUtils.isNotNull(trendInfo)){
            trendInfo.setCurrentValue(platBuildYearTrend.getCurrentValue());
            trendInfo.setLastYearValue(platBuildYearTrend.getLastYearValue());
            this.updateById(trendInfo);
        }else{
            this.save(platBuildYearTrend);
        }
    }

    /**
     * 获取趋势折线图图数据
     * @param type 0 碳排放量月度变化趋势1 年度综合能耗变化趋势图 2 年度电耗变化趋势图 3 年度水耗趋势接口 4 年度非供暖能耗趋势
     */
    @Override
    public Map<String, Object> getLineStatisticsYearTrendData(Integer type){
        List<String> xList = Arrays.asList("1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月");
        // 获取当前年份
        LocalDate today = LocalDate.now();
        int year = today.getYear();
        int lastYear = year - 1;
        List<String> nameList = Arrays.asList(year + "年", lastYear + "年");
        List<String> colorList = Arrays.asList("#c27518", "#02e1e0");
        List<String> keyList = Arrays.asList("year"+year,"year"+lastYear);
        List<List<BigDecimal>> valueList = Arrays.asList(StringUtils.getDefaultListBigDecimal(12),StringUtils.getDefaultListBigDecimal(12));
        List<PlatBuildYearTrend> list = this.selectPlatBuildYearTrendListByField("type", type.toString());
        if(!list.isEmpty()){
            for (PlatBuildYearTrend trend : list){
                valueList.get(0).set(xList.indexOf(trend.getMonthString()), trend.getCurrentValue());
                valueList.get(1).set(xList.indexOf(trend.getMonthString()), trend.getLastYearValue());
            }
        }
        Map<String,Object> yMap = new HashMap<>();
        for (int i = 0; i < nameList.size(); i++){
            Map<String,Object> tempMap = new HashMap<>();
            tempMap.put("name",nameList.get(i));
            tempMap.put("color",colorList.get(i));
            tempMap.put("dataList",valueList.get(i));
            yMap.put(keyList.get(i),tempMap);
        }
        yMap.put("xList",xList);
        return yMap;
    }
}
