package nc.bs.arap.util;

import nc.bs.dao.BaseDAO;
import nc.bs.logging.Logger;
import nc.jdbc.framework.SQLParameter;
import nc.vo.arap.gathering.AggGatheringBillVO;
import nc.vo.arap.gathering.GatheringBillItemVO;
import nccloud.commons.lang.StringUtils;

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * 更新到账通知的收款生成状态
 *
 * @author mzr
 * @date 2025/6/13
 */
public class GatheringBillAfter {

    /**
     * 更新收款单对应的到账通知 cmp_informer 的 generateflag 生成状态
     *
     * @param aggVos       收款单vo数组
     * @param generateFlag 目标状态（例如 "hasgenerate" 或 "hasnogenerate"）
     */
    public static void updateInformerGenerateFlag(AggGatheringBillVO[] aggVos, String generateFlag) {
        if (aggVos == null || aggVos.length == 0) {
            return; // 参数为空时直接返回
        }
        try {
            Set<String> codeSet = new LinkedHashSet<>();
            for (AggGatheringBillVO aggVo : aggVos) {
                GatheringBillItemVO[] bvos = aggVo.getBodyVOs();
                for (GatheringBillItemVO bvo : bvos) {
                    String bankrelatedCode = bvo.getBankrelated_code();
                    if (StringUtils.isNotEmpty(bankrelatedCode) && !"~".equals(bankrelatedCode)) {
                        codeSet.add(bankrelatedCode);
                    }
                }
            }
            String[] codes = codeSet.toArray(new String[0]);
            Logger.error("updateInformerGenerateFlag-codes = " + Arrays.toString(codes));
            if (codes.length == 0) {
                return;
            }
            BaseDAO baseDAO = new BaseDAO();
            String inStr = SqlUtils.getInStr("bankrelated_code", codes, true);
            String sql = "UPDATE cmp_informer SET generateflag = ? WHERE " + inStr;
            SQLParameter params = new SQLParameter();
            params.addParam(generateFlag);
            baseDAO.executeUpdate(sql, params);
        } catch (Exception e) {
            Logger.error("updateInformerGenerateFlag-exp:", e);
        }
    }
}
