package com.ruoyi.web.bst;

import com.ruoyi.bst.order.domain.Order;
import com.ruoyi.bst.order.domain.OrderQuery;
import com.ruoyi.bst.order.domain.OrderVO;
import com.ruoyi.bst.order.domain.dto.OrderRefundDTO;
import com.ruoyi.bst.order.service.OrderService;
import com.ruoyi.bst.order.service.OrderValidator;
import com.ruoyi.bst.storeStaff.domain.enums.StoreStaffPermission;
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.enums.LogBizType;
import com.ruoyi.common.utils.poi.ExcelUtil;
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.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单列表Controller
 *
 * @author ruoyi
 * @date 2025-04-26
 */
@RestController
@RequestMapping("/bst/order")
public class OrderController extends BaseController
{
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderValidator orderValidator;

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

    /**
     * 导出订单列表列表
     */
    @PreAuthorize("@ss.hasPermi('bst:order:export')")
    @Log(title = "订单列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderQuery query)
    {
        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)
    {
        return success(orderService.selectOrderById(id));
    }

    /**
     * 新增订单列表
     */
    @PreAuthorize("@ss.hasPermi('bst:order:add')")
    @Log(title = "订单列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Order order)
    {
        return toAjax(orderService.insertOrder(order));
    }

    /**
     * 修改订单列表
     */
    @PreAuthorize("@ss.hasPermi('bst:order:edit')")
    @Log(title = "订单列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Order order)
    {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除订单列表
     */
    @PreAuthorize("@ss.hasPermi('bst:order:remove')")
    @Log(title = "订单列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderService.deleteOrderByIds(ids));
    }

    /**
     * 退款
     */
    @Log(title = "订单退款", businessType = BusinessType.UPDATE, bizIdName = "arg0", bizType = LogBizType.ORDER)
    @PreAuthorize("@ss.hasPermi('bst:order:refund')")
    @PutMapping("/refund")
    public AjaxResult refund(@RequestBody @Validated OrderRefundDTO dto) {
        if (!orderValidator.canOperate(dto.getOrderId())) {
            return error("您无权退款ID为" + dto.getOrderId() + "的订单");
        }
        dto.setUserId(getUserId());
        dto.setUserName(getNickName());
        return toAjax(orderService.refund(dto));
    }
}
