package com.engine.htsc.wdzx.service.imp;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.doc.customize.util.RepalceBookMarkUtil;
import com.engine.core.impl.Service;
import com.engine.htsc.wdzx.dao.DocbaseDao;
import com.engine.htsc.wdzx.model.Doclog;
import com.engine.htsc.wdzx.service.DocmergeService;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.file.ImageFileManager;
import weaver.file.Prop;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.FileGateway;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author xf
 * @version 1.0.0
 * @ClassName DocmergeServiceImp.java
 * @createTime 2021年11月08日 15:27:00
 */
public class DocmergeServiceImp extends Service implements DocmergeService {



    @Override
    public JSONArray getdocmerge(JSONArray jsonparams) {
       JSONArray jsonArray=new JSONArray();
        if(jsonparams!=null && jsonparams.size()>0){
         jsonparams.stream().forEach(jsonobejct->{
           JSONObject jsonObject1= (JSONObject)jsonobejct;
            if(jsonObject1==null ||jsonObject1.size()<=0){
                return ;
            }
             String sysid= Util.null2String(jsonObject1.get("sysid"));
             String templateno= Util.null2String(jsonObject1.get("templateno"));
             String data=Util.null2String(jsonObject1.get("data"));
             String user=Util.null2String(jsonObject1.get("user"));
             String sysname=Util.null2String(jsonObject1.get("sysname"));
             String key=Util.null2String(jsonObject1.get("key"));
             //12-27
             String filename =Util.null2String(jsonObject1.get("filename"));
             JAVATools.SetCmdwritelog("12-27filename=" + filename);
             if("".equals(sysid) || "".equals(templateno) || "".equals(data)
             || "".equals(user) || "".equals(sysname) || "".equals(key)){
                 return ;
             }
             Map<String,String> maph=new HashMap<>();
             maph.put("sysid",sysid);
             maph.put("templateno",templateno);
             maph.put("data",data);
             maph.put("user",user);
             maph.put("sysname",sysname);
             maph.put("key",key);
             //12-27
             maph.put("filename",filename);
            JSONObject jsonObject= docmerge(maph);
            if(jsonObject!=null && jsonObject.size()>0){
                jsonArray.add(jsonObject);
            }
         });
        }
        return jsonArray;
    }

    public JSONObject docmerge(Map<String,String> map){
        JSONObject jsonObject=new JSONObject();
        int newimagefileid=0;
        String imagefilename="";
        String mbmcidstr="";
        String sysid= Util.null2String(map.get("sysid"));
        String templateno= Util.null2String(map.get("templateno"));
        String data=Util.null2String(map.get("data"));
        String user=Util.null2String(map.get("user"));
        String sysname=Util.null2String(map.get("sysname"));
        String key=Util.null2String(map.get("key"));
        //12-27
        String filename=Util.null2String(map.get("filename"));

        JAVATools.SetCmdwritelog("filename="+filename+"sysid："+sysid+"modeno:"+templateno+"data:"+data);
        if("".equals(sysid) || "".equals(templateno)){
            jsonObject.put("error","参数有误！");
            return jsonObject;
        }
        Map<String,String> ufmodelmap=DocbaseDao.getdetail(sysid,templateno);
        if(ufmodelmap==null || ufmodelmap.size()<=0){
            jsonObject.put("error","该文件查询不到生效的文件");
            return jsonObject;
        }
        mbmcidstr=Util.null2String(ufmodelmap.get("id"));
        String reqdocid=Util.null2String(ufmodelmap.get("wj"));
        JAVATools.SetCmdwritelog("测试docid:"+reqdocid);
        if("".equals(reqdocid)){
            jsonObject.put("error","该文件查询不到生效的文件");
            return jsonObject;
        }



        try {
            int docid = Util.getIntValue(reqdocid, 0);

            String var6 = Prop.getPropValue("QC1450247", "oaurl");
            Map datamap = (Map) JSON.parseObject(data, Map.class);
            RepalceBookMarkUtil var8 = new RepalceBookMarkUtil();
            String sql1 = " select DISTINCT id from docimagefile where docid=? and isextfile=1 group by id ";
            String sal2 = " select a.id,a.imagefileid,a.imagefilename,a.docfiletype,a.versionId,b.filesize,a.isextfile from docimagefile a,imagefile b where a.imagefileid=b.imagefileid and a.id=? order by versionid desc ";
            RecordSet rs1 = new RecordSet();
            RecordSet rs2 = new RecordSet();
            rs1.executeQuery(sql1, new Object[]{docid});
            int var13 = 0;
            String var14;
            if (rs1.next() && StringUtils.isNotEmpty(rs1.getString("id"))) {
                var14 = rs1.getString("id");
                rs2.executeQuery(sal2, new Object[]{var14});
                if (rs2.next()) {
                    int imagefileid = Util.getIntValue(rs2.getString("imagefileid"), 0);
                    imagefilename = rs2.getString("imagefilename");
                    //12-27
                    if(StringUtils.isNotBlank(filename)){
                        String imageEnding = imagefilename.substring(imagefilename.lastIndexOf("."));
                        JAVATools.SetCmdwritelog("imageEnding="+imageEnding);
//                        if(filename.lastIndexOf(".")>=0){}
//                        String filenameEnding = filename.substring(filename.lastIndexOf("."));
//                        JAVATools.SetCmdwritelog("filenameEnding="+filenameEnding);
//                        if(Objects.equals(imageEnding,filenameEnding)){
//                            imagefilename = filename;
//                            JAVATools.SetCmdwritelog("12-27imagefilename="+imagefilename);
//                        }else{
//                            imagefilename = filename.substring(0,filename.lastIndexOf(".")-1)+filenameEnding;
//                            JAVATools.SetCmdwritelog("12-27imagefilename="+imagefilename);
//                        }
                        imagefilename = filename+imageEnding;
                        JAVATools.SetCmdwritelog("12-27imagefilename="+imagefilename);
                    }
                    ImageFileManager var17 = new ImageFileManager();
                    var17.getImageFileInfoById(imagefileid);

                    try {
                        InputStream inputStream = var17.getInputStream();
                        Throwable var19 = null;

                        try {
                            newimagefileid = var8.getWord(inputStream, datamap, imagefilename);
                        } catch (Throwable var30) {
                            var19 = var30;
                            throw var30;
                        } finally {
                            if (inputStream != null) {
                                if (var19 != null) {
                                    try {
                                        inputStream.close();
                                    } catch (Throwable var29) {
                                        var19.addSuppressed(var29);
                                    }
                                } else {
                                    inputStream.close();
                                }
                            }

                        }
                    } catch (Exception var32) {
                        var32.printStackTrace();
                    }
                }
            }
            String fileid=FileGateway.getgateway(newimagefileid,imagefilename);
            if("".equals(fileid)){
                return null;
            }
            jsonObject.put("fileid",fileid);
            jsonObject.put("key",key);
            //2022-03-17cmj
            if(StringUtils.isNotBlank(ufmodelmap.get("bbh"))){
                jsonObject.put("bbh",ufmodelmap.get("bbh"));
            }else{
                jsonObject.put("bbh","");
            }

            Map<String,String> mapret=new HashMap<>();
            mapret.put("dyr",user);
            mapret.put("dyxt",sysname);
            mapret.put("mbmc",mbmcidstr);
            mapret.put("msg",data);
            mapret.put("mbwj",reqdocid);
            mapret.put("wgid",fileid);

            innsertmodel(mapret);
        } catch (Exception var33) {
            var33.printStackTrace();
        }
        return jsonObject;
    }

    public  void innsertmodel(Map<String,String> model){
        Doclog doclog=new Doclog();
        doclog.setDyr(Util.null2String(model.get("dyr")));
        doclog.setDyxt(Util.null2String(model.get("dyxt")));
        doclog.setMbmc(Util.null2String(model.get("mbmc")));
        doclog.setMbwj(Util.null2String(model.get("mbwj")));
        doclog.setWgid(Util.null2String(model.get("wgid")));
        doclog.setMsg(Util.null2String(model.get("msg")));
        doclog.setCzlx("合并文档");
        doclog.setDyrq( DateUtil.formatDateTime(new Date()));
        doclog.innsert();
    }
}
