package nckd.jimin.jyyy.fi.plugin.operate;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.metadata.IDataEntityType;
import kd.bos.entity.BillEntityType;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.entity.plugin.args.InitOperationArgs;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.fi.er.opplugin.trip.dailybiz.SyncReqBillToTripOp;
import java.util.List;
import java.util.Map;

/**
 * 同步商旅，继承标准的插件。
 */
public class SyncReqBillToTripExtOpPlugin extends SyncReqBillToTripOp {
    private static final Log logger = LogFactory.getLog(SyncReqBillToTripExtOpPlugin.class);
    private String key_entityNumber;
    private String key_operateKey;

    @Override
    public void initialize(InitOperationArgs e) {
        super.initialize(e);

        IDataEntityType entityType = this.billEntityType;
        BillEntityType mainType = null;
        if (entityType instanceof BillEntityType) {
            mainType = (BillEntityType)entityType;
        }

        if (mainType != null) {
            this.key_entityNumber = mainType.getName();
        }

        Map<String, Object> opParameter = this.operateMeta;
        if (opParameter != null && opParameter.get("key") != null) {
            this.key_operateKey = (String)opParameter.get("key");
        }

        logger.info("商旅集成,申请单同步,业务单据是:" + this.key_entityNumber + ", 业务操作是:" + this.key_operateKey);
    }

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);

        List<String> fieldKeys = e.getFieldKeys();

        switch (this.key_entityNumber) {
            case "er_dailyvehiclebill":
                fieldKeys.add("nckd_ischange");
                break;
        }
    }

    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {

        if("er_dailyvehiclebill".equals(this.key_entityNumber)){
            DynamicObject[] datas = e.getDataEntities();

            for(int i = 0; i < datas.length; i++) {
                DynamicObject dynamicObject = datas[i];

                //如果是变更过的就不同步到商旅
                if(!dynamicObject.getBoolean("nckd_ischange")) {
                    super.afterExecuteOperationTransaction(e);
                }
            }
        } else {
            super.afterExecuteOperationTransaction(e);
        }
    }
}
