package org.jeecg.ctop.finance.policy.service.impl;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.jeecg.ctop.finance.policy.service.ICreateSheetService;
import org.jeecg.ctop.finance.policy.utils.Check;
import org.jeecg.ctop.finance.settlement.entity.vo.SheetBytedanceVo;
import org.jeecg.ctop.finance.settlement.entity.vo.SheetKuaiShouVo;
import org.jeecg.ctop.finance.settlement.mapper.ReportSettlementMapper;
import org.jeecg.ctop.finance.utils.SettlementUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jeecg-boot
 * 2021-07-27
 * @version V1.0
 */
@Service
public class CreateSheetServiceImpl implements ICreateSheetService {

    @Resource
    private ReportSettlementMapper reportSettlementMapper;


    public void createBytedance(HSSFWorkbook wb, HSSFCellStyle style, List<String> accountIdList, String startTime, String endTime) {
        DecimalFormat sf = new DecimalFormat("#.####");//保留四位小数
        //2：创建sheet 设置sheet名称
        HSSFSheet sheet = wb.createSheet("汇总页");
        //3：添加 第一行单元格
        HSSFRow row0 = sheet.createRow(0);
        HSSFCell cell0 = row0.createCell(0);
        //设置 第一行 表头
        cell0.setCellValue(startTime + "~" + endTime + "-头条结算单汇总");
        CellRangeAddress region = new CellRangeAddress(0, 0, 0, 10);
        sheet.addMergedRegion(region);//合并行
        cell0.setCellStyle(style);
        //4：添加表头2行
        HSSFRow row = sheet.createRow(1);
        //5：创建第2行的单元格
        HSSFCell cell = row.createCell(0);     //第一个单元格
        cell.setCellValue("日期");         //设定值
        cell.setCellStyle(style);          //内容居中
        cell = row.createCell(1);
        cell.setCellValue("账户名称");
        cell.setCellStyle(style);

        cell = row.createCell(2);
        cell.setCellValue("账户ID");
        cell.setCellStyle(style);

        cell = row.createCell(3);
        cell.setCellValue("所属代理商名称");
        cell.setCellStyle(style);

        cell = row.createCell(4);
        cell.setCellValue("所属代理商ID");
        cell.setCellStyle(style);

        cell = row.createCell(5);
        cell.setCellValue("账户总支出(¥)");
        cell.setCellStyle(style);

        cell = row.createCell(6);
        cell.setCellValue("账户现金支出(¥)");
        cell.setCellStyle(style);

        cell = row.createCell(7);
        cell.setCellValue("账户赠款支出(¥)");
        cell.setCellStyle(style);

        cell = row.createCell(8);
        cell.setCellValue("账户总存入(¥)");
        cell.setCellStyle(style);

        cell = row.createCell(9);
        cell.setCellValue("账户总转入(¥)");
        cell.setCellStyle(style);

        cell = row.createCell(10);
        cell.setCellValue("账户总转出(¥)");
        cell.setCellStyle(style);

        cell = row.createCell(11);
        cell.setCellValue("账户日终结余(¥)");
        cell.setCellStyle(style);

        cell = row.createCell(12);
        cell.setCellValue("冻结(¥)");
        cell.setCellStyle(style);

        cell = row.createCell(13);
        cell.setCellValue("返点类型");
        cell.setCellStyle(style);

        cell = row.createCell(14);
        cell.setCellValue("返点比例");
        cell.setCellStyle(style);

        cell = row.createCell(15);
        cell.setCellValue("政策时间");
        cell.setCellStyle(style);

        cell = row.createCell(16);
        cell.setCellValue("激活");
        cell.setCellStyle(style);

        sheet.autoSizeColumn(0, true);
        List<SheetBytedanceVo> resultList = new ArrayList<>(accountIdList.size());
        //获取当前年月的 第一天 和 最后一天
//        Map<String, String> timeMap = SettlementUtil.getFirstAndLastDayOfMonth(uploadYears);
        //查看账户数据
        List<Map<String, Object>> list = reportSettlementMapper.getAccountTransactionDayBytedanceSheet(accountIdList, startTime, endTime);
        for (Map<String, Object> map : list) {
            SheetBytedanceVo voSource = new SheetBytedanceVo();
            voSource.setAgentId(map.get("agentId") + "");
            voSource.setAgentName(map.get("agentName") + "");
            voSource.setAccountId(map.get("accountId") + "");
            voSource.setAccountName(map.get("accountName") + "");

            //政策所有时间点
            List<String> policyDaysList = new ArrayList<>();
            if (!Check.isNull(map.get("productName"))) {
                //查询 头条利润表  返点类型和比列
                List<Map<String, Object>> rateMap = reportSettlementMapper.getProfitBytedance(map.get("advertiserName") + "", map.get("productName") + "", null, "1", startTime, endTime);
                List listPolicyTIme = new ArrayList();
                for (Map<String, Object> rate : rateMap) {
                    listPolicyTIme.add(rate.get("policyStartDate"));
                    listPolicyTIme.add(rate.get("policyEndDate"));
                }
                //政策时间 去重 排序
                List<Object> times = SettlementUtil.sortlistTest(listPolicyTIme);
                // 使用政策的时间段 拼接 开始 和 截至 时间
                List<Map<String, String>> arrayList = new ArrayList<>();
                for (int i = 0; i < times.size(); i++) {
                    HashMap<String, String> mapTime = new HashMap<>();
                    mapTime.put("startDate", times.get(i).toString());
                    mapTime.put("endDate", times.size() < 2 ? times.get(i).toString() : times.get(i + 1).toString());
                    i += 1;
                    arrayList.add(mapTime);
                }
                for (Map<String, String> stringMap : arrayList) {
                    SheetBytedanceVo vo = new SheetBytedanceVo();
                    //使用政策 返点类型和比例
                    Map<String, Object> policyRateMap = reportSettlementMapper.getProfitRebate(map.get("advertiserName") + "", map.get("productName") + "", null, "1", stringMap.get("startDate"), stringMap.get("endDate"));
                    if (!Check.isNull(policyRateMap)) {
                        if (Integer.valueOf(policyRateMap.get("rebateType").toString()) == 0) {
                            vo.setRebateType("返现");//返点类型
                        } else if (Integer.valueOf(policyRateMap.get("rebateType").toString()) == 1) {
                            vo.setRebateType("返货");
                        } else if (Integer.valueOf(policyRateMap.get("rebateType").toString()) == 2) {
                            vo.setRebateType("返现+返货");
                        } else if (Integer.valueOf(policyRateMap.get("rebateType").toString()) == 3) {
                            vo.setRebateType("服务费");
                        }
                        vo.setRebateRate(policyRateMap.getOrDefault("rebateRate", "") + "%");//返点比列
                    } else {
                        vo.setRebateType("");
                    }
                    // 时间段内 账户流水 信息
                    List<Map<String, Object>> listAccount = reportSettlementMapper.getProfitBytedanceCost(map.get("accountId") + "", stringMap.get("startDate"), stringMap.get("endDate"));
                    listAccount.removeAll(Collections.singleton(null));
                    vo.setCost(Check.isNull(listAccount) ? "0" : listAccount.get(0).getOrDefault("cost", "") + "");//总消耗
                    vo.setCashCost(Check.isNull(listAccount) ? "0" : listAccount.get(0).getOrDefault("cashCost", "") + "");//现金消耗
                    vo.setRewardCost(Check.isNull(listAccount) ? "0" : listAccount.get(0).getOrDefault("rewardCost", "") + "");//赠款消耗
                    vo.setBalance(Check.isNull(listAccount) ? "0" : listAccount.get(0).getOrDefault("balance", "") + "");//日终结余
                    vo.setFrozen(Check.isNull(listAccount) ? "0" : listAccount.get(0).getOrDefault("frozen", "") + "");//冻结
                    vo.setIncome(Check.isNull(listAccount) ? "0" : listAccount.get(0).getOrDefault("income", "") + "");//总存入
                    vo.setSharedWalletCost(Check.isNull(listAccount) ? "0" : listAccount.get(0).getOrDefault("sharedWalletCost", "") + "");//共享钱包支出
                    vo.setTransferIn(Check.isNull(listAccount) ? "0" : listAccount.get(0).getOrDefault("transferIn", "") + "");//总转入
                    vo.setTransferOut(Check.isNull(listAccount) ? "0" : listAccount.get(0).getOrDefault("transferOut", "") + "");//总转出
                    BeanUtils.copyProperties(voSource, vo, SettlementUtil.getNullPropertyNames(voSource));
                    vo.setTime(stringMap.get("startDate") + "----" + stringMap.get("endDate"));

                    String active = reportSettlementMapper.getBytedanceActive(map.get("accountId") + "", stringMap.get("startDate"), stringMap.get("endDate"));
                    vo.setActive(active);
                    resultList.add(vo);
                    policyDaysList.addAll(SettlementUtil.getAllDatesOfTwoTimes(stringMap.get("startDate"), stringMap.get("endDate")));
                }

            }

            //不使用政策 的时间段
            // 获取两个日期之间 所有的 时间 年-月-日
            List<String> betweenDaysList = SettlementUtil.getAllDatesOfTwoTimes(startTime, endTime);
            // 时间差集（所有时间段 - 使用政策的时间段）
            List<String> reduceList = betweenDaysList.stream().filter(item -> !policyDaysList.contains(item)).collect(Collectors.toList());
            //差集拼接  开始 和 结束时间
            List<Map<String, String>> stringList = SettlementUtil.getTime(reduceList);
            for (Map<String, String> stringMap : stringList) {
                SheetBytedanceVo vo = new SheetBytedanceVo();
                // 不使用 政策 则 返点为空
                //政策 返点类型和比例
                vo.setRebateType("");//返点类型
                vo.setRebateRate("");//返点比列
                //账户流水 信息
                List<Map<String, Object>> listAccount = reportSettlementMapper.getProfitBytedanceCost(map.get("accountId") + "", stringMap.get("startDate"), stringMap.get("endDate"));
                listAccount.removeAll(Collections.singleton(null));
                vo.setCost(Check.isNull(listAccount) ? "0" : listAccount.get(0).getOrDefault("cost", "") + "");//总消耗
                vo.setCashCost(Check.isNull(listAccount) ? "0" : listAccount.get(0).getOrDefault("cashCost", "") + "");//现金消耗
                vo.setRewardCost(Check.isNull(listAccount) ? "0" : listAccount.get(0).getOrDefault("rewardCost", "") + "");//赠款消耗
                vo.setBalance(Check.isNull(listAccount) ? "0" : listAccount.get(0).getOrDefault("balance", "") + "");//日终结余
                vo.setFrozen(Check.isNull(listAccount) ? "0" : listAccount.get(0).getOrDefault("frozen", "") + "");//冻结
                vo.setIncome(Check.isNull(listAccount) ? "0" : listAccount.get(0).getOrDefault("income", "") + "");//总存入
                vo.setSharedWalletCost(Check.isNull(listAccount) ? "0" : listAccount.get(0).getOrDefault("sharedWalletCost", "") + "");//共享钱包支出
                vo.setTransferIn(Check.isNull(listAccount) ? "0" : listAccount.get(0).getOrDefault("transferIn", "") + "");//总转入
                vo.setTransferOut(Check.isNull(listAccount) ? "0" : listAccount.get(0).getOrDefault("transferOut", "") + "");//总转出
                BeanUtils.copyProperties(voSource, vo, SettlementUtil.getNullPropertyNames(voSource));
                vo.setTime((!Check.isNull(vo.getRebateType()) ? stringMap.get("startDate") + "----" + stringMap.get("endDate") : ""));
                String active = reportSettlementMapper.getBytedanceActive(map.get("accountId") + "", stringMap.get("startDate"), stringMap.get("endDate"));
                vo.setActive(active);
                resultList.add(vo);
            }
        }

        for (int i = 0; i < resultList.size(); i++) {
            SheetBytedanceVo vo = resultList.get(i);
            //创建行 （表头占0-1行 所以 从 2开始）
            row = sheet.createRow(i + 2);
            sheet.autoSizeColumn(0, true);
            sheet.autoSizeColumn(i + 1, true);
            //创建单元格并且添加数据
            row.createCell(0).setCellValue(startTime + "~" + endTime);
            row.createCell(1).setCellValue(vo.getAccountName());
            row.createCell(2).setCellValue(vo.getAccountId());
            row.createCell(3).setCellValue(vo.getAgentName());
            row.createCell(4).setCellValue(vo.getAgentId());
            row.createCell(5).setCellValue(sf.format(new BigDecimal(vo.getCost())));
            row.createCell(6).setCellValue(sf.format(new BigDecimal(vo.getCashCost())));
            row.createCell(7).setCellValue(sf.format(new BigDecimal(vo.getRewardCost())));
            row.createCell(8).setCellValue(sf.format(new BigDecimal(vo.getIncome())));
            row.createCell(9).setCellValue(sf.format(new BigDecimal(vo.getTransferIn())));
            row.createCell(10).setCellValue(sf.format(new BigDecimal(vo.getTransferOut())));
            row.createCell(11).setCellValue(sf.format(new BigDecimal(vo.getBalance())));
            row.createCell(12).setCellValue(sf.format(new BigDecimal(vo.getFrozen())));
            row.createCell(13).setCellValue(vo.getRebateType());
            row.createCell(14).setCellValue(vo.getRebateRate());
            row.createCell(15).setCellValue(vo.getTime());
            row.createCell(16).setCellValue(vo.getActive());
            // row.setRowStyle(style);
        }
    }

    @Override
    public void createKuaiShou(HSSFWorkbook wb, HSSFCellStyle style, List<String> accountIdList, String startTime, String endTime) {
        DecimalFormat sf = new DecimalFormat("#.####");//保留四位小数
        //2：创建sheet 设置sheet名称
        HSSFSheet sheet = wb.createSheet("汇总页");
        //3：添加 第一行单元格
        HSSFRow row0 = sheet.createRow(0);
        HSSFCell cell0 = row0.createCell(0);
        //设置 第一行 表头
        cell0.setCellValue(startTime + "~" + endTime + "-快手结算单汇总");
        CellRangeAddress region = new CellRangeAddress(0, 0, 0, 10);
        sheet.addMergedRegion(region);//合并行
        cell0.setCellStyle(style);
        //4：添加表头2行
        HSSFRow row = sheet.createRow(1);
        //5：创建第2行的单元格
        HSSFCell cell = row.createCell(0);     //第一个单元格
        cell.setCellValue("日期");         //设定值
        cell.setCellStyle(style);          //内容居中

        cell = row.createCell(1);
        cell.setCellValue("账户快手ID");
        cell.setCellStyle(style);

        cell = row.createCell(2);
        cell.setCellValue("广告主账户ID");
        cell.setCellStyle(style);

        cell = row.createCell(3);
        cell.setCellValue("广告主账户名称");
        cell.setCellStyle(style);


        cell = row.createCell(4);
        cell.setCellValue("日总花费");
        cell.setCellStyle(style);

        cell = row.createCell(5);
        cell.setCellValue("财务流水");
        cell.setCellStyle(style);


        cell = row.createCell(6);
        cell.setCellValue("充值花费");
        cell.setCellStyle(style);

        cell = row.createCell(7);
        cell.setCellValue("框返花费");
        cell.setCellStyle(style);

        cell = row.createCell(8);
        cell.setCellValue("激励花费");
        cell.setCellStyle(style);

        cell = row.createCell(9);
        cell.setCellValue("充值转入");
        cell.setCellStyle(style);

        cell = row.createCell(10);
        cell.setCellValue("框返转入");
        cell.setCellStyle(style);

        cell = row.createCell(11);
        cell.setCellValue("激励转入");
        cell.setCellStyle(style);

        cell = row.createCell(12);
        cell.setCellValue("总转入");
        cell.setCellStyle(style);

        cell = row.createCell(13);
        cell.setCellValue("转出");
        cell.setCellStyle(style);

        cell = row.createCell(14);
        cell.setCellValue("日终结余");
        cell.setCellStyle(style);

        cell = row.createCell(15);
        cell.setCellValue("返点类型");
        cell.setCellStyle(style);

        cell = row.createCell(16);
        cell.setCellValue("返点比例");
        cell.setCellStyle(style);

        cell = row.createCell(17);
        cell.setCellValue("政策时间");
        cell.setCellStyle(style);

        cell = row.createCell(18);
        cell.setCellValue("激活");
        cell.setCellStyle(style);

        sheet.autoSizeColumn(0, true);

        List<SheetKuaiShouVo> resultList = new ArrayList<>(accountIdList.size());
        //查询该时段 快手 数据
        //获取当前年月的 第一天 和 最后一天
//        Map<String,String> timeMap = SettlementUtil.getFirstAndLastDayOfMonth(uploadYears);
        List<Map<String, Object>> accountList = reportSettlementMapper.getProfitKuaiShouAccountCostSheet(accountIdList, startTime, endTime);
        for (Map<String, Object> map : accountList) {
            SheetKuaiShouVo voSource = new SheetKuaiShouVo();
            voSource.setAccountId(map.get("accountId") + "");
            voSource.setKsId(map.get("ksId") + "");
            voSource.setAccountName(map.get("accountName") + "");

            //政策所有时间点
            List<String> policyDaysList = new ArrayList<>();
            if (!Check.isNull(map.get("productName"))) {
                //查询 快手利润表  返点类型和比列
                List<Map<String, Object>> rateMap = reportSettlementMapper.getProfitBytedance(map.get("advertiserName") + "", map.get("productName") + "", null, "2", startTime, endTime);
                List listPolicyTIme = new ArrayList();
                for (Map<String, Object> rate : rateMap) {
                    listPolicyTIme.add(rate.get("policyStartDate"));
                    listPolicyTIme.add(rate.get("policyEndDate"));
                }

                //政策时间 去重 排序
                List<Object> times = SettlementUtil.sortlistTest(listPolicyTIme);
                // 使用政策的时间段 拼接 开始 和 截至 时间
                List<Map<String, String>> arrayList = new ArrayList<>();
                if (!Check.isNull(times)) {
                    for (int i = 0; i < times.size(); i++) {
                        HashMap<String, String> mapTime = new HashMap<>();
                        mapTime.put("startDate", times.get(i).toString());
                        mapTime.put("endDate", times.size() < 2 ? times.get(i).toString() : times.get(i + 1).toString());
                        i += 1;
                        arrayList.add(mapTime);
                    }
                }

                for (Map<String, String> stringMap : arrayList) {
                    SheetKuaiShouVo vo = new SheetKuaiShouVo();
                    //使用政策 返点类型和比例
                    Map<String, Object> policyRateMap = reportSettlementMapper.getProfitRebate(map.get("advertiserName") + "", map.get("productName") + "", null, "2", stringMap.get("startDate"), stringMap.get("endDate"));
                    if (!Check.isNull(policyRateMap)) {
                        if (Integer.valueOf(policyRateMap.get("rebateType").toString()) == 0) {
                            vo.setRebateType("返现");//返点类型
                        } else if (Integer.valueOf(policyRateMap.get("rebateType").toString()) == 1) {
                            vo.setRebateType("返货");
                        } else if (Integer.valueOf(policyRateMap.get("rebateType").toString()) == 2) {
                            vo.setRebateType("返现+返货");
                        } else if (Integer.valueOf(policyRateMap.get("rebateType").toString()) == 3) {
                            vo.setRebateType("服务费");
                        }
                        vo.setRebateRate(policyRateMap.getOrDefault("rebateRate", "") + "%");//返点比列
                    } else {
                        vo.setRebateType("");
                    }

                    // 时间段内 账户流水 信息
                    List<SheetKuaiShouVo> listInfo = reportSettlementMapper.getTransactionDayKuaiShouSheet(map.get("accountId") + "", stringMap.get("startDate"), stringMap.get("endDate"));

                    listInfo.removeAll(Collections.singleton(null));
                    vo.setDailyCharge(listInfo.stream().filter(v -> !Check.isNull(v.getDailyCharge())).map(SheetKuaiShouVo::getDailyCharge).reduce(BigDecimal.ZERO, BigDecimal::add));
                    vo.setFlowDailyCharge(listInfo.stream().filter(v -> !Check.isNull(v.getFlowDailyCharge())).map(SheetKuaiShouVo::getFlowDailyCharge).reduce(BigDecimal.ZERO, BigDecimal::add));
                    vo.setRealCharged(listInfo.stream().filter(v -> !Check.isNull(v.getRealCharged())).map(SheetKuaiShouVo::getRealCharged).reduce(BigDecimal.ZERO, BigDecimal::add));
                    vo.setContractRebateRealCharged(listInfo.stream().filter(v -> !Check.isNull(v.getContractRebateRealCharged())).map(SheetKuaiShouVo::getContractRebateRealCharged).reduce(BigDecimal.ZERO, BigDecimal::add));
                    vo.setDirectRebateRealCharged(listInfo.stream().filter(v -> !Check.isNull(v.getDirectRebateRealCharged())).map(SheetKuaiShouVo::getDirectRebateRealCharged).reduce(BigDecimal.ZERO, BigDecimal::add));
                    vo.setDailyTransferIn(listInfo.stream().filter(v -> !Check.isNull(v.getDailyTransferIn())).map(SheetKuaiShouVo::getDailyTransferIn).reduce(BigDecimal.ZERO, BigDecimal::add));
                    vo.setDailyTransferOut(listInfo.stream().filter(v -> !Check.isNull(v.getDailyTransferOut())).map(SheetKuaiShouVo::getDailyTransferOut).reduce(BigDecimal.ZERO, BigDecimal::add));
                    //vo.setBalance(listInfo.stream().filter(v -> !Check.isNull(v.getBalance())).map(SheetKuaiShouVo::getBalance).reduce(BigDecimal.ZERO, BigDecimal::add));
                    vo.setBalance(Check.isNull(listInfo) ? new BigDecimal(0) : listInfo.stream().filter(v -> !Check.isNull(v.getBalance())).min((u1, u2) -> u1.getBalance().compareTo(u2.getBalance())).get().getBalance());
                    vo.setRealRecharged(listInfo.stream().filter(v -> !Check.isNull(v.getRealRecharged())).map(SheetKuaiShouVo::getRealRecharged).reduce(BigDecimal.ZERO, BigDecimal::add));
                    vo.setContractRebateRealRecharged(listInfo.stream().filter(v -> !Check.isNull(v.getContractRebateRealRecharged())).map(SheetKuaiShouVo::getContractRebateRealRecharged).reduce(BigDecimal.ZERO, BigDecimal::add));
                    vo.setDirectRebateRealRecharged(listInfo.stream().filter(v -> !Check.isNull(v.getDirectRebateRealRecharged())).map(SheetKuaiShouVo::getDirectRebateRealRecharged).reduce(BigDecimal.ZERO, BigDecimal::add));
                    BeanUtils.copyProperties(voSource, vo, SettlementUtil.getNullPropertyNames(voSource));
                    vo.setTime(stringMap.get("startDate") + "----" + stringMap.get("endDate"));
                    String activation = reportSettlementMapper.getKuaiShouActivation(map.get("accountId") + "", stringMap.get("startDate"), stringMap.get("endDate"));
                    vo.setActivation(activation);
                    resultList.add(vo);
                    policyDaysList.addAll(SettlementUtil.getAllDatesOfTwoTimes(stringMap.get("startDate"), stringMap.get("endDate")));
                }
            }

            //不使用政策 的时间段
            // 获取两个日期之间 所有的 时间 年-月-日
            List<String> betweenDaysList = SettlementUtil.getAllDatesOfTwoTimes(startTime, endTime);
            // 时间差集（所有时间段 - 使用政策的时间段）
            List<String> reduceList = betweenDaysList.stream().filter(item -> !policyDaysList.contains(item)).collect(Collectors.toList());
            //差集拼接  开始 和 结束时间
            List<Map<String, String>> stringList = SettlementUtil.getTime(reduceList);
            for (Map<String, String> stringMap : stringList) {
                SheetKuaiShouVo vo = new SheetKuaiShouVo();
                // 不使用 政策 则 返点为空
                //政策 返点类型和比例
                vo.setRebateType("");//返点类型
                vo.setRebateRate("");//返点比列
                // 时间段内 账户流水 信息
                List<SheetKuaiShouVo> listInfo = reportSettlementMapper.getTransactionDayKuaiShouSheet(map.get("accountId") + "", stringMap.get("startDate"), stringMap.get("endDate"));
                listInfo.removeAll(Collections.singleton(null));
                vo.setDailyCharge(listInfo.stream().filter(v -> !Check.isNull(v.getDailyCharge())).map(SheetKuaiShouVo::getDailyCharge).reduce(BigDecimal.ZERO, BigDecimal::add));
                vo.setFlowDailyCharge(listInfo.stream().filter(v -> !Check.isNull(v.getFlowDailyCharge())).map(SheetKuaiShouVo::getFlowDailyCharge).reduce(BigDecimal.ZERO, BigDecimal::add));
                vo.setRealCharged(listInfo.stream().filter(v -> !Check.isNull(v.getRealCharged())).map(SheetKuaiShouVo::getRealCharged).reduce(BigDecimal.ZERO, BigDecimal::add));
                vo.setContractRebateRealCharged(listInfo.stream().filter(v -> !Check.isNull(v.getContractRebateRealCharged())).map(SheetKuaiShouVo::getContractRebateRealCharged).reduce(BigDecimal.ZERO, BigDecimal::add));
                vo.setDirectRebateRealCharged(listInfo.stream().filter(v -> !Check.isNull(v.getDirectRebateRealCharged())).map(SheetKuaiShouVo::getDirectRebateRealCharged).reduce(BigDecimal.ZERO, BigDecimal::add));
                vo.setDailyTransferIn(listInfo.stream().filter(v -> !Check.isNull(v.getDailyTransferIn())).map(SheetKuaiShouVo::getDailyTransferIn).reduce(BigDecimal.ZERO, BigDecimal::add));
                vo.setDailyTransferOut(listInfo.stream().filter(v -> !Check.isNull(v.getDailyTransferOut())).map(SheetKuaiShouVo::getDailyTransferOut).reduce(BigDecimal.ZERO, BigDecimal::add));
                //vo.setBalance(listInfo.stream().filter(v -> !Check.isNull(v.getBalance())).map(SheetKuaiShouVo::getBalance).reduce(BigDecimal.ZERO, BigDecimal::add));
                vo.setBalance(Check.isNull(listInfo) ? new BigDecimal(0) : listInfo.stream().filter(v -> !Check.isNull(v.getBalance())).min((u1, u2) -> u1.getBalance().compareTo(u2.getBalance())).get().getBalance());
                vo.setRealRecharged(listInfo.stream().filter(v -> !Check.isNull(v.getRealRecharged())).map(SheetKuaiShouVo::getRealRecharged).reduce(BigDecimal.ZERO, BigDecimal::add));
                vo.setContractRebateRealRecharged(listInfo.stream().filter(v -> !Check.isNull(v.getContractRebateRealRecharged())).map(SheetKuaiShouVo::getContractRebateRealRecharged).reduce(BigDecimal.ZERO, BigDecimal::add));
                vo.setDirectRebateRealRecharged(listInfo.stream().filter(v -> !Check.isNull(v.getDirectRebateRealRecharged())).map(SheetKuaiShouVo::getDirectRebateRealRecharged).reduce(BigDecimal.ZERO, BigDecimal::add));
                BeanUtils.copyProperties(voSource, vo, SettlementUtil.getNullPropertyNames(voSource));
                vo.setTime(!Check.isNull(vo.getRebateType()) ? stringMap.get("startDate") + "----" + stringMap.get("endDate") : "");
                String activation = reportSettlementMapper.getKuaiShouActivation(map.get("accountId") + "", stringMap.get("startDate"), stringMap.get("endDate"));
                vo.setActivation(activation);
                resultList.add(vo);
            }
        }

        for (int i = 0; i < resultList.size(); i++) {
            SheetKuaiShouVo vo = resultList.get(i);
            //创建行 （表头占0-1行 所以 从 2开始）
            row = sheet.createRow(i + 2);
            sheet.autoSizeColumn(0, true);
            sheet.autoSizeColumn(i + 1, true);
            //创建单元格并且添加数据
            row.createCell(0).setCellValue(startTime + "~" + endTime);
            row.createCell(1).setCellValue(vo.getKsId());
            row.createCell(2).setCellValue(vo.getAccountId());
            row.createCell(3).setCellValue(vo.getAccountName());
            row.createCell(4).setCellValue(sf.format(vo.getDailyCharge()));
            row.createCell(5).setCellValue(sf.format(vo.getFlowDailyCharge()));
            row.createCell(6).setCellValue(sf.format(vo.getRealCharged()));
            row.createCell(7).setCellValue(sf.format(vo.getContractRebateRealCharged()));
            row.createCell(8).setCellValue(sf.format(vo.getDirectRebateRealCharged()));
            row.createCell(9).setCellValue(sf.format(vo.getRealRecharged()));
            row.createCell(10).setCellValue(sf.format(vo.getContractRebateRealRecharged()));
            row.createCell(11).setCellValue(sf.format(vo.getDirectRebateRealRecharged()));
            row.createCell(12).setCellValue(sf.format(vo.getDailyTransferIn()));
            row.createCell(13).setCellValue(sf.format(vo.getDailyTransferOut()));
            row.createCell(14).setCellValue(sf.format(vo.getBalance()));
            row.createCell(15).setCellValue(vo.getRebateType());
            row.createCell(16).setCellValue(vo.getRebateRate());
            row.createCell(17).setCellValue(vo.getTime());
            row.createCell(18).setCellValue(vo.getActivation());
        }
    }


}
