package com.yem.wm.sys.opplugin.op;

import com.yem.wm.sys.utils.NewVersionHelper;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.servicehelper.AttachmentServiceHelper;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;

import java.util.*;

/**
 * @author zhouc
 * @date 2024/1/30 20:33
 * @className AttachmentsEffectchangeOp
 * @description 变更单生效赋值源单附件面板
 */
public class AttachmentsEffectchangeOp extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> fieldKeys = e.getFieldKeys();
        fieldKeys.add("yem_masterid");//主数据内码
        fieldKeys.add("yem_version");//版本号
        fieldKeys.add("yem_effective");//已生效
        fieldKeys.add("billstatus");//单据状态
        fieldKeys.add("yem_changing");//变更中
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        String operationKey = e.getOperationKey();
        DynamicObject[] dataEntities = e.getDataEntities();
        for (DynamicObject dataEntity : dataEntities) {
            switch (operationKey) {
                case "effectchange":
                    this.effectNewVersion(dataEntity, operationKey);
                    break;
                case "testaccessory":
                    this.effectNewVersion(dataEntity, operationKey);
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * 变更单生效
     *
     * @param dataEntity
     * @param operationKey
     */
    private void effectNewVersion(DynamicObject dataEntity, String operationKey) {
        long yem_masterid = dataEntity.getLong("yem_masterid");
        List<Map<String, Object>> yemAttachmentpanelap =
                AttachmentServiceHelper.getAttachments(dataEntity.getDynamicObjectType().getName(), dataEntity.getPkValue(), "attachmentpanel");
        if (yemAttachmentpanelap.size() > 0) {
            if (yem_masterid > 0) {
                DynamicObject yem_es_salesorder = BusinessDataServiceHelper.loadSingle(yem_masterid, "yem_es_salesorder");
                for (int i = 0; i < yemAttachmentpanelap.size(); i++) {
                    Map<String, Object> map = yemAttachmentpanelap.get(i);
                    Map uploadFileMap = new HashMap();
                    uploadFileMap.put("entityNum", yem_es_salesorder.getDynamicObjectType().getName());
                    uploadFileMap.put("billPkId", yem_es_salesorder.getPkValue().toString());
                    uploadFileMap.put("lastModified", new Date().getTime());
                    uploadFileMap.put("name", map.get("name"));
                    uploadFileMap.put("url", map.get("relativeUrl"));
                    uploadFileMap.put("status", "success");
                    long time = new Date().getTime();
                    StringBuffer uid = new StringBuffer();
                    uid.append("rc-upload-");
                    uid.append(time);
                    uid.append("-");
                    uid.append("1");
                    uploadFileMap.put("uid", uid.toString());
                    uploadFileMap.put("size", map.get("size"));
                    uploadFileMap.put("creator", UserServiceHelper.getCurrentUserId());
                    List<Map<String, Object>> attachments = new ArrayList<>();
                    attachments.add(uploadFileMap);
                    AttachmentServiceHelper.upload(yem_es_salesorder.getDynamicObjectType().getName(),
                            yem_es_salesorder.getPkValue().toString(), "attachmentpanel", attachments);
                    Object[] save = SaveServiceHelper.save(new DynamicObject[]{yem_es_salesorder});
                    if (save.length > 0) {

                    }
                }
            }
        }
    }
}
