package com.engine.mingfeng.service.impl;

import com.engine.core.impl.Service;
import com.engine.kq.biz.KQGroupMemberComInfo;
import com.engine.kq.biz.KQHolidaySetBiz;
import com.engine.kq.biz.KQWorkTime;
import com.engine.mingfeng.service.OverTimeService;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OverTimeServiceImpl extends Service implements OverTimeService {

    BaseBean basebean = new BaseBean();
    @Override
    public Map<String, Object> getOverTimeType(Map<String, Object> params) {
        basebean.writeLog("OverTimeServiceImpl getOverTimeType start");
        Map<String, Object> result = new HashMap<>();

        String resourceId = Util.null2String(params.get("resourceId"));
        String kqDate = Util.null2String(params.get("kqDate"));

        if (StringUtils.isEmpty(resourceId)) {
            result.put("code",500);
            result.put("msg","请输入加班申请人");
            return result;
        }
        if (StringUtils.isEmpty(kqDate)) {
            result.put("code",500);
            result.put("msg","请输入加班开始日期");
            return result;
        }
        int changeType = getChangeType(resourceId, kqDate);
        if ( changeType == 1 || changeType == 2 || changeType == 3) {
            changeType = changeType - 1;
            result.put("code",200);
            result.put("msg","加班类型获取成功");
            result.put("data",changeType);
            return result;
        }

        result.put("code",500);
        result.put("msg","加班类型获取失败");
        return result;
    }

    /**
     * 根据指定人员ID以及指定日期判断这一天是工作日还是休息日还是节假日
     *
     * @param resourceId 指定人员ID
     *                   根据指定人员ID获取考勤组ID，根据考勤组得到对应日期是工作日还是休息日
     * @param date       指定日期
     *                   根据日期判断是工作日还是节假日还是休息日(这里综合考虑考勤组和节假日设置，节假日设置的优先级最高)
     * @return 1-节假日、2-工作日、3-休息日、-1-数据异常，无效数据
     */
    public int getChangeType(String resourceId, String date) {
        int changeType = -1;

        /*获取考勤组的ID，因为考勤组有有效期，所以需要传入日期*/
        KQGroupMemberComInfo kqGroupMemberComInfo = new KQGroupMemberComInfo();
        String groupId = kqGroupMemberComInfo.getKQGroupId(resourceId, date);

        /*该人员不存在于任意一个考勤组中，请为其设置考勤组*/
        if(groupId.equals("")){
            basebean.writeLog("该人员不存在于任意一个考勤组中，请为其设置考勤组。resourceId=" + resourceId + ",date=" + date);
        }

        changeType = KQHolidaySetBiz.getChangeType(groupId, date);
        if (changeType != 1 && changeType != 2 && changeType != 3) {
            KQWorkTime kqWorkTime = new KQWorkTime();
            changeType = kqWorkTime.isWorkDay(resourceId, date) ? 2 : 3;
        }
        return changeType;
    }
}
