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.DrillPlan;
import com.zksy.base.mapper.DrillPlanMapper;
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.*;

@RestController
@RequestMapping("/api/drill/plan")
@Api(tags = "演练管理-演练计划")
public class DrillPlanController {

    @Autowired
    private DrillPlanMapper drillPlanMapper;

    @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 category) {
        LambdaQueryWrapper<DrillPlan> qw = new LambdaQueryWrapper<>();
        if (keyword != null && !keyword.isEmpty()) qw.like(DrillPlan::getName, keyword);
        if (category != null && !category.isEmpty()) qw.eq(DrillPlan::getCategory, category);
        qw.orderByDesc(DrillPlan::getCreateTime);
        return AjaxResult.success(drillPlanMapper.selectPage(new Page<>(pageNum, pageSize), qw));
    }

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

    @PutMapping @ApiOperation("修改") @Anonymous
    public AjaxResult update(@RequestBody DrillPlan entity) {
        drillPlanMapper.updateById(entity);
        return AjaxResult.success("修改成功");
    }

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