package com.zksy.web.controller.gasbasic;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.domain.DrillCourse;
import com.zksy.base.mapper.DrillCourseMapper;
import com.zksy.common.annotation.Anonymous;
import com.zksy.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 培训课程 — DisCurriculum.vue
 */
@RestController
@RequestMapping("/api/drill/course")
@Api(tags = "演练管理-培训课程")
public class DrillCourseController {

    @Autowired
    private DrillCourseMapper drillCourseMapper;

    @GetMapping("/list")
    @ApiOperation("课程分页")
    @Anonymous
    public AjaxResult list(@RequestParam(defaultValue = "1") int pageNum,
                           @RequestParam(defaultValue = "20") int pageSize,
                           @RequestParam(required = false) String keyword,
                           @RequestParam(required = false) String method) {
        LambdaQueryWrapper<DrillCourse> qw = new LambdaQueryWrapper<>();
        if (keyword != null && !keyword.isEmpty()) {
            qw.like(DrillCourse::getName, keyword);
        }
        if (method != null && !method.isEmpty()) {
            qw.eq(DrillCourse::getMethod, method);
        }
        qw.orderByDesc(DrillCourse::getCreateTime);
        Page<DrillCourse> page = drillCourseMapper.selectPage(new Page<>(pageNum, pageSize), qw);
        return AjaxResult.success(page);
    }

    @PostMapping
    @ApiOperation("新增课程")
    @Anonymous
    public AjaxResult save(@RequestBody DrillCourse entity) {
        drillCourseMapper.insert(entity);
        return AjaxResult.success("新增成功", entity.getId());
    }

    @PutMapping
    @ApiOperation("修改课程")
    @Anonymous
    public AjaxResult update(@RequestBody DrillCourse entity) {
        drillCourseMapper.updateById(entity);
        return AjaxResult.success("修改成功");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除课程")
    @Anonymous
    public AjaxResult delete(@PathVariable Long id) {
        drillCourseMapper.deleteById(id);
        return AjaxResult.success("删除成功");
    }
}
