package com.engine.htsc.payment.dao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.payment.pojo.MallPayBizModel;
import com.engine.htsc.payment.pojo.UfCgspl;
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 UfCgsplDao {

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

    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 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 ("sslc".equals(key)||"fqbmlx".equals(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 String getBmysglysh(String bms) {
        String bmysglysh = "";
        if (!"".equals(bms)){
            RecordSet rs=new RecordSet();
            String sql = "select * from uf_ysgly where bm in ( "+bms+" )" ;
            JAVATools.writelog("获取uf_ysgly ："+sql);
            rs.execute(sql);
            while (rs.next()){
                bmysglysh+=bmysglysh==""? Util.null2String(rs.getString("ysgly")):","+ Util.null2String(rs.getString("ysgly"));
            }
        }
        return bmysglysh;
    }

    public Boolean updateMain(HashMap<String, String> reqMap, String tableName) {
        RecordSet rs = new RecordSet();
        String sqlUpdate = " update "+tableName+" set bmysglysh = '" + reqMap.get("bmysglysh")
                + "' , yybfzrsh = '" + reqMap.get("yybfzrsh")
                + "' , bmfzrsh = '" + reqMap.get("bmfzrsh")
                + "' , fgsfzrsh = '" + reqMap.get("fgsfzrsh")
                + "' , fgldsh = '" + reqMap.get("fgldsh")
                + "' , ysglyzs = '" + reqMap.get("ysglyzs")
                + "' , fyhj = '" + reqMap.get("fyhj")
                + "' , cwfzrsp = '" + reqMap.get("cwfzrsp")
                + "' , cn = '" + reqMap.get("cn")
                + "' , ysglycs = '" + reqMap.get("ysglycs")
                + "' , cnrygh = '" + reqMap.get("cnrygh") + "' where requestid = '" + reqMap.get("requestid") +"'";
        JAVATools.writelog("根据requestid写入审批链Sql: "+ sqlUpdate);
        Boolean result = rs.execute(sqlUpdate);
        return result;
    }

    public Boolean updateShoppingMalPool(String requestId) {
        RecordSet rs = new RecordSet();
        String sqlUpdate = "update uf_ShoppingMalPool set zt = '1'" + " where requtid = '" + requestId +"'";
        JAVATools.writelog("更新uf_ShoppingMalPool: "+ sqlUpdate);
        Boolean result = rs.execute(sqlUpdate);
        return result;
    }

    public Boolean updateShoppingMalPoolEnd(String requestId) {
        RecordSet rs = new RecordSet();
        String sqlUpdate = "update uf_ShoppingMalPool set zt = '2'" + " where requtid = '" + requestId +"'";
        JAVATools.writelog("更新uf_ShoppingMalPool: "+ sqlUpdate);
        Boolean result = rs.execute(sqlUpdate);
        return result;
    }

    public Boolean updateMainFyqr(HashMap<String, String> reqMap, String tableName) {
        RecordSet rs = new RecordSet();
        String sqlUpdate = " update "+tableName+" set scglysh = '" + reqMap.get("ysglycs")
                + "' , fgscwjlsh = '" + reqMap.get("fgscwjlsh")
                + "' , wzcgdjrysh = '" + reqMap.get("wzcgdjrysh")
                + "' , ysglysh = '" + reqMap.get("bmysglysh")
                + "' , yybfzrsh = '" + reqMap.get("yybfzrsh")
                + "' , bmfzrsh = '" + reqMap.get("bmfzrsh")
                + "' , fgsfzrsh = '" + reqMap.get("fgsfzrsh")
                + "' , fgldsh = '" + reqMap.get("fgldsh")
                + "' , scglybj = '" + reqMap.get("ysglyzs")
                + "' , fgscwjlbj = '" + reqMap.get("fgscwjlbj") + "' where requestid = '" + reqMap.get("requestid") +"'";
        JAVATools.writelog("根据requestid写入审批链Sql: "+ sqlUpdate);
        Boolean result = rs.execute(sqlUpdate);
        return result;
    }

    public Boolean updateMainJsfk(HashMap<String, String> reqMap, String tableName) {
        RecordSet rs = new RecordSet();
        String sqlUpdate = " update "+tableName+" set "
                + "    scglysh = '" + reqMap.get("ysglycs")
                + "' ,  fgscwjlsh = '" + reqMap.get("fgscwjlsh")
                + "' ,  wzcgdjrysh = '" + reqMap.get("wzcgdjrysh")
                + "' ,  wzcggkbmfzrsh = '" + reqMap.get("wzcggkbmfzr")
                + "' ,  cwzxhjsh = '" + reqMap.get("cwzxhj")
                + "' ,  fyhjsh = '" + reqMap.get("fyhj")
                + "' ,  cwzxjlsh = '" + reqMap.get("cwzxjl")
                + "' ,  cwfzrsp = '" + reqMap.get("cwfzrsp")
                + "' ,  cwzjsp = '" + reqMap.get("cwzj")
                + "' ,  fgsfzrsp = '" + reqMap.get("fgsfzrsh")
                + "' ,  fgldsp = '" + reqMap.get("fgldsh")
                + "' ,  ldsp = '" + reqMap.get("zc")
                + "' ,  cn = '" + reqMap.get("cn")
                + "' , cnrygh = '" + reqMap.get("cnrygh") + "' where requestid = '" + reqMap.get("requestid") +"'";
        JAVATools.writelog("根据requestid写入审批链Sql: "+ sqlUpdate);
        Boolean result = rs.execute(sqlUpdate);
        return result;
    }

    public Boolean updateRequestbase(String requestId, String lcbh) {
        RecordSet rs = new RecordSet();
        Boolean result = rs.executeUpdate("update workflow_requestbase set requestmark = ? where requestid = ?",
                lcbh, requestId);
        JAVATools.writelog("写入workflow_requestbaseSql: "+ "update workflow_requestbase set requestmark = "+lcbh+" where requestid = "+requestId);
        return result;
    }

    public Map<String, String> getDt5(String mainid, String oldTbaleName) {
        RecordSet rs = new RecordSet();
        String sql = "select a.mtrtypeid,sum(a.amount) as amount from "+oldTbaleName+"_dt5 a where a.mainid = '"+mainid+"' group by mtrtypeid" ;
        JAVATools.writelog("明细表5 ："+sql);
        rs.execute(sql);
        HashMap<String, String> map = new HashMap<>();
        while (rs.next()){
            map.put(Util.null2String(rs.getString("mtrtypeid")), Util.null2String(rs.getString("amount")));
        }
        return map;
    }


    public Map<String, String> getCwMap(List<String> departList, String fqbmlx) {
        Map<String, String> map = new HashMap<>();
        RecordSet rs = new RecordSet();
        if (null!=departList&&departList.size()>0){
            if ("zb".equals(fqbmlx)){
                String glbm = "";
                for (String bm : departList){
                    glbm += glbm==""?bm:","+bm;
                }
                String sql = "select hjry,cnry from uf_zbkjpz where glbm in ( "+glbm+" )" ;
                JAVATools.writelog("获取总部会计信息 ："+sql);
                rs.execute(sql);
                String kj = "";
                String cn = "";
                while (rs.next()){
                    if (!"".equals(Util.null2String(rs.getString("hjry")))){
                        kj += kj==""? Util.null2String(rs.getString("hjry")):","+ Util.null2String(rs.getString("hjry"));
                    }
                    if (!"".equals(Util.null2String(rs.getString("hjry")))){
                        cn += cn==""? Util.null2String(rs.getString("hjry")):","+ Util.null2String(rs.getString("cnry"));
                    }
                    map.put("kj",kj);//会计
                    map.put("cn",cn);//出纳
                }
            }else if ("fzjg".equals(fqbmlx)){
                String glbm = "";
                for (String bm : departList){
                    glbm += glbm==""?bm:","+bm;
                }
                String sql = "select kjjl,cn,kj from uf_fzjgfkgwpp where bm in ( "+glbm+" )" ;
                JAVATools.writelog("获取总部会计信息 ："+sql);
                rs.execute(sql);
                String kj = "";
                String cn = "";
                String kjjl = "";
                while (rs.next()){
                    if (!"".equals(Util.null2String(rs.getString("kj")))){
                        kj += kj==""? Util.null2String(rs.getString("kj")):","+ Util.null2String(rs.getString("kj"));
                    }
                    if (!"".equals(Util.null2String(rs.getString("cn")))){
                        cn += cn==""? Util.null2String(rs.getString("cn")):","+ Util.null2String(rs.getString("cn"));
                    }
                    if (!"".equals(Util.null2String(rs.getString("kjjl")))){
                        kjjl += kjjl==""? Util.null2String(rs.getString("kjjl")):","+ Util.null2String(rs.getString("kjjl"));
                    }
                    map.put("kj",kj);//会计
                    map.put("cn",cn);//出纳
                    map.put("kjjl",kjjl);//会计经理
                }
            }
        }
        return map;
    }

    public String getSs(String yxzh) {
        RecordSet rs = new RecordSet();
        String ss = "";
        String sql = "select yxzh,skdw,ss from uf_cgyhzhpz where yxzh =  '" +yxzh+"'";
        JAVATools.writelog("获取银行账号对应省市sql ："+sql);
        rs.execute(sql);
        if (rs.next()){
            ss = Util.null2String(rs.getString("ss"));
        }
        return ss;
    }

    public Map<String, String> getHtxx(String htbh){
        RecordSet rs = new RecordSet();
        Map<String, String> htxx = new HashMap<>();
        String sql = "select yxzh,khx,skdw,ss,jbr from uf_cgyhzhpz where htbh =  '" +htbh+"'";
        rs.execute(sql);
        if (rs.next()){
            htxx.put("yxzh",Util.null2String(rs.getString("yxzh")));
            htxx.put("khx",Util.null2String(rs.getString("khx")));
            htxx.put("skdw",Util.null2String(rs.getString("skdw")));
            htxx.put("ss",Util.null2String(rs.getString("ss")));
            htxx.put("jbr",Util.null2String(rs.getString("jbr")));
        }
        return htxx;
    }

    public String getHl(String bz) {

        RecordSet rs = new RecordSet();
        String hl = "";
        String sql = "select hl from uf_bz_hl where bz =  '" +bz+"'";
        JAVATools.writelog("获取汇率sql ："+sql);
        rs.execute(sql);
        if (rs.next()){
            hl = Util.null2String(rs.getString("hl"));
        }
        return hl;
    }

    public String getkjkm(String kmName) {
        RecordSet rs = new RecordSet();
        String hjkmbm = "";
        String sql = "select * from UF_KJKMPZ where hjkmmc = '" +kmName+"'";
        JAVATools.writelog("获取进项税sql ："+sql);
        rs.execute(sql);
        if (rs.next()){
            hjkmbm = Util.null2String(rs.getString("HJKMBM"));
        }
        return hjkmbm;
    }

    public String getfkqr(String rid) {
        RecordSet rs = new RecordSet();
        String table = JAVATools.getMaintable(rid);
        String glfyqrlc = "";
        String sql = "select glfyqrlc from "+table+" where requestid =  '" +rid+"'";
        JAVATools.writelog("获取关联流程requestid ："+sql);
        rs.execute(sql);
        if (rs.next()){
            glfyqrlc = Util.null2String(rs.getString("glfyqrlc"));
        }
        return glfyqrlc;
    }

    public List<MallPayBizModel> getJsfk(String fyqr, String fklc) {
        List<MallPayBizModel> mallPayBizNo = new ArrayList<>();
        if (!"".equals(Util.null2String(fklc))){
            String[] fklcList = fklc.split(",");
            String maintable = JAVATools.getMaintable(fklcList[0]);
            RecordSet rs = new RecordSet();
            String sql = "select a.lcbh,a.splb,a.glfyqrlc from "+maintable+" a where a.glfyqrlc = '"+fyqr+"'" + " and requestid in ("+fklc+")";
            JAVATools.writelog("获取采购商城费用确认生产的所有付款流程 ："+sql);
            rs.execute(sql);
            while (rs.next()){
                MallPayBizModel mallPayBizModel = new MallPayBizModel();
                mallPayBizModel.setMallPayBizNo(Util.null2String(rs.getString("lcbh")));
                if ("1".equals(Util.null2String(rs.getString("splb")))){
                    //办公品,耗材 mtrTypeId=1
                    mallPayBizModel.setPayType("C1018681,C1018682");
                }else if ("2".equals(Util.null2String(rs.getString("splb")))){
                    //宣传品
                    mallPayBizModel.setPayType("C1018684");
                }else if ("3".equals(Util.null2String(rs.getString("splb")))){
                    //电子设备, IT标准硬件
                    mallPayBizModel.setPayType("C1018683,ITBZYJ");
                }
                mallPayBizNo.add(mallPayBizModel);
            }
        }
        return mallPayBizNo;
    }

    public String getLcbh(String fyqr) {
        RecordSet rs = new RecordSet();
        String table = JAVATools.getMaintable(fyqr);
        String lcbh = "";
        String sql = "select lcbh from "+table+" where requestid =  '" +fyqr+"'";
        JAVATools.writelog("获取关联流程流程编号 ："+sql);
        rs.execute(sql);
        if (rs.next()){
            lcbh = Util.null2String(rs.getString("lcbh"));
        }
        return lcbh;
    }
}
