package com.engine.htsc.generaltrip.dao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.generaltrip.model.UfCcsqspl;
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 UfCcsqsplDao {

    public List<String> getCcsqsplGx(String bgl){
        List<String> zdmcList = new ArrayList<>();
        String sql = "select a.zdmc,a.sfzb from uf_allccsqspl_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<UfCcsqspl> getCcsqspl(Map<String,String> mapReqSql){
        List<UfCcsqspl> list = new ArrayList();
        String sql = "select * from uf_ccsqspl where 1 = 1";
        List<Map<String, String>> resp = getTableToListMap(sql,mapReqSql);
        JAVATools.writelog("获取审批链sql："+sql+ JSONObject.toJSONString(resp));
        for (Map<String, String> colunmn:resp){
            UfCcsqspl ufCcsqspl = JSON.parseObject(JSON.toJSONString(colunmn), UfCcsqspl.class);
            list.add(ufCcsqspl);
        }
        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;
    }

}
