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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.entity.plugin.args.BeginOperationTransactionArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import java.util.List;

/**
 * 出差申请单，用于更新二开加的分录开始时间、结束时间
 * @author wanghaiwu_kd
 * @date 2025/06/12
 */
public class TripReqBillSaveOpPlugin extends AbstractOperationServicePlugIn {
    private static final Log logger = LogFactory.getLog(TripReqBillSaveOpPlugin.class);
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);

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

//        fieldKeys.add("tripentry");
        fieldKeys.add("ischange");
        fieldKeys.add("applier");
        fieldKeys.add("tripentry.startdate");
        fieldKeys.add("tripentry.enddate");
        fieldKeys.add("tripentry.nckd_startdatetime");
        fieldKeys.add("tripentry.nckd_enddatetime");
    }

    @Override
    public void beforeExecuteOperationTransaction(BeforeOperationArgs e) {
        super.beforeExecuteOperationTransaction(e);
    }

    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        super.beginOperationTransaction(e);

        DynamicObject[] bills = e.getDataEntities();
        if (bills.length > 0) {
            for(DynamicObject bill : bills){
                DynamicObjectCollection currentEntities = bill.getDynamicObjectCollection("tripentry");
                if (currentEntities.size() != 0) {
                    for(DynamicObject entry : currentEntities){
                        entry.set("nckd_startdatetime", entry.getDate("startdate"));
                        entry.set("nckd_enddatetime", entry.getDate("enddate"));
                    }
                }
            }
//            SaveServiceHelper.save(bills);
        }
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
    }

    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        super.afterExecuteOperationTransaction(e);
    }
}
