package com.ruoyi.web.controller.info;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.info.order.domain.bo.OrderBo;
import com.ruoyi.info.order.domain.vo.OrderVo;
import com.ruoyi.info.order.enums.CancellationMethod;
import com.ruoyi.info.order.enums.OrderStatus;
import com.ruoyi.info.order.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单Controller
 *
 * @author ruoyi
 * @date 2025-09-20
 */
@Validated
@Api(value = "订单控制器", tags = {"订单管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/order/order")
public class OrderController extends BaseController {

    private final IOrderService iOrderService;

    /**
     * 分页查询订单列表
     */
    @ApiOperation("查询订单列表")
    @SaCheckPermission("order:order:list")
    @GetMapping("/page")
    public TableDataInfo<OrderVo> page(@Validated(QueryGroup.class) OrderBo bo, PageQuery pageQuery) {
        return iOrderService.queryAllPageList(bo, pageQuery);
    }

    @ApiOperation("枚举")
    @GetMapping("/enum")
    public R<Map<String,  Object>> getEnum() {
        Map<String, Object> map = new HashMap<>();
        map.put("orderStatus", OrderStatus.toJsonArray());
        map.put("cancellationMethod", CancellationMethod.toJsonArray());
        return R.ok(map);
    }

    /**
     * 获取订单详细信息
     */
    @ApiOperation("获取订单详细信息")
    @SaCheckPermission("order:order:query")
    @GetMapping("/info/{id}")
    public R<OrderVo> getInfo(@ApiParam("主键")
                                                  @NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return R.ok(iOrderService.queryById(id));
    }

    /**
     * 导出订单列表
     */
    @ApiOperation("导出订单列表")
    @SaCheckPermission("order:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated OrderBo bo, HttpServletResponse response) {
        List<OrderVo> list = iOrderService.queryAllList(bo);
        ExcelUtil.exportExcel(list, "订单", OrderVo.class, response);
    }

}
