package fi.cas.form;

import com.alibaba.druid.util.StringUtils;
import kd.bos.entity.operate.result.IOperateInfo;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.list.plugin.AbstractListPlugin;
import java.util.List;

/**
 * 付款处理列表
 * @author wanghaiwu_kd
 * @date 2025/06/13
 */
public class PaymentBillListPlugin extends AbstractListPlugin {
    public void afterDoOperation(AfterDoOperationEventArgs afterDoOperationEventArgs) {
        super.afterDoOperation(afterDoOperationEventArgs);

        String opKey = afterDoOperationEventArgs.getOperateKey();

        if(StringUtils.equals("nckd_synpaystatus", opKey)){
            OperationResult operationResult = afterDoOperationEventArgs.getOperationResult();
            operationResult.setShowMessage(false);
            String message = operationResult.getMessage();

            List<IOperateInfo> errorInfos = operationResult.getAllErrorOrValidateInfo();
            boolean isSuccess = operationResult.isSuccess();
            if(errorInfos.size()> 0){
                operationResult.setSuccess(false);
                StringBuffer msg = new StringBuffer();
                for (IOperateInfo errInfo: errorInfos) {
                    msg.append(errInfo.getMessage()).append("\r\n");
                }
                message = message + "\r\n" + msg.toString();
            }

            if(StringUtils.isEmpty(message)){
                this.getView().showSuccessNotification("资金支付状态同步完成！");
            } else {
                this.getView().showErrorNotification(message);
            }

            this.getView().invokeOperation("refresh");
        } else if(StringUtils.equals("paytojhx", opKey)){
            OperationResult operationResult = afterDoOperationEventArgs.getOperationResult();
            operationResult.setShowMessage(false);

            String message = operationResult.getMessage();

            if(!StringUtils.isEmpty(message) && message.contains("推送资金系统成功")){
                this.getView().showSuccessNotification(message);
                this.getView().invokeOperation("refresh");
            } else {
                List<IOperateInfo> errorInfos = operationResult.getAllErrorOrValidateInfo();
                boolean isSuccess = operationResult.isSuccess();
                if (errorInfos.size() > 0) {
                    operationResult.setSuccess(false);
                    StringBuffer msg = new StringBuffer();
                    for (IOperateInfo errInfo : errorInfos) {
                        msg.append(errInfo.getMessage()).append("\r\n");
                    }
                    message = message + "\r\n" + msg.toString();
                }

                if (!StringUtils.isEmpty(message)) {
                    this.getView().showErrorNotification(message);
                }
            }
        }
    }
}
