package com.engine.attendance.component.persongroup.job.scheduling;

import com.engine.attendance.component.persongroup.job.scheduling.tactics.RegularScheduling;
import com.engine.common.util.DbTools;
import com.engine.common.util.Utils;

import lombok.extern.slf4j.Slf4j;
import weaver.common.DateUtil;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

import java.util.List;
import java.util.Map;

@Slf4j
public class SchedulingJob extends BaseCronJob {

    /**
     * 执行日期
     */
    String executeDate;
    /**
     * 生成天数
     */
    private String day;
    private List<RegularScheduling> rulers;
    {
        rulers = new Utils<RegularScheduling>().getAllClassByInterface(RegularScheduling.class);

    }
    @Override
    public void execute() {
        if (executeDate == null || "".equals(executeDate)){
            executeDate = DateUtil.getCurrentDate();
        }
        String sql = "select * from uf_jcl_kq_glpb where edate > ? and bdate<=?";
        List<Map<String,Object>> dataList = DbTools.getSqlToList(sql,executeDate,executeDate);
        Map<String,String> formmodeIdMap = Utils.getFormmodeIdMap();
        //规律排班模块id
        String modeId = formmodeIdMap.get("uf_jcl_kq_glpb");
        //排班结果模块
        String formmodeid = formmodeIdMap.get("uf_pbjg");
        log.info("dataList : [{}]",dataList);
        try {
            dataList.forEach(e -> {
                e.put("modeId",modeId);
                e.put("day",day);
                e.put("formmodeid",formmodeid);
                e.put("executeDate",executeDate);
                String zzqlx = Util.null2String(e.get("zzqlx"));
                for (RegularScheduling workOverTimeComputeRuler :rulers){
                    if (workOverTimeComputeRuler.support(Integer.valueOf(zzqlx))){
                        workOverTimeComputeRuler.compute(e);
                    }
                }
            });
        }catch (Exception e){
            log.error("catch error: [{}]",e);
        }
    }
}
