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.biz.KQAttFlowSetComInfo;
import com.engine.kq.wfset.attendance.domain.HrmAttProcSet;
import com.engine.kq.wfset.attendance.manager.HrmAttProcSetManager;
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.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

/**
 * 保存考勤流程设置的 流程概览
 * @author pzy
 *
 */
public class SaveStateProcSetFlowCmd extends AbstractCommonCommand<Map<String, Object>>{

  private SimpleBizLogger logger;

  public SaveStateProcSetFlowCmd(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);
    String realLogSql = "";
    this.logger = hrmAttProcSetLogUtil.logStateSetLogSql(false, 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>();
		 
		try {
			if(!HrmUserVarify.checkUserRight("HrmAttendanceProcess:setting", user)){
				retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
				return retmap;
			}
      KQAttFlowSetComInfo kqAttFlowSetComInfo = new KQAttFlowSetComInfo();
			RecordSet rs = new RecordSet();
			HrmAttProcSetManager attProcSetManager = new HrmAttProcSetManager();

			int id = StringUtil.parseToInt(StringUtil.vString(params.get("id")), 0);
			int field001 = StringUtil.parseToInt(StringUtil.vString(params.get("field001")), 0);

			boolean hasDefined = checkHasDefined(field001,id);
			if(hasDefined){
        retmap.put("status", "-1");
        retmap.put("message",  SystemEnv.getHtmlLabelName(500705,user.getLanguage()));
        return retmap;
      }

			int field002 = StringUtil.parseToInt(StringUtil.vString(params.get("field002")), 0);
			int field003 = StringUtil.parseToInt(StringUtil.vString(params.get("field003")), 0);
			int field004 = StringUtil.parseToInt(StringUtil.vString(params.get("field004")), 0);
			int field005 = StringUtil.parseToInt(StringUtil.vString(params.get("field005")), 1);
			int field006 = StringUtil.parseToInt(StringUtil.vString(params.get("field006")));
			int field007 = StringUtil.parseToInt(StringUtil.vString(params.get("field007")), 0);
			String field008 = StringUtil.vString(StringUtil.vString(params.get("field008")), DateUtil.getCurrentDate());
			String field009 = StringUtil.vString(StringUtil.vString(params.get("field009")), DateUtil.getCurrentDate());
			String field010 = StringUtil.vString(StringUtil.vString(params.get("field010")));
      int usedetail = StringUtil.parseToInt(StringUtil.vString(params.get("usedetail")), 0);
      int flow_deduct_card = StringUtil.parseToInt(StringUtil.vString(params.get("flow_deduct_card")), 0);
      usedetail = usedetail < 0 ? 0 : usedetail;
      if(KqTempletEnum.SHIFT.getFlowtype().equalsIgnoreCase(""+field006)
      ||KqTempletEnum.Card.getFlowtype().equalsIgnoreCase(""+field006)
      ||KqTempletEnum.LEAVEBACK.getFlowtype().equalsIgnoreCase(""+field006)
          ||KqTempletEnum.PROCESSCHANGE.getFlowtype().equalsIgnoreCase(""+field006)){
        usedetail = 1;
      }
      if(!(KqTempletEnum.LEAVE.getFlowtype().equalsIgnoreCase(""+field006)
          ||KqTempletEnum.EVECTION.getFlowtype().equalsIgnoreCase(""+field006)
          ||KqTempletEnum.OUT.getFlowtype().equalsIgnoreCase(""+field006))){
        flow_deduct_card = 0;
      }
      String templetfile = StringUtil.vString(StringUtil.vString(params.get("templetfile")));
      String templetmobilefile = StringUtil.vString(StringUtil.vString(params.get("templetmobilefile")));
      String templetroute = StringUtil.vString(StringUtil.vString(params.get("templetroute")));
			
			rs.executeSql("select * from view_workflowForm_selectAll where isoldornew = 1 and id > 0 and id = "+field002);
			if(rs.next()) {
			  field003 = 1;
      }
			HrmAttProcSet bean = null;
			if(id > 0) {
			  bean = attProcSetManager.get(id);
      }
			bean = bean == null ? new HrmAttProcSet(true) : bean;
			bean.setField001(field001);
			bean.setField002(field002);
			bean.setField003(field003);
			bean.setField004(field004);
			bean.setField005(field005);
			bean.setField006(field006);
			if(id <= 0) {
				bean.setField007(field007);
				bean.setField008(field008);
			} else {
				bean.setField010(field010);
			}
			bean.setField009(field009);
			bean.setUsedetail(usedetail);
			bean.setTempletfile(templetfile);
			bean.setTempletmobilefile(templetmobilefile);
			bean.setTempletroute(templetroute);
      bean.setFlow_deduct_card(flow_deduct_card);
			retmap.put("id", String.valueOf(attProcSetManager.save(bean)));
			if(field005 == 1) {
        attProcSetManager.welcomeToSet(field001, field006,templetfile,templetmobilefile,templetroute);
      }
      kqAttFlowSetComInfo.removeKQAttFlowSetComInfoCache();
			
			retmap.put("status", "1");
		} catch (Exception e) {
			retmap.put("status", "-1");
			retmap.put("message",  SystemEnv.getHtmlLabelName(382661,user.getLanguage()));
			writeLog(e);
		}
			
		return retmap;
	}

  /**
   * 判断此流程是否已经关联考勤
   * @param field001
   * @param id
   */
  private boolean checkHasDefined(int field001, int id) {
    RecordSet rs = new RecordSet();
    String hasDefinedSql = "select 1 from kq_att_proc_set where field001= ? and id <> ?";
    rs.executeQuery(hasDefinedSql, field001,id);
    if(rs.next()){
      return true;
    }
    return false;
  }
}
