package com.zksy.web.controller.dispatch;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.common.annotation.Anonymous;
import com.zksy.common.annotation.Log;
import com.zksy.common.core.domain.AjaxResult;
import com.zksy.common.enums.BusinessType;
import com.zksy.dispatch.domain.DisPlanCategory;
import com.zksy.dispatch.dto.DisPlanCategoryPageDTO;
import com.zksy.dispatch.service.DisPlanCategoryService;
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.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/dispatch/plan-category")
@Api(tags = "预案分类")
public class DisPlanCategoryController {

    @Autowired
    private DisPlanCategoryService service;

    @GetMapping("/findByPage")
    @ApiOperation("预案分类分页查询")
    @Anonymous
    public AjaxResult findByPage(DisPlanCategoryPageDTO dto) {
        Page<DisPlanCategory> page = service.findByPage(dto);
        return AjaxResult.success(page);
    }

    @GetMapping("/getById/{id}")
    @ApiOperation("根据ID查询预案分类")
    @Anonymous
    public AjaxResult getById(@PathVariable Long id) {
        DisPlanCategory entity = service.getById(id);
        return entity != null ? AjaxResult.success(entity) : AjaxResult.error("预案分类不存在");
    }

    @GetMapping("/getCategoryTree")
    @ApiOperation("获取分类树")
    @Anonymous
    public AjaxResult getCategoryTree() {
        List<DisPlanCategory> tree = service.getCategoryTree();
        return AjaxResult.success(tree);
    }

    @PostMapping("/save")
    @ApiOperation("新增预案分类")
    @Log(title = "新增预案分类", businessType = BusinessType.INSERT)
    public AjaxResult save(@RequestBody DisPlanCategory entity) {
        boolean saved = service.save(entity);
        return saved ? AjaxResult.success("新增成功", saved) : AjaxResult.error("新增失败");
    }

    @PutMapping("/updateById")
    @ApiOperation("修改预案分类")
    @Log(title = "修改预案分类", businessType = BusinessType.UPDATE)
    public AjaxResult updateById(@RequestBody DisPlanCategory entity) {
        boolean updated = service.updateById(entity);
        return updated ? AjaxResult.success("修改成功", updated) : AjaxResult.error("修改失败");
    }

    @DeleteMapping("/deleteById")
    @ApiOperation("删除预案分类")
    @Log(title = "删除预案分类", businessType = BusinessType.DELETE)
    public AjaxResult deleteById(@RequestParam Long id) {
        boolean deleted = service.removeById(id);
        return deleted ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }
}
