/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.xrtygis.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.Date8Util;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.xrtygis.custom.entity.dataquery.GetMongWater;
import com.tofly.xrtygis.custom.entity.dataquery.WaterPlanSearch;
import com.tofly.xrtygis.custom.entity.enums.GetWater;
import com.tofly.xrtygis.custom.service.zsdata.DataTrendService;
import com.tofly.xrtygis.entity.Costrlfixed;
import com.tofly.xrtygis.entity.Costrlwater;
import com.tofly.xrtygis.service.CostrlfixedService;
import com.tofly.xrtygis.service.CostrlwaterService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.tofly.xrtygis.mapper.CostrlfixedMapper;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 实际制水单变填报信息表
 *
 * @author admin
 * @date Wed Mar 10 00:00:00 CST 2021
 */
@Service("costrlfixedService")
@AllArgsConstructor
public class CostrlfixedServiceImpl extends ServiceImpl<CostrlfixedMapper, Costrlfixed> implements CostrlfixedService {

    private final DataTrendService dataTrendService;



    /**
     * 制水材料
     *
     * @param response
     * @param search
     * @return
     */
    @Override
    public Map getWaterData(HttpServletResponse response, WaterPlanSearch search) {
        String startYear = search.getStartYear();
        String endYear = search.getEndYear();
        String type = search.getType();
        //根据月份展示
        List<Map> monthData = baseMapper.getWaterDataMonth(type, startYear, endYear);
        //根据季度展示
        List<Map> quarterData = baseMapper.getWaterDataQuarter(type, startYear, endYear);
        //根据年份展示
        List<Map> yearData = baseMapper.getWaterDataYear(type, startYear, endYear);
        //数据转换
        Map reslutMap = dataTrendService.transfer(monthData, quarterData, yearData, startYear, endYear);

        return reslutMap;
    }

    @Override
    public Map getUnitCost(HttpServletResponse response, WaterPlanSearch search) {

        String year = search.getYear();
        String month = search.getMonth();
        String yearDay =search.getYearDay();
        Long typeId = search.getTypeId();
        if (StringUtil.isNull(typeId)) {
            throw new ToflyDeniedException("请选择查询类别");
        }
        List<Map> cnameList = baseMapper.getCodeByFId(typeId);
        StringBuilder c = new StringBuilder("");
        cnameList.forEach(k->{
            c.append("nvl("+k.get("ccode")+",0)"+k.get("ccode")+",");
        });
        c.deleteCharAt(c.length()-1);

//        String code = cnameList.stream().collect(Collectors.joining(","));

        //年份查询
        List<Map> monthData = new ArrayList<>();
        if (StringUtil.isNotEmpty(year)) {
            List<GetMongWater> costrlwaterList = new ArrayList<>();
            //一厂供水量
            if(GetWater.ONE_GETWATER.getQueryType().equals(typeId) || GetWater.ONE_MATERIS.getQueryType().equals(typeId)) {
                costrlwaterList = baseMapper.getGetWater(GetWater.ONE_GETWATER.getQueryValue(),year);
            }
            //二厂供水量
            if(GetWater.TWO_GETWATER.getQueryType().equals(typeId) || GetWater.TWO_MATERIS.getQueryType().equals(typeId)) {
                costrlwaterList = baseMapper.getGetWater(GetWater.TWO_GETWATER.getQueryValue(),year);
            }
            //总供水量
            if(GetWater.ALL_GETWATER.getQueryType().equals(typeId) || GetWater.ALL_MATERIS.getQueryType().equals(typeId)) {
                costrlwaterList = baseMapper.getGetWater(GetWater.ALL_GETWATER.getQueryValue(),year);
            }
            //查询单位成本数据
            monthData = baseMapper.getUnitCostYear(c.toString(), year);
            if (costrlwaterList.size()>0){
                //转MAP
                Map<String , Double> monthAndGslMap = costrlwaterList.stream().collect(Collectors.toMap(GetMongWater::getMonth , GetMongWater::getGsl));
                if (monthData.size()>0){
                    monthData.forEach(k->{
                        Double gsl = monthAndGslMap.get(k.get("month").toString());
                        k.put("gsl",gsl);
                    });
                }
            }
        }
        else if (StringUtil.isNotEmpty(month) && StringUtil.isNotEmpty(yearDay)) {
            monthData = baseMapper.getUnitCostYearMonth(c.toString(), month , yearDay);
        }
        else {
            return null;
        }
        //得到月份数据 ； 键是月份 ； 值是 map对象结果
        Map mapMap = monthData.stream().collect(Collectors.toMap(m -> m.get("month"), k -> k ,(key1 , key2)-> key2 ));
        Map result = new HashMap();
        result.put("data" , mapMap);
        result.put("code" , cnameList);
        return result;
    }

    @Override
    public Map getWaterRate(HttpServletResponse response, WaterPlanSearch search) {
        String startYear = search.getStartYear();
        String endYear = search.getEndYear();
        String type = search.getType();
        //根据月份展示
        List<Map> monthData = baseMapper.getWaterDataMonth(type, startYear, endYear);
        //根据季度展示
        List<Map> quarterData = baseMapper.getWaterDataQuarter(type, startYear, endYear);
        //根据年份展示
        List<Map> yearData = baseMapper.getWaterDataYear(type, startYear, endYear);
        //数据转换
        Map reslutMap = dataTrendService.transfer(monthData, quarterData, yearData, startYear, endYear);
        return reslutMap;
    }
}
