package com.engine.attendance.attendanceanalysis.service.impl;

import com.engine.attendance.attendanceanalysis.cmd.GetAskForLeaveCmd;
import com.engine.attendance.attendanceanalysis.cmd.GetEvectionCmd;
import com.engine.attendance.attendanceanalysis.service.EvectionService;
import com.engine.attendance.enums.AccountingUnitEnum;
import com.engine.attendance.enums.AttendanceItemTypeEnum;
import com.engine.attendance.enums.CheckBoxEnum;
import com.engine.attendance.enums.WorkForTimeEnum;
import com.engine.common.util.DateUtil;
import com.engine.common.util.Utils;
import com.engine.core.impl.Service;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import weaver.general.Util;

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

public class EvectionServiceImpl extends Service implements EvectionService {
    @Override
    public Map<String,Object> evectionByTime(Map<String, Object> param) {
        //人员迟到、早退、漏卡等记录
        List<Map<String,Object>> abnormalClockInList = (List<Map<String,Object>>)param.get("abnormalClockInList");
        //请假开始时间
        String kssj = Util.null2String(param.get("kssj"));
        //请假结束时间
        String jssj = Util.null2String(param.get("jssj"));
        //作用时段
        String zysd = Util.null2String(param.get("zysd"));
        //分析日期
        String analysisDate = Util.null2String(param.get("analysisDate"));
        //排班
        List<Map<String,Object>> scheduleResult = (List<Map<String,Object>>)param.get("scheduleResult");

        List<Map<String,Object>> offsetEvectionAnomaly = Lists.newArrayList();
        for (int i=0;i<abnormalClockInList.size();i++){
            String bdlx = Util.null2String(abnormalClockInList.get(i).get("bdlx"));
            AttendanceItemTypeEnum itemType = (AttendanceItemTypeEnum)abnormalClockInList.get(i).get("itemType");
            String classStartTime = Util.null2String(abnormalClockInList.get(i).get("classStartTime"));
            String classEndTime = Util.null2String(abnormalClockInList.get(i).get("classEndTime"));
            if ((zysd.contains(Utils.getWorkFor(bdlx)) || zysd.contains(WorkForTimeEnum.ALL_TIME.getKey())) &&
                    !AttendanceItemTypeEnum.MISSE_CARD.equals(itemType)) {

                Map<String,Object> leaveMap = Maps.newHashMap();
                leaveMap.put("kssj",kssj);
                leaveMap.put("jssj",jssj);
                int betweenTime = Utils.removeRestTime(classStartTime,classEndTime,scheduleResult,analysisDate);
                int intersectionTime = Utils.getIntersectionTime(classStartTime,classEndTime,leaveMap,scheduleResult,analysisDate);
                if (betweenTime == intersectionTime){
                    offsetEvectionAnomaly.add(abnormalClockInList.get(i));
                }else {
                    String hsdw = Util.null2String(abnormalClockInList.get(i).get("hsdw"));
                    double hsl = Double.valueOf(Util.null2String(abnormalClockInList.get(i).get("hsl")));
                    double itemduration = Utils.getItemduration(hsl,hsdw,betweenTime-intersectionTime, AccountingUnitEnum.MINUTES);
                    abnormalClockInList.get(i).put("itemduration",itemduration);
                }
            }
        }
        abnormalClockInList.removeAll(offsetEvectionAnomaly);
        Map<String,Object> resultMap = Maps.newHashMap();
        resultMap.put("abnormalClockInList",abnormalClockInList);
        resultMap.put("offsetEvectionAnomaly",offsetEvectionAnomaly);
        return resultMap;
    }

    @Override
    public Map<String,Object> evectionByDurationTime(Map<String, Object> param) {
        //人员迟到、早退、漏卡等记录
        List<Map<String,Object>> abnormalClockInList = (List<Map<String,Object>>)param.get("abnormalClockInList");
        List<Map<String,Object>> forgetClockList = abnormalClockInList.stream().filter(e -> e.get("betweenMinutes") == null).collect(Collectors.toList());
        abnormalClockInList = abnormalClockInList.stream().filter(e -> e.get("betweenMinutes") != null).collect(Collectors.toList());
        abnormalClockInList = abnormalClockInList.stream().sorted(Comparator.comparing(e->Integer.valueOf(e.get("betweenMinutes").toString()))).collect(Collectors.toList());

        //请假时长,单位小时
        int ccsc = param.get("ccsc") == null?0:Double.valueOf(Double.valueOf(param.get("ccsc").toString())*60).intValue();
        //同一天可抵消多个异常
        List<Map<String,Object>> offsetEvectionAnomaly = Lists.newArrayList();
        //作用时段
        String zysd = Util.null2String(param.get("zysd"));

        String tybcndbjlhbjs = Util.null2String(param.get("tybcndbjlhbjs"));
        if (CheckBoxEnum.CHECKED.getKey().equals(tybcndbjlhbjs)){
            for (int i=abnormalClockInList.size() -1;i>=0;i--){
                int time = Integer.valueOf(abnormalClockInList.get(i).get("betweenMinutes").toString());
                String bdlx = Util.null2String(abnormalClockInList.get(i).get("bdlx"));
                if (ccsc > time && (zysd.contains(Utils.getWorkFor(bdlx)) || zysd.contains(WorkForTimeEnum.ALL_TIME.getKey()))){
                    ccsc = ccsc-time;
                    offsetEvectionAnomaly.add(abnormalClockInList.get(i));
                }
            }
            abnormalClockInList.removeAll(offsetEvectionAnomaly);
        }else{
            for (int i=abnormalClockInList.size() -1;i>=0;i--){
                int time = Integer.valueOf(abnormalClockInList.get(i).get("betweenMinutes").toString());
                String bdlx = Util.null2String(abnormalClockInList.get(i).get("bdlx"));
                if (ccsc > time && (zysd.contains(Utils.getWorkFor(bdlx)) || zysd.contains(WorkForTimeEnum.ALL_TIME.getKey()))){
                    offsetEvectionAnomaly.add(abnormalClockInList.get(i));
                    abnormalClockInList.remove(i);
                    break;
                }
            }
        }
        abnormalClockInList.addAll(forgetClockList);
        Map<String,Object> resultMap = Maps.newHashMap();
        resultMap.put("abnormalClockInList",abnormalClockInList);
        resultMap.put("offsetEvectionAnomaly",offsetEvectionAnomaly);
        return resultMap;
    }

    @Override
    public List<Map<String,Object>> evectionByHalfDay(Map<String, Object> param) {
        return null;
    }

    @Override
    public Map<String, Object> getEvection(Map<String, Object> param) {
        return commandExecutor.execute(new GetEvectionCmd(param));
    }
}
