package com.engine.htsc.zgk.flfgk.service.impl;

import com.engine.core.impl.Service;
import com.engine.htsc.zgk.flfgk.service.FlfgkService;
import com.engine.integration.util.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;

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

public class FlfgkServiceImpl extends Service implements FlfgkService {

    /**
     * 获取法规类别
     */
    @Override
    public List<Map<String, String>> getFglb() {
        List<Map<String, String>> listMap = new ArrayList<>();
        String id = "";
        String fglb = "";
        String fglbsj = "";
        RecordSet rs = new RecordSet();
        BaseBean bs = new BaseBean();
        String sql = "select id,fglb,fglbsj from uf_fglb";
        bs.writeLog(sql);
        rs.execute(sql);
        while (rs.next()){
            Map<String,String> map = new HashMap<>();
            id = Util.null2String(rs.getString("id"));
            fglb = Util.null2String(rs.getString("fglb"));
            fglbsj = Util.null2String(rs.getString("fglbsj"));
            map.put("id",id);
            map.put("fglb",fglb);
            map.put("fglbsj",fglbsj);
            listMap.add(map);
        }
        bs.writeLog(listMap);
        return listMap;
    }

    /**
     * 根据法规类别查询该类别下的所有法规的相关数据
     */
    @Override
    public List<Map<String, String>> getFgInfo(String fglbId) {
        PropBean pb = new PropBean();
        RecordSet rs = new RecordSet();
        RecordSet rs2 = new RecordSet();
        BaseBean bs = new BaseBean();
        List<String> fglbList = new ArrayList<>();
        List<String> fgbmList = new ArrayList<>();
        List<Map<String, String>> listMap = new ArrayList<>();

        String fglbTreeId = pb.getPropName("fglb_tree_id");
        String billid = pb.getPropName("uf_flfgk_billid");
        bs.writeLog("fglbTreeId",fglbTreeId);
        String fglb = "";
        String allFglb = "";
        String id = "";
        String fgbt = "";
        String xljbId = "";
        String sxxId = "";
        String xljb = "";
        String sxx = "";
        String fbbm = "";
        String fbrq = "";
        String fileid = "";
        String fbbmName = "";
        String sql = "";

        // 根据传的类别id，获取该类别及其所有子级类别id
        sql = "SELECT id FROM uf_fglb START WITH ID = "+fglbId+" CONNECT BY fglbsj = PRIOR ID";
        bs.writeLog(sql);
        rs.execute(sql);
        while (rs.next()){
            id = Util.null2String(rs.getString("id"));
            if(StringUtils.isBlank(id)){
                return null;
            }
            fglb = fglbTreeId + "_" + id;
            fglbList.add(fglb);
        }
        allFglb = fglbList.stream().map(s -> "\'" + s + "\'").collect(Collectors.joining(", "));

        sql = "select t.fgbt,t.xljb,t.sxx,t.fbbm,t.fbrq,s.FILEID\n" +
                "from uf_flfgk t\n" +
                "left Join DOCIMAGEFILE c On c.DOCID = t.scwj\n" +
                "left join IMAGEFILE s On s.IMAGEFILEID = c.IMAGEFILEID where t.fglb in ("+allFglb+")";
        bs.writeLog(sql);
        rs.execute(sql);
        while (rs.next()){
            Map<String,String> map = new HashMap<>();
            fgbt = Util.null2String(rs.getString("fgbt"));
            fbrq = Util.null2String(rs.getString("fbrq"));
            fileid = Util.null2String(rs.getString("fileid"));
            fbbm = Util.null2String(rs.getString("fbbm"));
            xljbId = Util.null2String(rs.getString("xljb"));
            sxxId = Util.null2String(rs.getString("sxx"));
            // 获取选择框 时效性 id对应的名称
            sql="select SELECTNAME from workflow_selectitem a\n" +
                    " inner join workflow_billfield b\n" +
                    " on a.FIELDID = b.ID and a.SELECTVALUE = "+sxxId+"\n" +
                    " where b.BILLID = '"+billid+"' and b.FIELDNAME = 'sxx'";
            rs2.execute(sql);
            if (rs2.next()){
                sxx = Util.null2String(rs2.getString("SELECTNAME"));
            }
            // 获取选择框 效力级别 id对应的名称
            sql="select SELECTNAME from workflow_selectitem a\n" +
                    " inner join workflow_billfield b\n" +
                    " on a.FIELDID = b.ID and a.SELECTVALUE = "+xljbId+"\n" +
                    " where b.BILLID = '"+billid+"' and b.FIELDNAME = 'xljb'";
            rs2.execute(sql);
            if (rs2.next()){
                xljb = Util.null2String(rs2.getString("SELECTNAME"));
            }
            // 获取所有发布部门名称
            sql = "select fbbm from uf_fbbm where id in("+fbbm+")";
            rs2.execute(sql);
            while (rs2.next()){
                fbbmName = Util.null2String(rs2.getString("fbbm"));
                fgbmList.add(fbbmName);
            }
            String bm = fgbmList.stream().collect(Collectors.joining(","));
            bs.writeLog("bm"+bm);

            map.put("fgbt",fgbt);
            map.put("xljb",xljb);
            map.put("sxx",sxx);
            map.put("fbbm",bm);
            map.put("fbrq",fbrq);
            map.put("fileid",fileid);
            listMap.add(map);
        }
        bs.writeLog(listMap);
        return listMap;
    }

    /**
     * 校验文件是否重复
     */
    @Override
    public Map<String , String > checkFile(String docId,String tableName) {
        RecordSet rs = new RecordSet();
        String sql = "";
        String imageFileName = "";
        String fileName = "";
        String fgbt = "";
        JAVATools.writelog("-----checkFile-----");
        JAVATools.writelog("docId--->"+docId);
        JAVATools.writelog("tableName--->"+tableName);
        Map<String , String > map = new HashMap<>();
        try {
            // 根据docid拿到文件名
            sql = "select IMAGEFILENAME from DOCIMAGEFILE where DOCID = "+docId+"";
            JAVATools.writelog(sql);
            rs.execute(sql);
            if (rs.next()){
                imageFileName = Util.null2String(rs.getString("IMAGEFILENAME"));
            }
            fileName = imageFileName.substring(0,imageFileName.lastIndexOf("."));

            // 根据上传的文件名去查有没有同名的文件 建模表  为空则不重复
            if ("uf_xcsck".equals(tableName)){
                sql = "select WJMC from "+tableName+" where WJMC = '"+fileName+"'";
                JAVATools.writelog(sql);
                rs.execute(sql);
                if (rs.next()){
                    fgbt = Util.null2String(rs.getString("WJMC"));
                    JAVATools.writelog("fgbt(WJMC)-->"+fgbt);
                }
            }else if ("uf_flfgk".equals(tableName)){
                sql = "select fgbt from "+tableName+" where fgbt = '"+fileName+"'";
                JAVATools.writelog(sql);
                rs.execute(sql);
                if (rs.next()){
                    fgbt = Util.null2String(rs.getString("fgbt"));
                    JAVATools.writelog("fgbt-->"+fgbt);
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        if (StringUtils.isEmpty(fgbt)){
            JAVATools.writelog("不重复 true");
            map.put("data","true");
        }else {
            JAVATools.writelog("重复 false");
            map.put("data","false");
        }
        return map;
    }

}
