package com.ruoyi.web.app;

import java.util.List;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.bst.order.domain.OrderQuery;
import com.ruoyi.bst.order.domain.OrderVO;
import com.ruoyi.bst.order.domain.dto.OrderCalcPrePriceDTO;
import com.ruoyi.bst.order.domain.dto.OrderChangeDeviceDTO;
import com.ruoyi.bst.order.domain.dto.OrderCloseDeviceDTO;
import com.ruoyi.bst.order.domain.dto.OrderCreateDTO;
import com.ruoyi.bst.order.domain.dto.OrderEndDTO;
import com.ruoyi.bst.order.domain.dto.OrderPayDTO;
import com.ruoyi.bst.order.domain.enums.OrderReturnType;
import com.ruoyi.bst.order.domain.enums.OrderStatus;
import com.ruoyi.bst.order.domain.vo.OrderEndVO;
import com.ruoyi.bst.order.domain.vo.OrderFeeVO;
import com.ruoyi.bst.order.domain.vo.OrderPrePriceVO;
import com.ruoyi.bst.order.service.OrderConverter;
import com.ruoyi.bst.order.service.OrderService;
import com.ruoyi.bst.order.service.OrderValidator;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ServiceUtil;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/app/order")
public class AppOrderController extends BaseController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderConverter orderConverter;

    @Autowired
    private OrderValidator orderValidator;

    @ApiOperation("获取我的订单列表")
    @GetMapping("/mineList")
    public TableDataInfo getMineList(OrderQuery query) {
        query.setUserId(getUserId());
        startPage();
        List<OrderVO> list = orderService.selectOrderList(query);
        return getDataTable(list);
    }

    @ApiOperation("获取我的订单详情")
    @GetMapping("/mineDetail")
    public AjaxResult getMineDetail(Long id) {
        OrderQuery query = new OrderQuery();
        query.setId(id);
        query.setUserId(getUserId());
        return success(orderService.selectOne(query));
    }

    @ApiOperation("获取我正在进行的订单")
    @GetMapping("/mineProcessing")
    public AjaxResult getMineProcessing() {
        OrderQuery query = new OrderQuery();
        query.setUserId(getUserId());
        query.setStatus(OrderStatus.PROCESSING.getCode());
        return success(orderService.selectOne(query));
    }

    @ApiOperation("支付前计算订单价格")
    @PostMapping("/calculatePrice")
    @Anonymous
    public AjaxResult calculatePrice(@RequestBody @Validated OrderCalcPrePriceDTO dto) {
        OrderPrePriceVO vo = orderConverter.toOrderPrePriceVO(dto);
        if (vo == null) {
            return error("计算价格失败，请检查");
        }
        return success(vo);
    }

    @ApiOperation("下单并支付")
    @PostMapping
    public AjaxResult createOrder(@RequestBody @Validated OrderCreateDTO dto) {
        dto.setUserId(getUserId());
        return success(orderService.createOrder(dto));
    }

    @ApiOperation("支付")
    @PostMapping("/pay")
    public AjaxResult pay(@RequestBody @Validated OrderPayDTO dto) {
        return error("当前接口已弃用，请使用下单并支付接口");
        // // 查询订单
        // OrderVO order = orderService.selectOrderByNo(dto.getNo());
        // ServiceUtil.assertion(!orderValidator.canPay(order, getUserId()), "您无权支付ID为%s的订单", order.getId());

        // // 支付
        // return success(orderService.pay(order, dto));
    }

    @ApiOperation("结束本人订单（还车）")
    @PutMapping("/end")
    public AjaxResult endOrder(@RequestBody @Validated OrderEndDTO dto) {
        OrderVO order = orderService.selectOrderById(dto.getOrderId());
        ServiceUtil.assertion(!orderValidator.canEnd(order, getUserId()), "您无权结束ID为%s的订单", order.getId());
        dto.setReturnType(OrderReturnType.NORMAL.getCode());
        dto.setEndReason("用户【" + getNickName() + "】手动还车");
        OrderEndVO vo = orderService.endOrder(dto);
        if (vo.getDb() > 0) {
            return success(vo);
        }
        return error("关闭订单失败");
    }

    @ApiOperation("计算订单费用")
    @PostMapping("/calcFee")
    public AjaxResult calcFee(@RequestBody @Validated OrderEndDTO dto) {
        dto.setReturnType(OrderReturnType.NORMAL.getCode());
        OrderFeeVO fee = orderService.calcFee(dto);
        ServiceUtil.assertion(fee == null, "价格计算失败");
        return success(fee);
    }

    @ApiOperation("操作订单设备开启")
    @PutMapping("/openDevice")
    public AjaxResult openDevice(Long orderId) {
        OrderVO order = orderService.selectOrderById(orderId);
        ServiceUtil.assertion(order == null, "订单不存在");
        ServiceUtil.assertion(!orderValidator.canOpenDevice(order, getUserId()), "您无权操作ID为%s的订单设备开启", order.getId());
        return success(orderService.openDevice(order, true));
    }

    @ApiOperation("操作订单设备关闭")
    @PutMapping("/closeDevice")
    public AjaxResult closeDevice(@Validated OrderCloseDeviceDTO dto) {
        OrderVO order = orderService.selectOrderById(dto.getOrderId());
        ServiceUtil.assertion(order == null, "订单不存在");
        ServiceUtil.assertion(!orderValidator.canCloseDevice(order, getUserId()), "您无权操作ID为%s的订单设备关闭", order.getId());
        dto.setRequiredIot(true);
        return success(orderService.closeDevice(dto));
    }

    @ApiOperation("订单换车")
    @PutMapping("/changeDevice")
    public AjaxResult changeDevice(@RequestBody @Validated OrderChangeDeviceDTO dto) {
        OrderVO order = orderService.selectOrderById(dto.getOrderId());
        ServiceUtil.assertion(!orderValidator.canChangeDevice(order, getUserId()), "您无权操作ID为%s的订单换车", order.getId());
        dto.setUserId(getUserId());
        dto.setUserName(getNickName());
        return toAjax(orderService.changeDevice(dto));
    }

}
