package com.api.htsc.browser.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.browser.service.impl.OrganizationBrowserService;
import java.util.HashMap;
import java.util.Map;
import javafx.util.Pair;
import weaver.conn.RecordSet;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.hrm.User;

public class HtscOrganizationBrowserService extends OrganizationBrowserService {
  
  @Override
  public Map<String, Object> getBrowserData(Map<String, Object> params) throws Exception {
    Map<String, Object> map = super.getBrowserData(params);
    Map<String, Object> result = new HashMap<>(map);
    String id = Util.null2String(params.get("id"));
    if (map.containsKey("datas")) {
      Object dataObject = JSON.toJSON(map.get("datas"));
      if (dataObject instanceof JSONArray) {
        JSONArray datas = (JSONArray) JSON.toJSON(map.get("datas"));
        if (datas != null) {
          //查询所有
          Map<String, Pair> pairMap = getAllHrmDepartmentDefined();
          //遍历结果
          for (int i = 0; i < datas.size(); i++) {
            JSONObject data = (JSONObject) datas.get(i);
            String deptId = data.getString("id");
            if (pairMap.containsKey(deptId)) {
              data.put("deptTypeCode", pairMap.get(deptId).getKey());
              data.put("deptClassCode", pairMap.get(deptId).getValue());
            } else {
              data.put("deptTypeCode", "");
              data.put("deptClassCode", "");
            }
          }
          result.put("datas", datas);
          if (pairMap.containsKey(id)) {
            result.put("deptTypeCode", pairMap.get(id).getKey());
            result.put("deptClassCode", pairMap.get(id).getValue());
          } else {
            result.put("deptTypeCode", "");
            result.put("deptClassCode", "");
          }
        }
      }
    }
    
    return result;
  }
  
  private Map<String, Pair> getAllHrmDepartmentDefined() {
    Map<String, Pair> result = new HashMap<>();
    RecordSet rs = new RecordSet();
    String sql = "select deptid,depttypecode,deptscalelevelcode from hrmdepartmentdefined";
    rs.executeQuery(sql);
    while (rs.next()) {
      String deptid = Util.null2String(rs.getString("deptid"));
      String depttypecode = Util.null2String(rs.getString("depttypecode"));
      String deptscalelevelcode = Util.null2String(rs.getString("deptscalelevelcode"));
      Pair<String, String> pair = new Pair<>(depttypecode, deptscalelevelcode);
      result.put(deptid, pair);
    }
    return result;
  }
  
  public static void main(String[] args) {
    //测试时将this.browserType = "57";置于HtscOrganizationBrowserService的getBrowserData方法内
    GCONST.setRootPath("WEB-INF/");
    GCONST.setServerName("ecology");
    Map<String, Object> params = new HashMap<>();
    String paramStr = "pageSize=10&current=1&min=1&max=10&wfid=5522&billid=-76&isbill=1&requestid=-1&f_weaver_belongto_userid=27022&f_weaver_belongto_usertype=0&wfTestStr=&fieldid=88024&viewtype=0&fromModule=workflow&wfCreater=27022&virtualCompanyid=1&__random__=1614070027173";
    String[] split = paramStr.split("&");
    for (String param : split) {
      String[] split1 = param.split("=");
      if (split1.length < 2) {
        params.put(split1[0], "");
      } else {
        params.put(split1[0], split1[1]);
      }
    }
    /**
     * types: dept|35
     * selectids: 35
     * virtualCompanyid: 1
     * wfid: 5522
     * billid: -76
     * isbill: 1
     * requestid: -1
     * f_weaver_belongto_userid: 27022
     * f_weaver_belongto_usertype: 0
     * wfTestStr:
     * fieldid: 88024
     * viewtype: 0
     * fromModule: workflow
     * wfCreater: 27022
     */
    // params.put("types","dept|35");
    // params.put("selectids","35");
    // params.put("virtualCompanyid","1");
    // params.put("wfid","5522");
    // params.put("billid","-76");
    // params.put("isbill","1");
    // params.put("requestid","-1");
    // params.put("f_weaver_belongto_userid","27022");
    // params.put("f_weaver_belongto_usertype","0");
    // params.put("wfTestStr","");
    // params.put("fieldid","0");
    // params.put("fromModule","workflow");
    // params.put("wfCreater","27022");
    HtscOrganizationBrowserService htscOrganizationBrowserService = new HtscOrganizationBrowserService();
    String f_weaver_belongto_userid = Util.null2String(params.get("f_weaver_belongto_userid"));
    String f_weaver_belongto_usertype = Util.null2String(params.get("f_weaver_belongto_usertype"));
    User user = User.getUser(Integer.parseInt(f_weaver_belongto_userid),
        Integer.parseInt(f_weaver_belongto_usertype));
    htscOrganizationBrowserService.setUser(user);
    Map<String, Object> result = null;
    try {
      result = htscOrganizationBrowserService.getBrowserData(params);
    } catch (Exception e) {
      e.printStackTrace();
    }
    System.out.println(JSON.toJSONString(result));
  }
}
