package com.engine.kq.cmd.attendanceEvent;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQGroupMemberComInfo;
import com.engine.kq.entity.KQGroupEntity;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.resource.ResourceComInfo;
import weaver.systeminfo.SystemEnv;

/**
 * 排班校验事件
 */
public class CheckShiftCmd extends AbstractCommonCommand<Map<String, Object>> {

	public CheckShiftCmd(Map<String, Object> params, User user) {
		this.user = user;
		this.params = params;
	}

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
		Map<String, Object> retmap = new HashMap<String, Object>();
		RecordSet rs = new RecordSet();
		String sql = "";
		try{
      ResourceComInfo resourceComInfo = new ResourceComInfo();
      String data = Util.null2String(params.get("data"));
      JSONArray dataArray = (JSONArray) JSON.parse(data);
      for(int i = 0 ; i < dataArray.size() ; i++){
        JSONObject dataObject = (JSONObject)dataArray.get(i);
        String resourceId = Util.null2String(dataObject.get("resourceId"));
        String fromDate = Util.null2String(dataObject.get("fromDate"));
        String toDate = Util.null2String(dataObject.get("toDate"));
        String shift = Util.null2String(dataObject.get("shift"));
        if(fromDate.length() ==0 || toDate.length() == 0){
          retmap.put("status", "2");
          retmap.put("message",  SystemEnv.getHtmlLabelName(528117,user.getLanguage()));
          return retmap;
        }
        LocalDate nowDate = LocalDate.now();
        LocalDate fromlocalDate = LocalDate.parse(fromDate);
        LocalDate tolocalDate = LocalDate.parse(toDate);
        if(fromlocalDate.isAfter(tolocalDate)){
          retmap.put("status", "2");
          retmap.put("message",  SystemEnv.getHtmlLabelName(15273,user.getLanguage()));
          return retmap;
        }
        KQGroupEntity kQGroupEntity = getGroupInfo(resourceId,fromDate);
        if(kQGroupEntity == null){
          retmap.put("status", "2");
          retmap.put("message",  resourceComInfo.getLastname(resourceId)+SystemEnv.getHtmlLabelName(390220,user.getLanguage()));
          return retmap;
        }
        String kqtype = kQGroupEntity.getKqtype();
        if (!"2".equalsIgnoreCase(kqtype)) {
          retmap.put("status", "2");
          retmap.put("message",  resourceComInfo.getLastname(resourceId)+SystemEnv.getHtmlLabelName(10000804,weaver.general.Util.getIntValue(user.getLanguage())));
          return retmap;
        }else{
          String serialids = kQGroupEntity.getSerialids();
          if(!"-1".equalsIgnoreCase(shift) && !serialids.contains(shift)){
            //如果这个人所在的考勤组里没有要排的班次，则不让排
            retmap.put("status", "2");
            retmap.put("message",  resourceComInfo.getLastname(resourceId)+SystemEnv.getHtmlLabelName(390745,weaver.general.Util.getIntValue(user.getLanguage())));
            return retmap;
          }

        }
      }

      retmap.put("status", "1");
		}catch (Exception e) {
			retmap.put("status", "-1");
			retmap.put("message",  SystemEnv.getHtmlLabelName(382661,user.getLanguage()));
			writeLog(e);
		}
		return retmap;
	}

  /**
   * 根据用户得到考勤组信息
   * @param resourceId
   * @param fromDate
   */
  private KQGroupEntity getGroupInfo(String resourceId,String fromDate) {
    KQGroupMemberComInfo kqGroupMemberComInfo = new KQGroupMemberComInfo();
    KQGroupEntity kQGroupEntity = kqGroupMemberComInfo.getUserKQGroupInfo(resourceId,fromDate,true);

    return kQGroupEntity;
  }

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

}
