package com.engine.htsc.seal.service;

import com.alibaba.fastjson.JSONObject;
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 CxSealApplicationService extends Service {

    /**
     * 获得常用章
     * @param szbm
     * @return
     */
    public Map<String,Object> getCommonSeal(String szbm){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String,Object> resultMap = new HashMap<>();

        try{
            String sql;

            //公司公章
            sql = "select t1.id,t1.yzmc,t1.yzlx,t1.pxzd from uf_yinzhang t1,uf_tsld t2 where t2.yz = t1.id and t2.lcxx = 'cxyy' and t2.qt = 'cxgsgz'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                Map<String,String> seal = new HashMap<>();
                seal.put("id", Util.null2String(rs.getString("id")));
                seal.put("yzmc",Util.null2String(rs.getString("yzmc")));
                seal.put("yzlx",Util.null2String(rs.getString("yzlx")));
                seal.put("pxzd",Util.null2String(rs.getString("pxzd")));
                resultMap.put("gsgz",seal);
            }

            //法人章
            sql = "select t1.id,t1.yzmc,t1.yzlx,t1.pxzd from uf_yinzhang t1,uf_tsld t2 where t2.yz = t1.id and t2.lcxx = 'cxyy' and t2.qt = 'fddbr'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                Map<String,String> seal = new HashMap<>();
                seal.put("id",Util.null2String(rs.getString("id")));
                seal.put("yzmc",Util.null2String(rs.getString("yzmc")));
                seal.put("yzlx",Util.null2String(rs.getString("yzlx")));
                seal.put("pxzd",Util.null2String(rs.getString("pxzd")));
                resultMap.put("frz",seal);
            }

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

        return resultMap;
    }

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

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

            List<Map<String,String>> kgshqMapList = new ArrayList<>();
            for(String deptid:deptSet){
                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;
    }
}
