package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.domain.BMainSolidWasteOutput;
import cn.cecep.talroad.mapper.BMainSolidWasteOutputMapper;
import cn.cecep.talroad.service.IBMainSolidWasteOutputService;
import cn.cecep.talroad.vo.EChartsVO;
import cn.cecep.talroad.vo.query.BMainSolidWasteOutputQuery;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
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 BMainSolidWasteOutputServiceImpl extends ServiceImpl<BMainSolidWasteOutputMapper,BMainSolidWasteOutput> implements IBMainSolidWasteOutputService
{
    @Autowired
    private BMainSolidWasteOutputMapper bMainSolidWasteOutputMapper;

    /**
     * 查询固废产生与处理列表
     *
     * @param bMainSolidWasteOutput 固废产生与处理
     * @return 固废产生与处理
     */
    @Override
    public List<BMainSolidWasteOutput> selectBMainSolidWasteOutputList(BMainSolidWasteOutput bMainSolidWasteOutput)
    {
        return bMainSolidWasteOutputMapper.selectBMainSolidWasteOutputList(bMainSolidWasteOutput);
    }

    @Override
    public EChartsVO getCharData(BMainSolidWasteOutputQuery bMainSolidWasteOutput) {
        List<BMainSolidWasteOutput> bMainSolidWasteOutputs = bMainSolidWasteOutputMapper.selectList(BMainSolidWasteOutputQuery.createLambdaQueryWrapper(bMainSolidWasteOutput));

        List<Map<String, String>> items = new ArrayList<>();
        items.add(new HashMap<String, String>() {{
            put("name", "产生量");
            put("prop", "materialsOutput");
        }});
        items.add(new HashMap<String, String>() {{
            put("name", "期初贮存量");
            put("prop", "materialsPreliminaryStage");
        }});
        items.add(new HashMap<String, String>() {{
            put("name", "累计贮存量");
            put("prop", "materialsAccumulated");
        }});
        items.add(new HashMap<String, String>() {{
            put("name", "自行利用数量");
            put("prop", "materialsUse");
        }});
        items.add(new HashMap<String, String>() {{
            put("name", "自行处置数量");
            put("prop", "materialsDispose");
        }});
        items.add(new HashMap<String, String>() {{
            put("name", "委托利用数量");
            put("prop", "materialsEntrustUse");
        }});
        items.add(new HashMap<String, String>() {{
            put("name", "委托处置数量");
            put("prop", "materialsEntrustDispose");
        }});

        List<DateTime> timeRange = DateUtil.rangeToList(DateUtil.parseDate(bMainSolidWasteOutput.getStartDate()), DateUtil.parseDate(bMainSolidWasteOutput.getEndDate()), DateField.MONTH);

        List<EChartsVO.SeriesObj> series = items.stream().map(item -> {
            EChartsVO.SeriesObj ser = new EChartsVO.SeriesObj();
            ser.setName(item.get("name"));
            ser.setUnit("吨");

            Object[] values = new Object[timeRange.size()];
            bMainSolidWasteOutputs.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;
            });

//            List<Object> values = bMainSolidWasteOutputs.stream().map(o -> BeanUtil.getProperty(o, item.get("prop"))).collect(Collectors.toList());
            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;
    }

}
