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

import com.hc.business.model.UserCourse;
import com.hc.business.service.UserCourseService;
import com.hc.core.utils.result.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 用户课程表(UserCourse)表控制层
 *
 * @author java
 * @since 2020-11-09 10:05:20
 */

@RestController
@RequestMapping("/new/api/back/userCourse")
public class BackUserCourseController {

    /**
     * 服务对象
     */
    private final UserCourseService userCourseService;

    public BackUserCourseController(UserCourseService userCourseService) {
        this.userCourseService = userCourseService;
    }

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

    @PostMapping("/insert")
    @ApiOperation(value = "新增 UserCourse", response = Result.class)
    public Result addUserCourse(@Valid @RequestBody UserCourse userCourse) {
        return Result.success(userCourseService.insert(userCourse));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改 UserCourse", response = Result.class)
    public Result updateUserCourse(@RequestBody UserCourse userCourse) {
        return Result.success(userCourseService.update(userCourse));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "依据主键删除 UserCourse", response = Result.class)
    public Result deleteUserCourse(@PathVariable("id") Integer id) {
        return Result.success(userCourseService.deleteById(id));
    }

    @GetMapping("/list")
    @ApiOperation(value = "根据用户id 获取 UserCourse列表(分页)", response = Result.class)
    public Result listUserCourse(@ApiParam(value = "用户id") @RequestParam Integer userId,
                                 @ApiParam(value = "班次id") @RequestParam Integer classId,
                                 @RequestParam(defaultValue = "0") Integer page,
                                 @RequestParam(defaultValue = "0") Integer size) {
        return Result.success(userCourseService.queryAllByLimit(userId, classId, page, size));
    }

    @GetMapping("/selectUserCourseCount")
    @ApiOperation(value = "根据用户Phone 和班次，获取是否报名，>0报名 ==0没有报名", response = Result.class)
    public Result selectUserCourseCount(@ApiParam(value = "手机号码") @RequestParam String phone,
                                        @ApiParam(value = "班次id") @RequestParam Integer classId) {
        return Result.success(userCourseService.selectUserCourseCount(phone, classId));
    }

}