package com.engine.kq.cmd.hrmAttProcSet;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.biz.SimpleBizLogger;
import com.engine.common.constant.BizLogSmallType4Hrm;
import com.engine.common.constant.BizLogType;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.wfset.attendance.domain.HrmAttProcSet;
import com.engine.kq.wfset.attendance.manager.HrmAttProcSetManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import weaver.common.DateUtil;
import weaver.common.StringUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

/**
 * 改变考勤流程设置的信息
 * @author pzy
 *
 */
public class ChangeStateProcSetStatusCmd extends AbstractCommonCommand<Map<String, Object>>{

  private SimpleBizLogger logger;

  public ChangeStateProcSetStatusCmd(Map<String, Object> params, User user) {
		this.user = user;
		this.params = params;
    HrmAttProcSetLogUtil hrmAttProcSetLogUtil = new HrmAttProcSetLogUtil(user,BizLogType.HRM_ENGINE, BizLogSmallType4Hrm.HRM_ENGINE_HRM_ATT_PROCSET, BizLogSmallType4Hrm.HRM_ENGINE_HRM_ATT_PROCSET);
    int id = StringUtil.parseToInt(StringUtil.vString(params.get("id")), 0);
    String realLogSql = "select *from kq_ATT_PROC_SET where "+Util.getSubINClause(""+id, "id", "in");;
    this.logger = hrmAttProcSetLogUtil.logStateSetLogSql(true, realLogSql, params,"basicinfo");
  }

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

  @Override
  public List<BizLogContext> getLogContexts() {
    return logger.getBizLogContexts();
  }

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
		Map<String,Object> retmap = new HashMap<String,Object>();
		 
		List<Object> lsGroup = new ArrayList<Object>();
		Map<String,Object> groupitem = null;
		List<Object> itemlist = null;
    RecordSet rs = new RecordSet();
		try {
			if(!HrmUserVarify.checkUserRight("HrmAttendanceProcess:setting", user)){
				retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
				return retmap;
			}

			int id = StringUtil.parseToInt(StringUtil.vString(params.get("id")), 0);
			HrmAttProcSetManager attProcSetManager = new HrmAttProcSetManager();
			HrmAttProcSet bean = attProcSetManager.get(id);
			if(bean != null) {
			  int workflowid = bean.getField001();
			  int field006 = bean.getField006();
			  int isuse = StringUtil.parseToInt(StringUtil.vString(params.get("field005")), 1);
			  if(isuse == 1){
			    //启用
          attProcSetManager.welcomeToSet(workflowid, field006,"","","");
        }else{
          //禁用
          rs.executeSql(new StringBuffer("update workflow_base set custompage = '', custompage4Emoble = '' where id = "+workflowid).toString());
        }
				bean.setField005(isuse);
				bean.setField009(DateUtil.getCurrentDate());
				attProcSetManager.update(bean);
				retmap.put("status", "true");
			} else retmap.put("status", "false");
		
			
			retmap.put("status", "1");
		} catch (Exception e) {
			retmap.put("status", "-1");
			retmap.put("message",  SystemEnv.getHtmlLabelName(382661,user.getLanguage()));
			writeLog(e);
		}
			
		return retmap;
	}
}
