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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.zcustom.controller.core.DateUtils;
import com.project.zcustom.controller.core.PageParamVo;
import com.project.zcustom.controller.core.StringUtils;
import com.project.zcustom.domain.post.PlatPostStationChart;

import com.project.zcustom.mapper.post.PlatPostStationChartMapper;
import com.project.zcustom.service.service.post.IPlatPostStationChartService;
import org.springframework.stereotype.Service;

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

/**
 * 近六个月报修分类占比Service业务层处理
 *
 * @author change
 * @date 2024-10-21
 */
@Service
public class PlatPostStationChartServiceImpl extends ServiceImpl<PlatPostStationChartMapper, PlatPostStationChart> implements IPlatPostStationChartService {

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

    /**
     * 分页查询近六个月报修分类占比列表
     *
     * @param platPostStationChart 近六个月报修分类占比
     * @return 近六个月报修分类占比集合
     */
    @Override
    public IPage<PlatPostStationChart> pagingPlatPostStationChartList(PlatPostStationChart platPostStationChart) {
        IPage<PlatPostStationChart> page = new Page<>(platPostStationChart.getPageNum(), platPostStationChart.getPageSize());
        LambdaQueryWrapper<PlatPostStationChart> lqw = getLambdaQueryWrapper(platPostStationChart);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatPostStationChart::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 分页查询近六个月报修分类占比列表
     *
     * @param lqw
     * @return 近六个月报修分类占比集合
     */
    @Override
    public IPage<PlatPostStationChart> pagingPlatPostStationChartList(PageParamVo pageParamVo, LambdaQueryWrapper<PlatPostStationChart> lqw) {
        IPage<PlatPostStationChart> page = new Page<>(pageParamVo.getPageNum(), pageParamVo.getPageSize());
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatPostStationChart::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 查询近六个月报修分类占比列表
     *
     * @param platPostStationChart 近六个月报修分类占比
     * @return 近六个月报修分类占比
     */
    @Override
    public List<PlatPostStationChart> selectPlatPostStationChartList(PlatPostStationChart platPostStationChart) {
        LambdaQueryWrapper<PlatPostStationChart> lqw = getLambdaQueryWrapper(platPostStationChart);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatPostStationChart::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询近六个月报修分类占比列表
     *
     * @param platPostStationChart 近六个月报修分类占比
     * @return 近六个月报修分类占比
     */
    @Override
    public List<PlatPostStationChart> selectPlatPostStationChartList(PlatPostStationChart platPostStationChart, Long limitNum) {
        LambdaQueryWrapper<PlatPostStationChart> lqw = getLambdaQueryWrapper(platPostStationChart);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatPostStationChart::getCreateTime);
        lqw.last("limit " + limitNum);
        return baseMapper.selectList(lqw);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 近六个月报修分类占比
     */
    @Override
    public PlatPostStationChart getPlatPostStationChartByField(String field, String filedValue) {
        QueryWrapper<PlatPostStationChart> lqw = new QueryWrapper<PlatPostStationChart>();
        lqw.eq(field, filedValue);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 近六个月报修分类占比
     */
    @Override
    public List<PlatPostStationChart> selectPlatPostStationChartListByField(String field, String filedValue, Long limitNum) {
        QueryWrapper<PlatPostStationChart> lqw = new QueryWrapper<PlatPostStationChart>();
        lqw.eq(field, filedValue);
        lqw.last("limit " + limitNum);
        lqw.orderByAsc("app_month");
        return this.list(lqw);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 近六个月报修分类占比
     */
    @Override
    public List<PlatPostStationChart> selectPlatPostStationChartListByField(String field, String filedValue) {
        QueryWrapper<PlatPostStationChart> lqw = new QueryWrapper<PlatPostStationChart>();
        lqw.eq(field, filedValue);
        lqw.orderByAsc("app_month");
        return this.list(lqw);
    }

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

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

    /**
     * 近六个月报修订单分类
     * @param appOrg
     * @return
     */
    @Override
    public Map<String, Object> getPastSixMonthChartsData(String appOrg){
        QueryWrapper<PlatPostStationChart> lqw = new QueryWrapper<PlatPostStationChart>();
        if(!Objects.equals(appOrg,"0000")){
            lqw.eq("app_org", appOrg);
            lqw.select("app_month,col_num,elevator_num,la_num,se_num,to_num,ar_num,di_num,ot_num,me_num,of_num");
        }else{
            lqw.groupBy("app_month");
            lqw.select("app_month,sum(col_num) as col_num,sum(elevator_num) as elevator_num,sum(la_num) as la_num,sum(se_num) as se_num,sum(to_num) as to_num,sum(ar_num) as ar_num,sum(di_num) as di_num,sum(ot_num) as ot_num,sum(me_num) as me_num,sum(of_num) as of_num");
        }
        lqw.last("limit 6");
        List<PlatPostStationChart> chartList = this.list(lqw);
        // 获取最近六个月的月份
        List<String> monthList = DateUtils.recentSixMonths();
        List<String> xList = monthList.stream().map(month -> {
            // 去month后两位 并转为数字
            return Integer.parseInt(month.substring(5)) + "月";
        }).collect(Collectors.toList());
        List<String> nameList = Arrays.asList("办公家具报修", "冷热水故障", "门禁故障", "其他报修故障", "墙类/地类报修", "电气故障报修", "电梯故障", "消防设施设备故障", "保洁服务", "空调故障");
        List<String> keyList = Arrays.asList("colNum", "elevatorNum", "laNum", "seNum", "toNum", "arNum", "diNum", "otNum", "meNum", "ofNum");
        List<String> colorList = Arrays.asList("#ff9d00", "#2f82fe", "#12db62", "#7565ef", "#079966", "#4e2ffe", "#00f6ec", "#ff7300", "#002aff", "#FF99C3");
        List<Long> defaultList = StringUtils.getDefaultListLong(6);
        List<List<Long>> yList = Arrays.asList(defaultList,defaultList,defaultList,defaultList,defaultList,defaultList,defaultList,defaultList,defaultList,defaultList);
        Map<String,Object> yMap = new HashMap<>();
        if(!chartList.isEmpty()){
            for (int i = 0; i < chartList.size(); i++) {
                yList.get(0).set(i,chartList.get(i).getColNum());
                yList.get(1).set(i,chartList.get(i).getElevatorNum());
                yList.get(2).set(i,chartList.get(i).getLaNum());
                yList.get(3).set(i,chartList.get(i).getSeNum());
                yList.get(4).set(i,chartList.get(i).getToNum());
                yList.get(5).set(i,chartList.get(i).getArNum());
                yList.get(6).set(i,chartList.get(i).getDiNum());
                yList.get(7).set(i,chartList.get(i).getOtNum());
                yList.get(8).set(i,chartList.get(i).getMeNum());
                yList.get(9).set(i,chartList.get(i).getOfNum());
            }
        }
        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",yList.get(i));
            yMap.put(keyList.get(i),tempMap);
        }
        yMap.put("xList",xList);
        return yMap;
    }


}
