package com.yem.wm.im.intauditbusa.op;


import com.yem.wm.es.encasement.util.AttachmentUtil;
import com.yem.wm.utils.DynamicObjectUtil;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.AttachmentServiceHelper;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.web.actions.utils.FilePathUtil;
import kd.sdk.plugin.Plugin;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;


/**
 * @author ljw
 * @date 2024/10/12 10:28
 * @description IntAuditBuSaAttachmentsContractOp
 */
public class IntAuditBuSaAttachmentsContractOp extends AbstractOperationServicePlugIn implements Plugin {
    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        DynamicObject[] dataEntities = e.getDataEntities();
        for (DynamicObject dataEntitie : dataEntities) {
            dataEntitie = BusinessDataServiceHelper.loadSingle(dataEntitie.getPkValue(), dataEntitie.getDynamicObjectType().getName());
//            writeBackSumCost(dataEntitie);
            createPdf(dataEntitie);
        }
    }

    public void createPdf(DynamicObject dataEntitie) {
        if (dataEntitie.getBoolean("yem_whethercontract")) {
            return;
        }
        DynamicObject loadSingle = BusinessDataServiceHelper.loadSingle("yem_es_salesorder", DynamicObjectUtil.getSelectfields("yem_es_salesorder", false)
                , new QFilter[]{new QFilter("id", QCP.equals, dataEntitie.getLong("yem_sourcebillid"))});
        if (loadSingle == null) {
            DynamicObject salesorder = BusinessDataServiceHelper.loadSingle("yem_es_xsalesorder", DynamicObjectUtil.getSelectfields("yem_es_xsalesorder", false)
                    , new QFilter[]{new QFilter("id", QCP.equals, dataEntitie.getLong("yem_sourcebillid"))});
            if (salesorder != null) {
                DynamicObject yemExportcontractnum = BusinessDataServiceHelper.loadSingle("yem_es_salesorder", DynamicObjectUtil.getSelectfields("yem_es_salesorder", false)
                        , new QFilter[]{new QFilter("billno", QCP.equals, salesorder.getString("yem_exportcontractnum"))});
                if (yemExportcontractnum != null) {
                    uploadAttachment(dataEntitie, yemExportcontractnum);
                }
                uploadAttachment(dataEntitie, salesorder);
            }
        } else {
            uploadAttachment(dataEntitie, loadSingle);
        }
        dataEntitie.set("yem_whethercontract", true);
    }

    /**
     * add by zhangh 20230727
     * 源单中的附件面板上传到目标单的附件面板上
     *
     * @param purInBill 源单
     * @param to        目标单
     */
    public static void uploadAttachment(DynamicObject purInBill, DynamicObject to) {
        List<Map<String, Object>> attachments = AttachmentServiceHelper.getAttachmentsByExport(purInBill.getDataEntityType().getName(),
                purInBill.getLong("id"));
        for (Map<String, Object> map : attachments) {
            map.put("lastModified", null);
            map.put("url", map.get("url"));
        }
        AttachmentServiceHelper.upload(to.getDataEntityType().getName(), to.getLong("id"), "attachmentpanel",
                attachments);
    }

    /**
     * add by zhangh 20230727
     * 获取文件服务器相对路径
     *
     * @param url
     * @return
     * @throws IOException
     */
    public static String getPathfromDownloadUrl(String url) throws IOException {
        String path = StringUtils.substringAfter(url, "path=");
        path = URLDecoder.decode(path, "UTF-8");
        return FilePathUtil.dealPath(path, "attach");
    }
}
