package com.hc.web.controller.browser.back;

import com.hc.business.dto.LiveClassDto;
import com.hc.business.model.Live;
import com.hc.business.service.LiveClassService;
import com.hc.business.service.LiveService;
import com.hc.core.utils.enums.InfoEnum;
import com.hc.core.utils.result.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 直播间表(Live)表控制层
 *
 * @author makejava
 * @since 2024-01-20 11:48:46
 */

@RestController
@RequestMapping("/new/api/back/live")
public class BackLiveController {


    /**
     * 服务对象
     */
    private final LiveService liveService;
    private final LiveClassService liveClassService;

    public BackLiveController(LiveService liveService,
                                LiveClassService liveClassService) {
        this.liveService = liveService;
        this.liveClassService = liveClassService;
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "依据主键获取 Live", response = Result.class)
    public Result selectDetail(@PathVariable("id") Integer id) {
        return Result.success(this.liveService.queryById(id));
    }

    @PostMapping("/insert")
    @ApiOperation(value = "新增 Live", response = Result.class)
    public Result addLive(@Valid @RequestBody Live live) {
        return Result.success(liveService.insert(live));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改 Live", response = Result.class)
    public Result updateLive(@RequestBody Live live) {
        return Result.success(liveService.update(live));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "依据主键删除 Live", response = Result.class)
    public Result deleteLive(@PathVariable("id") Integer id) {
        List<LiveClassDto> liveClasses = liveClassService.queryByLiveId(id);
        //被关联提醒返回
        if(liveClasses.stream().count()>0) {
            return Result.info(InfoEnum.LIVE_ERROR);
        }
        return Result.success(liveService.deleteById(id));
    }



    @GetMapping("/list")
    @ApiOperation(value = "获取 Live列表(分页)", response = Result.class)
    public Result listLive(@RequestParam(defaultValue = "0") Integer page,
                           @RequestParam(defaultValue = "0") Integer size) {
        return Result.success(liveService.queryAllByLimit(page, size));
    }


}
