package com.engine.kq.cmd.attendanceButton;

import com.api.browser.bean.SearchConditionItem;
import com.api.hrm.bean.HrmFieldBean;
import com.api.hrm.util.HrmFieldSearchConditionComInfo;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQAutoCheckComInfo;
import com.engine.kq.biz.KQGroupComInfo;
import com.engine.kq.biz.KQGroupMemberComInfo;
import com.engine.kq.log.KQLog;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import weaver.common.DateUtil;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;
import weaver.upgradetool.wscheck.Util;

/**
 * EM端自动打卡设置界面
 */
public class GetAutoEMSetCmd extends AbstractCommonCommand<Map<String, Object>> {
  private KQLog kqLog = new KQLog();
  private HttpServletRequest request;
  private HttpServletResponse response;

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

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
		Map<String, Object> retmap = new HashMap<String, Object>();
		try{
		  String resourceId = ""+user.getUID();
		  String curDate = DateUtil.getCurrentDate();
      KQGroupMemberComInfo kqGroupMemberComInfo = new KQGroupMemberComInfo();
      KQGroupComInfo kqGroupComInfo = new KQGroupComInfo();
      String groupId = kqGroupMemberComInfo.getKQGroupId(resourceId, curDate);
      boolean can_auto_checkin = false;
      boolean can_auto_checkout = false;
      String auto_checkin = "0";
      String auto_checkout = "0";
      boolean can_set = false;
      if(groupId.length() > 0){
        auto_checkin = Util.null2String(kqGroupComInfo.getAuto_checkin(groupId));
        auto_checkout = Util.null2String(kqGroupComInfo.getAuto_checkout(groupId));
        if(auto_checkin.length() > 0 && auto_checkout.length() > 0){
          if("1".equalsIgnoreCase(auto_checkin) || "1".equalsIgnoreCase(auto_checkout)){
            //只要自动打卡开了一个就可以
            can_set = true;
          }
          if("1".equalsIgnoreCase(auto_checkin)){
            can_auto_checkin = true;
          }
          if("1".equalsIgnoreCase(auto_checkout)){
            can_auto_checkout = true;
          }
        }
      }
      if(!can_set){
        retmap.put("status", "1");
        retmap.put("can_set", "0");
        return retmap;
      }else{
        retmap.put("can_set", "1");
      }
      KQAutoCheckComInfo kqAutoCheckComInfo = new KQAutoCheckComInfo();
      String cus_auto_checkin = Util.null2String(kqAutoCheckComInfo.getAuto_checkin(resourceId));
      String cus_auto_checkout = Util.null2String(kqAutoCheckComInfo.getAuto_checkout(resourceId));
      if(cus_auto_checkin.length() > 0){
        auto_checkin = cus_auto_checkin;
      }
      if(cus_auto_checkout.length() > 0){
        auto_checkout = cus_auto_checkout;
      }

      List<HrmFieldBean> lsField = new ArrayList<HrmFieldBean>();
      HrmFieldBean hrmFieldBean = null;

      hrmFieldBean = new HrmFieldBean();
      hrmFieldBean.setFieldname("auto_checkin");
      hrmFieldBean.setFieldlabel("518736");
      hrmFieldBean.setFieldhtmltype("4");
      hrmFieldBean.setType("2");
      hrmFieldBean.setFieldvalue(auto_checkin);
      if(can_auto_checkin){
        lsField.add(hrmFieldBean);
      }

      hrmFieldBean = new HrmFieldBean();
      hrmFieldBean.setFieldname("auto_checkin_desc");
      hrmFieldBean.setFieldlabel("518736");
      hrmFieldBean.setFieldhtmltype("8");
      hrmFieldBean.setType("1");
      hrmFieldBean.setFieldvalue(SystemEnv.getHtmlLabelName(518800, user.getLanguage()));
      if(can_auto_checkin){
        lsField.add(hrmFieldBean);
      }

      hrmFieldBean = new HrmFieldBean();
      hrmFieldBean.setFieldname("auto_checkout");
      hrmFieldBean.setFieldlabel("518738");
      hrmFieldBean.setFieldhtmltype("4");
      hrmFieldBean.setType("2");
      hrmFieldBean.setFieldvalue(auto_checkout);
      if(can_auto_checkout){
        lsField.add(hrmFieldBean);
      }

      hrmFieldBean = new HrmFieldBean();
      hrmFieldBean.setFieldname("auto_checkout_desc");
      hrmFieldBean.setFieldlabel("518736");
      hrmFieldBean.setFieldhtmltype("8");
      hrmFieldBean.setType("1");
      hrmFieldBean.setFieldvalue(SystemEnv.getHtmlLabelName(518801, user.getLanguage()));
      if(can_auto_checkout){
        lsField.add(hrmFieldBean);
      }

      List<Map<String,Object>> grouplist = new ArrayList<Map<String,Object>>();
      Map<String,Object> groupitem = new HashMap<String, Object>();
      List<Object> itemlist = new ArrayList<>();
      HrmFieldSearchConditionComInfo hrmFieldSearchConditionComInfo = new HrmFieldSearchConditionComInfo();
      SearchConditionItem searchConditionItem = null;

      for (int j = 0; j < lsField.size(); j++) {
        hrmFieldBean = lsField.get(j);
        searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
        itemlist.add(searchConditionItem);
      }
      groupitem.put("items", itemlist);
      grouplist.add(groupitem);

      retmap.put("formField", grouplist);
      retmap.put("status", "1");
      return retmap;
    }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;
	}

}
