package com.app.controller.admin.basics;

import com.app.annotation.platform.OperationLog;
import com.app.dto.PageResult;
import com.app.dto.Result;
import com.app.dto.SearchDto;
import com.app.dto.basics.operation.CourseDto;
import com.app.dto.basics.search.SearchCourseDto;
import com.app.entity.basics.Course;
import com.app.service.basics.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * 功能描述：课程
 *
 * @Auther: 梁展鹏
 * @Date: 2018/7/24 11:38
 */
@Api(description = "课程")
@Slf4j
@Validated
@RestController("AdminCourseController")
@RequestMapping("/admin/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 功能描述：查询[课程]详情
     *
     * @param id 标识
     * @return
     */
    @ApiOperation(value = "查询[课程]详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "标识", dataType = "String", paramType = "path", required = true, defaultValue = "")
    })
    @GetMapping("/{id}")
    public ResponseEntity<Result<Course>> findById(@NotBlank @PathVariable("id") String id) {
        return new ResponseEntity(new Result().ok(courseService.findById(id)), HttpStatus.OK);
    }

    /**
     * 功能描述：查询[课程]列表
     *
     * @return
     */
    @ApiOperation(value = "查询[课程]列表")
    @PostMapping("/page")
    public ResponseEntity<Result<PageResult<Course>>> page(@Valid @RequestBody SearchCourseDto params) {
        return new ResponseEntity(new Result().ok(courseService.page(params)), HttpStatus.OK);
    }

    /**
     * 功能描述：保存/更新[课程]
     *
     * @param courseDto
     * @return
     */
    @OperationLog("更新 - 课程")
    @ApiOperation(value = "保存/更新[课程]")
    @PostMapping("/save-or-update")
    public ResponseEntity<Result> saveOrUpdate(@Valid @RequestBody CourseDto courseDto) {
        courseService.saveOrUpdate(courseDto);
        return new ResponseEntity(new Result().ok(), HttpStatus.OK);
    }

    /**
     * 功能描述：替换[课程]删除状态
     *
     * @param searchdto
     * @return
     */
    @OperationLog("替换删除状态 - 课程")
    @ApiOperation(value = "替换[课程]删除状态")
    @PostMapping("/handle-is-del")
    public ResponseEntity<Result> handleIsDel(@Valid @RequestBody SearchDto searchdto) {
        courseService.handleIsDel(searchdto.getId());
        return new ResponseEntity(new Result().ok(), HttpStatus.OK);
    }

}
