package com.engine.htsc.jgxt.dao;

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

import java.text.SimpleDateFormat;
import java.util.*;

public class Jgxtbase {

    /*
     * @title getttablebase
     * @remark 获取消息所需要的模板数据
     * @author xf
     * @Params：
       * @param: map
     * @updateTime 2021/9/15
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     **/
    public static Map<String,Object> getttablebase(Map<String,Object> map){
        String requestid= Util.null2String(map.get("requestid"));
        String tablename=Util.null2String(map.get("tablename"));
        String sql="select (select departmentname from hrmdepartment where id = t.szbm) as szbmname ," +
                "       (select deptdirectors from hrmdepartmentdefined hdt join hrmdepartment ht on ht.id=hdt.deptid  and ht.id=t.szbm) as sjbm," +
                "       t.ngrq," +
                "       t.lcbt," +
                "       t.lcbh," +
                "       t.hjly," +
                "       t.xzjy," +
                "       t.xzsj," +
                "       t.hjjsrq," +
                "       t.ycjyxw," +
                "       t.yqhhrq" +
                "  from  "+tablename+" t where 1=1";
        Map<String,String> params=new HashMap<>();
        params.put("requestid",requestid);
        return JAVATools.GetTableToMap(sql,params);

    }

    public static Map<String,Object> getgatewayfileid(String imagefileid){
        if("".equals(imagefileid)){
            return null;
        }
        String sql="select fileid from imagefile  where 1=1";
        Map<String,String> wheremap=new HashMap<>();
        wheremap.put("imagefileid",imagefileid);
        return JAVATools.GetTableToMap(sql,wheremap);
    }

    public static Map<String,Object> getfreecount(String requestid,String nodeid){
        if("".equals(requestid) || "".equals(nodeid)){
            return null;
        }
     String sql=       "select count(*) as sumcount from workflow_freenode where  nodename='分公司意见'   ";
     Map<String,String> wheremap=new HashMap<>();

        wheremap.put("requestid",requestid);
        wheremap.put("id",nodeid);

        return JAVATools.GetTableToMap(sql,wheremap);
    }


    public static boolean  insertdetail(Map<String,Object> map){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        RecordSet rs=new RecordSet();
        String requestid=Util.null2String(map.get("requestid"));
        String nodeid=Util.null2String(map.get("nodeid"));
        String dt9fkxx=Util.null2String(map.get("dt9fkxx"));
        String dt9fkfj=Util.null2String(map.get("dt9fkfj"));
        String tablename=JAVATools.getMaintable(requestid);
        String sql= "select id from "+tablename+" where 1=1 ";
        Map<String,String> wheremap=new HashMap<>();
        wheremap.put("requestid",requestid);
        Map<String, Object> retmap=JAVATools.GetTableToMap(sql,wheremap);
        if(retmap==null || retmap.size()<=0){
            return false;
        }
        String id=Util.null2String(retmap.get("id"));
        //删除主表数据
        if("".equals(id)||"".equals(dt9fkfj)){
            return false;
        }
        String updatesql="update  "+tablename +" set fkfj='' where id='"+id+"'";
        JAVATools.SetCmdwritelog("删除语句"+updatesql);
        rs.executeQuery(updatesql);

        Map<String,String> innertmap=new HashMap<>();
        innertmap.put("mainid",id);
        innertmap.put("sdbm",Util.null2String(map.get("sdbm")));
        innertmap.put("fkr",Util.null2String(map.get("fkr")));
        innertmap.put("fkxx",dt9fkxx);
        innertmap.put("fkfj",dt9fkfj);
        innertmap.put("scsj",sdf.format(new Date()));
        return JAVATools.Excetesql(innertmap,tablename+"_dt9");
    }

    public static List<String> getFreeNodeName(String requestid) {
        List<String> list = new ArrayList<>();
        RecordSet rs = new RecordSet();
        String sql = "select distinct (select nodename from workflow_freenode where id =t.nodeid) as nodename from workflow_requestlog t where t.requestid = '"+requestid+"' and t.nodeid < 0";
        rs.execute(sql);
        while (rs.next()){
            list.add(rs.getString("nodename"));
        }
        return list;
    }
    /*
     * @params:propName 通过prop获取的前端配置值
     * @params:selectResult sql执行的结果集
     * */
    public static JSONObject freenode(String propName, List<String> selectResult){
        boolean result = true;
        List<Object> aboutResult = new ArrayList<>();
        JSONObject jsonObject = new JSONObject();
        boolean resultout = true;
        for (String beforemini : propName.split("&&")) {
//                总部反馈，会签部门负责人，营业部反馈
            boolean result1 = false;
            for (String datamini : selectResult) {
//                    (总部反馈||营业部反馈),(会签部门负责人||指定人员),(部门领导)
                result1 = result1 || beforemini.contains(datamini);
            }
            resultout = resultout && result1;
            if (!result1){
                aboutResult.add(beforemini);
            }
        }
        result = resultout;

        jsonObject.put("result",result);
        jsonObject.put("main",aboutResult);
        return jsonObject;
    }
}
