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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.mapper.CostrlfixedMapper;
import com.tofly.xrtygis.service.CostrlfixedService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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();

        Map<String,Object> result = new HashMap<>();
        Map<String, Map> month;
        Map<String, Map> resultMonth = new HashMap<>();
        Map<String, Object> quater;
        Map<String, Object> resultQuater = new HashMap<>();
        Map<String, Object> yearMap;
        Map<String, Object> resultYear = new HashMap<>();

        for (String type : search.getType().split(",")) {

            //指标名
            String indexName = baseMapper.getIndexName(type);
            //根据月份展示
            List<Map> monthData = baseMapper.getWaterDataMonth(type, startYear, endYear);
            //根据季度展示
            List<Map> quarterData = baseMapper.getWaterDataQuarterTest(type, startYear, endYear);
            //根据年份展示
            List<Map> yearData = baseMapper.getWaterDataYearTest(type, startYear, endYear);
            //数据转换
            Map<String, Object> reslutMap = dataTrendService.transfer(monthData, quarterData, yearData, startYear, endYear);
            month = (Map<String, Map>) reslutMap.get("month");
            int start = Integer.parseInt(startYear);
            int end = Integer.parseInt(endYear);
            quater = new HashMap<>();
            yearMap = new HashMap<>();
            if (month != null && !month.isEmpty()) {
                for (int i = start; i <= end; i++) {
                    String year = String.valueOf(i);
                    Map map = month.get(year);
                    Double sum = baseMapper.sumWaterDataYear(type, year);
                    if (map != null && map.size() > 0) {
                        map.put("sum", sum);
                    }
                    if (sum != null) {
                        yearMap.put(year, sum);
                    }
                    List<Map> qua = baseMapper.quaWaterDataYear(type, year);
                    if (qua != null && !qua.isEmpty()) {
                        quater.put(year, qua);
                    }
                }
            }
            resultMonth.put(indexName,month);
            resultQuater.put(indexName,quater);
            resultYear.put(indexName,yearMap);
        }

        result.put("month",resultMonth);
        result.put("yearMap",resultYear);
        result.put("quarterMap",resultQuater);
        return result;
    }

    @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("round(" + k.get("ccode") + ",4)" + k.get("ccode") + ",");
        });
        c.deleteCharAt(c.length() - 1);

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

        //年份查询
        List<Map> monthData;
        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);
        if (!StringUtils.isEmpty(year)) {
            result.put("sumData", baseMapper.getUnitCostSumYear(typeId, year));
        }


        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> quarterData = baseMapper.getWaterDataQuarterTest(type, startYear, endYear);
        //根据年份展示
//        List<Map> yearData = baseMapper.getWaterDataYear(type, startYear, endYear);
        List<Map> yearData = baseMapper.getWaterDataYearTest(type, startYear, endYear);
        //数据转换
        return dataTrendService.transfer(monthData, quarterData, yearData, startYear, endYear);
    }
}
