package com.zm.order.tool.server.manager;

import com.google.common.base.Throwables;
import com.zm.order.tool.server.constants.CommonConstants;
import com.zm.order.tool.server.hithistory.HintHistoryOrderDao;
import com.zm.order.tool.server.hithistory.HintHistoryPayDao;
import com.zm.order.tool.server.model.Order;
import com.zm.order.tool.server.model.OrderPayDetails;
import com.zm.order.tool.server.query.OrderQuery;
import com.zm.order.tool.server.sharding.OrderHintManager;
import org.apache.shardingsphere.api.hint.HintManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author nianchang
 * @date 2021/12/21 4:13 下午
 */
@Component
public class OBHistoryHintManager {

    private static final Logger logger = LoggerFactory.getLogger(OBHistoryHintManager.class);

    @Autowired
    private HintHistoryOrderDao hintOrderDao;

    @Autowired
    private HintHistoryPayDao payDao;

    /**
     * 扫描订单
     */
    public List<Order> scanOrderByOrderNo(List<String> orderNoList, int dbIndex, int tbIndex) {
        HintManager hintManager = null;
        try {
            hintManager = OrderHintManager.initScanHint(CommonConstants.LOGIC_TABLE_ORDER, dbIndex, tbIndex);
            return hintOrderDao.selectByOrderNo(orderNoList);
        } catch (Throwable throwable) {
            logger.error("CommonManager[scanOrder] failed, cause by:{}", Throwables.getStackTraceAsString(throwable));
            throw new RuntimeException(throwable.getMessage());
        } finally {
            OrderHintManager.closeHint(hintManager);
        }
    }

    /**
     * 扫描订单
     */
    public List<Order> scanOrderById(List<Long> list, int dbIndex, int tbIndex) {
        HintManager hintManager = null;
        try {
            hintManager = OrderHintManager.initScanHint(CommonConstants.LOGIC_TABLE_ORDER, dbIndex, tbIndex);
            return hintOrderDao.selectById(list);
        } catch (Throwable throwable) {
            logger.error("CommonManager[scanOrder] failed, cause by:{}", Throwables.getStackTraceAsString(throwable));
            throw new RuntimeException(throwable.getMessage());
        } finally {
            OrderHintManager.closeHint(hintManager);
        }
    }

    /**
     * 扫描订单
     */
    public List<Order> scanOrderList(OrderQuery query, int dbIndex, int tbIndex) {
        HintManager hintManager = null;
        try {
            hintManager = OrderHintManager.initScanHint(CommonConstants.LOGIC_TABLE_ORDER, dbIndex, tbIndex);
            return hintOrderDao.scanList(query);
        } catch (Throwable throwable) {
            logger.error("CommonManager[scanOrder] failed, cause by:{}", Throwables.getStackTraceAsString(throwable));
            throw new RuntimeException(throwable.getMessage());
        } finally {
            OrderHintManager.closeHint(hintManager);
        }
    }

    /**
     * 扫描订单
     */
    public List<OrderPayDetails> scanPayById(List<Long> list, int dbIndex, int tbIndex) {
        HintManager hintManager = null;
        try {
            hintManager = OrderHintManager.initScanHint(CommonConstants.LOGIC_TABLE_ORDER_PAY_DETAILS, dbIndex, tbIndex);
            return payDao.selectRespId(list);
        } catch (Throwable throwable) {
            logger.error("CommonManager[scanPayById]failed, cause by:{}", Throwables.getStackTraceAsString(throwable));
            throw new RuntimeException(throwable.getMessage());
        } finally {
            OrderHintManager.closeHint(hintManager);
        }
    }

}
