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 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 ChangeFieldGroupCmd extends AbstractCommonCommand<Map<String, Object>> {

  private SimpleBizLogger logger;
  public ChangeFieldGroupCmd(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 where id in ("+ids+")";
    logger.setMainSql(mainSql);//主表sql
    logger.setMainPrimarykey("id");//主日志表唯一key
    logger.setMainTargetNameMethod("com.engine.kq.util.KQTransMethod.getLabelName");
    logger.setMethodOtherParams("column:fieldlabel+" + user.getLanguage());
    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);
      String groupid = Util.null2String(params.get("groupid"));
      if (ids.length() > 0 && groupid.length() > 0) {
        sql = " update kq_report_field set groupid=? where id in ("+objects[0]+")";
        rs.executeUpdate(sql, groupid, sqlParams);
        new KQReportFieldComInfo().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();
  }
}
