package com.engine.kq.cmd.reportfielddefine;

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.KQReportFieldComInfo;
import com.engine.kq.biz.KQReportFieldGroupComInfo;
import weaver.conn.DBUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import java.util.*;

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

  private SimpleBizLogger logger;
  SimpleBizLogger.SubLogInfo subLogInfo;
  public DeleteGroupCmd(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_KQREPORT_DEFINED);
    logContext.setLogSmallType(BizLogSmallType4Hrm.HRM_ENGINE_KQREPORT_DEFINED);
    logContext.setParams(params);
    logger.setUser(user);//当前操作人

    String ids = Util.null2String(params.get("ids"));
    String mainSql = " select * from kq_report_field_group where id in ("+ids+")";
    logger.setMainSql(mainSql);//主表sql
    logger.setMainPrimarykey("id");//主日志表唯一key
    logger.setMainTargetNameColumn("groupname");

    subLogInfo = logger.getNewSubLogInfo();
    String subSql = " select * from kq_report_field where groupid in ("+ids+")";
    subLogInfo.setSubSql(subSql);
    subLogInfo.setSubPrimarykey("id");
    subLogInfo.setSubTargetNameMethod("com.engine.kq.util.KQTransMethod.getLabelName");
    subLogInfo.setSubMethodOtherParams("column:fieldlabel+" + user.getLanguage());
    subLogInfo.setGroupId("1");  //所属分组， 按照groupid排序显示在详情中， 不设置默认按照add的顺序。
    subLogInfo.setSubGroupNameLabel(33943); //在详情中显示的分组名称，不设置默认显示明细x
    logger.addSubLogInfo(subLogInfo);

    logger.before(logContext);
  }

  @Override
  public Map<String, Object> execute(CommandContext commandContext) {
    Map<String, Object> retmap = new HashMap<String, Object>();
    String sql = "";
    RecordSet rs = new RecordSet();
    try {
      if (!HrmUserVarify.checkUserRight("KQReportFieldDefine:Add", user)) {
        retmap.put("status", "-1");
        retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
        return retmap;
      }

      String ids = Util.null2String(params.get("ids"));
      List sqlParams = new ArrayList();
      Object[] objects= DBUtil.transListIn(ids,sqlParams);

      sql = " update kq_report_field set groupid=null where groupid in ("+objects[0]+")";
      rs.executeUpdate(sql, sqlParams);

      sql = " delete from kq_report_field_group where id in ("+objects[0]+")";
      rs.executeUpdate(sql, sqlParams);

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

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

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