package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeUserLearningVideo;
import shop.alien.store.service.LifeUserLearningVideoService;

@Api(tags = {"学习视频"})
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/lifeUserLearningVideo")
@RequiredArgsConstructor
public class LifeUserLearningVideoController {

    private final LifeUserLearningVideoService lifeUserLearningVideoService;

    @ApiOperation("新增学习视频")
    @ApiOperationSupport(order = 1)
    @PostMapping("/add")
    public R<String> add(@RequestBody LifeUserLearningVideo lifeUserLearningVideo) {
        try {
            return lifeUserLearningVideoService.add(lifeUserLearningVideo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @ApiOperation("根据ID删除学习视频")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/deleteById")
    public R<String> deleteById(@RequestParam Integer id) {
        return lifeUserLearningVideoService.deleteById(id);
    }

    @ApiOperation("更新学习视频")
    @ApiOperationSupport(order = 3)
    @PostMapping("/update")
    public R<String> update(@RequestBody LifeUserLearningVideo lifeUserLearningVideo) {
        return lifeUserLearningVideoService.update(lifeUserLearningVideo);
    }

    @ApiOperation("根据ID查询学习视频")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getById")
    public R<LifeUserLearningVideo> getById(@RequestParam Integer id) {
        return lifeUserLearningVideoService.getInfoById(id);
    }

    @ApiOperation("分页查询学习视频列表")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public R<IPage<LifeUserLearningVideo>> list(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String name) {
        return lifeUserLearningVideoService.list(pageNum, pageSize, name);
    }
}

