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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.metadata.dynamicobject.DynamicObjectType;
import kd.bos.entity.EntityMetadataCache;
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.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.entity.validate.AbstractValidator;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 付款单取消流水指定
 * @author wanghaiwu_kd
 * @date 2026/01/07
 */
public class PaymentUnAssignDetailOpPlugin extends AbstractOperationServicePlugIn {
    private static final Log logger = LogFactory.getLog(PaymentUnAssignDetailOpPlugin.class);

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

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

        fieldKeys.add("billno");
        fieldKeys.add("billstatus");
        fieldKeys.add("nckd_isassigndetail");
    }

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
        e.addValidator(new AbstractValidator() {
            @Override
            public void validate() {
                ExtendedDataEntity[] dataEntitys = this.getDataEntities();
                for (ExtendedDataEntity dataEntity : dataEntitys) {
                    if(dataEntity == null){
                        continue;
                    }

                    DynamicObject data = dataEntity.getDataEntity();
                    String msg = "";

                    if(!data.getBoolean("nckd_isassigndetail")) {
                        msg = "未指定流水";
                    }

                    if(!"".equals(msg)){
                        msg = "付款处理单(" + data.getString("billno") + ")" + msg + "，不允许取消指定";

                        this.addErrorMessage(dataEntity, msg);
                    }
                }
            }
        });
    }

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

        DynamicObject[] dataEntitys = e.getDataEntities();
        List<Long> ids = (List) Arrays.stream(dataEntitys).map((o) -> {
            return o.getLong("id");
        }).collect(Collectors.toList());

        DynamicObjectType type = EntityMetadataCache.getDataEntityType("cas_paybill");
        DynamicObject[] billArray = BusinessDataServiceHelper.load(ids.toArray(), type);

        //2、更新指定标记
        List<DynamicObject> listBill = new ArrayList<>();
        for (DynamicObject bill : billArray) {
            bill.set("nckd_isassigndetail", false);
            bill.set("detailseqid", null);

            listBill.add(bill);
        }

        if(listBill.size() > 0){
            SaveServiceHelper.update(listBill.toArray(new DynamicObject[]{}));
        }
    }

    @Override
    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        super.beginOperationTransaction(e);
    }

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

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

//        DynamicObject[] dataEntitys = e.getDataEntities();
//        List<Long> ids = (List) Arrays.stream(dataEntitys).map((o) -> {
//            return o.getLong("id");
//        }).collect(Collectors.toList());
//
//        DynamicObjectType type = EntityMetadataCache.getDataEntityType("cas_paybill");
//        DynamicObject[] billArray = BusinessDataServiceHelper.load(ids.toArray(), type);
//
//        //2、更新指定标记
//        List<DynamicObject> listBill = new ArrayList<>();
//        for (DynamicObject bill : billArray) {
//            bill.set("nckd_isassigndetail", false);
//            bill.set("detailseqid", null);
//
//            listBill.add(bill);
//        }
//
//        if(listBill.size() > 0){
//            SaveServiceHelper.update(listBill.toArray(new DynamicObject[]{}));
//        }
    }
}
