package com.engine.kq.cmd.shiftmanagement;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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.KQShiftManagementComInfo;
import com.engine.kq.biz.KQShiftOnOffWorkSectionComInfo;
import com.engine.kq.biz.KQShiftRestTimeSectionComInfo;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import weaver.conn.RecordSetTrans;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

/**
 * 删除班次管理
 * @author pzy
 *
 */
public class DelShiftManagementFormCmd extends AbstractCommonCommand<Map<String, Object>>{

  private SimpleBizLogger logger;

  public DelShiftManagementFormCmd() {
  }

  public DelShiftManagementFormCmd(Map<String, Object> params, User user) {
    this.user = user;
    this.params = params;
    this.logger = new SimpleBizLogger();
    BizLogContext logContext = new BizLogContext();
    logContext.setDateObject(new Date());
    logContext.setLogType(BizLogType.HRM_ENGINE);
    logContext.setBelongType(BizLogSmallType4Hrm.HRM_ENGINE_SHIFTMANAGER);
    logContext.setLogSmallType(BizLogSmallType4Hrm.HRM_ENGINE_SHIFTMANAGER);
    logContext.setParams(params);
    logger.setUser(user);//当前操作人
    String ids = Util.null2String(params.get("ids"));

    if(ids.length() > 0){
      String mainSql = " select * from kq_ShiftManagement where "+Util.getSubINClause(ids, "id", "in") +" ";
      logger.setMainSql(mainSql);//主表sql
      logger.setMainPrimarykey("id");//主日志表唯一key
      logger.setMainTargetNameMethod(this.getClass().getName() + ".getTargetName", "column:serial");

      SimpleBizLogger.SubLogInfo subLogInfo1 = logger.getNewSubLogInfo();
      String subSql1 = "select * from kq_ShiftOnOffWorkSections where "+Util.getSubINClause(ids, "serialid", "in") ;
      subLogInfo1.setSubTargetNameColumn("times");
      subLogInfo1.setGroupId("0");  //所属分组， 按照groupid排序显示在详情中， 不设置默认按照add的顺序。
      subLogInfo1.setSubGroupNameLabel(27961); //在详情中显示的分组名称，不设置默认显示明细x
      subLogInfo1.setSubSql(subSql1);
      logger.addSubLogInfo(subLogInfo1);

      SimpleBizLogger.SubLogInfo subLogInfo = logger.getNewSubLogInfo();
      String subSql = " select * from kq_ShiftRestTimeSections where "+Util.getSubINClause(ids, "serialid", "in");
      subLogInfo.setSubSql(subSql);
      subLogInfo.setSubTargetNameColumn("time");
      subLogInfo.setGroupId("1");  //所属分组， 按照groupid排序显示在详情中， 不设置默认按照add的顺序。
      subLogInfo.setSubGroupNameLabel(505603); //在详情中显示的分组名称，不设置默认显示明细x
      logger.addSubLogInfo(subLogInfo);
    }
    logger.before(logContext);

  }

	@Override
	public BizLogContext getLogContext() {
		// TODO Auto-generated method stub
		return null;
	}

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

  /**
   * 获取日志对象的名称
   * @param id
   * @param para2
   * @return
   */
  public String getTargetName(String id,String para2){
    try {
      return para2;
    } catch (Exception e) {
      e.printStackTrace();
      return "";
    }
  }

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
    Map<String,Object> retmap = new HashMap<String,Object>();
    String ids = Util.null2String(params.get("ids"));
    RecordSetTrans rst = new RecordSetTrans();
    if(!HrmUserVarify.checkUserRight("KQClass:Management",user)) {
      retmap.put("status", "-1");
      retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
      return retmap;
    }
    try{
      if(ids.length() > 0){
        rst.setAutoCommit(false);
        String[] idArr = ids.split(",");
        for(String id : idArr){
          //删除基本信息表
          String delBaseSql = "update kq_ShiftManagement set isdelete = '1' where id = ? ";
          boolean isUpdated = rst.executeUpdate(delBaseSql,id);
          //删除基本信息上班时间表
          String delBaseSql1 = "update kq_ShiftOnOffWorkSections set isdelete = '1' where serialid = ? ";
          rst.executeUpdate(delBaseSql1,id);
          //删除基本信息休息时间表
          String delBaseSql2 = "update kq_ShiftRestTimeSections set isdelete = '1' where serialid = ? ";
          rst.executeUpdate(delBaseSql2,id);
          //删除个性化信息表
          String delPersonalizedSql = "update kq_ShiftPersonalizedRule set isdelete = '1' where serialid = ? ";
          rst.executeUpdate(delPersonalizedSql,id);
          //删除个性化信息规则表
          String delPersonalizedSql1 = "update kq_ShiftPersonalizedRuleDetail set isdelete = '1' where personalizedruleid in (select id from kq_ShiftPersonalizedRule where serialid = ?)";
          rst.executeUpdate(delPersonalizedSql1,id);
        }
        rst.commit();
      }
      KQShiftManagementComInfo kqShiftManagementComInfo = new KQShiftManagementComInfo();
      kqShiftManagementComInfo.removeShiftManagementCache();
      retmap.put("status", "1");
    }catch (Exception e){
      rst.rollback();
      retmap.put("status", "-1");
      retmap.put("message",  SystemEnv.getHtmlLabelName(382661,user.getLanguage()));
      writeLog(e);
    }

    return retmap;
	}

}
