package com.engine.kq.cmd.reportfielddefine;

import com.api.browser.bean.SearchConditionItem;
import com.api.browser.bean.SearchConditionOption;
import com.api.hrm.bean.HrmFieldBean;
import com.api.hrm.util.HrmFieldSearchConditionComInfo;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.LabelUtil;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import java.util.*;

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

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

  @Override
  public Map<String, Object> execute(CommandContext commandContext) {
    Map<String, Object> retmap = new HashMap<String, Object>();
    List<Map<String, Object>> grouplist = new ArrayList<Map<String, Object>>();
    Map<String, Object> groupitem = null;
    List<Object> itemlist = null;
    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 id = Util.null2String(params.get("id"));
      Map<String, List<HrmFieldBean>> fieldGroups = new LinkedHashMap<String, List<HrmFieldBean>>();
      List<HrmFieldBean> lsField = new ArrayList<HrmFieldBean>();
      HrmFieldBean hrmFieldBean = null;
      List<SearchConditionOption> options = null;

      String fieldname = "";//字段名
      String fieldlabel = "";//展示列名称
      String fieldtype = "1";//类型
      String fieldvalue = "";//区间（分钟）

      if (id.length() > 0) {
        sql = "select * from kq_report_field where id=?";
        rs.executeQuery(sql, id);
        if (rs.next()) {
          fieldname = Util.null2String(rs.getString("fieldname"));
          fieldlabel = Util.null2String(rs.getString("fieldlabel"));
          fieldtype = Util.null2String(rs.getString("formula")).indexOf("beLateMins")>-1?"1":"2";
          fieldvalue = Util.null2String(rs.getString("formula"));
          fieldvalue = fieldvalue.replace("${beLateMins}>","").replace("${beLateMins}<=","").replace("?1:0","").replace(" && ","-");
          fieldvalue = fieldvalue.replace("${leaveEarlyMins}>","").replace("${leaveEarlyMins}<=","").replace("?1:0","").replace(" && ","-");
        }
      }

      hrmFieldBean = new HrmFieldBean();
      hrmFieldBean.setFieldname("fieldlabel");
      hrmFieldBean.setFieldlabel("516154");
      hrmFieldBean.setFieldhtmltype("1");
      hrmFieldBean.setType("1");
      hrmFieldBean.setFieldvalue(LabelUtil.getMultiLangLabel(fieldlabel));
      hrmFieldBean.setViewAttr(3);
      hrmFieldBean.setMultilang(true);
      hrmFieldBean.setIsFormField(true);
      hrmFieldBean.setRules("required|string");
      lsField.add(hrmFieldBean);

      hrmFieldBean = new HrmFieldBean();
      hrmFieldBean.setFieldname("fieldname");
      hrmFieldBean.setFieldlabel("685");
      hrmFieldBean.setFieldhtmltype("1");
      hrmFieldBean.setType("1");
      hrmFieldBean.setFieldvalue(fieldname);
      hrmFieldBean.setViewAttr(id.length()>0?1:3);
      hrmFieldBean.setMultilang(false);
      hrmFieldBean.setRules("required|string");
      lsField.add(hrmFieldBean);

      hrmFieldBean = new HrmFieldBean();
      hrmFieldBean.setFieldname("fieldtype");
      hrmFieldBean.setFieldlabel("63");
      hrmFieldBean.setFieldhtmltype("5");
      hrmFieldBean.setType("1");
      hrmFieldBean.setFieldvalue(fieldtype);

      options = new ArrayList<SearchConditionOption>();
      options.add(new SearchConditionOption("1", SystemEnv.getHtmlLabelName(20081, user.getLanguage()), fieldtype.equals("1")));
      options.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(20082, user.getLanguage()), fieldtype.equals("2")));
      hrmFieldBean.setSelectOption(options);
      hrmFieldBean.setViewAttr(2);
      lsField.add(hrmFieldBean);

      hrmFieldBean = new HrmFieldBean();
      hrmFieldBean.setFieldname("fieldvalue");
      hrmFieldBean.setFieldlabel("516156");
      hrmFieldBean.setFieldhtmltype("1");
      hrmFieldBean.setType("1");
      hrmFieldBean.setFieldvalue(fieldvalue);
      hrmFieldBean.setViewAttr(3);
      hrmFieldBean.setMultilang(false);
      hrmFieldBean.setRules("required|string");
      lsField.add(hrmFieldBean);
      fieldGroups.put("20331", lsField);

      hrmFieldBean = new HrmFieldBean();
      hrmFieldBean.setFieldname("msg");
      hrmFieldBean.setFieldlabel("85");
      hrmFieldBean.setFieldhtmltype("8");
      hrmFieldBean.setType("1");
      hrmFieldBean.setFieldvalue(SystemEnv.getHtmlLabelName(516157, user.getLanguage()));
      lsField.add(hrmFieldBean);
      fieldGroups.put("20331", lsField);

      HrmFieldSearchConditionComInfo hrmFieldSearchConditionComInfo = new HrmFieldSearchConditionComInfo();
      SearchConditionItem searchConditionItem = null;
      Iterator<Map.Entry<String, List<HrmFieldBean>>> iter = fieldGroups.entrySet().iterator();
      while (iter.hasNext()) {
        Map.Entry<String, List<HrmFieldBean>> entry = iter.next();
        String grouplabel = entry.getKey();
        List<HrmFieldBean> fields = entry.getValue();
        groupitem = new HashMap<String, Object>();
        groupitem.put("title", SystemEnv.getHtmlLabelNames(grouplabel, user.getLanguage()));
        groupitem.put("defaultshow", true);

        itemlist = new ArrayList<Object>();
        for (int j = 0; j < fields.size(); j++) {
          hrmFieldBean = fields.get(j);
          searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
          itemlist.add(searchConditionItem);
        }
        groupitem.put("items", itemlist);
        grouplist.add(groupitem);
      }
      retmap.put("formField", grouplist);
      retmap.put("id", id);
      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;
  }

}
