package com.engine.htsc.subcompany.dao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.subcompany.model.UfZgsspl;
import org.apache.commons.collections4.CollectionUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UfZgssplDao {
    //部门自定义表
    private static final String TABLE_DEPARTMENT_DEFINED = "hrmdepartmentdefined";

    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+"' and a.status=1 ";
        JAVATools.writelog("sql根据人员工号和部门code查找人员id："+sql);
        rs.execute(sql);
        if(rs.next()){
            result= Util.null2String(rs.getString("id"));
        }
        return result;
    }

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

    public List<String> getUfzgssplGx(String bgl){
        List<String> zdmcList = new ArrayList<>();
        String sql = "select a.zdmc,a.sfzb from uf_zgsspl_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 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(CollectionUtils.isEmpty(list)){
            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 Map<String, String> getCwBybm(String depId) {
        Map<String,String> result = new HashMap<>();
        String sql = "select a.bm,a.kj,a.kjjl,a.cn from uf_fzjgfkgwpp a where a.bm = " + "'"+depId+"'";
        RecordSet rs = new RecordSet();
        JAVATools.writelog("获取uf_fzjgfkgwpp财务数据sql："+sql);
        rs.execute(sql);
        if(rs.next()){
            result.put("kj", Util.null2String(rs.getString("kj")));
            result.put("kjjl", Util.null2String(rs.getString("kjjl")));
            result.put("cn", Util.null2String(rs.getString("cn")));
        }
        return result;
    }

    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;
    }

    /**
     * 获取投行负责人
     *
     * @param departmentID
     */
    public String getThfzr(String departmentID) {
        String result = "";
        RecordSet rs = new RecordSet();
        String sql = String.format("select txqyfzr from %s where deptid = ?", TABLE_DEPARTMENT_DEFINED);
        rs.executeQuery(sql, departmentID);
        JAVATools.writelog("获取投行负责人："+sql+departmentID);
        while (rs.next()) {
            result = rs.getString("txqyfzr");
            break;
        }
        return result;
    }

    /**
     * 获取费用会计
     *
     * @param bxdd
     */
    public String getFykj(String bxdd) {
        String result = "";
        RecordSet rs = new RecordSet();
        String sql = "select a.fyhj from uf_fykjjd a where a.bgdd = "+bxdd;
        JAVATools.writelog("获取费用会计："+sql);
        rs.execute(sql);
        if(rs.next()){
            result = Util.null2String(rs.getString("fyhj"));
        }
        return result;
    }

    //获取出纳信息子公司通用
    public Map<String,String> getCnMap(String bm,String jobCode){
        Map<String,String> result = new HashMap<>();
        String sql = "select a.jobcode,a.cn,a.bmbh,a.bmmc from uf_zgscn a where bmbh = '"+bm+"' and jobcode = '"+jobCode+"'";
        RecordSet rs = new RecordSet();
        JAVATools.writelog("获取出纳信息子公司通用sql："+sql);
        rs.execute(sql);
        String cn = "";
        while (rs.next()){
            String cnId = JAVATools.GetWorkcodetoid(Util.null2String(rs.getString("cn")));
            cn+=cn==""?cnId:","+cnId;
        }
        result.put("cn",cn);
        return result;
    }

    //获取出纳信息紫金
    public Map<String,String> getZjCnMap(String bm){
        Map<String,String> result = new HashMap<>();
        String sql = "select a.cn from uf_zjbmb a where bmbh = '"+bm+"'";
        RecordSet rs = new RecordSet();
        JAVATools.writelog("获取出纳信息紫金sql："+sql);
        rs.execute(sql);
        if(rs.next()){
            result.put("cn",Util.null2String(rs.getString("cn")));
        }
        return result;
    }

    public Map<String,Double> gethlMap() {
        Map<String,Double> result = new HashMap<>();
        String sql = "select a.bz,a.hl from uf_HKdolexcrateall a";
        RecordSet rs = new RecordSet();
        JAVATools.writelog("获取汇率信息sql："+sql);
        rs.execute(sql);
        while(rs.next()){
            String bzStr = Util.null2String(rs.getString("bz"));
            String hlStr = Util.null2String(rs.getString("hl"));
            if(!"".equals(hlStr)){
                Double hl = Double.parseDouble(hlStr);
                result.put(bzStr,hl);
            }
        }
        return result;
    }

    //获取出纳信息紫金
    public Map<String,String> getZjfzrMap(String bm){
        Map<String,String> result = new HashMap<>();
        String sql = "select bmfzrid,sjbmbh from uf_zjbmb a where bmbh = '"+bm+"'";
        RecordSet rs = new RecordSet();
        JAVATools.writelog("获取紫金费用承担部门负责人sql："+sql);
        rs.execute(sql);
        if(rs.next()){
            result.put("bmfzrid",Util.null2String(rs.getString("bmfzrid")));
            result.put("sjbmbh",Util.null2String(rs.getString("sjbmbh")));
            result.put("cn",Util.null2String(rs.getString("cn")));
        }
        return result;
    }

    //获取紫金部门各节点操作人
    public List<Map<String,String>> getZjbmgjdcz(String bmbh,String lclx){
        List<Map<String,String>> mapList = new ArrayList<>();
        String sql = "select bm,fklx,lclx,ysgly,cwzxhj,cwzxjl,zgszjl from uf_zjsplwh where bm = '"+bmbh+"' and lclx = '"+lclx+"'";
        RecordSet rs = new RecordSet();
        JAVATools.writelog("获取紫金费用承担部门负责人sql："+sql);
        rs.execute(sql);
        while (rs.next()){
            Map<String,String> result = new HashMap<>();
            result.put("bmbh",Util.null2String(rs.getString("bmbh")));
            result.put("fklx",Util.null2String(rs.getString("fklx")));
            result.put("lclx",Util.null2String(rs.getString("lclx")));
            result.put("ysgly",Util.null2String(rs.getString("ysgly")));
            result.put("cwzxhj",Util.null2String(rs.getString("cwzxhj")));
            result.put("cwzxjl",Util.null2String(rs.getString("cwzxjl")));
            result.put("zgszjl",Util.null2String(rs.getString("zgszjl")));
            mapList.add(result);
        }
        return mapList;
    }
}
