package com.engine.kq.cmd.leavetypes;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQTimeSelectionComInfo;
import com.engine.kq.enums.KqSplitFlowTypeEnum;
import java.util.HashMap;
import java.util.Map;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

public class SaveTimeSelectionFormCmd extends AbstractCommonCommand<Map<String, Object>> {

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

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

  @Override
  public Map<String, Object> execute(CommandContext commandContext) {
    Map<String, Object> retmap = new HashMap<String, Object>();
    try{
      //半天设置的类型，是请假单位，还是出差，公出，加班单位
      int selectiontype = Util.getIntValue(Util.null2String(params.get("selectiontype")),-1);
      //请假可以细分到每一种请假类型，leaveruleid为请假类型的id
      int leaveruleid = Util.getIntValue(Util.null2String(params.get("leaveruleid")),0);
      //最小请假单位：2-按半天请假、4-按整天请假
      //目前只做2半天的情况
      int minimumUnit = Util.getIntValue((String) params.get("minimumUnit"), 2);
      String half_on = Util.null2String(params.get("half_on"));
      String half_off = Util.null2String(params.get("half_off"));
      String whole = Util.null2String(params.get("whole"));

      if(selectiontype == KqSplitFlowTypeEnum.LEAVE.getFlowtype()){
        if(selectiontype < 0 || leaveruleid == 0){
          retmap.put("status", "-1");
          writeLog("SaveTimeSelectionForm:参数有问题:selectiontype:"+selectiontype+":leaveruleid:"+leaveruleid);
          retmap.put("message", SystemEnv.getHtmlLabelName(514241, user.getLanguage()));
          return retmap;
        }
      }else{
        if(selectiontype < 0){
          retmap.put("status", "-1");
          writeLog("SaveTimeSelectionForm:参数有问题:selectiontype:"+selectiontype+":leaveruleid:"+leaveruleid);
          retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
          return retmap;
        }
      }
      RecordSet rs = new RecordSet();
      String delSql = "";
      if(selectiontype == KqSplitFlowTypeEnum.LEAVE.getFlowtype()){
        delSql = "delete from kq_timeselection where selectiontype="+ selectiontype+" and leaveruleid="+leaveruleid+" and minimumUnit="+minimumUnit;
      }else {
        delSql = "delete from kq_timeselection where selectiontype="+ selectiontype+" and minimumUnit="+minimumUnit;
      }
      boolean isDel = rs.executeUpdate(delSql);
      if(isDel){
        String addSql = "insert into kq_timeselection(selectiontype,leaveruleid,minimumUnit,half_on,half_off,whole) values(?,?,?,?,?,?)";
        rs.executeUpdate(addSql,selectiontype,leaveruleid,minimumUnit,half_on,half_off,whole);
      }
      KQTimeSelectionComInfo kqTimeSelectionComInfo = new KQTimeSelectionComInfo();
      kqTimeSelectionComInfo.removeCache();
      retmap.put("status", "1");

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

