package com.bohuikeji.delivery.backstage.controller.api.order;

import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.entity.TOrderExceptionEntity;
import com.bohuikeji.delivery.backstage.entity.socket.AdminNotice;
import com.bohuikeji.delivery.backstage.service.ISocketService;
import com.bohuikeji.delivery.backstage.service.ITOrderExceptionService;
import com.bohuikeji.delivery.backstage.service.ITOrderService;
import com.bohuikeji.frame.module.admin.base.ApiController;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 订单异常接口
 * @author MasterBoot
 */
@Slf4j
@Api(tags = {"异常订单接口"})
@RestController
@RequestMapping("/api/order/exception")
@RequiredArgsConstructor
public class OrderExceptionApi extends ApiController {
    final ITOrderExceptionService orderExceptionService;
    final ITOrderService orderService;
    final ISocketService socketService;

    /**
     * 提交异常订单
     * @param orderExceptionEntity
     * @return
     */
    @PostMapping("save")
    @ApiOperation("异常订单提交")
    public APIEntity save(@RequestBody TOrderExceptionEntity orderExceptionEntity){
        log.info("#####################################异常上交：{}",orderExceptionEntity);
        if (orderExceptionEntity == null){
            return APIEntity.fail("填写内容为空");
        }
        /* 查询订单信息如果为异常状态 直接返回 */
        TOrder order = orderService.getById(orderExceptionEntity.getOrderId());
        if(order==null){
            return APIEntity.fail("未找到订单信息，请重新打开页面再试！");
        }
        if ("异常".equals(order.getOrderStatus())){
            return APIEntity.fail("当前订单已经是异常状态,请勿重复填写");
        }
        /* 修改订单状态 */
        order.setOrderStatus("异常");
        orderService.updateById(order);
        /*orderExceptionEntity.setLogisticsNum(logisticsService.getOne(new QueryWrapper<TLogistics>().eq("orderId",order.getOrderId())).getLogisticsNum());*/
        orderExceptionEntity.setOrderNum(order.getOrderNum());
        /* 录入异常信息 */
        orderExceptionService.save(orderExceptionEntity);
        socketService.sendAdminMsg(new AdminNotice("您好，有订单出现异常，请及时处理!")
                .setAudio("https://oss.bhynm.com/delivery/upload/2022/3/15/164731326572347.mp3")
                .setFranId(order.getFranId()));
        return APIEntity.success();
    }

}
