package com.tuoheng.controller.op;

import com.tuoheng.model.dto.LoginUser;
import com.tuoheng.model.entity.Menu;
import com.tuoheng.model.query.MenuQuery;
import com.tuoheng.service.CurrentUser;
import com.tuoheng.service.op.MenusService;
import com.tuoheng.until.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author xiaoying
 * @Date 2023/3/20 17:08
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenusService menuService;


    /**
     * 获取菜单列表
     *
     * @param menuQuery 查询条件
     * @return
     */
    @GetMapping("/index")
    public JsonResult index(MenuQuery menuQuery) {
        return menuService.getList(menuQuery);
    }

    /**
     * 添加菜单
     *
     * @param entity 实体对象
     * @return
     */
    @PostMapping("/add")
    public JsonResult add(@RequestBody Menu entity, @CurrentUser LoginUser loginUser) {
        return menuService.edit(entity, loginUser);
    }

    /**
     * 编辑菜单
     *
     * @param entity 实体对象
     * @return
     */
    @PostMapping("/edit")
    public JsonResult edit(@RequestBody Menu entity, @CurrentUser LoginUser loginUser) {
        return menuService.edit(entity, loginUser);
    }

    /**
     * 删除菜单
     *
     * @param menuId 菜单ID
     * @return
     */
    @DeleteMapping("/delete/{menuId}")
    public JsonResult delete(@PathVariable("menuId") Integer menuId) {
        boolean flag = menuService.update().eq("id", menuId).set("mark", 0).update();
        if (flag) {
            return JsonResult.success();
        } else {
            return JsonResult.error();
        }
    }
}
