package com.yem.em.ass.assatlasdet.op;


import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.fileservice.FileServiceFactory;
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.servicehelper.operation.SaveServiceHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhouc
 * @date 2023/11/2 18:14
 * @className AssAtlasdetUnAuditOpEdit
 * @description 总成图册明细【根据附件面板获取URL】服务插件
 */
public class AssAtlasdetOpEdit extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> keys = e.getFieldKeys();
        keys.add("createorg");
        keys.add("number");
        keys.add("name");
        keys.add("yem_issup");
        keys.add("yem_imageurl");
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        if (e.getDataEntities() != null && e.getDataEntities().length != 0) {
            DynamicObject[] bills = e.getDataEntities();
            for (DynamicObject bill : bills) {
                List<Map<String, Object>> attachments = AttachmentServiceHelper.getAttachments(bill.getDynamicObjectType().getName(), bill.getString("id"), "yem_attachmentpanelap");
                List list = new ArrayList<>();
                for (int i = 0; i < attachments.size(); i++) {
                    Map<String, Object> stringObjectMap = attachments.get(i);
                    if (stringObjectMap != null) {
                        list.add(stringObjectMap.get("attPkId"));
                    }
                }
                String yem_imageurl = "";
                if (list.size() > 0) {
                    QFilter qFilter1 = new QFilter("id", QCP.in, list);
                    DynamicObject[] atts = BusinessDataServiceHelper.load("bos_attachment",
                            "id,ffileid,fattachmentname,fattachmentsize",
                            new QFilter[]{qFilter1});

                    for (DynamicObject att : atts) {
                        String ffileid = FileServiceFactory.getAttachmentFileService().getFileServiceExt().getRealPath(att.getString("ffileid"));
                        if (yem_imageurl == "") {
                            yem_imageurl = ffileid + ";";
                        } else {
                            yem_imageurl = yem_imageurl + ffileid + ";";
                        }
                    }
                }
                bill.set("yem_imageurl", yem_imageurl);
                SaveServiceHelper.update(new DynamicObject[]{bill});
            }
        }
    }
}
