package com.engine.htsc.GwRelated.fzjg.Service.imp;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.htsc.GwRelated.fzjg.Dao.FzjgfwBaseDao;
import com.engine.htsc.GwRelated.fzjg.Service.FzjgfwBaseService;
import com.engine.htsc.workflow.ld.DeptWHUtil;
import com.fasterxml.jackson.databind.annotation.JsonAppend;

import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.authority.manager.HrmDepartmentManager;
import weaver.interfaces.htsc.GlobatTools.DBTools;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author xf
 * @version 1.0.0
 * @ClassName FzjgfwBaseService.java
 * @createTime 2021年11月17日 11:16:00
 */
public class FzjgfwBaseServiceimp extends Service implements FzjgfwBaseService {

    @Override
    public JSONObject getfqxtype(Map<String, Object> map, User user) {
        PropBean propBean=new PropBean();
        JSONObject jsonObject=new JSONObject();
        String hgry="";
        String nbsh="";
        String othernbsh="";
        String szbm= Util.null2String(map.get("szbm"));
        if("".equals(szbm)){
            return jsonObject;
        }
        //判断是否是营业部
        String hgfktdfzr=propBean.getPropName("fzjgfw.hgfktdfzr");
        String fxqfkg=propBean.getPropName("fzjgfw.fxqfkg");
        //判断当前人员是不是合规风控部、或者是部门负责人
        String userid=Util.null2String(user.getUID());
        Map<String,String> vfmap=new HashMap<>();
        vfmap.put("hgfktdfzr",hgfktdfzr);
        vfmap.put("fxqfkg",fxqfkg);
        vfmap.put("userid",Util.null2String(user.getUID()));
        vfmap.put("deptid",szbm);
        boolean flag=FzjgfwBaseDao.isgwuservf(vfmap);
        if(!flag){
            //是合规人员或者是负责人
            return jsonObject;
        }
        String szbmvalue= FzjgfwBaseDao.getdepttype(szbm);
        if("1".equals(szbmvalue)){
        /*
           分公司拟稿，将附件表格该分公司对应的合规人员、
           合规管理团队负责人加载到拟稿部门内审，不可删除；
           如果两岗位为同一人则去重
          **/
            hgry= getlcgwrymodel(szbm,fxqfkg);
            nbsh= getlcgwrymodel(szbm,hgfktdfzr);
          //  hgry= JAVATools.leavedistinct(hgry,nbsh);
            jsonObject.put("type","0");
//            jsonObject.put("hgry",hgry);
//            if(!"".equals(hgry)){
//                jsonObject.put("hgryvalue",hrmusercode(hgry));
//            }
            if(!"".equals(nbsh) && !"".equals(hgry)){
                nbsh+=","+hgry;
            }
            if("".equals(nbsh) && !"".equals(hgry)){
                nbsh=hgry;
            }
            nbsh= Arrays.stream(nbsh.split(",")).distinct().collect(Collectors.joining(","));
            jsonObject.put("nbsh",nbsh);
            if(!"".equals(nbsh)){
                jsonObject.put("nbshvalue",hrmusercode(nbsh));
            }
            //总部
        }else{
            jsonObject.put("type","1");
         /*
            1、营业部拟稿，加载附件表格中该营业部对应合规人员至拟稿部门内审，不可删除；
            将分公司管理岗设为附件表格中该分公司对应的合规人员，
             若分公司合规人员非合规管理团队负责人，
             再将合规管理团队负责人加载至分公司审核子流程的内部审核，不可删除
          */
            //获取营业部数据
            String supdeptid=JAVATools.getsupdepid(szbm);
            nbsh = getlcgwrymodel(szbm,fxqfkg);
            hgry = getlcgwrymodel(supdeptid,fxqfkg);
            nbsh= JAVATools.leavedistinct(nbsh,hgry);
            String hgryvalue=","+hgry+",";
            othernbsh= getlcgwrymodel(supdeptid,hgfktdfzr);
            if(!"".equals(othernbsh) && !"".equals(hgry)){
                othernbsh=  Arrays.stream(othernbsh.split(",")).filter((model)->
                  hgryvalue.indexOf(","+model+",")<0
                ).collect(Collectors.joining(","));
            }
            jsonObject.put("fgsglg",hgry);
            if(!"".equals(hgry)){
                jsonObject.put("fgsglgvalue",hrmusercode(hgry));
            }
            jsonObject.put("nbsh",nbsh);
            if(!"".equals(nbsh)){
                jsonObject.put("nbshvalue",hrmusercode(nbsh));
            }
            jsonObject.put("othernbsh",othernbsh);
            if(!"".equals(othernbsh)){
                jsonObject.put("othernbshvalue",hrmusercode(othernbsh));
            }
        }
        return jsonObject;
    }


    @Override
    public JSONObject getfzjggwadmin(Map<String, Object> map, User user) {
        JSONObject jsonObject=new JSONObject();
        JSONArray jsonArray=new JSONArray();
        List<String> strlist=new ArrayList<>();
        String deptid= Util.null2String(map.get("deptid"));
           if("".equals(deptid)){
               return jsonObject;
           }
           String sql="select wm_concat_old(gwgly) as gwgly from uf_fzjg_gwpzb where 1=1 ";
           Map<String,String> wheremap=new HashMap<>();
           wheremap.put("bm",deptid);
           Map<String,Object> returnmap=JAVATools.GetTableToMap(sql,wheremap);
            if(returnmap==null || returnmap.size()<=0){
                return jsonObject;
            }
        String gwgly=Util.null2String(returnmap.get("gwgly"));
        if("".equals(gwgly)){
            return jsonObject;
        }
        Arrays.stream(gwgly.split(",")).distinct().forEach(value->{
            String  gwpzname= JAVATools.getHrmName(value);
            JSONObject jsondetail=new JSONObject();
            jsondetail.put("id",value);
            jsondetail.put("name",gwpzname);
            jsonArray.add(jsondetail);
            strlist.add(value);
        });
        if(jsonArray.size()>0){
            jsonObject.put("value",strlist.stream().collect(Collectors.joining(",")));
            jsonObject.put("specialobj",jsonArray);
        }
        return jsonObject;
    }

    private String getlcgwrymodel(String deptid, String typevalue){
        String sql=" select ry from uf_lcgwry where xzlc ='fzjgfw' ";
        Map<String,String> map=new HashMap<>();
        map.put("jg",deptid);
        map.put("xzgw",typevalue);
        Map<String,Object> returnmap= JAVATools.GetTableToMap(sql,map);
        if(returnmap==null ||returnmap.size()<=0){
            return "";
        }
        return Util.null2String(returnmap.get("ry"));
    }

    public JSONArray hrmusercode(String ids){
        JSONArray jsonArray=new JSONArray();
        if("".equals(ids)){
            return jsonArray;
        }
        String sql="select * from hrmresource where id in ("+ids+")";
        List<Map<String,Object>> list= JAVATools.GetTableToListMap(sql,null);
        if(list==null && list.size()<=0){
            return jsonArray;
        }
        list.forEach((Map<String,Object> model) ->{
          String id=Util.null2String(model.get("id"));
          String lastname=Util.null2String(model.get("lastname"));
          if(!"".equals(id) && !"".equals(lastname)){
              JSONObject jsonObject=new JSONObject();
              jsonObject.put("id",id);
              jsonObject.put("name",lastname);
              jsonArray.add(jsonObject);
          }
        });
        return jsonArray;
    }

}
