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

import com.yem.wm.utils.DynamicObjectUtil;
import kd.bos.dataentity.entity.DynamicObject;
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.servicehelper.operation.DeleteServiceHelper;

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

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

    private void createPdf(DynamicObject dataEntitie) {
        // 清空上传的附件面板
        List<Map<String, Object>> beforeUpload = AttachmentServiceHelper.getAttachments("yem_im_intauditbusa", dataEntitie.getLong("id"), "attachmentpanel");
        List<Map<String, Object>> salesorder = AttachmentServiceHelper.getAttachments("yem_es_salesorder", dataEntitie.getLong("yem_sourcebillid"), "attachmentpanel");
        for (Map<String, Object> map : salesorder) {
            Boolean bool = false;
            if (!bool) {
                for (Map<String, Object> beforeUploadmap : beforeUpload) {
                    if (beforeUploadmap.get("uid").equals(map.get("uid"))) {
                        bool = true;
                    }
                }
            }
            if (bool) {
                DeleteServiceHelper.delete("bos_attachment", new QFilter[]{new QFilter("id", QCP.equals, map.get("attPkId"))});
            }
        }
    }
}
