package nccloud.resources.uapbd.customer.customer_grp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.bs.businessevent.bd.BDCommonEvent;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.vo.bd.supplier.SupplierVO;
import nc.vo.pub.BusinessException;

/**
 * 适配2005代码
 * zhangxinah
 * 供应商变更名称后，供应商银行户名同时变更。
 */
public class updateAfterCheck implements IBusinessListener {

    @Override
    public void doAction(IBusinessEvent event) throws BusinessException {

        try {
            if (event != null) {
                String eventType = event.getEventType();
                Map<String, Object> map = this.getBills(event);
                Object reObj = map.get("obj");
                if (reObj instanceof Object[]) {  // 检查 object 是否是 Object[] 类型
                    Object[] objectArray = (Object[]) reObj;  // 将 Object 强制转换为 Object[] 类型
                    for (Object item : objectArray) {
                        SupplierVO itemVo = (SupplierVO) item;
                        String pk_supplierStr = itemVo.getPk_supplier();
                        String nameStr = itemVo.getName();
                        List<Map<String, Object>> pk_bankaccsubList = getSaleorderPK(pk_supplierStr);
                        StringBuilder vsrcbidStr = new StringBuilder();//客商银行账户子户PK
                        StringBuilder bankaccbasStr = new StringBuilder();//客商银行账户PK
                        for (int i = 0; i < pk_bankaccsubList.size(); i++) {
                            Map itemvo = pk_bankaccsubList.get(i);
                            if (i > 0) {
                                vsrcbidStr.append(","); // 在每个元素前加逗号，避免首个元素前有逗号
                                bankaccbasStr.append(",");
                            }
                            vsrcbidStr.append("'").append(itemvo.get("pk_bankaccsub")).append("'"); // 每个值加上单引号
                            bankaccbasStr.append("'").append(itemvo.get("pk_bankaccbas")).append("'"); // 每个值加上单引号
                        }
                        if (!vsrcbidStr.toString().equals("")) {
                            String updateSql =
                                    "update bd_bankaccsub set accname='" + nameStr + "' where pk_bankaccsub in ("
                                            + vsrcbidStr + ")";//客商银行账户子户
                            updateSaleBSQty(updateSql.toString());
                        }
                        if (!bankaccbasStr.toString().equals("")) {
                            String updateSql2 =
                                    "update bd_bankaccbas set accname='" + nameStr + "' where pk_bankaccbas in ("
                                            + bankaccbasStr + ")";//客商银行账户
                            updateSaleBSQty(updateSql2);
                        }
                    }
                }
            }
        } catch (Exception e) {
            throw new BusinessException("根据供应商更新供应商银行账号户名失败" + e);
        }

    }

    private List<Map<String, Object>> getSaleorderPK(String csourcebillbidStr) throws BusinessException {
        IUAPQueryBS queryBS = NCLocator.getInstance().lookup(IUAPQueryBS.class);
        String sql = " select bd_custbank.pk_bankaccsub pk_bankaccsub,bd_custbank.pk_bankaccbas pk_bankaccbas \n"
                + "from bd_custbank bd_custbank where bd_custbank.pk_cust in ( '" + csourcebillbidStr + "' ) ";
        List<Map<String, Object>> values3 = (List<Map<String, Object>>) queryBS.executeQuery(sql,
                new MapListProcessor());
        return values3;
    }

    private int updateSaleBSQty(String sql) throws BusinessException {
        BaseDAO baseDAO = new BaseDAO();
        int succInt = baseDAO.executeUpdate(sql);
        return succInt;
    }

    private Map<String, Object> getBills(IBusinessEvent event) throws BusinessException {
        Object object = null;
        Object old = null;
        Map<String, Object> retMap = new HashMap<>();
        //类型判断和赋值
        if (event instanceof BDCommonEvent) {
            BDCommonEvent e = (BDCommonEvent) event;
            object = e.getNewObjs();
            old = e.getOldObjs();
        } else {
            throw new BusinessException("未找到单据类型");
        }
        // 将 object 和 old 转换为 SupplierVO[] 并添加到 retMap
        retMap.put("new", object);
        retMap.put("old", old);
        // 根据 new 或 old 来设置 "obj"
        retMap.put("obj", retMap.get("new") != null ? retMap.get("new") : retMap.get("old"));
        return retMap;
    }


}
