package com.czg.controller.admin;

import com.czg.account.dto.menu.MenuAddDTO;
import com.czg.account.dto.menu.MenuDelDTO;
import com.czg.account.dto.menu.MenuEditDTO;
import com.czg.account.entity.SysMenu;
import com.czg.account.service.SysMenuService;
import com.czg.account.vo.MenuVO;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.annotation.SaAdminCheckRole;
import com.czg.resp.CzgResult;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单管理
 * @author zs
 */
@RestController
@RequestMapping("/admin/menus")
public class MenuController {

    @Resource
    private SysMenuService menuService;

    /**
     * 获取当前用户菜单列表
     * @return 菜单结构
     */
    @GetMapping
    public CzgResult<List<MenuVO>> get() {
        return CzgResult.success(menuService.getMenu());
    }

    /**
     * 获取所有菜单
     * @return 菜单结构
     */
    @SaAdminCheckRole("管理员")
    @SaAdminCheckPermission(value = "menu:list", name = "菜单列表")
    @GetMapping("/list")
    public CzgResult<List<MenuVO>> all(String title, String startTime, String endTime) {
        return CzgResult.success(menuService.getAll(title, startTime, endTime));
    }

    /**
     * 菜单详情
     * @return 菜单结构
     */
    @SaAdminCheckRole("管理员")
    @SaAdminCheckPermission(value = "menu:detail", name = "菜单详情")
    @GetMapping("/detail")
    public CzgResult<SysMenu> detail(@RequestParam Integer id) {
        return CzgResult.success(menuService.getById(id));
    }

    /**
     * 菜单添加
     * @return 是否成功
     */
    @SaAdminCheckRole("管理员")
    @SaAdminCheckPermission(value = "menu:add", name = "菜单添加")
    @PostMapping
    public CzgResult<Boolean> add(@RequestBody @Validated MenuAddDTO menuAddDTO) {
        return CzgResult.success(menuService.add(menuAddDTO));
    }

    /**
     * 菜单修改
     * @return 是否成功
     */
    @SaAdminCheckRole("管理员")
    @SaAdminCheckPermission(value = "menu:edit", name = "菜单编辑")
    @PutMapping()
    public CzgResult<Boolean> edit(@RequestBody @Validated MenuEditDTO menuEditDTO) {
        return CzgResult.success(menuService.edit(menuEditDTO));
    }

    /**
     * 菜单删除
     * @return 是否成功
     */
    @SaAdminCheckRole("管理员")
    @SaAdminCheckPermission(value = "menu:del", name = "菜单删除")
    @DeleteMapping()
    public CzgResult<Boolean> edit(@RequestBody @Validated MenuDelDTO menuDelDTO) {
        return CzgResult.success(menuService.removeById(menuDelDTO.getId()));
    }

}
