package com.engine.attendance.workflow.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.attendance.workflow.cmd.GetCqSummarySearchConditionCmd;
import com.engine.attendance.workflow.service.AttendanceSummaryService;
import com.engine.common.util.DbTools;
import com.engine.core.impl.Service;
import com.engine.kq.biz.KQHolidaySetBiz;
import com.engine.kq.biz.KQReportBiz;
import com.engine.kq.util.PageUidFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.company.SubCompanyComInfo;
import weaver.hrm.job.JobTitlesComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.systeminfo.SystemEnv;

import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: sy
 * @Description: 出勤汇总实现类
 * @Date: 2024/3/19
 **/
public class AttendanceSummaryServiceImpl extends Service implements AttendanceSummaryService {
    BaseBean bs = new BaseBean();
    /**
     * 新增出勤汇总
     * 基于入参中的出勤汇总主表id,获取主表信息，根据主表中的分部、考勤时间区间等字段，
     * 对出勤结果表、补打卡表、出勤津贴表中数据根据分布和时间区间筛选后，
     * 根据分部、部门、人员id、考勤项目进行分组统计
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> addAttendanceSummary(Map<String, Object> params) {

        bs.writeLog("params : " +  params);
        String mainIds = Util.null2String(params.get("mainIds"));
        bs.writeLog("mainIds : " +  mainIds);
        ExecutorService taskExecutor = Executors.newCachedThreadPool();
        taskExecutor.execute(() -> {
            List<String> mainIdList = new ArrayList<>();
            if (!mainIds.equals("")) {
                mainIdList = Arrays.asList(mainIds.split(","));
            }
            for (String mainId : mainIdList) {
                List<Map<String, String>> addList = new ArrayList<>();
                //查询出勤汇总主表信息
                String sql = "select * from uf_jcl_kq_cqhz where id="+mainId;
                Map<String,Object> data = DbTools.getSqlToMap(sql);
                String subCompany = Util.null2String(data.get("fb"));
                String startDate = Util.null2String(data.get("ksrq"));
                String endDate = Util.null2String(data.get("jsrq"));
                bs.writeLog("subCompany : " +  subCompany + ", startDate : " + startDate + ", endDate : " + endDate);
                if (!subCompany.equals("") && !startDate.equals("") && !endDate.equals("")) {
                    String subCompanyListStr = SubCompanyComInfo.getAllChildSubcompanyId(subCompany, subCompany);
                    //汇总出勤结果表、补打卡表、出勤津贴表数据
                    //出勤结果表
                    List<Map<String, String>> cqAddList = getCqAddList(mainId, subCompanyListStr, startDate, endDate);
                    bs.writeLog("cqAddList_size : " +  cqAddList.size());
                    if (cqAddList.size() > 0) {
                        addList.addAll(cqAddList);
                    }
                    //补打卡表
                    List<Map<String, String>> bdkAddList = getBdkAddList(mainId, startDate, endDate);
                    bs.writeLog("bdkAddList_size : " +  bdkAddList.size());
                    if (bdkAddList.size() > 0) {
                        addList.addAll(bdkAddList);
                    }
                    //出勤津贴表
                    List<Map<String, String>> cqjtAddList = getCqjtAddList(mainId, startDate, endDate);
                    bs.writeLog("cqjtAddList_size : " +  cqjtAddList.size());
                    if (cqjtAddList.size() > 0) {
                        addList.addAll(cqjtAddList);
                    }
                    bs.writeLog("addList_size : " +  addList.size());
                    //删除已有汇总数据
                    String delSql = "delete from uf_jcl_kq_cqhz_dt1 where mainid = " + mainId;
                    boolean delSign = DbTools.update(delSql);
                    bs.writeLog("delSign : " +  delSign + ", delSql : " + delSql);
                    //插入汇总数据
                    List<List> dataList = new ArrayList<>();
                    String insertSql = " insert into uf_jcl_kq_cqhz_dt1(mainid,ry,kqxm," +
                            " lc,lf,ls,lt," +
                            " gzfb,gzbm) values(?,?,?,?,?,?,?,?,?)";
                    for (Map<String, String> map : addList) {
                        List<String> list = new ArrayList<>();
                        list.add(mainId);
                        list.add(map.get("ry"));
                        list.add(map.get("kqxm"));
                        list.add(map.get("lc"));
                        list.add(map.get("lf"));
                        list.add(map.get("ls"));
                        list.add(map.get("lt"));
                        list.add(map.get("gzfb"));
                        list.add(map.get("gzbm"));

                        dataList.add(list);
                    }
                    bs.writeLog("dataList_size : " +  dataList.size());
                    if(dataList.size() > 0){
                        RecordSet rs = new RecordSet();
                        bs.writeLog("入库开始---");
                        boolean insertSign =rs.executeBatchSql(insertSql,dataList);
                        bs.writeLog("insertSign : " + insertSign);
                    }
                }
            }
        });
        return new HashMap<>();
    }

    @Override
    public Map<String, Object> getCqSummaryReport(Map<String, Object> params) {
        Map<String,Object> retmap = new HashMap<String,Object>();
        try{
            String pageUid = PageUidFactory.getHrmPageUid("CqSummaryReport");

            SubCompanyComInfo subCompanyComInfo = new SubCompanyComInfo();
            DepartmentComInfo departmentComInfo = new DepartmentComInfo();
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            JobTitlesComInfo jobTitlesComInfo = new JobTitlesComInfo();

            JSONObject jsonObj = JSON.parseObject(Util.null2String(params.get("data")));
            String attendanceSerial = Util.null2String(jsonObj.get("attendanceSerial"));
            String fromDate = Util.null2String(jsonObj.get("fromDate"));
            String toDate = Util.null2String(jsonObj.get("toDate"));
            String typeSelect =Util.null2String(jsonObj.get("typeselect"));
            if(typeSelect.length()==0) {typeSelect = "3";}
            if(!typeSelect.equals("") && !typeSelect.equals("0")&& !typeSelect.equals("6")){
                if(typeSelect.equals("1")){
                    fromDate = TimeUtil.getCurrentDateString();
                    toDate = TimeUtil.getCurrentDateString();
                }else{
                    fromDate = TimeUtil.getDateByOption(typeSelect,"0");
                    toDate = TimeUtil.getDateByOption(typeSelect,"1");
                }
            }
            //人员状态
            String status = Util.null2String(jsonObj.get("status"));
            String subCompanyId = Util.null2String(jsonObj.get("subCompanyId"));
            String departmentId = Util.null2String(jsonObj.get("departmentId"));
            String resourceId = Util.null2String(jsonObj.get("resourceId"));
            bs.writeLog("status : " + status + ", subCompanyId : " + subCompanyId + ", departmentId : " + departmentId + ", resourceId : " + resourceId);
            String allLevel = Util.null2String(jsonObj.get("allLevel"));
            String isNoAccount = Util.null2String(jsonObj.get("isNoAccount"));
            String viewScope = Util.null2String(jsonObj.get("viewScope"));
            String isFromMyAttendance = Util.null2String(jsonObj.get("isFromMyAttendance"));//是否是来自我的考勤的请求，如果是，不加载考勤报表权限共享的限制，不然我的考勤会提示无权限
            int pageIndex = Util.getIntValue(Util.null2String(jsonObj.get("pageIndex")), 1);
            int pageSize =  KQReportBiz.getPageSize(Util.null2String(jsonObj.get("pageSize")),pageUid,user.getUID());
            int count = 0;
            int pageCount = 0;
            int isHavePre = 0;
            int isHaveNext = 0;
            //设置列头
            List<Object> columns = new ArrayList();
            Map<String,Object> column = null;
            //人员相关信息
            column = new HashMap<>();
            column.put("title", SystemEnv.getHtmlLabelName(413, user.getLanguage())); //姓名
            column.put("unit", "");
            column.put("dataIndex", "lastname");
            column.put("type", "lastname");
            column.put("key", "lastname");
            column.put("rowSpan", 3);
            column.put("width", 105);
            column.put("showDetial", "1");
            columns.add(column);

            column = new HashMap<>();
            column.put("title", SystemEnv.getHtmlLabelName(528842, user.getLanguage())); //分部
            column.put("unit", "");
            column.put("dataIndex", "subcompany");
            column.put("type", "subcompany");
            column.put("key", "subcompany");
            column.put("rowSpan", 3);
            column.put("width", 105);
            column.put("showDetial", "1");
            columns.add(column);

            column = new HashMap<>();
            column.put("title", SystemEnv.getHtmlLabelName(528843, user.getLanguage())); //部门
            column.put("unit", "");
            column.put("dataIndex", "department");
            column.put("type", "department");
            column.put("key", "department");
            column.put("rowSpan", 3);
            column.put("width", 105);
            column.put("showDetial", "1");
            columns.add(column);
            //考勤项目
            String kqxmSql = "select * from uf_jcl_kq_kqxm";
            List<Map<String,Object>> kqxmDates = DbTools.getSqlToList(kqxmSql);
            for(Map<String,Object> kqxm : kqxmDates) {
                String unit = getKqxmUnit(Util.null2String(kqxm.get("hsdw")));
                column = new HashMap<>();
                column.put("title", kqxm.get("mc"));
                column.put("unit", unit);
                column.put("dataIndex", "kqxm" + "_" + kqxm.get("id"));
                column.put("type", "kqxm" + "_" + kqxm.get("id"));
                column.put("key", "kqxm" + "_" + kqxm.get("id"));
                column.put("rowSpan", 3);
                column.put("width", 105);
                column.put("showDetial", "1");
                columns.add(column);
            }
            bs.writeLog("columns : " + columns);
            //获取列表结果，typeSelect为9、10时查询出勤汇总明细表数据，其他则生成临时汇总数据
            List<Map<String,Object>> dtDates = null;
            LocalDate targetDate = LocalDate.now(); // 获取当前日期
            bs.writeLog("currentDate : " + targetDate);
            if (typeSelect.equals("9")) {
                String targetYear = String.valueOf(targetDate.getYear());
                String targetMonth = String.valueOf(targetDate.getMonthValue());
                bs.writeLog("targetYear : " + targetYear + ", targetMonth : " + targetMonth);
                dtDates = getDtDates(targetYear, targetMonth, status, subCompanyId, departmentId, resourceId);
            } else if (typeSelect.equals("10")) {
                targetDate = targetDate.minusMonths(1);
                String targetYear = String.valueOf(targetDate.getYear());
                String targetMonth = String.valueOf(targetDate.getMonthValue());
                bs.writeLog("targetYear : " + targetYear + ", targetMonth : " + targetMonth);
                dtDates = getDtDates(targetYear, targetMonth, status, subCompanyId, departmentId, resourceId);
            } else {
                //调用临时汇总数据方法，生成数据
                dtDates = getTemAttendanceSummary(jsonObj);
            }
            bs.writeLog("dtDates_size : " +  (dtDates==null ? "null" : dtDates.size()));
            //处理明细表数据，分组生成列表数据
            Map<String, Map<String, String>> groupData = new HashMap<>();
            for (Map<String, Object> map : dtDates) {
                String groupKey = map.get("ry") + "_" + map.get("gzfb") + "_" + map.get("gzbm");
                Map<String, String> itemMap = new HashMap<>();
                if (groupData.get(groupKey) == null) {
                    //尚未存在则新建，加入
                    itemMap.put("resourceId", Util.null2String(map.get("ry")));
                    itemMap.put("lastname", resourceComInfo.getLastname(Util.null2String(map.get("ry"))));
                    itemMap.put("subcompanyId", Util.null2String(map.get("gzfb")));
                    itemMap.put("subcompany", subCompanyComInfo.getSubcompanyname(Util.null2String(map.get("gzfb"))));
                    itemMap.put("departmentId", Util.null2String(map.get("gzbm")));
                    itemMap.put("department", departmentComInfo.getDepartmentName(Util.null2String(map.get("gzbm"))));
                    //设置考勤项目累计数值，累天、累时、累分最多有一个有值，都没有则取累次
                    String cumulativeValue = Util.null2String(map.get("lt")) + Util.null2String(map.get("ls")) + Util.null2String(map.get("lf"));
                    if (!cumulativeValue.equals("")) {
                        itemMap.put("kqxm" + "_" + map.get("kqxm").toString(), cumulativeValue);
                    } else {
                        itemMap.put("kqxm" + "_" + map.get("kqxm").toString(), map.get("lc").toString());
                    }
                    groupData.put(groupKey, itemMap);
                } else {
                    //已存在则新增元素
                    itemMap = groupData.get(groupKey);
                    //设置考勤项目累计数值，累天、累时、累分最多有一个有值，都没有则取累次
                    String cumulativeValue = Util.null2String(map.get("lt")) + Util.null2String(map.get("ls")) + Util.null2String(map.get("lf"));
                    if (!cumulativeValue.equals("")) {
                        itemMap.put("kqxm" + "_" + map.get("kqxm").toString(), cumulativeValue);
                    } else {
                        itemMap.put("kqxm" + "_" + map.get("kqxm").toString(), map.get("lc").toString());
                    }
                    groupData.put(groupKey, itemMap);
                }
            }
            //获取最终的list，根据人员id排序
            List<Map<String, String>> dataList = new ArrayList<>(groupData.values());
            Collections.sort(dataList, new Comparator<Map<String, String>>() {
                @Override
                public int compare(Map<String, String> o1, Map<String, String> o2) {
                    String str1 = o1.get("resourceId").toString();
                    String str2 = o2.get("resourceId").toString();
                    //str1在前，str2在后，默认升序
                    return str1.compareTo(str2);

                }
            });
            //分页处理
            int start = pageIndex * pageSize;
            int end = Math.min((pageIndex + 1) * pageSize, dataList.size());
            List<Map<String, String>> resultList = new ArrayList<>();
            if (start <= dataList.size()) {
                resultList = dataList.subList(start, end);
            }
            count = dataList.size();
            if (count <= 0) {
                pageCount = 0;
            } else {
                pageCount = count / pageSize + ((count % pageSize > 0) ? 1 : 0);
            }
            isHaveNext = (pageIndex + 1 <= pageCount) ? 1 : 0;
            isHavePre = (pageIndex - 1 >= 1) ? 1 : 0;

            List<Object> lsHolidays = KQHolidaySetBiz.getHolidaySetListByScope(""+user.getUID(),fromDate,toDate);
            retmap.put("holidays", lsHolidays);

            retmap.put("columns",columns);
            retmap.put("datas",resultList);
            retmap.put("pagesize", pageSize);
            retmap.put("pageindex", pageIndex);
            retmap.put("count", count);
            retmap.put("pagecount", pageCount);
            retmap.put("ishavepre", isHavePre);
            retmap.put("ishavenext", isHaveNext);
        }catch (Exception e){
            bs.writeLog(e);
        }
        return retmap;
    }

    @Override
    public Map<String, Object> getCqSummarySearchCondition(Map<String, Object> params) {
        return this.commandExecutor.execute(new GetCqSummarySearchConditionCmd(params, user));
    }

    private String getKqxmUnit(String hsdw) {
        if (hsdw.equals("0")) {
            return SystemEnv.getHtmlLabelName(518514, user.getLanguage()); //天
        } else if (hsdw.equals("1")) {
            return SystemEnv.getHtmlLabelName(518515, user.getLanguage()); //小时
        } else if (hsdw.equals("2")) {
            return SystemEnv.getHtmlLabelName(518516, user.getLanguage()); //分钟
        } else if (hsdw.equals("3")) {
            return SystemEnv.getHtmlLabelName(18083, user.getLanguage()); //次
        } else {
            return "";
        }
    }

    private List<Map<String,Object>> getDtDates(String targetYear, String targetMonth, String status, String subCompanyIds, String depIds, String empIds) {
        String mainListSql = "select * from uf_jcl_kq_cqhz where nd= " + targetYear + " and yf = " + targetMonth;
        List<Map<String,Object>> mainDates = DbTools.getSqlToList(mainListSql);
        List<String> mainIds = new ArrayList<>();
        for (Map<String, Object> map : mainDates) {
            mainIds.add(map.get("id").toString());
        }
        bs.writeLog("mainIds : " + mainIds);
        String dtListSql = "select * from uf_jcl_kq_cqhz_dt1 a left join hrmresource b on a.ry = b.id where 1=1";
        if (!status.equals("") && !status.equals("9")) {
            dtListSql = dtListSql + " and b.status = " + status;
        }
        if (!subCompanyIds.equals("")) {
            dtListSql = dtListSql + " and a.gzfb in (" + subCompanyIds + ")";
        }
        if (!depIds.equals("")) {
            dtListSql = dtListSql + " and a.gzbm in (" + depIds + ")";
        }
        if (!empIds.equals("")) {
            dtListSql = dtListSql + " and a.ry in (" + empIds + ")";
        }
        if (mainIds.size() > 0) {
            dtListSql = dtListSql + " and a.mainid in (" + String.join(",", mainIds) + ")";
            List<Map<String,Object>> dtDates = DbTools.getSqlToList(dtListSql);
            bs.writeLog("dtDates_size : " + dtDates.size() + ", dtListSql : " + dtListSql);
            return dtDates;
        } else {
            return new ArrayList<>();
        }

    }

    /**
     * 获取临时出勤汇总数据
     * 基于入参中分部、部门、人员id、人员状态、考勤时间区间等字段，
     * 对出勤结果表、补打卡表、出勤津贴表中数据根据分布和时间区间筛选后，
     * 根据分部、部门、人员id、考勤项目进行分组统计
     * @param params
     * @return
     */
    public List<Map<String, Object>> getTemAttendanceSummary(Map<String, Object> params) {

        bs.writeLog("params : " + params);
        List<Map<String, Object>> addList = new ArrayList<>();
        //处理筛选条件
        //时间范围，今天-1，本周-2，本月-3，本季-4，本年-5，指定日期范围-6，上个月-7，上一年-8
        String dateRange = Util.null2String(params.get("typeselect"));//时间范围
        String startDate = Util.null2String(params.get("fromDate"));
        String endDate = Util.null2String(params.get("toDate"));
        if (dateRange.length() == 0) {
            dateRange = "3";
        }
        if (!dateRange.equals("") && !dateRange.equals("0") && !dateRange.equals("6")) {
            if (dateRange.equals("1")) {
                startDate = TimeUtil.getCurrentDateString();
                endDate = TimeUtil.getCurrentDateString();
            } else {
                startDate = TimeUtil.getDateByOption(dateRange, "0");
                endDate = TimeUtil.getDateByOption(dateRange, "1");
            }
        }
        //查看范围，总部-0，分部-1，部门-2，人员-3
        String viewRange = Util.null2String(params.get("viewScope"));//查看范围
        String subCompanyIds = Util.null2String(params.get("subCompanyId"));//可以多个分部id，以,分隔
        String departmentIds = Util.null2String(params.get("departmentId"));//可以多个部门id，以,分隔
        String empIds = Util.null2String(params.get("resourceId"));//可以多个人员id，以,分隔
        //人员状态，试用-0，正式-1，临时-2，试用延期-3，解聘-4，离职-5，退休-6，无效-7，在职-8，全部-9
        String empStatus = Util.null2String(params.get("status"));//人员状态
        bs.writeLog("startDate : " + startDate + ", endDate : " + endDate + ", subCompanyIds : " +  subCompanyIds +  ", departmentIds : " + departmentIds + ", empIds : " + empIds + ", empStatus : " + empStatus);
        if (!startDate.equals("") && !endDate.equals("")) {
            //临时汇总出勤结果表、补打卡表、出勤津贴表数据
            //出勤结果表
            List<Map<String, Object>> cqAddList = getTemCqAddList(startDate, endDate, subCompanyIds, departmentIds, empIds, empStatus);
            bs.writeLog("cqAddList_size : " +  cqAddList.size());
            if (cqAddList.size() > 0) {
                addList.addAll(cqAddList);
            }
            //补打卡表
            List<Map<String, Object>> bdkAddList = getTemBdkAddList(startDate, endDate, subCompanyIds, departmentIds, empIds, empStatus);
            bs.writeLog("bdkAddList_size : " +  bdkAddList.size());
            if (bdkAddList.size() > 0) {
                addList.addAll(bdkAddList);
            }
            //出勤津贴表
            List<Map<String, Object>> cqjtAddList = getTemCqjtAddList(startDate, endDate, subCompanyIds, departmentIds, empIds, empStatus);
            bs.writeLog("cqjtAddList_size : " +  cqjtAddList.size());
            if (cqjtAddList.size() > 0) {
                addList.addAll(cqjtAddList);
            }

        }
        return addList;
    }

    //汇总出勤结果表
    public List<Map<String, String>> getCqAddList(String mainId, String subCompanyListStr, String startDate, String endDate) {
        String cqSql = "select * from uf_jcl_kq_cqjg where fbid in (" + subCompanyListStr + ") and rq >= '" + startDate +"'" + " and rq <= '" + endDate +"'";
        List<Map<String,Object>> cqDates = DbTools.getSqlToList(cqSql);
        //先根据分部、部门、人员、项目分组
        Map<String, List<Map<String,Object>>> toDealCqDataWithGroup = new HashMap<>();
        for (Map<String,Object> map : cqDates) {
            //出勤状态
            String cqzt = Util.null2String(map.get("cqzt"));
            if (!cqzt.equals("")) {
                //匹配出勤状态对应的考勤项目id
                String kqxmId = cqzt.equals("0") ? "1" : "5";

                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + kqxmId;
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", kqxmId);
                itemMap.put("sc", "1");

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            }
            //日期类型
            String rqlx = Util.null2String(map.get("rqlx"));
            if (!rqlx.equals("")) {
                //匹配日期类型对应的考勤项目id
                String kqxmId = null;
                if (rqlx.equals("0") || rqlx.equals("4")) {
                    kqxmId = "2";
                } else if (rqlx.equals("2") || rqlx.equals("3")) {
                    kqxmId = "3";
                } else if (rqlx.equals("1")) {
                    kqxmId = "4";
                }

                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + kqxmId;
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", kqxmId);
                itemMap.put("sc", "1");

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            }
            //项目1
            if (!Util.null2String(map.get("xm1")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm1");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm1"));
                itemMap.put("sc", map.get("sc1"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目2
            if (!Util.null2String(map.get("xm2")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm2");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm2"));
                itemMap.put("sc", map.get("sc2"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目3
            if (!Util.null2String(map.get("xm3")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm3");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm3"));
                itemMap.put("sc", map.get("sc3"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目4
            if (!Util.null2String(map.get("xm4")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm4");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm4"));
                itemMap.put("sc", map.get("sc4"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目5
            if (!Util.null2String(map.get("xm5")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm5");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm5"));
                itemMap.put("sc", map.get("sc5"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目6
            if (!Util.null2String(map.get("xm6")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm6");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm6"));
                itemMap.put("sc", map.get("sc6"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目7
            if (!Util.null2String(map.get("xm7")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm7");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm7"));
                itemMap.put("sc", map.get("sc7"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目8
            if (!Util.null2String(map.get("xm8")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm8");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm8"));
                itemMap.put("sc", map.get("sc8"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            }

        }
        //处理分组好的数据
        List<Map<String, String>> cqAddList = new ArrayList<>();
        for(List<Map<String,Object>> list : toDealCqDataWithGroup.values()) {
            String ry = list.get(0).get("ygid").toString();
            String kqxm = list.get(0).get("xm").toString();
            String gsfb = list.get(0).get("fbid").toString();
            String gsbm = list.get(0).get("bm").toString();

            int num = 0;
            double time = 0;
            //查询考勤项目核算单位：天,小时,分钟,次,即0，1，2，3
            String kqxmSql = "select * from uf_jcl_kq_kqxm where id = " + kqxm;
            Map<String,Object> kqxmData = DbTools.getSqlToMap(kqxmSql);
            String accountUnit = Util.null2String(kqxmData.get("hsdw"));
            //汇总区间数据
            for (Map<String, Object> map : list) {
                if (!accountUnit.equals("3")) {
                    num++;
                    double sc = Util.null2String(map.get("sc")).equals("") ? 0 : Double.parseDouble(map.get("sc").toString());
                    time += sc;
                } else {
                    num ++;
                }
            }
            //设置出勤汇总子表数据
            Map<String, String> itemMap = new HashMap<>();
            itemMap.put("mainid", mainId);
            itemMap.put("ry", ry);
            itemMap.put("kqxm", kqxm);
            itemMap.put("gzfb", gsfb);
            itemMap.put("gzbm", gsbm);
            itemMap.put("lc", String.valueOf(num));
            if (accountUnit.equals("0")) {
                itemMap.put("lt", String.valueOf(time));
            }
            if (accountUnit.equals("1")) {
                itemMap.put("ls", String.valueOf(time));
            }
            if (accountUnit.equals("2")) {
                itemMap.put("lf", String.valueOf(time));
            }
            cqAddList.add(itemMap);
        }
        return cqAddList;
    }

    //汇总补打卡表
    public List<Map<String, String>> getBdkAddList(String mainId, String startDate, String endDate) {
        String bdkSql = "select a.*, b.fbid, b.bm from uf_jcl_kq_bdkjl a left join uf_jcl_kq_cqjg b on a.bdkry = b.ygid and a.dkrq = b.rq  where a.dkrq >= '" + startDate +"'" + " and a.dkrq <= '" + endDate +"'";
        List<Map<String,Object>> bdkDates = DbTools.getSqlToList(bdkSql);
        //先根据分部、部门、人员、项目分组
        Map<String, List<Map<String,Object>>> toDealBdkDataWithGroup = new HashMap<>();
        for (Map<String,Object> map : bdkDates) {
            //补打卡项目
            if (!Util.null2String(map.get("bdklx")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("bdkry") + "_" + map.get("bdklx");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("bdkry", map.get("bdkry"));
                itemMap.put("xm", map.get("bdklx"));
                if (toDealBdkDataWithGroup.get(groupKey) != null) {
                    toDealBdkDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealBdkDataWithGroup.put(groupKey, list);
                }
            }
        }
        //处理分组好的数据
        List<Map<String, String>> bdkAddList = new ArrayList<>();
        for(List<Map<String,Object>> list : toDealBdkDataWithGroup.values()) {
            String ry = list.get(0).get("bdkry").toString();
            String kqxm = list.get(0).get("xm").toString();
            String gsfb = list.get(0).get("fbid").toString();
            String gsbm = list.get(0).get("bm").toString();
            int num = list.size();
            //设置出勤汇总子表数据
            Map<String, String> itemMap = new HashMap<>();
            itemMap.put("mainid", mainId);
            itemMap.put("ry", ry);
            itemMap.put("kqxm", kqxm);
            itemMap.put("gzfb", gsfb);
            itemMap.put("gzbm", gsbm);
            itemMap.put("lc", String.valueOf(num));

            bdkAddList.add(itemMap);
        }
        return bdkAddList;
    }

    //汇总出勤津贴表
    public List<Map<String, String>> getCqjtAddList(String mainId, String startDate, String endDate) {
        String cqjtSql = "select a.*, b.fbid, b.bm from uf_jcl_kq_cqjt a left join uf_jcl_kq_cqjg b on a.yg = b.ygid and a.rq = b.rq  where a.rq >= '" + startDate +"'" + " and a.rq <= '" + endDate+"'";
        List<Map<String,Object>> cqjtDates = DbTools.getSqlToList(cqjtSql);
        //先根据分部、部门、人员、项目分组
        Map<String, List<Map<String,Object>>> toDealCqjtDataWithGroup = new HashMap<>();
        for (Map<String,Object> map : cqjtDates) {
            //津贴类型
            if (!Util.null2String(map.get("jtlx")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("bdkry") + "_" + map.get("bdklx");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("yg", map.get("yg"));
                itemMap.put("xm", map.get("jtlx"));
                if (toDealCqjtDataWithGroup.get(groupKey) != null) {
                    toDealCqjtDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqjtDataWithGroup.put(groupKey, list);
                }
            }
        }
        //处理分组好的数据
        List<Map<String, String>> cqjtAddList = new ArrayList<>();
        for(List<Map<String,Object>> list : toDealCqjtDataWithGroup.values()) {
            String ry = list.get(0).get("yg").toString();
            String kqxm = list.get(0).get("xm").toString();
            String gsfb = list.get(0).get("fbid").toString();
            String gsbm = list.get(0).get("bm").toString();
            int num = list.size();
            double time = 0;
            //查询考勤项目核算单位：天,小时,分钟,次,即0，1，2，3
            String kqxmSql = "select * from uf_jcl_kq_kqxm where id = " + kqxm;
            Map<String,Object> kqxmData = DbTools.getSqlToMap(kqxmSql);
            String accountUnit = Util.null2String(kqxmData.get("hsdw"));
            //汇总区间数据
            for (Map<String, Object> map : list) {
                if (!accountUnit.equals("3")) {
                    double sc = Util.null2String(map.get("sc")).equals("") ? 0 : Double.parseDouble(map.get("sc").toString());
                    time += sc;
                }
            }
            //设置出勤汇总子表数据
            Map<String, String> itemMap = new HashMap<>();
            itemMap.put("mainid", mainId);
            itemMap.put("ry", ry);
            itemMap.put("kqxm", kqxm);
            itemMap.put("gzfb", gsfb);
            itemMap.put("gzbm", gsbm);
            itemMap.put("lc", String.valueOf(num));
            if (accountUnit.equals("0")) {
                itemMap.put("lt", String.valueOf(time));
            }
            if (accountUnit.equals("1")) {
                itemMap.put("ls", String.valueOf(time));
            }
            if (accountUnit.equals("2")) {
                itemMap.put("lf", String.valueOf(time));
            }
            cqjtAddList.add(itemMap);
        }
        return cqjtAddList;
    }

    //临时汇总出勤结果表
    private List<Map<String, Object>> getTemCqAddList(String startDate, String endDate, String subCompanyIds, String departmentIds, String empIds, String empStatus) {
        String cqSql = "select a.* from uf_jcl_kq_cqjg a left join hrmresource b on a.ygid = b.id where b.status = " + empStatus + " and a.rq >= '" + startDate +"'" + " and a.rq <= '" + endDate +"'";
        if (!subCompanyIds.equals("")) {
            cqSql = cqSql + " and a.fbid in (" + subCompanyIds + ")";
        }
        if (!departmentIds.equals("")) {
            cqSql = cqSql + " and a.bm in (" + departmentIds + ")";
        }
        if (!empIds.equals("")) {
            cqSql = cqSql + " and a.ygid in (" + empIds + ")";
        }
        List<Map<String,Object>> cqDates = DbTools.getSqlToList(cqSql);
        //先根据分部、部门、人员、项目分组
        Map<String, List<Map<String,Object>>> toDealCqDataWithGroup = new HashMap<>();
        for (Map<String,Object> map : cqDates) {
            //出勤状态
            String cqzt = Util.null2String(map.get("cqzt"));
            if (!cqzt.equals("")) {
                //匹配出勤状态对应的考勤项目id
                String kqxmId = cqzt.equals("0") ? "1" : "5";

                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + kqxmId;
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", kqxmId);
                itemMap.put("sc", "1");

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            }
            //日期类型
            String rqlx = Util.null2String(map.get("rqlx"));
            if (!rqlx.equals("")) {
                //匹配日期类型对应的考勤项目id
                String kqxmId = null;
                if (rqlx.equals("0") || rqlx.equals("4")) {
                    kqxmId = "2";
                } else if (rqlx.equals("2") || rqlx.equals("3")) {
                    kqxmId = "3";
                } else if (rqlx.equals("1")) {
                    kqxmId = "4";
                }

                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + kqxmId;
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", kqxmId);
                itemMap.put("sc", "1");

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            }
            //项目1
            if (!Util.null2String(map.get("xm1")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm1");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm1"));
                itemMap.put("sc", map.get("sc1"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目2
            if (!Util.null2String(map.get("xm2")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm2");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm2"));
                itemMap.put("sc", map.get("sc2"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目3
            if (!Util.null2String(map.get("xm3")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm3");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm3"));
                itemMap.put("sc", map.get("sc3"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目4
            if (!Util.null2String(map.get("xm4")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm4");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm4"));
                itemMap.put("sc", map.get("sc4"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目5
            if (!Util.null2String(map.get("xm5")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm5");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm5"));
                itemMap.put("sc", map.get("sc5"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目6
            if (!Util.null2String(map.get("xm6")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm6");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm6"));
                itemMap.put("sc", map.get("sc6"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目7
            if (!Util.null2String(map.get("xm7")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm7");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm7"));
                itemMap.put("sc", map.get("sc7"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目8
            if (!Util.null2String(map.get("xm8")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm8");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm8"));
                itemMap.put("sc", map.get("sc8"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            }

        }
        //处理分组好的数据
        List<Map<String, Object>> cqAddList = new ArrayList<>();
        for(List<Map<String,Object>> list : toDealCqDataWithGroup.values()) {
            String ry = list.get(0).get("ygid").toString();
            String kqxm = list.get(0).get("xm").toString();
            String gsfb = list.get(0).get("fbid").toString();
            String gsbm = list.get(0).get("bm").toString();

            int num = 0;
            double time = 0;
            //查询考勤项目核算单位：天,小时,分钟,次,即0，1，2，3
            String kqxmSql = "select * from uf_jcl_kq_kqxm where id = " + kqxm;
            Map<String,Object> kqxmData = DbTools.getSqlToMap(kqxmSql);
            String accountUnit = Util.null2String(kqxmData.get("hsdw"));
            //汇总区间数据
            for (Map<String, Object> map : list) {
                if (!accountUnit.equals("3")) {
                    num++;
                    double sc = Util.null2String(map.get("sc")).equals("") ? 0 : Double.parseDouble(map.get("sc").toString());
                    time += sc;
                } else {
                    num ++;
                }
            }
            //设置出勤汇总子表数据
            Map<String, Object> itemMap = new HashMap<>();
            itemMap.put("ry", ry);
            itemMap.put("kqxm", kqxm);
            itemMap.put("gzfb", gsfb);
            itemMap.put("gzbm", gsbm);
            itemMap.put("lc", String.valueOf(num));
            if (accountUnit.equals("0")) {
                itemMap.put("lt", String.valueOf(time));
            }
            if (accountUnit.equals("1")) {
                itemMap.put("ls", String.valueOf(time));
            }
            if (accountUnit.equals("2")) {
                itemMap.put("lf", String.valueOf(time));
            }
            cqAddList.add(itemMap);
        }
        return cqAddList;
    }
    //临时汇总补打卡表
    private List<Map<String, Object>> getTemBdkAddList(String startDate, String endDate, String subCompanyIds, String departmentIds, String empIds, String empStatus) {
        String bdkSql = "select a.*, b.fbid, b.bm from uf_jcl_kq_bdkjl a " +
                "left join uf_jcl_kq_cqjg b on a.bdkry = b.ygid and a.dkrq = b.rq " +
                "left join hrmresource c on a.bdkry = c.id " +
                "where c.status = " + empStatus + " and a.dkrq >= '" + startDate +"'" + " and a.dkrq <= '" + endDate +"'";
        if (!subCompanyIds.equals("")) {
            bdkSql = bdkSql + " and b.fbid in (" + subCompanyIds + ")";
        }
        if (!departmentIds.equals("")) {
            bdkSql = bdkSql + " and b.bm in (" + departmentIds + ")";
        }
        if (!empIds.equals("")) {
            bdkSql = bdkSql + " and a.bdkry in (" + empIds + ")";
        }
        List<Map<String,Object>> bdkDates = DbTools.getSqlToList(bdkSql);
        //先根据分部、部门、人员、项目分组
        Map<String, List<Map<String,Object>>> toDealBdkDataWithGroup = new HashMap<>();
        for (Map<String,Object> map : bdkDates) {
            //补打卡项目
            if (!Util.null2String(map.get("bdklx")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("bdkry") + "_" + map.get("bdklx");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("bdkry", map.get("bdkry"));
                itemMap.put("xm", map.get("bdklx"));
                if (toDealBdkDataWithGroup.get(groupKey) != null) {
                    toDealBdkDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealBdkDataWithGroup.put(groupKey, list);
                }
            }
        }
        //处理分组好的数据
        List<Map<String, Object>> bdkAddList = new ArrayList<>();
        for(List<Map<String,Object>> list : toDealBdkDataWithGroup.values()) {
            String ry = list.get(0).get("bdkry").toString();
            String kqxm = list.get(0).get("xm").toString();
            String gsfb = list.get(0).get("fbid").toString();
            String gsbm = list.get(0).get("bm").toString();
            int num = list.size();
            //设置出勤汇总子表数据
            Map<String, Object> itemMap = new HashMap<>();
            itemMap.put("ry", ry);
            itemMap.put("kqxm", kqxm);
            itemMap.put("gzfb", gsfb);
            itemMap.put("gzbm", gsbm);
            itemMap.put("lc", String.valueOf(num));

            bdkAddList.add(itemMap);
        }
        return bdkAddList;
    }
    //临时汇总出勤津贴表
    private List<Map<String, Object>> getTemCqjtAddList(String startDate, String endDate, String subCompanyIds, String departmentIds, String empIds, String empStatus) {
        String cqjtSql = "select a.*, b.fbid, b.bm from uf_jcl_kq_cqjt a " +
                "left join uf_jcl_kq_cqjg b on a.yg = b.ygid and a.rq = b.rq " +
                "left join hrmresource c on a.yg = c.id " +
                "where c.status = " + empStatus + " and a.rq >= '" + startDate +"'" + " and a.rq <= '" + endDate +"'";
        if (!subCompanyIds.equals("")) {
            cqjtSql = cqjtSql + " and b.fbid in (" + subCompanyIds + ")";
        }
        if (!departmentIds.equals("")) {
            cqjtSql = cqjtSql + " and b.bm in (" + departmentIds + ")";
        }
        if (!empIds.equals("")) {
            cqjtSql = cqjtSql + " and a.yg in (" + empIds + ")";
        }
        List<Map<String,Object>> cqjtDates = DbTools.getSqlToList(cqjtSql);
        //先根据分部、部门、人员、项目分组
        Map<String, List<Map<String,Object>>> toDealCqjtDataWithGroup = new HashMap<>();
        for (Map<String,Object> map : cqjtDates) {
            //津贴类型
            if (!Util.null2String(map.get("jtlx")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("bdkry") + "_" + map.get("bdklx");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("yg", map.get("yg"));
                itemMap.put("xm", map.get("jtlx"));
                if (toDealCqjtDataWithGroup.get(groupKey) != null) {
                    toDealCqjtDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqjtDataWithGroup.put(groupKey, list);
                }
            }
        }
        //处理分组好的数据
        List<Map<String, Object>> cqjtAddList = new ArrayList<>();
        for(List<Map<String,Object>> list : toDealCqjtDataWithGroup.values()) {
            String ry = list.get(0).get("yg").toString();
            String kqxm = list.get(0).get("xm").toString();
            String gsfb = list.get(0).get("fbid").toString();
            String gsbm = list.get(0).get("bm").toString();
            int num = list.size();
            double time = 0;
            //查询考勤项目核算单位：天,小时,分钟,次,即0，1，2，3
            String kqxmSql = "select * from uf_jcl_kq_kqxm where id = " + kqxm;
            Map<String,Object> kqxmData = DbTools.getSqlToMap(kqxmSql);
            String accountUnit = Util.null2String(kqxmData.get("hsdw"));
            //汇总区间数据
            for (Map<String, Object> map : list) {
                if (!accountUnit.equals("3")) {
                    double sc = Util.null2String(map.get("sc")).equals("") ? 0 : Double.parseDouble(map.get("sc").toString());
                    time += sc;
                }
            }
            //设置出勤汇总子表数据
            Map<String, Object> itemMap = new HashMap<>();
            itemMap.put("ry", ry);
            itemMap.put("kqxm", kqxm);
            itemMap.put("gzfb", gsfb);
            itemMap.put("gzbm", gsbm);
            itemMap.put("lc", String.valueOf(num));
            if (accountUnit.equals("0")) {
                itemMap.put("lt", String.valueOf(time));
            }
            if (accountUnit.equals("1")) {
                itemMap.put("ls", String.valueOf(time));
            }
            if (accountUnit.equals("2")) {
                itemMap.put("lf", String.valueOf(time));
            }
            cqjtAddList.add(itemMap);
        }
        return cqjtAddList;
    }
}
