package com.leromro.core.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leromro.common.core.domain.R;
import com.leromro.common.utils.SecurityUtils;
import com.leromro.core.domain.MainOrders;
import com.leromro.core.domain.SecondOrder;
import com.leromro.core.domain.dto.OrderRequestDTO;
import com.leromro.core.domain.vo.OrdersInfoVO;
import com.leromro.core.service.AddressService;
import com.leromro.core.service.IMainOrderService;
import com.leromro.core.service.ISecondOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.leromro.common.annotation.Log;
import com.leromro.common.core.controller.BaseController;
import com.leromro.common.core.domain.AjaxResult;
import com.leromro.common.enums.BusinessType;

/**
 * 订单主Controller
 *
 * @author ruoyi
 * @date 2025-04-09
 */
@RestController
@Api(tags = "用户订单")
@RequestMapping("/core/users/orders")
public class OrdersController extends BaseController {
    @Autowired
    private IMainOrderService mainOrderService;
    @Autowired
    private ISecondOrderService secondOrderService;

/*  @Autowired
    private IUserBalanceService userBalanceService;*/

    /**
     * 查询订单主列表
     */
    @ApiOperation("用户查询订单列表")
    @GetMapping("/mainOrderList")
    public R<List<MainOrders>> selectOrderList(Integer orderStatus) {
        startPage();
        Long userId = SecurityUtils.getUserId();
        List<MainOrders> list = mainOrderService.selectByUserId(orderStatus, userId);
        return R.ok(list);
    }

/*    @ApiOperation("用户查询小订单列表")
    @GetMapping("/secondOrderList")
    public R<List<SecondOrder>> selectSecondOrderList(String orderId) {
        startPage();
        Long userId = SecurityUtils.getUserId();
        QueryWrapper<SecondOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderId).eq("user_id", userId);
        List<SecondOrder> list = secondOrderService.list(wrapper);
        return R.ok(list);
    }*/


    /**
     * 获取订单主详细信息
     */
    @ApiOperation("用户获取订单详细信息以及大订单所属小订单")
    @GetMapping("orderInfo/{orderId}")
    public R<OrdersInfoVO> getMainOrderInfo(@PathVariable("orderId") String orderId) {
        QueryWrapper<MainOrders> wrapper = new QueryWrapper<>();
        Long userId = SecurityUtils.getUserId();
        wrapper.eq("order_id", orderId);
        MainOrders mainOrders = mainOrderService.list(wrapper).get(0);
        QueryWrapper<SecondOrder> wrapper1 = new QueryWrapper<>();
        wrapper.eq("order_id", orderId).eq("user_id", userId);
        List<SecondOrder> list = secondOrderService.list(wrapper1);
        OrdersInfoVO ordersInfoVO = new OrdersInfoVO();
        ordersInfoVO.setMainOrders(mainOrders);
        ordersInfoVO.setSecondOrderList(list);
        return R.ok(ordersInfoVO);
    }

    /**
     * 新增订单主
     */
    @ApiOperation("用户创建订单")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/createOrder")
    public AjaxResult createOrder(@RequestBody OrderRequestDTO orderRequest) {
        mainOrderService.createOrders(orderRequest);
        return AjaxResult.success();
    }

    /**
     * 修改订单主
     */
/*    @ApiOperation("修改订单主")
    @PreAuthorize("@ss.hasPermi('system:orders:edit')")
    @Log(title = "订单主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MainOrders orders)
    {
        return toAjax(lOrdersService.updateLOrders(orders));
    }*/

    /**
     * 删除订单主
     */
/*    @ApiOperation("删除订单主")
    @PreAuthorize("@ss.hasPermi('system:orders:remove')")
    @Log(title = "订单主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public R<Integer> remove(@PathVariable String[] orderIds)
    {
        return R.ok(lOrdersService.deleteLOrdersByOrderIds(orderIds));
    }*/

}
