package cn.com.victorysoft.business.indicator.service;

import cn.com.victorysoft.business.indicator.dao.MoreMeasureSummaryDao;
import cn.com.victorysoft.business.indicator.dao.MoreSummaryDao;
import cn.com.victorysoft.business.indicator.dto.MoreSummaryDto;
import cn.com.victorysoft.business.indicator.entity.MoreMeasureSummary;
import cn.com.victorysoft.business.indicator.entity.MoreSummary;
import cn.com.victorysoft.business.sys.dto.DynamicColumnData;
import cn.com.victorysoft.business.sys.entity.SysFlag;
import cn.com.victorysoft.business.sys.param.UnitMonthParam;
import cn.com.victorysoft.business.sys.param.UnitMonthRangeParam;
import cn.com.victorysoft.business.sys.service.SysDictService;
import cn.com.victorysoft.business.util.DateUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
public class MoreSummaryService {

    @Resource
    private MoreSummaryDao moreSummaryDao;
    @Resource
    private MoreMeasureSummaryDao moreMeasureSummaryDao;
    @Resource
    private SysDictService sysDictService;

    /**
     * 查询本单位数据
     */
    public MoreSummaryDto selectUnit(UnitMonthParam param){
        List<MoreSummary> summaryList = moreSummaryDao.selectUnit(param);
        return summaryList.size() > 0 ? this.parseDto(summaryList).get(0) : new MoreSummaryDto(param);
    }

    /**
     *
     * 查询子单位数据
     */
    public List<MoreSummaryDto> selectChildren(UnitMonthParam param){
        List<MoreSummary> summaryList = moreSummaryDao.selectChildren(param);
        return this.parseDto(summaryList);
    }

    /**
     * 查询本月和上月数据
     */
    public List<MoreSummaryDto> selectMonth2(UnitMonthParam param){
        List<MoreSummaryDto> result = new ArrayList<>(2);
        // 本月
        MoreSummaryDto current = this.selectUnit(param);
        // 上月
        UnitMonthParam preParam = new UnitMonthParam();
        preParam.setUnitcode(param.getUnitcode());
        preParam.setMonth(DateUtils.getPreviousMonth(param.getMonth(), 1));
        MoreSummaryDto previous = this.selectUnit(preParam);
        result.add(current);
        result.add(previous);
        return result;
    }

    /**
     * 查询单位范围数据
     */
    public List<MoreSummaryDto> selectRange(UnitMonthParam param, int previous){
        UnitMonthRangeParam rangeParam = new UnitMonthRangeParam();
        rangeParam.setUnitcode(param.getUnitcode());
        rangeParam.setMonth1(DateUtils.getPreviousMonth(param.getMonth(), previous));
        rangeParam.setMonth2(param.getMonth());
        List<MoreSummary> summaryList = moreSummaryDao.selectRange(rangeParam);
        return this.parseDto(summaryList);
    }

    /**
     * 查询治理对策统计
     */
    public DynamicColumnData selectMeasure(UnitMonthParam param) {
        DynamicColumnData data = new DynamicColumnData();
        // 查询表头
        List<SysFlag> measures = sysDictService.queryDict("MORE_MEASURE");
        for (SysFlag measure : measures) {
            DynamicColumnData.Column column = new DynamicColumnData.Column(measure.getFlagCode(), measure.getFlagName());
            data.getColumnList().add(column);
        }
        DynamicColumnData.Column column = new DynamicColumnData.Column("all", "合计");
        data.getColumnList().add(column);
        // 查询统计数据
        List<MoreMeasureSummary> list = moreMeasureSummaryDao.selectChildren(param);
        list.addAll(moreMeasureSummaryDao.selectUnit(param));
        for (MoreMeasureSummary summary : list) {
            Optional<Map<String, Object>> optional = data.getDataList().stream().filter(map -> {
                return summary.getUnitcode().equals(map.get("unitcode"))
                        && summary.getMonth().equals(map.get("month"));
            }).findAny();
            Map<String, Object> map = optional.orElseGet(() -> {
                Map<String, Object> item = new HashMap<>();
                item.put("unitcode", summary.getUnitcode());
                item.put("unitname", summary.getUnitname());
                item.put("month", summary.getMonth());
                // 放入数据列表中
                data.getDataList().add(item);
                return item;
            });
            map.put(summary.getMeasureId() + "_WELL", summary.getWell());
            map.put(summary.getMeasureId() + "_SECTION", summary.getSection());
            map.put(summary.getMeasureId() + "_WATER", summary.getWater());
        }
        return data;
    }

    /**
     * bean转dto
     */
    private List<MoreSummaryDto> parseDto(List<MoreSummary> list) {
        List<MoreSummaryDto> result = new ArrayList<>();
        for (MoreSummary summary : list) {
            MoreSummaryDto dto = null;
            Optional<MoreSummaryDto> optional = result.stream().filter(item -> item.equalSummary(summary)).findAny();
            if (optional.isPresent()) {
                dto = optional.get();
            }else {
                dto = new MoreSummaryDto(summary);
                result.add(dto);
            }
            dto.setSummaryValue(summary);
        }
        return result;
    }

    /**
     * 汇总单位数据
     */
    @Async
    @Transactional
    public void summary(String month) {
        String prevMonth = DateUtils.getPreviousMonth(month, 1);
        moreSummaryDao.deleteByMonth(month);
        moreSummaryDao.summaryStation(month, prevMonth);
        moreSummaryDao.summaryStationIncrease(month, prevMonth);
        moreSummaryDao.summaryStationReduce(month, prevMonth);
        moreSummaryDao.summaryArea(month);
        moreSummaryDao.summaryCompany(month);
        moreSummaryDao.summaryGroup(month);
    }

    /**
     * 汇总单位措施数据
     */
    @Async
    @Transactional
    public void summaryMeasure(String month) {
        moreMeasureSummaryDao.deleteByMonth(month);
        moreMeasureSummaryDao.summaryStation(month);
        moreMeasureSummaryDao.summaryArea(month);
        moreMeasureSummaryDao.summaryCompany(month);
        moreMeasureSummaryDao.summaryGroup(month);
    }

}
