package com.engine.kq.cmd.attendanceButton;

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.KQGroupMemberComInfo;
import com.engine.kq.log.KQLog;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

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

	public SaveAutoEMSetCmd(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 auto_checkin = Util.null2s(Util.null2String(params.get("auto_checkin")),"0");
      String auto_checkout = Util.null2s(Util.null2String(params.get("auto_checkout")),"0");
		  String resourceId = ""+user.getUID();
      KQGroupMemberComInfo kqGroupMemberComInfo = new KQGroupMemberComInfo();
		  String curDate = DateUtil.getCurrentDate();
      String groupid = kqGroupMemberComInfo.getKQGroupId(user.getUID()+"",curDate);
      RecordSet rs = new RecordSet();
      String delSql = "delete from kq_autocheck_set where resourceid=? ";
      boolean isDel = rs.executeUpdate(delSql, resourceId);
      if(isDel){
        String sql = "insert into kq_autocheck_set(resourceid,auto_checkin,auto_checkout,groupid) values(?,?,?,?)";
        boolean isok = rs.executeUpdate(sql, resourceId,auto_checkin,auto_checkout,groupid);
        if(isok){
          retmap.put("status", "1");
        }else{
          retmap.put("status", "-1");
          retmap.put("message",  SystemEnv.getHtmlLabelName(382661,user.getLanguage()));
        }
      }else{
        retmap.put("status", "-1");
        retmap.put("message",  SystemEnv.getHtmlLabelName(382661,user.getLanguage()));
      }
      KQAutoCheckComInfo kqAutoCheckComInfo = new KQAutoCheckComInfo();
		  kqAutoCheckComInfo.removeCache();

      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;
	}

}
