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.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import java.util.*;

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

  public GetReportMoudleFormCmd(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 {
      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 moudlename = "";
      if (id.length() > 0) {
        sql = "select * from kq_report_moudle where id=?";
        rs.executeQuery(sql, id);
        if (rs.next()) {
          moudlename = Util.null2String(rs.getString("moudlename"));
        }
      }

      hrmFieldBean = new HrmFieldBean();
      hrmFieldBean.setFieldname("moudlename");
      hrmFieldBean.setFieldlabel("18151");
      hrmFieldBean.setFieldhtmltype("1");
      hrmFieldBean.setType("1");
      hrmFieldBean.setFieldvalue(moudlename);
      hrmFieldBean.setViewAttr(3);
      hrmFieldBean.setMultilang(true);
      hrmFieldBean.setIsFormField(true);
      hrmFieldBean.setRules("required|string");
      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;
  }

}
