package com.jpsoft.shinestar.mq.listener;

import com.jpsoft.shinestar.modules.base.entity.OrderInfo;
import com.jpsoft.shinestar.modules.base.entity.ParkingApply;
import com.jpsoft.shinestar.modules.base.entity.ParkingInfo;
import com.jpsoft.shinestar.modules.base.service.OrderInfoService;
import com.jpsoft.shinestar.modules.base.service.ParkingApplyService;
import com.jpsoft.shinestar.modules.base.service.ParkingInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author 墨鱼_mo
 * @date 2020-11-5 9:00
 */
@Slf4j
@Component
@RabbitListener(queues = "orderQueue",concurrency = "10")
public class OrderListener {

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private ParkingApplyService parkingApplyService;

    @Autowired
    private ParkingInfoService parkingInfoService;

    @RabbitHandler
    public void process(String id) {

        try{
            OrderInfo orderInfo = orderInfoService.get(id);
            if (orderInfo != null){
                if (orderInfo.getPayStatus() == 10){
                    orderInfo.setPayStatus(30);
                    orderInfoService.update(orderInfo);

                    ParkingApply parkingApply = parkingApplyService.findByOrderId(id);
                    if (parkingApply != null){
                        ParkingInfo parkingInfo = parkingInfoService.get(parkingApply.getParkingId());
                        if (parkingInfo != null){
                            parkingInfo.setApplyEndTime(new Date());
                            parkingInfoService.update(parkingInfo);
                        }
                    }


                }
            }
        }catch (Exception ex){
            log.error(ex.getMessage(),ex);
        }

    }
}
