package com.winsun.utils;

import com.alibaba.excel.EasyExcel;
import com.winsun.bean.YrymReport;

import java.io.InputStream;
import java.util.*;

/**
 * 一人一码发展统计表
 *
 * @Author LiuRunYuan
 * @CreateDate 2020/03/07
 */
public class YrymReportUtils {

    public Integer startRow = 0;
    public Integer endRow = 0;

    /**
     * 获取统计表模板县分字段
     *
     * @return
     */
    public LinkedList<String> getSubNameList(InputStream in) {
        List<Object> list = EasyExcel.read(in).sheet(0).doReadSync();
        LinkedList<String> subNameList = new LinkedList<>();

        for (int i = 0; i < list.size(); i++) {
            LinkedHashMap rowList = (LinkedHashMap) list.get(i);
            if (rowList.size() >= 1) {
                String rowStr = rowList.get(0).toString();
                if ("业务类型".equals(rowStr)) {
                    startRow = i + 3;
                }
                if (startRow == 0) {
                    continue;
                }
                if ("合计".equals(rowStr)) {
                    endRow = i + 3;
                    break;
                }
                subNameList.add(rowStr);
            }
        }
        subNameList.removeFirst();
        return subNameList;
    }

    /**
     * 填入合计值
     */
    public static void setAllSum(List<YrymReport> list) {
        YrymReport sum = new YrymReport();
        sum.setSubName("合计");
        for (YrymReport yrymReport : list) {
            sum.setSchoolNum(sum.getSchoolNum() + yrymReport.getSchoolNum());
            sum.setStudentNum(sum.getStudentNum() + yrymReport.getStudentNum());
            sum.setNewUserNumWeek(sum.getNewUserNumWeek() + yrymReport.getNewUserNumWeek());
            sum.setNewAloneNumWeek(sum.getNewAloneNumWeek() + yrymReport.getNewAloneNumWeek());
            sum.setNewMixNumWeek(sum.getNewMixNumWeek() + yrymReport.getNewMixNumWeek());
            sum.setOldUserNumWeek(sum.getOldUserNumWeek() + yrymReport.getOldUserNumWeek());
            sum.setOldContinueNumWeek(sum.getOldContinueNumWeek() + yrymReport.getOldContinueNumWeek());
            sum.setFinishNewNumWeek(sum.getFinishNewNumWeek() + yrymReport.getFinishNewNumWeek());
            sum.setFinishOldNumWeek(sum.getFinishOldNumWeek() + yrymReport.getFinishOldNumWeek());
            sum.setSumWeek(sum.getSumWeek() + yrymReport.getSumWeek());
            sum.setNewUserNumMonth(sum.getNewUserNumMonth() + yrymReport.getNewUserNumMonth());
            sum.setNewAloneNumMonth(sum.getNewAloneNumMonth() + yrymReport.getNewAloneNumMonth());
            sum.setNewMixNumMonth(sum.getNewMixNumMonth() + yrymReport.getNewMixNumMonth());
            sum.setOldUserNumMonth(sum.getOldUserNumMonth() + yrymReport.getOldUserNumMonth());
            sum.setOldContinueNumMonth(sum.getOldContinueNumMonth() + yrymReport.getOldContinueNumMonth());
            sum.setFinishNewNumMonth(sum.getFinishNewNumMonth() + yrymReport.getFinishNewNumMonth());
            sum.setFinishOldNumMonth(sum.getFinishOldNumMonth() + yrymReport.getFinishOldNumMonth());
            sum.setSumMonth(sum.getSumMonth() + yrymReport.getSumMonth());
            sum.setNewUserNumAll(sum.getNewUserNumAll() + yrymReport.getNewUserNumAll());
            sum.setNewAloneNumAll(sum.getNewAloneNumAll() + yrymReport.getNewAloneNumAll());
            sum.setNewMixNumAll(sum.getNewMixNumAll() + yrymReport.getNewMixNumAll());
            sum.setOldUserNumAll(sum.getOldUserNumAll() + yrymReport.getOldUserNumAll());
            sum.setOldContinueNumAll(sum.getOldContinueNumAll() + yrymReport.getOldContinueNumAll());
            sum.setFinishNewNumAll(sum.getFinishNewNumAll() + yrymReport.getFinishNewNumAll());
            sum.setFinishOldNumAll(sum.getFinishOldNumAll() + yrymReport.getFinishOldNumAll());
            sum.setSumAll(sum.getSumAll() + yrymReport.getSumAll());
        }
        list.add(sum);
    }

    /**
     * 填充数据
     *
     * @param resultList 填充目标
     * @param dataList   填充数据
     * @param flag       1：填充学校数量 2：填充学生数量 3：填充统计数量
     * @param type       统计数量时间 week：周 month：月 all：全部
     */
    public static void joinData(List<YrymReport> resultList, List<Map<String, Object>> dataList, int flag, String type) {
        for (YrymReport tempYrym : resultList) {
            Iterator iterator = dataList.iterator();
            while (iterator.hasNext()) {
                Map<String, Object> tempMap = (Map<String, Object>) iterator.next();
                if (tempYrym.getSubName().equals(tempMap.get("subName").toString())) {

                    if (flag == 1) {
                        tempYrym.setSchoolNum(Integer.valueOf(tempMap.get("schoolNum").toString()));
                    } else if (flag == 2) {
                        tempYrym.setStudentNum(Integer.valueOf(tempMap.get("studentNum").toString()));
                    } else {
                        tempYrym.setDataBytype(tempMap, type);
                    }

                    iterator.remove();
                    break;
                }
            }
        }
    }
}
