package cn.freemud.management.service.handle;

import cn.freemud.amqp.Header;
import cn.freemud.amqp.MQAction;
import cn.freemud.amqp.MQMessage;
import cn.freemud.amqp.MQService;
import cn.freemud.management.entities.dto.request.order.OrderManagerRequest;
import com.freemud.application.sdk.api.log.ApiLog;
import com.freemud.application.sdk.api.log.ErrorLog;
import com.freemud.sdk.api.assortment.order.request.order.OrderStatusChangeRequestDto;
import com.freemud.sdk.api.assortment.order.response.order.QueryOrdersResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * All rights Reserved, Designed By www.freemud.cn
 *
 * @version V1.0
 * @Title: InventoryService
 * @Package cn.freemud.management.service.impl
 * @Description:
 * @author: shuhu.hou
 * @date: 2020/4/24 17:56
 * @Copyright: 2020 www.freemud.cn Inc. All rights reserved.
 * 注意：本内容仅限于上海非码科技内部传阅，禁止外泄以及用于其他的商业目
 */
@Component
public class StockHandle {

    private static final String backOrdersChangeOrderStatusConsumerQueue="program.backorders_change_order_status_consumer_queue";
    private static final String backOrdersNotifyActivityExchange="program.backorders_notify_activity_exchange";

    @Autowired
    private MQService mqService;

    /**
     * 冲正库存、券码等
     * @param request
     * @param orderBean
     */
    public void revert(OrderManagerRequest request, QueryOrdersResponse.DataBean.OrderBean orderBean){
        ApiLog.info("backOrdersStatusChange", orderBean.getOid(), String.valueOf(orderBean.getStatus()));
        OrderStatusChangeRequestDto requestDto = new OrderStatusChangeRequestDto();
        requestDto.setOid(orderBean.getOid());
        requestDto.setOrderStatus(orderBean.getStatus());

        try {
            Header header = new Header(MQAction.INSERT.getAction(), "backOrdersStatusChange", orderBean.getOid(), backOrdersChangeOrderStatusConsumerQueue);
            MQMessage<OrderStatusChangeRequestDto> message = new MQMessage<>(header, requestDto);
            mqService.convertAndSend(backOrdersNotifyActivityExchange, backOrdersChangeOrderStatusConsumerQueue, message);
        } catch (Exception e) {
            ErrorLog.errorConvertJson(this.getClass(),"activityReverseError:"+orderBean.getOid(),e);
        }
    }
}
