package com.engine.htsc.branch.approvalChain.dao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.branch.approvalChain.model.UfFzjgspl;
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 FzjgTravelLinkDao {

    public List<String> getUfFzjgsplGx(String bgl){
        List<String> zdmcList = new ArrayList<>();
        String sql = "select a.zdmc,a.sfzb from uf_fzjgspl_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<UfFzjgspl> getUfjgspl(Map<String,String> mapReqSql){
        List<UfFzjgspl> list = new ArrayList();
        String sql = "select * from uf_fzjgspl where 1 = 1";
        List<Map<String, String>> resp = getTableToListMap(sql,mapReqSql);
        JAVATools.writelog("获取审批链sql"+sql+ JSONObject.toJSONString(resp));
        for (Map<String, String> colunmn:resp){
            UfFzjgspl ufFzjgspl = JSON.parseObject(JSON.toJSONString(colunmn), UfFzjgspl.class);
            list.add(ufFzjgspl);
        }
        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){
            for (String key:parmasmap.keySet()) {
                if("cxsprjs".equals(key)){
                    //   where+=where==""?" key=?":" and key=?";
                    where+=" and (cxsprjs = ? or cxsprjs = 'ST_ALL')";
                    list.add(parmasmap.get(key));
                }else {
                    //   where+=where==""?" key=?":" and key=?";
                    where+=" and "+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 Boolean saveMainSpl(HashMap<String, String> reqMap, String tableName) {
        RecordSet rs = new RecordSet();
        String sqlUpdate = " update "+tableName+" set "
                + "    cnrygh = '" + reqMap.get("cnrygh")
                + "' ,  ejbmfzr = '" + reqMap.get("ejbmfzr")
                + "' ,  cwzxhj = '" + reqMap.get("cwzxhj")
                + "' ,  cwzxhjsh = '" + reqMap.get("cwzxjl")
                + "' ,  yybbmfzr = '" + reqMap.get("yybbmfzr")
                + "' ,  zbcw = '" + reqMap.get("zbcw")
                + "' ,  cn = '" + reqMap.get("cn")
                + "' ,  fgsbmfzr = '" + reqMap.get("fgsbmfzr")
                + "' ,  bjxbyybfzr = '" + reqMap.get("bjxbyybfzr")
                + "' , sjbmfzr = '" + reqMap.get("sjbmfzr") + "' where requestid = '" + reqMap.get("requestid") +"'";
        JAVATools.writelog("根据requestid写入审批链Sql: "+ sqlUpdate);
        Boolean result = rs.execute(sqlUpdate);
        return result;
    }
}
