package com.yem.wm.es.storagetrans.workflow;

import com.yem.wm.utils.DynamicObjectUtil;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.workflow.api.AgentExecution;
import kd.bos.workflow.api.AgentTask;
import kd.bos.workflow.engine.extitf.IWorkflowPlugin;

import java.util.List;

/**
 * @Description: 订舱通知单单证第一次审核通过时，将该审批节点的审批人员反写到单证审核字段
 * @Date: 2024/7/19 11:28
 * @Created: by ZZSLL
 */

public class StorageTransDocSignWorkflowPlugin implements IWorkflowPlugin {
    @Override
    public void notify(AgentExecution e) {
        IWorkflowPlugin.super.notify(e);

        String bizKey = e.getBusinessKey();
        String entityNumber = e.getEntityNumber();
        AgentTask currentTask = e.getCurrentTask();
        Long userId = currentTask.getAssigneeId();//当前同意的审批人
//        List<Long> currentApprover = e.getCurrentApprover();//审批人
        DynamicObject single = BusinessDataServiceHelper.loadSingle(bizKey, entityNumber);
        if (single == null) return;
        String selectfields = DynamicObjectUtil.getSelectfields("bos_user");
        DynamicObject[] load = BusinessDataServiceHelper.load("bos_user", selectfields, new QFilter[]{new QFilter("id", "=", userId)});
        if (load == null) return;
        DynamicObjectCollection docreview = single.getDynamicObjectCollection("yem_docreview");
        docreview.clear();
        for (DynamicObject user : load) {
            DynamicObject newObj = new DynamicObject(docreview.getDynamicObjectType());
            newObj.set("fbasedataid", user);
            docreview.add(newObj);
        }
        single.set("yem_docreview", docreview);
        SaveServiceHelper.save(new DynamicObject[]{single});
    }
}
