package com.engine.kq.cmd.attendanceEvent;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQReportBiz;
import com.engine.kq.biz.KQTimesArrayComInfo;
import com.engine.kq.cmd.attendanceButton.ButtonStatusEnum;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

/**
 * 获取补卡信息
 */
public class GetAttendanceCardCmd extends AbstractCommonCommand<Map<String, Object>> {

	public GetAttendanceCardCmd(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();
		try{
      String resourceId = Util.null2String(params.get("resourceId"));
      String fromDate = Util.null2String(params.get("fromDate"));
      String toDate = Util.null2String(params.get("toDate"));

      DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
      LocalDate fromLocalDate = LocalDate.parse(fromDate);
      LocalDate toLocalDate = LocalDate.parse(toDate);

      if(fromLocalDate.isAfter(toLocalDate)){
        retmap.put("status", "-1");
        return retmap;
      }

      KQTimesArrayComInfo kqTimesArrayComInfo = new KQTimesArrayComInfo();

      List<Map<String,String>> cardList = new ArrayList<>();
      Map<String,String> cardMap = new HashMap<>();

      KQReportBiz kqReportBiz = new KQReportBiz();

      List<Object>  reporsts = kqReportBiz.getKqDateInfo(resourceId, fromDate, toDate, false);
      for(int i = 0 ; i < reporsts.size()  ; i++) {
        Map<String, Object> kqdateMap = (Map<String, Object>) reporsts.get(i);
        String kqdate = Util.null2String(kqdateMap.get("kqdate"));
        List<Object> checkInfoList = (List<Object>) kqdateMap.get("checkInfo");
        //每一个工作时段里的考勤状态
        for (int j = 0; j < checkInfoList.size(); j++) {
          Map<String, Object> data = (Map<String, Object>) checkInfoList.get(j);
          String status = Util.null2String(data.get("status"));
          String workbegindate = Util.null2String(data.get("workbegindate"));
          String workenddate = Util.null2String(data.get("workenddate"));
          String signintime = Util.null2String(data.get("signintime"));
          String signouttime = Util.null2String(data.get("signouttime"));
          String workbegintime = Util.null2String(data.get("workbegintime"));
          String workendtime = Util.null2String(data.get("workendtime"));

          int workbegintime_index = kqTimesArrayComInfo.getArrayindexByTimes(workbegintime);
          int workbegintime_index_n = workbegintime_index > KQTimesArrayComInfo.getOneDayArraySize() ? workbegintime_index-KQTimesArrayComInfo.getOneDayArraySize() : workbegintime_index;
          workbegintime = kqTimesArrayComInfo.getTimesByArrayindex(workbegintime_index_n);

          int workendtime_index = kqTimesArrayComInfo.getArrayindexByTimes(workendtime);
          int workendtime_index_n = workendtime_index > KQTimesArrayComInfo.getOneDayArraySize() ? workendtime_index-KQTimesArrayComInfo.getOneDayArraySize() : workendtime_index;
          workendtime = kqTimesArrayComInfo.getTimesByArrayindex(workendtime_index_n);
          String scheduletime = workbegintime + "-" + workendtime;

          String[] status_s = status.split(",");
          if(status_s != null && status.length() > 0){
            for(int s = 0 ; s < status_s.length ; s++){
              String tmp_status = status_s[s];
              cardMap = new HashMap<>();
              if (ButtonStatusEnum.BELATE.getStatusCode().equalsIgnoreCase(tmp_status)) {
                //迟到
                cardMap = new HashMap<>();
                cardMap.put("signdate", workbegindate);
                cardMap.put("scheduletime", scheduletime);
                cardMap.put("atteStatus", SystemEnv.getHtmlLabelName(10000805,weaver.general.Util.getIntValue(user.getLanguage()))+signintime);
                //下拉框0是签到，1是签退
                cardMap.put("signtype", "0");
                cardMap.put("signtime", workbegintime);
                cardList.add(cardMap);
              }
              if (ButtonStatusEnum.LEAVEERALY.getStatusCode().equalsIgnoreCase(tmp_status)) {
                //早退
                cardMap = new HashMap<>();
                cardMap.put("signdate", workenddate);
                cardMap.put("scheduletime", scheduletime);
                cardMap.put("atteStatus", SystemEnv.getHtmlLabelName(10000806,weaver.general.Util.getIntValue(user.getLanguage()))+signouttime);
                //下拉框0是签到，1是签退
                cardMap.put("signtype", "1");
                cardMap.put("signtime", workendtime);
                cardList.add(cardMap);
              }
              if (ButtonStatusEnum.ABSENT.getStatusCode().equalsIgnoreCase(tmp_status)) {
                //旷工
                cardMap = new HashMap<>();
                cardMap.put("signdate", workbegindate);
                cardMap.put("scheduletime", scheduletime);
                String atteStatus0 = SystemEnv.getHtmlLabelName(20085,weaver.general.Util.getIntValue(user.getLanguage()));
                if(signintime.length() > 0){
                  atteStatus0 += SystemEnv.getHtmlLabelName(10000807,weaver.general.Util.getIntValue(user.getLanguage()))+signintime;
                }

                cardMap.put("atteStatus", atteStatus0);
                //下拉框0是上班，1是下班
                cardMap.put("signtype", "0");
                cardMap.put("signtime", workbegintime);
                cardList.add(cardMap);

                cardMap = new HashMap<>();
                cardMap.put("signdate", workenddate);
                cardMap.put("scheduletime", scheduletime);
                String atteStatus1 = SystemEnv.getHtmlLabelName(20085,weaver.general.Util.getIntValue(user.getLanguage()));
                if(signouttime.length() > 0){
                  atteStatus1 += SystemEnv.getHtmlLabelName(10000808,weaver.general.Util.getIntValue(user.getLanguage()))+signouttime;
                }
                cardMap.put("atteStatus", atteStatus1);
                //下拉框0是上班，1是下班
                cardMap.put("signtype", "1");
                cardMap.put("signtime", workendtime);
                cardList.add(cardMap);

              }
              if (ButtonStatusEnum.NOSIGN.getStatusCode().equalsIgnoreCase(tmp_status)) {
                //漏签
                cardMap = new HashMap<>();
                cardMap.put("signdate", workenddate);
                cardMap.put("scheduletime", scheduletime);
                cardMap.put("atteStatus", SystemEnv.getHtmlLabelName(20086,weaver.general.Util.getIntValue(user.getLanguage())));
                //下拉框0是上班，1是下班
                cardMap.put("signtype", "1");
                cardMap.put("signtime", workendtime);
                cardList.add(cardMap);
              }
              if (ButtonStatusEnum.NOSIGN_ON.getStatusCode().equalsIgnoreCase(tmp_status)) {
                //上班漏签
                cardMap = new HashMap<>();
                cardMap.put("signdate", workbegindate);
                cardMap.put("scheduletime", scheduletime);
                cardMap.put("atteStatus", SystemEnv.getHtmlLabelName(20086,weaver.general.Util.getIntValue(user.getLanguage())));
                //下拉框0是上班，1是下班
                cardMap.put("signtype", "0");
                cardMap.put("signtime", workbegintime);
                cardList.add(cardMap);
              }
            }
          }
        }
      }

      retmap.put("cardlist", cardList);
      retmap.put("status", "1");

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

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

}
