package com.engine.htsc.seal.service;

import com.engine.core.impl.Service;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;

import java.util.*;

public class ZjSealApplicationService extends Service {

    /**
     * 根据印章和文件类别类型获取领导和跨公司会签
     * @param yz
     * @param wjlb
     * @return
     */
    public Map<String,Object> getDeptBySealAndFileType(String yz, String wjlb, String szbm){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String,Object> resultMap = new HashMap<>();

        try{
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            Set<String> gsldSet = new HashSet<>();
            Set<String> kgshqSet = new HashSet<>();
            String sql = "select qzsld,qzkgshq from uf_zjyysyfw where wjlb = '"+wjlb+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                gsldSet.addAll(Arrays.asList(Util.null2String(rs.getString("qzsld")).split(",")));
                kgshqSet.addAll(Arrays.asList(Util.null2String(rs.getString("qzkgshq")).split(",")));
            }

            List<Map<String,String>> gsldMapList = new ArrayList<>();
            for(String id:gsldSet){
                if(StringUtils.isNotEmpty(id)){
                    Map<String,String> userMap = new HashMap<>();
                    userMap.put("id",id);
                    userMap.put("name",resourceComInfo.getLastname(id));
                    gsldMapList.add(userMap);
                }
            }
            resultMap.put("gsld",gsldMapList);//公司领导

            List<Map<String,String>> kgshqMapList = new ArrayList<>();
            for(String deptid:kgshqSet){
                sql = "select deptdirectors from hrmdepartmentdefined where deptid = '"+deptid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    String deptdirectors = Util.null2String(rs.getString("deptdirectors"));
                    if(StringUtils.isNotBlank(deptdirectors)){
                        for(String userid:deptdirectors.split(",")){
                            Map<String,String> deptMap = new HashMap<>();
                            deptMap.put("id",userid);
                            deptMap.put("name",resourceComInfo.getLastname(userid));
                            kgshqMapList.add(deptMap);
                        }
                    }
                }
            }
            resultMap.put("kgshq",kgshqMapList);//跨公司会签

        }catch (Exception e){
            e.printStackTrace();
        }

        return resultMap;
    }

    /**
     * 根据印章获取用印管理员
     * @param yz
     * @return
     */
    public Map<String,Object> getYzgly(String yz){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String,Object> resultMap = new HashMap<>();

        try{
            String sql = "";
            ResourceComInfo resourceComInfo = new ResourceComInfo();

            List<Map<String,String>> yzglyMapList = new ArrayList<>();//印章管理员
            Set<String> yzglySet = new HashSet<>();
            sql = "select yzgly from uf_zjyyyz where id in ("+yz+")";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            while(rs.next()){
                yzglySet.addAll(Arrays.asList(Util.null2String(rs.getString("yzgly")).split(",")));
            }

            for(String id:yzglySet){
                if(StringUtils.isNotBlank(id)){
                    Map<String,String> leaderMap = new HashMap<>();
                    leaderMap.put("id",id);
                    leaderMap.put("name",resourceComInfo.getLastname(id));
                    yzglyMapList.add(leaderMap);
                }
            }
            resultMap.put("yzgly",yzglyMapList);

        }catch (Exception e){
            e.printStackTrace();
        }

        return resultMap;
    }
}
