package com.yem.em.bd.project.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 ProjectSyncOp
 * @description: 公司项目同步操作
 * @date 2023年07月12日
 * @version: 1.0
 */
public class ProjectSyncOp extends AbstractOperationServicePlugIn {

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

    @Override
    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        super.beginOperationTransaction(e);
        DynamicObject[] bills = e.getDataEntities();
        for (DynamicObject dynamicObject : bills) {
            DynamicObject bill = dynamicObject;
            bill = BusinessDataServiceHelper.loadSingle(bill.getPkValue(), bill.getDynamicObjectType().getName());
            DynamicObject etcmanual = bill.getDynamicObject("yem_model");
            boolean flag = false;
            if (etcmanual != null) {
                DynamicObject etcmanualObj = BusinessDataServiceHelper.loadSingle(etcmanual.getPkValue(),
                        "yem_em_etcmanual");
                DynamicObject brand = bill.getDynamicObject("yem_brand");
                if (brand != null) {
                    etcmanualObj.set("yem_brand", brand.getPkValue());
                }
                DynamicObject projecttype = bill.getDynamicObject("yem_projecttype");
                if (projecttype != null) {
                    etcmanualObj.set("yem_eqctype", projecttype.getPkValue());
                }
                SaveServiceHelper.save(new DynamicObject[]{etcmanualObj});

                String picturefield = etcmanual.getString("yem_picture");
                if (EmptyUtils.isNotEmpty(picturefield)) {
                    bill.set("yem_picture", picturefield);
                    flag = true;
                }
            }
            if (!flag) {
                DynamicObject equipmentname = bill.getDynamicObject("yem_materiel");
                if (equipmentname != null) {
                    DynamicObject material = BusinessDataServiceHelper.loadSingle(equipmentname.getPkValue(),
                            "bd_material");
                    String picturefield = material.getString("picturefield");
                    if (EmptyUtils.isNotEmpty(picturefield)) {
                        bill.set("yem_picture", picturefield);
                    }
                }
            }
            SaveServiceHelper.save(new DynamicObject[]{bill});
        }
    }

}
