package com.yida.data.user.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.annotation.ControllerLog;
import com.yida.data.common.core.common.ModuleName;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.system.enums.OperationLogTypeEnum;
import com.yida.data.common.core.entity.user.EduCourse;
import com.yida.data.common.core.utils.Asserts;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.log.annotation.OperationLog;
import com.yida.data.user.service.EduCourseService;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import javax.annotation.Resource;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "课程")
@RestController
@RequestMapping("/course")
public class EduCourseController {
    @Resource
    private EduCourseService eduCourseService;

    @ApiOperation("根据学校查询课程")
    @GetMapping("/listCourse")
    public ResultBean<List<EduCourse>> listCourse(@ApiParam("学校id") @RequestParam Long schoolId) {
        return ResultBean.buildSuccess(eduCourseService.list(Wrappers.lambdaQuery(new EduCourse()).eq(EduCourse::getSchoolId, schoolId)));
    }

    @ApiOperation("添加课程")
    @PostMapping("/saveCourse")
    @OperationLog(module = ModuleName.COURSE, methods = "添加课程", type = OperationLogTypeEnum.INSERT)
    public ResultBean saveCourse(@RequestBody EduCourse course) {
        Asserts.isNotNull(course.getCourseName(), "请输入科目名称");
        Asserts.isTrue(eduCourseService.count(Wrappers.<EduCourse>lambdaQuery()
                .eq(EduCourse::getSchoolId, course.getSchoolId())
                .eq(EduCourse::getCourseName, course.getCourseName())) == 0, "已存在该科目");
        eduCourseService.saveOrUpdate(course);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("删除课程")
    @PostMapping("/delCourse")
    @OperationLog(module = ModuleName.COURSE, methods = "删除课程", type = OperationLogTypeEnum.DELETE)
    public ResultBean delCourse(@RequestBody List<Long> ids) {
        eduCourseService.removeByIds(ids);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("初始化基础课程")
    @GetMapping("/initCourse")
    public ResultBean initCourse(Long schoolId) {
        eduCourseService.initCourse(schoolId, FebsUtil.getCurrentUser());
        return ResultBean.buildSuccess();
    }

    @GetMapping("/getCourseByName")
    ResultBean<EduCourse> getCourseByName(@RequestParam("courseName") String courseName,
                                          @RequestParam("schoolId") Long schoolId) {
        return ResultBean.buildSuccess(eduCourseService.getOne(Wrappers.lambdaQuery(new EduCourse())
                .eq(EduCourse::getCourseName, courseName)
                .eq(EduCourse::getSchoolId, schoolId)));
    }
}
