package com.engine.htsc.general.dao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.general.model.UfZbspl;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

import java.util.*;

public class UfZbsplDao {

    public List<String> getZbsplgx(String bgl){
        List<String> zdmcList = new ArrayList<>();
        String sql = "select a.zdmc,a.sfzb from uf_zbspl_gx a where a.bgl = " + "'"+bgl+"'";
        RecordSet rs = new RecordSet();
        JAVATools.writelog("获取审批链关系表数据sql："+sql);
        rs.execute(sql);
        while(rs.next()){
            zdmcList.add(Util.null2String(rs.getString("zdmc")));
        }
        return zdmcList;
    }

    public List<UfZbspl> getUfZbspl(Map<String,String> mapReqSql){
        List<UfZbspl> list = new ArrayList();
        String sql = "select * from uf_zbspl where 1 = 1";
        List<Map<String, String>> resp = getTableToListMap(sql,mapReqSql);
        JAVATools.writelog("获取审批链sql："+sql+ JSONObject.toJSONString(resp));
        for (Map<String, String> colunmn:resp){
            UfZbspl ufZbspl = JSON.parseObject(JSON.toJSONString(colunmn), UfZbspl.class);
            list.add(ufZbspl);
        }
        return list;
    }

    public static List getTableToListMap(String sql,Map<String,String> parmasmap){
        RecordSet rs = new RecordSet();
        List<String> list = new ArrayList<>();
        String where = "";
        List<Map<String,Object>> listreturn = new ArrayList<>();
        if(parmasmap!=null&&parmasmap.size()>0){
            where += " and (cxsprjs = ? or cxsprjs = ? or cxsprjs = 'ST_ALL')";
            for (String key:parmasmap.keySet()) {
                if("cxsprjs".equals(key)||"userCode".equals(key)) {
                    //   where+=where==""?" key=?":" and key=?";
                    list.add(parmasmap.get(key));
                }
            }
            for (String key:parmasmap.keySet()) {
                if(!"cxsprjs".equals(key)&&!"userCode".equals(key)&&!"tj".equals(key)) {
                    where+=" and "+key+"=? ";
                    list.add(parmasmap.get(key));
                }
            }
            for (String key:parmasmap.keySet()) {
                if("tj".equals(key)) {
                    where+=" or "+key+"=? ";
                    list.add(parmasmap.get(key));
                }
            }
        }

        if("".equals(where)){
            rs.execute(sql);
        }else{
            rs.executeQuery(sql+where,new Object[]{list});
        }

        while(rs.next()){
            Map<String,Object> map=new HashMap<>();
            String[] colnums=rs.getColumnName();
            for (String colnumvalue:colnums ) {
                map.put(colnumvalue.toLowerCase(),rs.getString(colnumvalue));
            }
            listreturn.add(map);

        }
        return listreturn;
    }


    public String getUserIdByCode(String workcode, String deptcode) {
        String result = "";
        RecordSet rs = new RecordSet();
        String sql = "select a.id,a.workcode,b.departmentcode from hrmresource a,hrmdepartment b  where a.workcode = '"+workcode+
                "' and a.departmentid = b.id and b.departmentcode = '"+deptcode+"'";
        JAVATools.writelog("sql根据人员工号和部门code查找人员id："+sql);
        rs.execute(sql);
        if(rs.next()){
            result= Util.null2String(rs.getString("id"));
        }
        return result;
    }

    public String getBmysgly(String bm) {
        String result = "";
        RecordSet rs = new RecordSet();
        String sql = "select ysgly from uf_ysgly where bm = '"+bm+"'";
        JAVATools.writelog("根据部门id查询部门预算管理员："+sql);
        rs.execute(sql);
        if(rs.next()){
            result= Util.null2String(rs.getString("ysgly"));
        }
        return result;
    }

    public String getFykj(String depId) {
        String result = "";
        RecordSet rs = new RecordSet();
        String sql = "select hjry from uf_zbkjpz where lx = 'ZBKJ' and glbm = '"+depId+"'";
        JAVATools.writelog("根据部门id查询总部费用会计人员："+sql);
        rs.execute(sql);
        if(rs.next()){
            result= Util.null2String(rs.getString("hjry"));
        }
        return result;
    }

    public TreeMap<String,ArrayList<String>> getSpljd(String unnumbercode,String type) {
        TreeMap<String,ArrayList<String>> resultTreeMap = new TreeMap<>();
        RecordSet rs = new RecordSet();
        String sql = "select * from uf_zbspljdsxb where sslc = '"+unnumbercode+"' and qcfs = '"+type+"' order by lcjdjb desc";
        JAVATools.writelog("查询总部审批链节点顺序表："+sql);
        rs.execute(sql);
        while (rs.next()){
            if(null!=resultTreeMap.get(Util.null2String(rs.getString("jdjbsx")))){
                resultTreeMap.get(Util.null2String(rs.getString("jdjbsx"))).add(Util.null2String(rs.getString("lcjd")));
            }else {
                resultTreeMap.put(Util.null2String(rs.getString("jdjbsx")),new ArrayList());
                resultTreeMap.get(Util.null2String(rs.getString("jdjbsx"))).add(Util.null2String(rs.getString("lcjd")));
            }
        }
        return resultTreeMap;
    }

    public List<Map<String, String>> getUserMap(String userids) {
        List<Map<String, String>> userMapList = new ArrayList<>();
        Map<String, String> codeAndIds = new HashMap<>();
        Map<String, String> idAndCodes = new HashMap<>();
        if (userids==null || "".equals(userids)){
            userMapList.add(idAndCodes);
            userMapList.add(codeAndIds);
            return  userMapList;
        }
        RecordSet rs=new RecordSet();
        String sql="select workcode,id from hrmresource where id in ("+userids+")";
        JAVATools.writelog("获取人员id和工号sql："+sql);
        rs.execute(sql);
        if(rs.getCounts()<=0){
            userMapList.add(idAndCodes);
            userMapList.add(codeAndIds);
            return  userMapList;
        }
        while(rs.next()){
            String workcode= Util.null2String(rs.getString("workcode"));
            String id= Util.null2String(rs.getString("id"));
            if(!"".equals(workcode)){
                idAndCodes.put(id,workcode);
                codeAndIds.put(workcode,id);
            }
        }
        userMapList.add(idAndCodes);
        userMapList.add(codeAndIds);
        return userMapList;
    }

    public String getSfld(String deptcode) {
        String result = "";
        RecordSet rs = new RecordSet();
        String sql = "select sfld from uf_fkxnbmpzb where dept_code = '"+deptcode+"'";
        JAVATools.writelog("查询uf_fkxnbmpzb表中部门是否为领导部门："+sql);
        rs.execute(sql);
        if(rs.next()){
            result= Util.null2String(rs.getString("sfld"));
        }
        return result;
    }
}
