package nc.bs.uapbd.util;

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.logging.Logger;
import nc.bs.trade.business.HYSuperDMO;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.jdbc.framework.processor.MapProcessor;
import nc.vo.bd.defdoc.DefdocVO;
import nc.vo.cmp.util.StringUtils;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.pub.SqlBuilder;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

/**
 * 工具类
 *
 * @author mzr
 * @date 2025/7/3
 */
public class MyHelper {
    private static final BaseDAO dao = new BaseDAO();

    /**
     * 单个字段值翻译
     */
    public static String transferField(String tableName, String selectField, String pkField, String pk) throws BusinessException {
        if (StringUtils.isEmpty(pk)) {
            return null;
        }
        SqlBuilder sqlBuilder = new SqlBuilder();
        sqlBuilder.append(" select " + selectField);
        sqlBuilder.append(" from " + tableName);
        sqlBuilder.append(" where ");
        sqlBuilder.append(pkField, pk);
        Object o = dao.executeQuery(sqlBuilder.toString(), new ColumnProcessor());
        if (o == null) {
            throw new BusinessException("未查询到编码信息，sql【" + sqlBuilder + "】");
        }
        return o.toString();
    }

    /**
     * 多个字段值翻译
     */
    public static Map<String, Object> transferFields(String tableName, String selectFields, String pkField, String pk) throws BusinessException {
        if (StringUtils.isEmpty(pk)) {
            return null;
        }
        SqlBuilder sqlBuilder = new SqlBuilder();
        sqlBuilder.append(" select " + selectFields);
        sqlBuilder.append(" from " + tableName);
        sqlBuilder.append(" where ");
        sqlBuilder.append(pkField, pk);

        Map<String, Object> result = (Map<String, Object>) dao.executeQuery(sqlBuilder.toString(), new MapProcessor());

        if (result == null || result.isEmpty()) {
            result = new HashMap<>();
        }

        return result;
    }


    /**
     * 检查当前组织是否为电力电子
     */
    public static boolean checkIfDldzOrg(String code, Map<String, String> configParams) throws BusinessException {
        String targetCode = configParams.get("dldzOrg");
        if (targetCode == null || nc.vo.am.common.util.StringUtils.isEmpty(targetCode)) {
            throw new BusinessException("未配置组织参数");
        }
        String[] orgItem = targetCode.split(",");
        for (String orgCode : orgItem) {
            if (!orgCode.isEmpty() && orgCode.equals(code)) {
                Logger.info("当前处理组织校验为电力电子:" + code);
                return false;
            }
        }
        return true;
    }

    /**
     * 转换特殊字段  如 1/1 转换为小数 1.0
     */

    public static String transferSpecialField(String field) {
        if (field == null || field.trim().isEmpty()) {
            return null;
        }
        String[] split = field.split("/");
        if (split.length == 2) {
            String numStr = split[0].trim();
            String denStr = split[1].trim();
            if (denStr.equals("0")) {
                return "0.00"; // 分母不能为零
            }
            try {
                BigDecimal numerator = new BigDecimal(numStr);
                BigDecimal denominator = new BigDecimal(denStr);
                return numerator.divide(denominator, 2, RoundingMode.HALF_UP).toString();
            } catch (NumberFormatException e) {
                return field; // 非法数字，返回原字段
            }
        }
        return field;
    }

    public static Map<String, String> getConfigParams(String code, String pkOrg) {
        Map<String, String> map = new HashMap<String, String>();
        String strWhere = " pk_defdoclist in (select pk_defdoclist from bd_defdoclist where code='[code]' and dr=0 ) and dr=0";
        strWhere = strWhere.replace("[code]", code);
        if (!StringUtils.isEmpty(pkOrg)) {
            strWhere = strWhere + " and = pk_org '" + pkOrg + "'";
        }
        try {
            DefdocVO[] defdocVOs = (DefdocVO[]) new HYSuperDMO().queryByWhereClause(DefdocVO.class, strWhere);
            if (defdocVOs != null) {
                for (DefdocVO defdocVO : defdocVOs) {
                    String value = StringUtils.isEmpty(defdocVO.getMemo()) ? defdocVO.getName() : defdocVO.getMemo();
                    map.put(defdocVO.getCode().trim(), value);
                }
            }
        } catch (DAOException e) {
            Logger.error("getConfigParams-exp:" + e.getMessage());
        }
        return map;

    }
}
