package com.engine.service.ModeForm.impl;

import com.engine.service.entity.Result;
import com.engine.service.ModeForm.ModeFormService;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ModeFormServiceImpl implements ModeFormService {
    @Override
    public Result getDepartmentFormField(Map<String, Object> paramMap) {
        int level = Util.getIntValue((String) paramMap.get("level"),0);
        int alllevel = Util.getIntValue((String)paramMap.get("alllevel"),0);

        RecordSet recordSet = new RecordSet();
        ArrayList<Map<String, String>> records = new ArrayList<>();
        if (level == 1){ //点击分部
            int id = Util.getIntValue((String) paramMap.get("id"), 0);
            recordSet.executeQuery("select dept.id id ,  SUBCOMPANYID1 SUBCOMPANYID ,SUPDEPID, defined.FZXBSJB  FZXBSJB,defined.SFYJZXYWBS SFYJZXYWBS from HRMDEPARTMENT dept " +
                                     "left join hrmdepartmentdefined defined "+
                                     "on dept.id = defined.DEPTID where dept.SUBCOMPANYID1 = ?",id);
            while (recordSet.next()){
                HashMap<String, String> record = new HashMap<>();
                String deptid = Util.null2String(recordSet.getString("id"));
                record.put("id",deptid);
                //是否一级支行业务部室  0 是 1 否
                String fzxbsjb = Util.null2String(recordSet.getString("FZXBSJB"));
                record.put("fzxbsjb",fzxbsjb);
                //分支行部室级别
                String sfyjzxywbs = Util.null2String(recordSet.getString("SFYJZXYWBS"));
                record.put("sfyjzxywbs",sfyjzxywbs);
                String subcompanyid = Util.null2String(recordSet.getString("SUBCOMPANYID"));
                record.put("subcompanyid",subcompanyid);
                String supdepid = Util.null2String(recordSet.getString("SUPDEPID"));
                record.put("supdepid",supdepid);
                records.add(record);
            }
            return new Result(records,200);
        }else if(level == 2 || level == 3 || level == 3){
            int id = Util.getIntValue((String) paramMap.get("id"), 0);
            recordSet.executeQuery("select dept.id id ,SUBCOMPANYID1 SUBCOMPANYID ,SUPDEPID, defined.FZXBSJB  FZXBSJB,defined.SFYJZXYWBS SFYJZXYWBS from HRMDEPARTMENT dept " +
                    "left join hrmdepartmentdefined defined "+
                    "on dept.id = defined.DEPTID where dept.SUBCOMPANYID1 =  (select SUBCOMPANYID1 from HRMDEPARTMENT  where id = ? )",id);
            while (recordSet.next()){
                HashMap<String, String> record = new HashMap<>();
                String deptid = Util.null2String(recordSet.getString("id"));
                record.put("id",deptid);
                //是否一级支行业务部室  0 是 1 否
                String fzxbsjb = Util.null2String(recordSet.getString("FZXBSJB"));
                record.put("fzxbsjb",fzxbsjb);
                //分支行部室级别
                String sfyjzxywbs = Util.null2String(recordSet.getString("SFYJZXYWBS"));
                record.put("sfyjzxywbs",sfyjzxywbs);
                String subcompanyid = Util.null2String(recordSet.getString("SUBCOMPANYID"));
                record.put("subcompanyid",subcompanyid);
                String supdepid = Util.null2String(recordSet.getString("SUPDEPID"));
                record.put("supdepid",supdepid);
                records.add(record);
            }
            return new Result(records,200);
        }else if(alllevel ==1){
            String types = Util.null2String(paramMap.get("types"));
            String[] typeArr = null;
            if (types.length() > 0){
                typeArr = types.split(",");
            }
            ArrayList<String> deptList = new ArrayList<>();
            ArrayList<String> subcomList = new ArrayList<>();
            for (String type : typeArr) {
               String[] item =  type.split("\\|");
               String itemType = item[0];
               if ("subcom".equals(itemType)){
                   subcomList.add(item[1]);
               }
               if ("dept".equals(itemType)){
                   deptList.add(item[1]);
               }
            }

            String a = String.join(",",subcomList);
            String b = String.join(",",deptList);
            String sql = "select distinct dept.id id ,SUBCOMPANYID1 SUBCOMPANYID ,SUPDEPID, defined.FZXBSJB  FZXBSJB,defined.SFYJZXYWBS SFYJZXYWBS from HRMDEPARTMENT dept " +
                    "left join hrmdepartmentdefined defined "+
                    "on dept.id = defined.DEPTID where ";
            if (subcomList.size() > 0){
                sql  = sql + "dept.SUBCOMPANYID1 in ("+a+")";
            }
            if(subcomList.size() > 0 && deptList.size() > 0){
                sql = sql + " or ";
            }
            if (deptList.size() > 0){
                sql  = sql + " dept.SUBCOMPANYID1 in (select SUBCOMPANYID1 from HRMDEPARTMENT  where id in ("+b+") )";
            }
            recordSet.executeQuery(sql);
            new BaseBean().writeLog("select distinct dept.id id ,SUBCOMPANYID1 SUBCOMPANYID ,SUPDEPID, defined.FZXBSJB  FZXBSJB,defined.SFYJZXYWBS SFYJZXYWBS from HRMDEPARTMENT dept " +
                    "left join hrmdepartmentdefined defined "+
                    "on dept.id = defined.DEPTID where dept.SUBCOMPANYID1 in ("+a+") or dept.SUPDEPID in (select SUBCOMPANYID1 from HRMDEPARTMENT  where id in ("+b+") )");
            while (recordSet.next()){
                HashMap<String, String> record = new HashMap<>();
                String deptid = Util.null2String(recordSet.getString("id"));
                record.put("id",deptid);
                //是否一级支行业务部室  0 是 1 否
                String fzxbsjb = Util.null2String(recordSet.getString("FZXBSJB"));
                record.put("fzxbsjb",fzxbsjb);
                //分支行部室级别
                String sfyjzxywbs = Util.null2String(recordSet.getString("SFYJZXYWBS"));
                record.put("sfyjzxywbs",sfyjzxywbs);
                String subcompanyid = Util.null2String(recordSet.getString("SUBCOMPANYID"));
                record.put("subcompanyid",subcompanyid);
                String supdepid = Util.null2String(recordSet.getString("SUPDEPID"));
                record.put("supdepid",supdepid);
                records.add(record);
            }
            new BaseBean().writeLog(records);
            return new Result(records,200);
        }
        return new Result(null,500);
    }
}
