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

import com.alibaba.druid.util.StringUtils;
import fi.cas.opplugin.PayBillToolUtil;
import kd.bos.dataentity.entity.DynamicObject;
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 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.List;

/**
 * 费用申请：nckd_er_dailyapplybil_ext
 * @author wanghaiwu_kd
 * @date 2026/01/14
 */
public class DailyApplyBillSetHideOpPlugin extends AbstractOperationServicePlugIn {
    private static final Log logger = LogFactory.getLog(DailyApplyBillSetHideOpPlugin.class);

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

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

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

        String msg = "";
        StringBuilder sb = new StringBuilder();

        DynamicObject[] billEntities = e.getDataEntities();
        String operationKey = e.getOperationKey();

        //获取操作按钮操作编码
        if ("nckd_hidebill".equals(operationKey) || "nckd_unhidebill".equals(operationKey)){
            boolean isHide = false;
            if("nckd_hidebill".equals(operationKey)){
                isHide = true;
            } else if("nckd_unhidebill".equals(operationKey)){
                isHide = false;
            }

            List<DynamicObject> listObj = new ArrayList<>();

            for (DynamicObject dataEntity : billEntities) {
                DynamicObject payBillEntity = BusinessDataServiceHelper.loadSingle(dataEntity.getPkValue(), dataEntity.getDynamicObjectType().getName());

                String billStatus = payBillEntity.getString("billstatus");
                if("E".equals(billStatus)){
                    sb.append((sb.length() > 0 ? "," : "")).append(payBillEntity.getString("billno"));

                    continue;
                }

                payBillEntity.set("nckd_handhide", isHide);

                listObj.add(payBillEntity);
            }

            if(sb.length() > 0) {
                msg = "以下单据不是审核状态，请取消选择后再操作！\r\n" + sb.toString();
            } else {
                if (listObj.size() > 0) {
                    SaveServiceHelper.update(listObj.toArray(new DynamicObject[]{}));

                    msg = "nckd_hidebill".equals(operationKey) ? "设置隐藏成功" : "取消隐藏成功";
                } else {
                    msg = "无符合条件的记录！";
                }
            }
        }

        e.setCancelMessage(msg);
        e.setCancel(true);
    }

    @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);
    }
}
