package com.gxwebsoft.open.controller;

import com.gxwebsoft.common.core.exception.BusinessException;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.entity.Order;
import com.gxwebsoft.shop.param.OrderParam;
import com.gxwebsoft.shop.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户-我的订单
 *
 * @author 科技小王子
 * @since 2022-11-25 19:04:43
 */
@Api(tags = "我的订单")
@RestController
@RequestMapping("/api/open/user-order")
public class OpenUserOrderController extends BaseController {
    @Resource
    private OrderService orderService;

    @ApiOperation("分页查询用户订单")
    @GetMapping("/page")
    public ApiResult<PageResult<Order>> page(OrderParam param) {
      // 验证签名
      isCheckSign();
      PageParam<Order, OrderParam> page = new PageParam<>(param);
      page.setDefaultOrder("create_time desc");
      // 记录当前登录用户id、租户id
      User user = getLoginUser();
      if (user != null) {
        param.setUserId(user.getUserId());
        param.setTenantId(user.getTenantId());
      }
      // 使用关联查询
      return success(orderService.pageRel(param));
    }

    @ApiOperation("查询全部用户订单")
    @GetMapping()
    public ApiResult<List<Order>> list(OrderParam param) {
        // 验证签名
        isCheckSign();
        PageParam<Order, OrderParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        // 记录当前登录用户id、租户id
        User user = getLoginUser();
        if (user != null) {
          param.setUserId(user.getUserId());
          param.setTenantId(user.getTenantId());
        }

        // 使用关联查询
        return success(orderService.listRel(param));
    }

    @ApiOperation("测试")
    @GetMapping("/test")
    public ApiResult<PageResult<Order>> test(OrderParam param) {
      // 验证签名
      isCheckSign();
      PageParam<Order, OrderParam> page = new PageParam<>(param);
      page.setDefaultOrder("create_time desc");
      // 记录当前登录用户id、租户id
      User user = getLoginUser();
      if (user != null) {
        param.setUserId(user.getUserId());
        param.setTenantId(user.getTenantId());
      }
      // 使用关联查询
      return success(orderService.pageRel(param));
    }
}
