package com.engine.htxc.contract.service.impl;

import com.engine.core.impl.Service;
import com.engine.htxc.commonutil.db.DbTools;
import com.engine.htxc.commonutil.fileutil.GatewayUtil;
import com.engine.htxc.contract.service.ContractdoctopdfService;
import com.engine.integration.util.StringUtils;
import weaver.general.Util;
import weaver.wps.doccenter.convert.DocTypeConvert;
import weaver.wps.doccenter.utils.Tools;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>节点后将doc转pdf，目前为测试，后期会采用异步的方式</p>
 * @author K1810015
 * @version 1.0.0
 * @ClassName ContractdoctopdfServiceimpl.java
 * @createTime 2022年07月29日 13:50:00
 */
public class ContractdoctopdfServiceImpl extends Service implements ContractdoctopdfService {


    private DbTools dbTools=new DbTools();

    private String inerttablename="uf_htqc_dt5";

    public String detaillist="1,3";

    @Override
    public void doctopdf(Map<String,Object> map) {
        String billid= Util.null2String(map.get("billid"));
        if(StringUtils.isBlank(billid)){
            return ;
        }

        Arrays.stream(detaillist.split(",")).forEach((item)->{
            List<Map<String,Object>> listmap=  gettablemap(billid,item);
            if(listmap!=null && listmap.size()>0){
                listmap.forEach((Map<String,Object> retmap)->{
                    // String  wjmcvalue=Util.null2String(retmap.get("wjmc"));
                    String  imagefilename=Util.null2String(retmap.get("imagefilename"));
                    String  imagefileid=Util.null2String(retmap.get("imagefileid"));
                    Map<String,String>  pdfmap = new HashMap(5);
                    String oldimagefilenamepdf = (!Tools.isEmptyOrNull(imagefilename) && imagefilename.contains(".") ? imagefilename.substring(0, imagefilename.lastIndexOf(".")) : imagefilename) + ".pdf";
                    pdfmap.put("imagefileid", imagefileid + "");
                    pdfmap.put("fromMould","ecology");
                    pdfmap.put("fileName", imagefilename);
                    pdfmap.put("targetFileFormat", "PDF");
                    pdfmap.put("targetFilename", oldimagefilenamepdf);
                    DocTypeConvert convertDocToPdf = new DocTypeConvert(this.user);
                    int newimagefileid= convertDocToPdf.convertDocType(pdfmap);
                    if(newimagefileid>0){
                        String fileid=GatewayUtil.sendGateway(newimagefileid,oldimagefilenamepdf);
                        if(!StringUtils.isBlank(fileid)) {
                            Map<String, String> wheremap = new HashMap<>(4);
                            wheremap.put("mainid", billid);
                            wheremap.put("wgid", fileid);
                            wheremap.put("imagefileid", imagefileid);
                            wheremap.put("newimagefileid", Util.null2String(newimagefileid));
                            wheremap.put("name",oldimagefilenamepdf);
                            dbTools.insert(inerttablename, wheremap);
                        }
                    }
                });
            }
        });
    }

    public List gettablemap(String mainid,String detailid){
        if(StringUtils.isBlank(mainid) || StringUtils.isBlank(detailid) ){
            return null;
        }
        String sql="select wjmc,d.imagefileid ,d.imagefilename ,dt5.imagefileid old_image from uf_htqc_dt"+detailid+" t " +
            "left join docimagefile d on t.wjmc=d.docid " +
            "left join uf_htqc_dt5 dt5 on d.imagefileid=dt5.imagefileid " +
            "where t.mainid='"+mainid+"' and (dt5.imagefileid is null or dt5.imagefileid !=d.imagefileid ) ";
        return new DbTools().getSqlToList(sql);
    }


}
