package com.yem.wm.utils;

import kd.bos.cache.CacheFactory;
import kd.bos.cache.TempFileCache;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.IFormView;
import kd.bos.form.control.AttachmentPanel;
import kd.bos.servicehelper.AttachmentServiceHelper;
import kd.bos.servicehelper.PrintServiceHelper;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author xwudd
 * @Description
 * @date 10:04
 **/
public class PrintFileUtil {

    public void print(IFormView view,IDataModel model){
        AttachmentPanel attachmentPanel = view.getControl("attachmentpanel");
        List<Map<String, Object>> attachmentData = attachmentPanel.getAttachmentData();
        //附件数量
        int size = attachmentData.size();
        if (size >= 2) {
            view.showErrorNotification("超过附件数量最大值2,请删除后再次生成!");
            return;
        }
        String pageId = view.getPageId();
        Object pkValue = model.getValue("id");
        //参数说明 : 当前页的id 单据标识 打印模板标识 当前单据标识
        byte[] createPdf = PrintServiceHelper.createSinglePdf(pageId, "KEY_FORMID", "KEY_PRT_MODELID", pkValue);
        //限制生成的附件大小
        int attachSize = 2;
        //大于10M,提示信息 1048576 = 1M
        if(createPdf.length >= 1048576 * attachSize){
            view.showErrorNotification("附件大小不能超过"+attachSize+"M !");
            return;
        }
        //拿到字节输入流
        InputStream pdfIs = new ByteArrayInputStream(createPdf);
        //附件缓存
        TempFileCache tfc = CacheFactory.getCommonCacheFactory().getTempFileCache();
        int timeout = 7200;
        //上传到附件
        StringBuffer uid = new StringBuffer("rc-upload-");
        uid.append((new Date()).getTime());
        uid.append("-");
        int index = (int) (1.0D + Math.random() * 10.0D);
        uid.append(index);
        Map<String, Object> attachmentInfo = new HashMap();
        List<Map<String, Object>> attachments = new ArrayList();
        String pkid = pkValue.toString();
        String formid = "KEY_FORMID";
        String billno = (String) model.getValue("billno");
        String appId = model.getDataEntityType().getAppId();
        String neddPaths = tfc.saveAsUrl(billno + ".pdf", pdfIs, timeout);
        Map<String, Object> tempAttachment = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
        String nowTime = sdf.format(new Date());
        tempAttachment.put("uid", uid);
        tempAttachment.put("url", neddPaths);
        tempAttachment.put("size", createPdf.length);
        tempAttachment.put("description", "生成pdf文件");
        tempAttachment.put("name", billno+"-"+nowTime + ".pdf");
        attachments.add(tempAttachment);
        //构造附件数据
        attachmentInfo.put("attachmentpanel", attachments);
        //AppId 应用的编码
        DynamicObjectCollection collection = AttachmentServiceHelper.saveTempAttachments(formid, pkid, appId, attachmentInfo);
        if (collection != null && collection.size() != 0) {
            view.showSuccessNotification("附件已生成!");
            view.updateView("attachmentpanel");
        }
        try {
            pdfIs.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}
