package com.engine.attendance.attendanceanalysis.job;


import com.engine.attendance.attendanceanalysis.wrapper.AttendanceAnalysisWrapper;
import com.engine.attendance.attendanceplan.service.AttendancePlanService;
import com.engine.attendance.attendanceplan.service.impl.AttendancePlanServiceImpl;
import com.engine.attendance.workflow.service.AllowanceService;
import com.engine.attendance.workflow.service.impl.AllowanceServiceImpl;
import com.engine.common.util.DateUtil;
import com.engine.common.util.DbTools;
import com.engine.common.util.ServiceUtil;
import com.engine.common.util.Utils;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

import java.time.ZoneOffset;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class AttendanceAnalysisJob extends BaseCronJob {
    private AttendancePlanService basicsetService = ServiceUtil.getService(AttendancePlanServiceImpl.class);

    private AttendanceAnalysisWrapper attendanceAnalysisService = ServiceUtil.getService(AttendanceAnalysisWrapper.class);

    private AllowanceService allowanceService = ServiceUtil.getService(AllowanceServiceImpl.class);

    private String executeDate;
    @Override
    public void execute() {
        log.info("********AttendanceAnalysisJob  start********");

        String queryUserSql = "select id from hrmresource where  (status = 0 or status = 1 or status = 2 or status = 3)  and status != 10";
        List<Map<String,Object>> userList = DbTools.getSqlToList(queryUserSql);

        String beforeFourDayDate = DateUtil.getCurrentDateMinusDay(4);
        String nowDate = DateUtil.getCurrentDate();
        if (executeDate != null && !"".equals(executeDate)){
            nowDate = executeDate;
            beforeFourDayDate = DateUtil.beforeDay(executeDate,4);
        }
        String sql = "select id,userid,signdate,signtime from hrmschedulesign where isincom=1 and signdate >= ? and signdate <= ? order by signdate,signtime";
        List<Map<String,Object>> dataList = DbTools.getSqlToList(sql,beforeFourDayDate,nowDate);
        //补打卡记录
        sql = "select id,bdkry userid,dkrq signdate,dksj signtime from uf_jcl_kq_bdkjl where dkrq >= ? and dkrq <= ? order by dkrq,dksj";
        List<Map<String,Object>> makeUpList = DbTools.getSqlToList(sql,beforeFourDayDate,nowDate);
        if (makeUpList.size() > 0){
            dataList.addAll(makeUpList);
        }
        dataList = dataList.stream().sorted(Comparator.comparing(e->DateUtil.getTime(e.get("signdate")+" "+e.get("signtime")).toInstant(ZoneOffset.of("+8")).toEpochMilli())).collect(Collectors.toList());


        Map<String, List<Map<String,Object>>> collect = dataList.stream().collect(Collectors.groupingBy(e -> Util.null2String(e.get("userid"))));

        for (Map<String,Object> userMap:userList){
            String userId = Util.null2String(userMap.get("id"));
            //日期对应的打卡数据
            Map<String,Map<String,Object>> clockInTimeMap = Maps.newHashMap();
            /**获得人员考勤项目*/
            Map<String,Object> paramMap = Maps.newHashMap();
            paramMap.put("startDate",beforeFourDayDate);
            paramMap.put("endDate",nowDate);
            paramMap.put("resourceId",userId);
            Map<String,Object> dataMap = basicsetService.getAttendanceItemsByPerson(paramMap);
            List<Map<String,Object>> attendanceItems = (List<Map<String,Object>>)dataMap.get("data");
            /**
             *获得人员综合工时
             */
            Map<String,Object> workHourdataMap = basicsetService.getWorkHoursItemByPerson(paramMap);
            List<Map<String,Object>> workHourItems = (List<Map<String,Object>>)workHourdataMap.get("data");

            attendanceAnalysisService.attendanceAnalysis(userId,collect.get(userMap.get("id")),attendanceItems,workHourItems,clockInTimeMap);
        }
        Map<String,Object> param = Maps.newHashMap();
        param.put("startDate",DateUtil.beforeDay(nowDate,1));
        param.put("endDate",nowDate);
        allowanceService.addAllowanceRecords(param);
        log.info("********AttendanceAnalysisJob  end********");
    }
}
