package cn.ysk.cashier.controller.order;

import cn.ysk.cashier.annotation.rest.AnonymousGetMapping;
import cn.ysk.cashier.annotation.rest.AnonymousPostMapping;
import cn.ysk.cashier.dto.shop.TbShopSongOrderQueryCriteria;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.utils.CacheKey;
import cn.ysk.cashier.utils.RedisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.ysk.cashier.mybatis.entity.TbShopSongOrder;
import cn.ysk.cashier.mybatis.service.TbShopSongOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * (TbShopSongOrder)表控制层
 *
 * @author ww
 * @since 2024-07-08 09:24:23
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "歌曲订单管理")
@RequestMapping("/api/tbShopSongOrder")
public class TbShopSongOrderController{
    private final RedisUtils redisUtils;
    /**
     * 服务对象
     */
    @Resource
    private TbShopSongOrderService tbShopSongOrderService;

    /**
     * 分页查询所有数据
     * @param tbShopSongOrder 查询实体
     * @return 所有数据
     */
    @GetMapping
    @ApiOperation("获取歌曲列表 分页")
    @AnonymousGetMapping
    public ResponseEntity<Object> selectAll(TbShopSongOrderQueryCriteria tbShopSongOrder) {
        return new ResponseEntity<>(tbShopSongOrderService.queryAll(tbShopSongOrder), HttpStatus.OK);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("订单详情")
    public ResponseEntity<Object> selectOne(@PathVariable Serializable id) {
        return new ResponseEntity<>(tbShopSongOrderService.getById(id),HttpStatus.OK);
    }

    /**
     * 歌手页 歌曲列表
     * @param key 有效key
     * @return 所有数据
     */
    @GetMapping("songs")
    @AnonymousGetMapping
    public ResponseEntity<Object> singerSongs(String key) {
        String shopId="";
        if (redisUtils.hasKey(CacheKey.SONG_URL + key)) {
            shopId = (String)redisUtils.get(CacheKey.SONG_URL + key);
        }else {
            throw new BadRequestException("地址已失效，请重新获取地址。");
        }
        return new ResponseEntity<>(tbShopSongOrderService.singerSongs(Integer.valueOf(shopId)), HttpStatus.OK);
    }

    /**
     * 下一首
     * @param shopId 店铺Id
     */
    @PostMapping("next")
    @AnonymousPostMapping
    public ResponseEntity<Object> next(@RequestBody Integer shopId) {
        tbShopSongOrderService.next(shopId);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}

