package com.sqx.modules.discSpinning.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.RedisKeys;
import com.sqx.common.utils.RedisUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.discSpinning.entity.DiscSpinningAmount;
import com.sqx.modules.discSpinning.service.DiscSpinningAmountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@Api(value = "现金红包 抽奖配置", tags = {"现金红包 抽奖金额配置"})
@RequestMapping(value = "/discSpinningAmount")
public class DiscSpinningAmountController {
    /**
     * 服务对象
     */
    @Autowired
    private DiscSpinningAmountService discSpinningAmountService;
    @Autowired
    private RedisUtils redisUtils;

    @PostMapping("/insertDiscSpinningAmount")
    @ApiOperation("添加现金红包 抽奖配置")
    public Result insertDiscSpinningAmount(@RequestBody DiscSpinningAmount discSpinningAmount) {
        discSpinningAmountService.save(discSpinningAmount);
        redisUtils.delete(RedisKeys.getDateKey("spinning:amount"));
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("通过Id查询详情")
    public Result selectOne(@PathVariable Integer id) {
        return Result.success().put("data", discSpinningAmountService.getById(id));
    }

    @PostMapping("/updateDiscSpinningAmount")
    @ApiOperation("修改现金红包 抽奖配置")
    public Result updateDiscSpinningAmount(@RequestBody DiscSpinningAmount discSpinningAmount) {
        discSpinningAmountService.updateById(discSpinningAmount);
        redisUtils.delete(RedisKeys.getDateKey("spinning:amount"));
        return Result.success();
    }

    @PostMapping("/deleteDiscSpinningAmount")
    @ApiOperation("删除现金红包 抽奖配置")
    public Result deleteDiscSpinningAmount(Long id) {
        discSpinningAmountService.removeById(id);
        redisUtils.delete(RedisKeys.getDateKey("spinning:amount"));
        return Result.success();
    }

    @GetMapping("/selectDiscSpinningAmount")
    @ApiOperation("查询现金红包 抽奖配置")
    public Result selectDiscSpinningAmount(Integer page, Integer limit) {
        return Result.success().put("data", discSpinningAmountService.page(new Page<>(page, limit), new QueryWrapper<DiscSpinningAmount>().orderByDesc("status").orderByAsc("num","random","max_amount")));
    }
}

