package com.webchat.controller.admin;

import com.webchat.common.bean.APIPageResponseBean;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.helper.SessionHelper;
import com.webchat.config.annotation.ValidatePermission;
import com.webchat.domain.vo.request.lottery.LotteryOrderStatusUpdateVO;
import com.webchat.domain.vo.response.lottery.LotteryUserOrderVO;
import com.webchat.service.lottery.LotteryOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * @Author 程序员七七
 * @webSite https://www.coderutil.com
 * @Date 2022/11/16 23:40
 * @description
 */
@Api(tags = "【幸运抽奖】订单管理")
@RestController
@RequestMapping("/api/lottery/order")
public class LotteryOrderController {

    @Autowired
    private LotteryOrderService lotteryOrderService;


    /**
     * 查询当前登录用户中奖订单
     *
     * @return
     */
    @ApiOperation("查询当前登录用户中奖订单")
    @GetMapping("/byCurrUser/{activityId}")
    public APIResponseBean<List<LotteryUserOrderVO>> listUserOrder(@PathVariable String activityId) {
        String userId = SessionHelper.getCurrentUserId();
        if (StringUtils.isBlank(userId)) {
            return APIResponseBeanUtil.success(Collections.emptyList());
        }
        return APIResponseBeanUtil.success(lotteryOrderService.queryActivityUserOrder(activityId, userId, Integer.MAX_VALUE));
    }

    /**
     * 查询活动下的TOP50中奖订单记录
     *
     * @param activityId
     * @return
     */
    @ApiOperation("查询活动下的TOP50中奖订单记录")
    @GetMapping("/byActivity/{activityId}")
    public APIResponseBean<List<LotteryUserOrderVO>> listActivityOrder(@PathVariable String activityId) {
        String userId = SessionHelper.getCurrentUserId();
        if (StringUtils.isBlank(userId)) {
            return APIResponseBeanUtil.success(Collections.emptyList());
        }
        return APIResponseBeanUtil.success(lotteryOrderService.queryActivityOrder(activityId, 100));
    }

    /**
     * 订单详情翻页
     * @return
     */
    @ApiOperation("中奖订单列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "activityId", value = "活动ID", dataType = "Long"),
            @ApiImplicitParam(name = "orderId", value = "订单ID", dataType = "Long"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "Integer"),
            @ApiImplicitParam(name = "pageNo", value = "页码，默认1", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "页条数，默认8", dataType = "Integer")
    })
    @ValidatePermission
    @GetMapping("/page")
    public APIPageResponseBean<List<LotteryUserOrderVO>> pageList(@RequestParam(value = "activityId", required = false) String activityId,
                                                                  @RequestParam(value = "orderId", required = false) String orderId,
                                                                  @RequestParam(value = "status", required = false) Integer status,
                                                                  @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                                                  @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return lotteryOrderService.pageList(activityId, orderId, status, pageNo, pageSize);
    }

    /**
     * 更新活动状态
     *
     * @param lotteryOrderStatusUpdate
     * @return
     */
    @ApiOperation("更新活动状态")
    @ValidatePermission
    @PostMapping("/updateStatus")
    public APIResponseBean<Boolean> updateStatus(@RequestBody LotteryOrderStatusUpdateVO lotteryOrderStatusUpdate) {
        String userId = SessionHelper.getCurrentUserId();
        lotteryOrderService.updateLotteryOrderStatus(lotteryOrderStatusUpdate, userId);
        return APIResponseBeanUtil.success(true);
    }
}
