package com.engine.kq.cmd.reportfielddefine;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
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.KQReportFieldComInfo;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class SaveFieldDefineListCmd extends AbstractCommonCommand<Map<String, Object>> {
  private SimpleBizLogger logger;
  public SaveFieldDefineListCmd(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 = "";
    String datas = Util.null2String(params.get("datas"));
    JSONArray jArray = JSON.parseArray(datas);
    for (int i = 0; i < jArray.size(); i++) {
      JSONObject jObject = (JSONObject) jArray.get(i);
      String id = Util.null2String(jObject.get("id"));
      if(ids.length()>0)ids+=",";
      ids+=id;
    }

    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>();
    RecordSet rs = new RecordSet();
    String sql = "";
    try {
      if (!HrmUserVarify.checkUserRight("KQReportFieldDefine:Add", user)) {
        retmap.put("status", "-1");
        retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
        return retmap;
      }

      String datas = Util.null2String(params.get("datas"));
      JSONArray jArray = JSON.parseArray(datas);

      if (jArray != null) {
        for (int i = 0; i < jArray.size(); i++) {
          JSONObject jObject = (JSONObject) jArray.get(i);
          String id = Util.null2String(jObject.get("id"));
          String isenable = Util.null2String(jObject.get("isenable"));
          String defaultshow = Util.null2String(jObject.get("defaultshow"));
          String fieldOrder = Util.null2String(jObject.get("fieldOrder"));
          // #1276494 概述：满足薪酬指标项关联考勤报表列数据需求 By T.W
          String ifSalaryNeed = Util.null2String(jObject.get("ifSalaryNeed"));
//          sql = "update kq_report_field set isenable=?,defaultshow=?,showorder=?,if_salary_need=? where id = ? ";
//          rs.executeUpdate(sql, isenable, defaultshow, fieldOrder, ifSalaryNeed, id);

          sql = "update kq_report_field set isenable=?,defaultshow=?,showorder=? where id = ? ";
          rs.executeUpdate(sql,isenable,defaultshow,fieldOrder,id);
        }
        new KQReportFieldComInfo().removeCache();
      }
      retmap.put("status", "1");
    } catch (Exception e) {
      retmap.put("status", "-1");
      retmap.put("message", SystemEnv.getHtmlLabelName(382661, user.getLanguage()));
      writeLog(e);
    }

    return retmap;
  }

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

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