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.CashMenu;
import com.czg.account.entity.QuickMenu;
import com.czg.account.entity.SysMenu;
import com.czg.account.service.QuickMenuService;
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 com.czg.sa.StpKit;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
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;
    @Resource
    private QuickMenuService quickMenuService;

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


    /**
     * 收银机菜单
     *
     * @return 所有菜单
     */
    @GetMapping("/list/cash")
    public CzgResult<List<CashMenu>> all() {
        return CzgResult.success(menuService.getCashAll());
    }

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

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

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

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

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

}
