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.validate.AbstractValidator;
import nckd.jimin.jyyy.fi.common.constant.ErPrintSetConstant;

public class ErPrintSetOp extends AbstractOperationServicePlugIn {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        e.getFieldKeys().add(ErPrintSetConstant.BillPtSetEntry.ENTITYID);
        e.getFieldKeys().add("seq");
        e.getFieldKeys().add(ErPrintSetConstant.BillPtSetEntry.KEY_NCKD_ISPTCOVER);
        e.getFieldKeys().add(ErPrintSetConstant.BillPtSetEntry.KEY_NCKD_COVERORDER);
        e.getFieldKeys().add(ErPrintSetConstant.BillPtSetEntry.KEY_NCKD_ISPTVOUCHER);
        e.getFieldKeys().add(ErPrintSetConstant.BillPtSetEntry.KEY_NCKD_VOUCHERORDER);
        e.getFieldKeys().add(ErPrintSetConstant.BillPtSetEntry.KEY_NCKD_ISPTRECEIPT);
        e.getFieldKeys().add(ErPrintSetConstant.BillPtSetEntry.KEY_NCKD_RECEIPTORDER);
    }

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
        e.getValidators().add(new AbstractValidator() {
            @Override
            public void validate() {
                ExtendedDataEntity[] dataEntities = getDataEntities();
                for(ExtendedDataEntity data : dataEntities){
                    DynamicObject dataEntity = data.getDataEntity();
                    DynamicObjectCollection entryCol = dataEntity.getDynamicObjectCollection(ErPrintSetConstant.BillPtSetEntry.ENTITYID);
                    for(DynamicObject entry : entryCol){
                        if(entry.getBoolean(ErPrintSetConstant.BillPtSetEntry.KEY_NCKD_ISPTCOVER)
                                && entry.getLong(ErPrintSetConstant.BillPtSetEntry.KEY_NCKD_COVERORDER) == 0L){
                            this.addErrorMessage(data, String.format("第%s行单据封面顺序不允许为空。",entry.getString("seq")));
                        }
                        if(entry.getBoolean(ErPrintSetConstant.BillPtSetEntry.KEY_NCKD_ISPTVOUCHER)
                                && entry.getLong(ErPrintSetConstant.BillPtSetEntry.KEY_NCKD_VOUCHERORDER) == 0L){
                            this.addErrorMessage(data, String.format("第%s行凭证打印顺序不允许为空。",entry.getString("seq")));
                        }
                        if(entry.getBoolean(ErPrintSetConstant.BillPtSetEntry.KEY_NCKD_ISPTRECEIPT)
                                && entry.getLong(ErPrintSetConstant.BillPtSetEntry.KEY_NCKD_RECEIPTORDER) == 0L){
                            this.addErrorMessage(data, String.format("第%s行电子回单顺序不允许为空。",entry.getString("seq")));
                        }
                    }
                }
            }
        });
    }

}
