package com.engine.htsc.itcssx.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 weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.*;

public class ITTestOnlineService extends Service {

    /**
     * 获取会签部门
     * @param requestids
     * @return
     */
    public Map<String,Object> getSignDept(String requestids){
        RecordSet rs = new RecordSet();
        BaseBean bs = new BaseBean();
        PropBean pb = new PropBean();
        Map<String,Object> resultMap = new HashMap<>();

        Set<String> deptSet = new HashSet<>();
        String sql = "";

        for(String requestid:requestids.split(",")){
            String workflowid = JAVATools.getWorkflowidByRequestid(requestid);
            sql = "select UNNUMBER from WORKFLOW_BASE where ID="+workflowid;
            String wfunnumber = "";
            JAVATools.writelog(sql);
            rs.execute(sql);
            if (rs.next()){
                wfunnumber = Util.null2String(rs.getString("UNNUMBER"));
            }
            String wfId = pb.getWorkflowIdsByUnNumber(wfunnumber);
            if(workflowid.equals(wfId)){
                String tableName = JAVATools.getMaintable(requestid);
                String mainid = "";
                sql = "select id from "+tableName+" where requestid = '"+requestid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    mainid = Util.null2String(rs.getString("id"));
                }

                sql = "select hqbm from "+tableName+"_dt2 where mainid = '"+mainid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                while(rs.next()){
                    deptSet.add(Util.null2String(rs.getString("hqbm")));
                }
            }
        }

        List<Map<String,String>> deptMapList = new ArrayList<>();
        for(String deptid:deptSet){
            if(StringUtils.isNotEmpty(deptid)){
                Map<String,String> deptMap = new HashMap<>();
                String deptName = "";
                sql = "select departmentname from hrmdepartment where id = '"+deptid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    deptName = Util.null2String(rs.getString("departmentname"));
                }
                deptMap.put("id",deptid);
                deptMap.put("name",deptName);
                deptMapList.add(deptMap);
            }
        }
        resultMap.put("dept",deptMapList);

        return resultMap;
    }

    /**
     * 获取默认通知阅读
     * @param deptid
     * @return
     */
    public Map<String,Object> getDefaultNotice(String deptid){
        BaseBean bs = new BaseBean();
        PropBean pb = new PropBean();
        RecordSet rs = new RecordSet();
        Map<String,Object> resultMap = new HashMap<>();

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

            Set<String> noticeSet = new HashSet<>();
            sql = "select tzydr from uf_itxqgw where ngbm = '"+deptid+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                String tzydr = Util.null2String(rs.getString("tzydr"));
                noticeSet.addAll(Arrays.asList(tzydr.split(",")));
            }

            List<Map<String,String>> tzydrMapList = new ArrayList<>();
            for(String id:noticeSet){
                Map<String,String> userMap = new HashMap<>();
                userMap.put("id",id);
                userMap.put("name",resourceComInfo.getLastname(id));
                tzydrMapList.add(userMap);
            }

            String noticeUser = Util.null2String(pb.getPropName("itcssx_notice_user"));
            for(String loginid:noticeUser.split(",")){
                Map<String,String> userMap = new HashMap<>();
                sql = "select id,lastname from hrmresource where loginid = '"+loginid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    userMap.put("id",Util.null2String(rs.getString("id")));
                    userMap.put("name",Util.null2String(rs.getString("lastname")));
                    tzydrMapList.add(userMap);
                }
            }

            resultMap.put("tzydr",tzydrMapList);
        }catch (Exception e){
            e.printStackTrace();
        }

        return resultMap;
    }
}
