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.common.util.DateUtil;
import com.engine.common.util.DbTools;
import com.engine.common.util.ServiceUtil;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

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);
    @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 beforeThreeDayDate = DateUtil.getCurrentDateMinusDay(3);
        String beforeOneDayDate = DateUtil.getCurrentDateMinusDay(1);
        String sql = "select id,userid,signdate,signtime from hrmschedulesign where isincom=1 and signdate >= ? and signdate <= ?";
        List<Map<String,Object>> dataList = DbTools.getSqlToList(sql,beforeThreeDayDate,beforeOneDayDate);

        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,Object> paramMap = Maps.newHashMap();
            paramMap.put("startDate",beforeThreeDayDate);
            paramMap.put("endDate",beforeOneDayDate);
            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);
        }
        log.info("********AttendanceAnalysisJob  end********");
    }
}
