package com.ruoyi.web.bst;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RequestParam;
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.OrderEndDTO;
import com.ruoyi.bst.order.domain.dto.OrderRefundDTO;
import com.ruoyi.bst.order.domain.dto.OrderVerifyDTO;
import com.ruoyi.bst.order.service.OrderAssembler;
import com.ruoyi.bst.order.service.OrderService;
import com.ruoyi.common.annotation.Log;
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.enums.BusinessType;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 订单Controller
 *
 * @author ruoyi
 * @date 2025-03-24
 */
@RestController
@RequestMapping("/bst/order")
public class OrderController extends BaseController
{
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderAssembler orderAssembler;

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('bst:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderQuery query)
    {
        startPage();
        startOrderBy();
        query.setScope(true);
        List<OrderVO> list = orderService.selectOrderList(query);
        orderAssembler.assembleOrderDeviceList(list);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('bst:order:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderQuery query)
    {
        query.setScope(true);
        List<OrderVO> list = orderService.selectOrderList(query);
        ExcelUtil<OrderVO> util = new ExcelUtil<OrderVO>(OrderVO.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('bst:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id, 
        @RequestParam(required = false, defaultValue = "false") Boolean assembleOrderDeviceList)
    {
        OrderVO order = orderService.selectOrderById(id, true);
        if (assembleOrderDeviceList) {
            orderAssembler.assembleOrderDeviceList(Collections.singletonList(order));
        }
        return success(order);
    }

    /**
     * 结束订单（辅助还车）
     */
    @PreAuthorize("@ss.hasPermi('bst:order:end')")
    @PutMapping("/end")
    public AjaxResult end(@RequestBody @Validated OrderEndDTO dto) {
        OrderVO order = orderService.selectOrderById(dto.getOrderId(), true);
        ServiceUtil.assertion(order == null, "订单不存在");
        dto.setIgnoreInParking(true);
        dto.setEndReason("管理员【" + getNickName() + "】手动还车");
        return toAjax(orderService.endOrder(dto));
    }

    /**
     * 退款
     */
    @PreAuthorize("@ss.hasPermi('bst:order:refund')")
    @PutMapping("/refund")
    public AjaxResult refund(@RequestBody @Validated OrderRefundDTO dto) {
        // 查询订单
        OrderVO order = orderService.selectOrderById(dto.getOrderId(), true);
        ServiceUtil.assertion(order == null, "订单不存在");

        // 退款
        dto.setUserId(getUserId());
        dto.setUserName(getNickName());
        return toAjax(orderService.refund(dto));
    }

    /**
     * 还车审核
     */
    @PreAuthorize("@ss.hasPermi('bst:order:verify')")
    @PutMapping("/verify")
    public AjaxResult verify(@RequestBody @Validated OrderVerifyDTO dto) {
        // 查询订单
        OrderVO order = orderService.selectOrderById(dto.getId(), true);
        ServiceUtil.assertion(order == null, "订单不存在");

        return toAjax(orderService.verify(dto));
    }

}
