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

import java.util.ArrayList;
import java.util.List;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.exception.KDBizException;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;


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

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

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(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());
                boolean isvehicle = bill.getBoolean("yem_isvehicle");
                if (isvehicle) {
                    DynamicObject carfile = bill.getDynamicObject("yem_carfile");
                    if (carfile != null) {
                        DynamicObject devicefiles = BusinessDataServiceHelper.loadSingle(carfile.getPkValue(),
                                "yem_em_devicefiles");
                        DynamicObjectCollection trailerentry = devicefiles
                                .getDynamicObjectCollection("yem_trailerentry");
                        List<Long> ids = new ArrayList<>();
                        if (trailerentry.size() > 0) {
                            for (DynamicObject entry : trailerentry) {
                                DynamicObject trailerfile = entry.getDynamicObject("yem_trailerfiles");
                                if (trailerfile != null) {
                                    if (!ids.contains(Long.valueOf(trailerfile.getPkValue().toString()))) {
                                        ids.add(Long.valueOf(trailerfile.getPkValue().toString()));
                                    }
                                }
                            }
                            if (ids.size() > 0) {
                                if (!ids.contains(Long.valueOf(bill.getPkValue().toString()))) {
                                    setEntryData(devicefiles, bill);
                                }
                            }
                        } else {
                            setEntryData(devicefiles, bill);
                        }

                    } else {
                        throw new KDBizException(ResManager.loadKDString("当前车辆档案为挂车，关联车辆档案不能为空！",
                                "DevicefilesAuditOp_0", "bfcl_equipment"));
                    }
                }
            }
        }
    }

    private void setEntryData(DynamicObject devicefiles, DynamicObject bill) {
        DynamicObjectCollection trailerentry = devicefiles.getDynamicObjectCollection("yem_trailerentry");
        DynamicObject newentry = trailerentry.addNew();
        newentry.set("yem_trailerfiles", bill.getPkValue());
        newentry.set("yem_trailernoplate", bill.getString("yem_carnumber"));
        newentry.set("yem_trailertype", bill.getString("yem_vehicletype"));
        newentry.set("yem_trailerframeno", bill.getString("yem_framenumber"));
        newentry.set("yem_trailerenginecode", bill.getString("yem_enginecode"));
        newentry.set("yem_trailercolor", bill.getString("yem_color"));
        newentry.set("yem_trailerbuydate", bill.getDate("yem_dateofpurchase"));
        newentry.set("yem_trailercreatedate", bill.getDate("yem_dateofmanufacture"));
        newentry.set("yem_trailactivationdate", bill.getDate("yem_activationdate"));
        SaveServiceHelper.save(new DynamicObject[] { devicefiles });
    }

}
