package com.engine.attendance.attendanceanalysis.cmd.item;

import com.engine.attendance.enums.AttendanceItemTypeEnum;
import com.engine.attendance.enums.WorkForTimeEnum;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 迟到时获取迟到项目
 */
@Slf4j
public class BeLateItemCmd extends AbstractCommonCommand<Map<String,Object>> {
    @Override
    public BizLogContext getLogContext() {
        return null;
    }
    public BeLateItemCmd(Map<String,Object> params){
        this.params=params;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        //作用时段
        String workfor = Util.null2String(params.get("workfor"));
        //迟到分钟数
        int time = Util.null2String(params.get("time")).equals("")? 0:Util.getIntValue(Util.null2String(params.get("time")));
        Map<String, Object> resultMap = new HashMap<>();

        List<Map<String,Object>> attendanceItems = (List<Map<String,Object>>)params.get("attendanceItems");
        attendanceItems = attendanceItems.stream().filter(e -> {
            //项目类型
            String xmlx = Util.null2String(e.get("xmlx"));
            //作用时段
            String zysd = Util.null2String(e.get("zysd"));
            //最小核算分钟数（不包含）
            int zxhsl = Util.null2String(e.get("zxhsl")).equals("")?0:Util.getIntValue(Util.null2String(e.get("zxhsl")));
            //最大核算分钟数（包含）
            int zdhsl = Util.null2String(e.get("zdhsl")).equals("")?0:Util.getIntValue(Util.null2String(e.get("zdhsl")));

            if(AttendanceItemTypeEnum.LATE.getKey().equals(xmlx) && (zysd.contains(workfor) || zysd.contains(WorkForTimeEnum.ALL_TIME.getKey())) && time > zxhsl && time<=zdhsl) {
                return true;
            }else {
                return false;
            }
        }).collect(Collectors.toList());
        log.info("absenteeismCmd attendanceItems : [{}]",attendanceItems);
        resultMap.put("attendanceItems",attendanceItems);

        return resultMap;
    }
}
