package com.ruoyi.api.controller.info;

import com.ruoyi.api.controller.common.AbstractApiController;
import com.ruoyi.common.annotation.RepeatSubmit;
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.AddGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.FilePathSplicingType;
import com.ruoyi.common.filepathsplicing.FilePathSplicing;
import com.ruoyi.info.order.domain.bo.OrderBo;
import com.ruoyi.info.order.domain.bo.OrderReviewsBo;
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.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;

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

    private final IOrderService iOrderService;

    /**
     * 分页查询订单列表
     */
    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
    @ApiOperation("查询订单列表")
    @GetMapping("/page")
    public TableDataInfo<OrderVo> page(@Validated(QueryGroup.class) OrderBo bo, PageQuery pageQuery) {
        bo.setUserId(getUserId());
        return iOrderService.queryPageList(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);
    }

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

    /**
     * 新增订单
     */
    @ApiOperation("新增订单")
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OrderBo bo) {
        bo.setUserId(getUserId());
        return toAjax(iOrderService.insertByBo(bo) ? 1 : 0);
    }

    @ApiOperation("取消订单")
    @RepeatSubmit()
    @PostMapping("/cancel/{id}")
    public R<Void> cancel(@ApiParam("主键")
                              @NotNull(message = "主键不能为空")
                              @PathVariable("id") Long id) {
        return toAjax(iOrderService.cancel(id, getUserId(), CancellationMethod.user_canceled) ? 1 : 0);
    }

    @ApiOperation("评价订单")
    @RepeatSubmit()
    @PostMapping("/evaluate")
    public R<Void> evaluate(@Validated(AddGroup.class) @RequestBody OrderReviewsBo bo) {
        bo.setUserId(getUserId());
        return toAjax(iOrderService.evaluate(bo) ? 1 : 0);
    }
}
