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 java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 出差申请单，校验结束时间
 * @author wanghaiwu_kd
 * @date 2025/11/27
 */
public class TripReqBillResetEndDateOpPlugin extends AbstractOperationServicePlugIn {
    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);

        SimpleDateFormat timeformat = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat shortformat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat longformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        DynamicObject[] bills = e.getDataEntities();
        for(DynamicObject bill : bills){
            DynamicObjectCollection tripEntrys = bill.getDynamicObjectCollection("tripentry");
            for(DynamicObject entry : tripEntrys){
                Date endDate = entry.getDate("enddate");
                Date endTime = entry.getDate("nckd_enddatetime");

                if(endDate != null && "23:59:59".equals(timeformat.format(endDate))){
                    String newEndDate = shortformat.format(endDate) + " 23:58:59";
                    try {
                        entry.set("enddate", longformat.parse(newEndDate));
                    } catch (ParseException ex) {
                        throw new RuntimeException(ex);
                    }
                }

                if(endTime != null && "23:59:59".equals(timeformat.format(endTime))){
                    String newEndTime = shortformat.format(endTime) + " 23:58:59";
                    try {
                        entry.set("nckd_enddatetime", longformat.parse(newEndTime));
                    } catch (ParseException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }
        }
    }

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

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