package com.yem.em.bd.devicefiles.op;

import java.util.List;

import com.yem.em.utils.EmptyUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.BeginOperationTransactionArgs;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

/**
 * @author 夜子
 * @ClassName DevicefilesSyncOp
 * @description: 设备档案
 * @date 2023年07月12日
 * @version: 1.0
 */
public class DevicefilesSyncOp extends AbstractOperationServicePlugIn {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> keys = e.getFieldKeys();
        keys.add("yem_model");
    }

    @Override
    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        super.beginOperationTransaction(e);
        DynamicObject[] bills = e.getDataEntities();
        for (DynamicObject dynamicObject : bills) {
            DynamicObject bill = dynamicObject;
            if (!bill.getPkValue().toString().equals("0")) {
                bill = BusinessDataServiceHelper.loadSingle(bill.getPkValue(), bill.getDynamicObjectType().getName());
                DynamicObject ecatalog = bill.getDynamicObject("yem_model");
                if (ecatalog != null) {
                    DynamicObject etcmanual = BusinessDataServiceHelper.loadSingle(ecatalog.getPkValue(),
                            "yem_em_etcmanual");
                    String picturefield = etcmanual.getString("yem_picture");
                    if (EmptyUtils.isNotEmpty(picturefield)) {
                        bill.set("yem_picture", picturefield);
                    }
                }
                SaveServiceHelper.save(new DynamicObject[]{bill});
            }
        }
    }
}
