package com.yiqi.admin.controller.order;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.domain.R;
import com.yiqi.order.domain.OrderDelivery;
import com.yiqi.order.domain.dto.OrderThirdDeliveryCancelDTO;
import com.yiqi.order.domain.dto.OrderThirdDeliveryPathDTO;
import com.yiqi.order.domain.dto.OrderThirdDeliveryTestUpdateStatusDTO;
import com.yiqi.order.domain.vo.ExpressDeliveryVO;
import com.yiqi.order.service.IOrderClothService;
import com.yiqi.order.service.IOrderDeliveryService;
import com.yiqi.order.service.IThirdDeliveryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author xiaohei
 * @date 2024/1/21
 */
@Api(tags = "第三方配送相关接口")
@RestController
@RequestMapping(value = UrlConstants.managerApi + "/third/delivery")
public class ThirdDeliveryController {

    @Autowired
    private IThirdDeliveryService thirdDeliveryService;
    @Lazy
    @Autowired
    private IOrderClothService orderClothService;

    @Lazy
    @Autowired
    private IOrderDeliveryService orderDeliveryService;

    @ApiOperation("获取所有可支持的快递公司")
    @GetMapping(value = "all")
    public R<List<ExpressDeliveryVO>> all() throws Exception{
        return R.ok(thirdDeliveryService.getAllDeliveryList());
    }

    @ApiOperation("获取运单的运行轨迹")
    @PostMapping(value = "getPath")
    public R getPath(@RequestBody @Valid OrderThirdDeliveryPathDTO orderThirdDeliveryPathDTO) throws Exception{
        return R.ok(thirdDeliveryService.getDeliveryPath(orderThirdDeliveryPathDTO));
    }

    @ApiOperation("根据洗衣订单号获取运单的运行轨迹")
    @GetMapping(value = "getPathByClothOrderNo")
    public R getPathByClothOrderNo(String orderNo,String deliveryType) throws Exception{
        OrderDelivery orderDelivery = orderDeliveryService.getOne(new QueryWrapper<OrderDelivery>().lambda()
                .eq(OrderDelivery::getOrderSn,orderNo)
                .eq(OrderDelivery::getClothOrderType,deliveryType));
        OrderThirdDeliveryPathDTO orderThirdDeliveryPathDTO = new OrderThirdDeliveryPathDTO();
        orderThirdDeliveryPathDTO.setDeliveryId(orderDelivery.getServiceTransId());
        orderThirdDeliveryPathDTO.setWaybillId(orderDelivery.getTransOrderId());
        return R.ok(thirdDeliveryService.getDeliveryPath(orderThirdDeliveryPathDTO));
    }

    @ApiOperation("模拟更新快递订单状态")
    @PostMapping(value = "testUpdateOrder")
    public R testUpdateOrder(@RequestBody @Valid OrderThirdDeliveryTestUpdateStatusDTO orderThirdDeliveryTestUpdateStatusDTO) throws Exception{
        thirdDeliveryService.testUpdateOrder(orderThirdDeliveryTestUpdateStatusDTO);
        return R.ok();
    }

    @ApiOperation("取消订单")
    @PostMapping(value = "cancel")
    public R cancelOrder(OrderThirdDeliveryCancelDTO orderThirdDeliveryCancelDTO) throws Exception{
        thirdDeliveryService.cancelOrder(orderThirdDeliveryCancelDTO);
        return R.ok();
    }



}
