package com.engine.jucailinkq.attendance.component.persongroup.cmd;

import com.engine.jucailinkq.attendance.component.persongroup.job.scheduling.tactics.RegularScheduling;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.jucailinkq.common.util.DbTools;
import com.engine.jucailinkq.common.util.Utils;
import com.engine.core.interceptor.CommandContext;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import weaver.common.DateUtil;
import weaver.general.Util;

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

@Slf4j
public class GenerateScheduleCmd extends AbstractCommonCommand<Map<String,Object>> {
    public GenerateScheduleCmd(Map<String,Object> params){
        this.params=params;
    }

    private List<RegularScheduling> rulers;
    {
        rulers = new Utils<RegularScheduling>().getAllClassByInterface(RegularScheduling.class);

    }
    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        String executeDate = Util.null2String(params.get("executeDate"));
        String day = Util.null2String(params.get("day"));

        Map<String,Object> resultMap = Maps.newHashMap();
        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.debug("dataList : [{}]",dataList);

        String finalExecuteDate = executeDate;
        dataList.forEach(e -> {
            e.put("modeId",modeId);
            e.put("day",day);
            e.put("formmodeid",formmodeid);
            e.put("executeDate", finalExecuteDate);
            String zzqlx = Util.null2String(e.get("zzqlx"));
            for (RegularScheduling workOverTimeComputeRuler :rulers){
                if (workOverTimeComputeRuler.support(Integer.valueOf(zzqlx))){
                    workOverTimeComputeRuler.compute(e);
                }
            }
        });
        resultMap.put("message","生成成功");

        return resultMap;
    }
}
