package com.engine.htsc.doc.service.impl;

import com.engine.core.impl.Service;
import com.engine.htsc.doc.service.LetterReadService;
import com.engine.integration.util.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;

import java.util.*;

public class LetterReadServiceImpl extends Service implements LetterReadService {


    @Override
    public Map<String, Object> getDocFileInfoByDocId(Map<String, Object> params) {
        Map<String,Object> datas = new HashMap<>();
        RecordSet rs = new RecordSet();
        try{
            String zwpdf_docid = (String) params.get("zwpdf_docid");
            String per_docid = (String) params.get("per_docid");
            rs.executeQuery("select a.imagefileid,a.versionid,a.imagefilename,b.doccreaterid from docimagefile a ,DOCDETAIL b\n" +
                    "where docid = ? and a.DOCID = b.id\n" +
                    "and versionid = (select max(to_number(versionid)) from docimagefile where docid = ?)",zwpdf_docid,zwpdf_docid);
            if (rs.next()) {
                datas.put("imagefileid", rs.getString("imagefileid"));
                datas.put("versionid", rs.getString("versionid"));
                datas.put("imagefilename", rs.getString("imagefilename"));
            }
            rs.executeQuery("select yxck,yxxz from uf_gwky_dt4 where wdid = ?",per_docid);
            if (rs.next()) {
                datas.put("yxck", rs.getString("yxck"));
                datas.put("yxxz", rs.getString("yxxz"));
            }


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

        return datas;
    }

    @Override
    public Map<String, Object> getAttachmentFileInfoByDocId(Map<String, Object> params) {
        Map<String,Object> datas = new HashMap<>();
        RecordSet rs = new RecordSet();
        try{
            String docids = (String) params.get("docids");
            List<String> allowReadList = new ArrayList<>();
            List<String> allowDownloadList = new ArrayList<>();
            Arrays.asList(docids.split(",")).forEach(docid -> {
                rs.executeQuery("select yxck,yxxz from uf_gwky_dt4 where wdid = ?",docid);
                if (rs.next()) {
                    if ("1".equals(Util.null2String(rs.getString("yxck")))) {
                        allowReadList.add(docid);
                    }
                    if ("1".equals(Util.null2String(rs.getString("yxxz")))) {
                        allowDownloadList.add(docid);
                    }
                }
            });
            datas.put("allowReadDocid", StringUtils.join(allowReadList,","));
            datas.put("allowDownloadDocid", StringUtils.join(allowDownloadList,","));

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