package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.domain.BMainHazardousWasteOutput;
import cn.cecep.talroad.mapper.BMainHazardousWasteOutputMapper;
import cn.cecep.talroad.service.IBMainHazardousWasteOutputService;
import cn.cecep.talroad.vo.EChartsVO;
import cn.cecep.talroad.vo.query.BMainHazardousWasteOutputQuery;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * 危废产生与处理Service业务层处理
 *
 * @author szhpt
 * @date 2023-01-10
 */
@Service
public class BMainHazardousWasteOutputServiceImpl extends ServiceImpl<BMainHazardousWasteOutputMapper, BMainHazardousWasteOutput> implements IBMainHazardousWasteOutputService {
    @Autowired
    private BMainHazardousWasteOutputMapper bMainHazardousWasteOutputMapper;

    /**
     * 查询危废产生与处理列表
     *
     * @param bMainHazardousWasteOutput 危废产生与处理
     * @return 危废产生与处理
     */
    @Override
    public List<BMainHazardousWasteOutput> selectBMainHazardousWasteOutputList(BMainHazardousWasteOutput bMainHazardousWasteOutput) {
        return bMainHazardousWasteOutputMapper.selectBMainHazardousWasteOutputList(bMainHazardousWasteOutput);
    }

    @Override
    public EChartsVO getCharData(BMainHazardousWasteOutputQuery bMainHazardousWasteOutput) {
        List<BMainHazardousWasteOutput> bMainHazardousWasteOutputs = bMainHazardousWasteOutputMapper.selectList(BMainHazardousWasteOutputQuery.createLambdaQueryWrapper(bMainHazardousWasteOutput));
        List<Map<String, String>> items = new ArrayList<>();
        items.add(new HashMap<String, String>() {{
            put("name", "上月贮存量");
            put("prop", "materialsLastMonth");
        }});
        items.add(new HashMap<String, String>() {{
            put("name", "产生量");
            put("prop", "materialsOutput");
        }});
        items.add(new HashMap<String, String>() {{
            put("name", "修正量");
            put("prop", "materialsAllowance");
        }});
        items.add(new HashMap<String, String>() {{
            put("name", "转移量");
            put("prop", "materialsAsFollows");
        }});
        items.add(new HashMap<String, String>() {{
            put("name", "利用处置量");
            put("prop", "materialsUse");
        }});
        items.add(new HashMap<String, String>() {{
            put("name", "期末库存");
            put("prop", "materialsRepertory");
        }});
        items.add(new HashMap<String, String>() {{
            put("name", "期末超一年库存");
            put("prop", "materialsRepertoryYear");
        }});

        List<DateTime> timeRange = DateUtil.rangeToList(DateUtil.parseDate(bMainHazardousWasteOutput.getStartDate()+"-01"), DateUtil.parseDate(bMainHazardousWasteOutput.getEndDate()+"-01"), DateField.MONTH);
        List<EChartsVO.SeriesObj> series = items.stream().map(item -> {
            EChartsVO.SeriesObj ser = new EChartsVO.SeriesObj();
            ser.setName(item.get("name"));
            ser.setUnit(bMainHazardousWasteOutputs.stream().map(BMainHazardousWasteOutput::getUnit).findFirst().orElse("吨"));
            Object[] values = new Object[timeRange.size()];
            bMainHazardousWasteOutputs.forEach(x -> {
                Object value = BeanUtil.getProperty(x, item.get("prop"));
                DateTime dateTime = DateUtil.parse(x.getParticularYear() + (x.getParticularMonth().length() > 1 ? "" : "0") + x.getParticularMonth(), "yyyyMM");
                values[CollUtil.indexOf(timeRange, y -> DateUtil.compare(y, dateTime, "yyyyMM") == 0)] = value;
            });
            ser.setValues(Arrays.asList(values));
            return ser;
        }).collect(Collectors.toList());
        EChartsVO chartsVO = new EChartsVO();
        chartsVO.setXData(CollUtil.map(timeRange, x -> DateUtil.format(x, "yyyy-MM"), true));
        chartsVO.setSeries(series);
        return chartsVO;
    }

    @Override
    public EChartsVO getAppCharData(BMainHazardousWasteOutputQuery bMainHazardousWasteOutput) {
        List<BMainHazardousWasteOutput> bMainHazardousWasteOutputs = bMainHazardousWasteOutputMapper.selectList(BMainHazardousWasteOutputQuery.createLambdaQueryWrapper(bMainHazardousWasteOutput));
        List<Map<String, String>> items = new ArrayList<>();
        items.add(new HashMap<String, String>() {{
            put("name", "上月贮存量");
            put("prop", "materialsLastMonth");
        }});
        items.add(new HashMap<String, String>() {{
            put("name", "产生量");
            put("prop", "materialsOutput");
        }});
        items.add(new HashMap<String, String>() {{
            put("name", "修正量");
            put("prop", "materialsAllowance");
        }});
        items.add(new HashMap<String, String>() {{
            put("name", "转移量");
            put("prop", "materialsAsFollows");
        }});
        items.add(new HashMap<String, String>() {{
            put("name", "利用处置量");
            put("prop", "materialsUse");
        }});
        items.add(new HashMap<String, String>() {{
            put("name", "期末库存");
            put("prop", "materialsRepertory");
        }});
        items.add(new HashMap<String, String>() {{
            put("name", "期末超一年库存");
            put("prop", "materialsRepertoryYear");
        }});
        List<DateTime> timeRange = DateUtil.rangeToList(DateUtil.parseDate(bMainHazardousWasteOutput.getStartDate()+"-01"), DateUtil.parseDate(bMainHazardousWasteOutput.getEndDate()+"-01"), DateField.MONTH);
        List<EChartsVO.MaterialsSeriesObj> materialsSeries = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(bMainHazardousWasteOutputs)){
            for (BMainHazardousWasteOutput mainHazardousWasteOutput : bMainHazardousWasteOutputs) {
                EChartsVO.MaterialsSeriesObj materialsSeriesObj = new EChartsVO.MaterialsSeriesObj();
                List<EChartsVO.SeriesObj> series = items.stream().map(item -> {
                    EChartsVO.SeriesObj ser = new EChartsVO.SeriesObj();
                    ser.setName(item.get("name"));
                    ser.setUnit(StringUtils.isEmpty(mainHazardousWasteOutput.getUnit()) ? "吨" : mainHazardousWasteOutput.getUnit());
                    Object[] values = new Object[timeRange.size()];
                    Object value = BeanUtil.getProperty(mainHazardousWasteOutput, item.get("prop"));
                    DateTime dateTime = DateUtil.parse(mainHazardousWasteOutput.getParticularYear() + (mainHazardousWasteOutput.getParticularMonth().length() > 1 ? "" : "0") + mainHazardousWasteOutput.getParticularMonth(), "yyyyMM");
                    values[CollUtil.indexOf(timeRange, y -> DateUtil.compare(y, dateTime, "yyyyMM") == 0)] = value;
                    ser.setValue(Arrays.asList(values));
                    return ser;
                }).collect(Collectors.toList());
                materialsSeriesObj.setName(mainHazardousWasteOutput.getMaterialsName());
                materialsSeriesObj.setUnit(mainHazardousWasteOutput.getUnit());
                materialsSeriesObj.setSeries(series);
                materialsSeries.add(materialsSeriesObj);
            }
        }

        EChartsVO chartsVO = new EChartsVO();
        chartsVO.setXData(CollUtil.map(timeRange, x -> DateUtil.format(x, "yyyy-MM"), true));
        chartsVO.setMaterialsSeries(materialsSeries);
        return chartsVO;
    }

}
