package com.engine.jucailinkq.attendance.component.AttendanceCycle.job;

import com.engine.jucailinkq.common.util.DbTools;
import com.time.util.DateUtil;
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;

/**
 * @Author: sy
 * @Description: 考勤周期关账
 * @Date: 2024/8/2
 **/
@Slf4j
public class AttendanceCycleCloseJob extends BaseCronJob {
    @Override
    public void execute() {
        log.info("********AttendanceCycleCloseJob  start********");
        //查询当前时间大于等于关账日期+时间的考勤周期
        String currentTime = DateUtil.getCurrentTime("yyyy-MM-dd HH:mm");
        //处理考勤周期主表
        String sql = "select id, gzrq, gzsj, zt from uf_jcl_kq_kqzq where zt = 0";
        List<Map<String, Object>> mainData = DbTools.getSqlToList(sql);
        mainData = mainData.stream().filter(f -> (Util.null2String(f.get("gzrq")) + " " + Util.null2String(f.get("gzsj"))).compareTo(currentTime) <= 0).collect(Collectors.toList());
        if (mainData.size() > 0) {
            List<String> ids = mainData.stream().map(e->e.get("id").toString()).collect(Collectors.toList());
            //更新考勤周期的状态为已关账
            sql = "update uf_jcl_kq_kqzq set zt = 1 where id in (" + String.join(",", ids) + ")";
            boolean updateSign = DbTools.update(sql);
            log.info("AttendanceCycleCloseJob-考勤周期主表更新状态结果：" + updateSign);
        }
        //处理考勤周期明细表
        sql = "select id, gzrq, gzsj, zt from uf_jcl_kq_kqzq_dt1 where zt != 1";
        List<Map<String, Object>> detailData = DbTools.getSqlToList(sql);
        detailData = detailData.stream().filter(f -> (Util.null2String(f.get("gzrq")) + " " + Util.null2String(f.get("gzsj"))).compareTo(currentTime) <= 0).collect(Collectors.toList());
        if (detailData.size() > 0) {
            List<String> ids = detailData.stream().map(e->e.get("id").toString()).collect(Collectors.toList());
            //更新考勤周期的状态为已关账
            sql = "update uf_jcl_kq_kqzq_dt1 set zt = 1 where id in (" + String.join(",", ids) + ")";
            boolean updateSign = DbTools.update(sql);
            log.info("AttendanceCycleCloseJob-考勤周期明细表更新状态结果：" + updateSign);
        }

        log.info("********AttendanceCycleCloseJob  end********");
    }
}
