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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.fi.er.common.ErBillStatusEnum;
import java.util.*;

/**
 * 外办申请单，变更行程
 */
public class DailyVehicleBillUnAuditOpPlugin extends AbstractOperationServicePlugIn {
    private static final Log logger = LogFactory.getLog(DailyVehicleBillUnAuditOpPlugin.class);

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

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

        fieldKeys.add("applier");
        fieldKeys.add("startdate");
        fieldKeys.add("enddate");
        fieldKeys.add("nckd_ischange");
        fieldKeys.add("vehiclecity");
    }

    public void onAddValidators(AddValidatorsEventArgs e) {
        this.addDailyVehicleChangeValidator(e);
    }

    public void endOperationTransaction(EndOperationTransactionArgs e) {
        DynamicObject[] bills = e.getDataEntities();
        if (bills.length > 0) {
            this.billsEntryChange(bills);
            SaveServiceHelper.save(bills);
        }
    }

    protected void billsEntryChange(DynamicObject[] bills) {
        DynamicObject[] var2 = bills;
        int var3 = bills.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            DynamicObject bill = var2[var4];
            bill.set("nckd_ischange", Boolean.TRUE);

            this.addTripEntryHistory(bill);
        }
    }

    protected void addTripEntryHistory(DynamicObject bill) {
        DynamicObjectCollection historyColl = bill.getDynamicObjectCollection("nckd_historyentry");
        Date changeDate = new Date();
        int seq = historyColl.size() + 1;

        DynamicObject newHistory = historyColl.addNew();

//        newHistory.set("seq", seq);
        newHistory.set("nckd_changetime", changeDate);

        Map<String, String> historyMapping = new HashMap();
        historyMapping.put("nckd_historycity", "vehiclecity");
        historyMapping.put("nckd_historystartdate", "startdate");
        historyMapping.put("nckd_historyenddate", "enddate");


        Iterator var10 = historyMapping.entrySet().iterator();
        while(var10.hasNext()) {
            Map.Entry<String, String> mappingEntry = (Map.Entry)var10.next();
            String mappingKey = (String)mappingEntry.getKey();
            String value = (String)mappingEntry.getValue();

            newHistory.set(mappingKey, bill.get(value));
        }
    }

    protected void addDailyVehicleChangeValidator(AddValidatorsEventArgs e) {
        e.addValidator(new DailyVehicleInfoChangeValiator());
    }

    private static class DailyVehicleInfoChangeValiator extends AbstractValidator {
        private DailyVehicleInfoChangeValiator() {
        }

        public void validate() {
            ExtendedDataEntity[] dataEntities = this.getDataEntities();
            ExtendedDataEntity[] var2 = dataEntities;
            int var3 = dataEntities.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                ExtendedDataEntity dataEntity = var2[var4];
                DynamicObject billObj = dataEntity.getDataEntity();

                if (!billObj.getString("billstatus").equals(ErBillStatusEnum.E.name())) {
                    this.addErrorMessage(dataEntity, ResManager.loadKDString("只有审核通过的出差申请单才能变更。", "TripEntryInfoChangeOP_1", "fi-er-opplugin", new Object[0]));
                }
//                else if (billObj.getInt("reimbursetime") > 0) {
//                    this.addErrorMessage(dataEntity, ResManager.loadKDString("出差申请单已经报销，不能变更。", "TripEntryInfoChangeOP_2", "fi-er-opplugin", new Object[0]));
//                }
            }

        }
    }
}
