package com.app.controller.admin.basics;

import com.app.annotation.platform.OperationLog;
import com.app.constant.OpenCourseConstant;
import com.app.dto.PageResult;
import com.app.dto.Result;
import com.app.dto.SearchDto;
import com.app.dto.basics.operation.OpenCourseDto;
import com.app.dto.basics.search.SearchOpenCourseDto;
import com.app.dto.order.OpenCourseOrderExportDto;
import com.app.entity.basics.OpenCourse;
import com.app.service.basics.OpenCourseService;
import com.app.service.order.OpenCourseOrderExportService;
import io.swagger.annotations.Api;
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;
import java.util.List;

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

    @Autowired
    private OpenCourseService openCourseService;

    @Autowired
    private OpenCourseOrderExportService openCourseOrderExportService;

    /**
     * 功能描述：查询[430开课]列表
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "查询[430开课]列表")
    @PostMapping("/page")
    public ResponseEntity<Result<PageResult<OpenCourse>>> page(@Valid @RequestBody SearchOpenCourseDto params) {
        return new ResponseEntity(new Result().ok(openCourseService.page(params.getOrganIds(),params.getSchoolYear(), params.getSemester(), params.getSchoolId(),
                params.getName(), null, OpenCourseConstant.OpenCourseType.AFTER_CLASS_COURSE, params.getIsDel(),
                params.getPage(), params.getSize())), HttpStatus.OK);
    }

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

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

    @ApiOperation(value = "根据开课id查询订单项")
    @PostMapping("/{openCourseId}/order-item")
    public ResponseEntity<Result<List<OpenCourseOrderExportDto>>> findByOpenCourseId(@NotBlank @PathVariable("openCourseId") String openCourseId) {
        return new ResponseEntity(new Result().ok(openCourseOrderExportService.findByOpenCourseId(openCourseId)), HttpStatus.OK);
    }

    /**
     * 功能描述：查询[430开课]列表
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "查询[430开课]列表")
    @PostMapping("/timetable")
    public ResponseEntity<Result<String>> timetable(@Valid @RequestBody SearchOpenCourseDto params) {
        return new ResponseEntity(new Result().ok(openCourseService.findTimetable(params)), HttpStatus.OK);
    }
}
