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

import cn.com.victorysoft.business.indicator.dao.SectionRateSummaryDao;
import cn.com.victorysoft.business.indicator.entity.SectionRateSummary;
import cn.com.victorysoft.business.indicator.param.WellSectionMonthParam;
import cn.com.victorysoft.business.sys.entity.SysUnitinfo;
import cn.com.victorysoft.business.sys.service.SysBaseService;
import cn.com.victorysoft.business.util.DateUtils;
import cn.com.victorysoft.business.util.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SectionRateSummaryService {

    @Resource
    private SectionRateSummaryDao sectionRateSummaryDao;
    @Resource
    private SysBaseService sysBaseService;
    @Resource
    private UnitMonthService unitMonthService;

    /**
     * 查询本单位及子单位数据
     */
    public List<SectionRateSummary> selectUnitChildren(WellSectionMonthParam param){
        setColumnNames(param);
        List<SectionRateSummary> list = sectionRateSummaryDao.selectUnitChildren(param);
        setUnitname(param.getUnitcode(), list);
        return list;
    }

    /**
     * 查询单位范围数据
     */
    public List<SectionRateSummary> selectRange(WellSectionMonthParam param, int previous){
        setColumnNames(param);
        param.setMonth1(DateUtils.getPreviousMonth(param.getMonth2(), previous));
        return sectionRateSummaryDao.selectRange(param);
    }

    /**
     * 分层统计
     */
    public List<SectionRateSummary> summaryByInjectType(WellSectionMonthParam param) {
        setColumnNames(param);
        List<SectionRateSummary> list = sectionRateSummaryDao.summaryByInjectType(param);
        setUnitname(param.getUnitcode(), list);
        return list;
    }

    /**
     * 单注统计
     */
    public List<SectionRateSummary> summarySingle(WellSectionMonthParam param) {
        setColumnNames(param);
        List<SectionRateSummary> list = sectionRateSummaryDao.summarySingle(param);
        setUnitname(param.getUnitcode(), list);
        return list;
    }

    /**
     * 单元统计
     */
    public List<SectionRateSummary> summaryBlock(WellSectionMonthParam param) {
        setColumnNames(param);
        List<SectionRateSummary> list = sectionRateSummaryDao.summaryBlock(param);
        setUnitname(param.getUnitcode(), list);
        return list;
    }

    /**
     * 分注统计
     */
    public List<SectionRateSummary> summarySplit(WellSectionMonthParam param) {
        setColumnNames(param);
        List<SectionRateSummary> list = sectionRateSummaryDao.summarySplit(param);
        setUnitname(param.getUnitcode(), list);
        return list;
    }

    private void setColumnNames(WellSectionMonthParam param) {
        String[] unitColumn = sysBaseService.getUnitColumns(param.getUnitcode());
        param.setSearchUnitColumn(unitColumn[0]);
        param.setUnitColumn(unitColumn[1]);
    }

    private void setUnitname(String unitcode, List<SectionRateSummary> list) {
        if (list.size() > 0 && StringUtils.isEmpty(list.get(0).getUnitcode())) {
            SysUnitinfo unitinfo = sysBaseService.selectUnitByCode(unitcode);
            list.get(0).setUnitcode(unitinfo.getUnitcode());
            list.get(0).setUnitname(unitinfo.getUnitname());
        }
    }

    /**
     * 汇总单位数据
     */
    @Async
    @Transactional
    public void summary(String month) {
        // 不再使用统计表，直接从井月度数据统计
        sectionRateSummaryDao.deleteByMonth(month);
        sectionRateSummaryDao.summary(month, "unitcode");
        sectionRateSummaryDao.summary(month, "unitcode_area");
        sectionRateSummaryDao.summary(month, "unitcode_company");
        sectionRateSummaryDao.summary(month, "unitcode_group");
        // 同步月度数据
        unitMonthService.syncUnitMonth(month);
    }

    /**
     * 导出注水井分注层段合格率月报
     */
    public List<Map<String, Object>> zsjExport(List<SectionRateSummary> list) {

        Map<String, Object> hy = new HashMap<>();
        Map<String, Object> he = new HashMap<>();
        Map<String, Object> hs = new HashMap<>();
        Map<String, Object> hsi = new HashMap<>();
        Map<String, Object> cyc = new HashMap<>();
        hy.put("dwmc", "海一采油管理区");
        he.put("dwmc", "海二采油管理区");
        hs.put("dwmc", "海三采油管理区");
        hsi.put("dwmc", "海四采油管理区");
        cyc.put("dwmc", "采油厂");

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getUnitcode().equals("30200320")) {
                hy.put("zjs", list.get(i).getTotalWell());
                hy.put("kjs", list.get(i).getOpenWell());
                hy.put("jccds", list.get(i).getCheckSection());
                hy.put("hgcds", list.get(i).getNormalSection());
                hy.put("fzjcdhgl", list.get(i).getNormalRate());
                hy.put("njh", "");
            }
            if (list.get(i).getUnitcode().equals("30200012")) {
                he.put("zjs", list.get(i).getTotalWell());
                he.put("kjs", list.get(i).getOpenWell());
                he.put("jccds", list.get(i).getCheckSection());
                he.put("hgcds", list.get(i).getNormalSection());
                he.put("fzjcdhgl", list.get(i).getNormalRate());
                he.put("njh", "");
            }
            if (list.get(i).getUnitcode().equals("30200322")) {
                hs.put("zjs", list.get(i).getTotalWell());
                hs.put("kjs", list.get(i).getOpenWell());
                hs.put("jccds", list.get(i).getCheckSection());
                hs.put("hgcds", list.get(i).getNormalSection());
                hs.put("fzjcdhgl", list.get(i).getNormalRate());
                hs.put("njh", "");
            }
            if (list.get(i).getUnitcode().equals("50000407")) {
                hsi.put("zjs", list.get(i).getTotalWell());
                hsi.put("kjs", list.get(i).getOpenWell());
                hsi.put("jccds", list.get(i).getCheckSection());
                hsi.put("hgcds", list.get(i).getNormalSection());
                hsi.put("fzjcdhgl", list.get(i).getNormalRate());
                hsi.put("njh", "");
            }
            if (list.get(i).getUnitcode().equals("30200012")) {
                cyc.put("zjs", list.get(i).getTotalWell());
                cyc.put("kjs", list.get(i).getOpenWell());
                cyc.put("jccds", list.get(i).getCheckSection());
                cyc.put("hgcds", list.get(i).getNormalSection());
                cyc.put("fzjcdhgl", list.get(i).getNormalRate());
                cyc.put("njh", "");
            }
        }

        List<Map<String, Object>> result = new ArrayList<>();
        result.add(hy);
        result.add(he);
        result.add(hs);
        result.add(hsi);
        result.add(cyc);

        return result;
    }


}
