package cn.ysk.cashier.controller.shop;

import cn.ysk.cashier.annotation.Log;
import cn.ysk.cashier.annotation.rest.AnonymousPostMapping;
import cn.ysk.cashier.dto.shop.TbShopSongQueryCriteria;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.mybatis.entity.TbShopSong;
import cn.ysk.cashier.mybatis.service.TbShopSongService;
import cn.ysk.cashier.utils.CacheKey;
import cn.ysk.cashier.utils.RedisUtils;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequiredArgsConstructor
@Api(tags = "歌曲管理")
@RequestMapping("/api/tbShopSong")
public class TbShopSongController {
    private final RedisUtils redisUtils;
    private final TbShopSongService tbShopSongService;

    @GetMapping
    @ApiOperation("查询歌曲列表")
    public ResponseEntity<Object> queryTbShopPurveyor(TbShopSongQueryCriteria criteria){
        String code = "";
        if(redisUtils.hasKey(CacheKey.SONG_URL + criteria.getShopId())){
            code = (String) redisUtils.get(CacheKey.SONG_URL + criteria.getShopId());
        }else {
            code = RandomStringUtils.randomAlphanumeric(12);
            redisUtils.set(CacheKey.SONG_URL + criteria.getShopId(), code);
            redisUtils.set(CacheKey.SONG_URL + code, criteria.getShopId());
        }
        Map<String, Object> stringObjectMap = tbShopSongService.queryAll(criteria);
        stringObjectMap.put("songUrl",code);
        return new ResponseEntity<>(stringObjectMap, HttpStatus.OK);
    }

    @PostMapping("createUrl")
    @ApiOperation("更新歌手页地址")
    @AnonymousPostMapping
    public ResponseEntity<Object> createUrl(String shopId) {
        String key = RandomStringUtils.randomAlphanumeric(12);
        redisUtils.set(CacheKey.SONG_URL + shopId, key);
        redisUtils.set(CacheKey.SONG_URL + key, shopId);
        return new ResponseEntity<>(key,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增歌曲:#resources.name")
    @ApiOperation("新增歌曲")
    public ResponseEntity<Object> createTbShopPurveyor(@Validated @RequestBody TbShopSong resources){
        if (StringUtils.isBlank(resources.getName())) {
            throw new BadRequestException("请填写歌曲名称");
        }
        if (StringUtils.isBlank(resources.getSinger())) {
            throw new BadRequestException("请填写演出歌手名称");
        }
//        resources.setSalesNumber(0);
        resources.setStatus(1);
        resources.setCreateTime(new Date());
        return new ResponseEntity<>(tbShopSongService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改歌曲:#resources.name")
    @ApiOperation("修改歌曲")
    public ResponseEntity<Object> updateTbShopPurveyor(@Validated @RequestBody TbShopSong resources){
        resources.setUpdateTime(new Date());
        tbShopSongService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除歌曲:#ids")
    @ApiOperation("删除歌曲")
    public ResponseEntity<Object> deleteTbShopPurveyor(@RequestBody List<Long> ids) {
        tbShopSongService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
