package com.zm.order.tool.server.handler.refund;

import com.github.dozermapper.core.Mapper;
import com.zm.order.tool.server.dao.OrderDao;
import com.zm.order.tool.server.enums.BizTypeEnum;
import com.zm.order.tool.server.model.Order;
import com.zm.order.tool.server.model.Refund;
import com.zm.order.tool.server.model.refund.BaseRefundIndex;
import com.zm.order.tool.server.model.refund.CateringRefundIndex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * 餐饮退款文档处理器
 *
 * @author zhuxiaofeng
 * @date 2022-11-10 16:07:11
 */
@Component
public class CateringRefundDocHandler implements RefundDocHandler {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private Mapper mapper;

    @Override
    public Integer bizType() {
        return BizTypeEnum.CATERING.getType();
    }

    @Override
    public BaseRefundIndex buildRefundDoc(Refund refund) {
        CateringRefundIndex refundIndex = mapper.map(refund, CateringRefundIndex.class);
        buildBaseInfo(refundIndex, refund);
        return refundIndex;
    }

    private void buildBaseInfo(CateringRefundIndex refundIndex, Refund refund) {
        refundIndex.setRefundId(refund.getId());
        Order order = orderDao.selectByOrderNo(refund.getOrderNo(), refund.getUserId());
        if (Objects.isNull(order)) {
            return;
        }
        if (!StringUtils.isEmpty(order.getStoreOrderNo())) {
            refundIndex.setStoreOrderNo(order.getStoreOrderNo());
        }
    }
}
