package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StorePlatformMenu;
import shop.alien.entity.store.vo.StorePlatformMenuVo;
import shop.alien.store.service.StorePlatformMenuService;

/**
 * 商家菜单管理
 *
 */
@Slf4j
@Api(tags = {"平台-菜单管理"})
@ApiSort(1)
@CrossOrigin
@RestController
@RequestMapping("/platform")
@RequiredArgsConstructor
public class StorePlatformMenuController {
    private final StorePlatformMenuService storePlatformMenuService;

    @ApiOperation("分页查询菜单列表")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "页容", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "menuName", value = "菜单名称（支持模糊查询）", dataType = "String", paramType = "query")
    })
    @GetMapping("/getMenulist")
    public R<IPage<StorePlatformMenuVo>> getMenuPage(
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "size", defaultValue = "10") int size,
            @RequestParam(value = "menuName", required = false) String menuName) {
        log.info("StorePlatformMenuController.getMenuPage?page={}, size={}, menuName={}",
                page, size, menuName);
        IPage<StorePlatformMenuVo> store = storePlatformMenuService.getMenuPage(page, size, menuName);
        return R.data(store);
    }


    @ApiOperation("查询/编辑当前菜单（JSON方式）")
    @ApiOperationSupport(order = 2)
    @PostMapping(value = "/getMenuById", consumes = "application/json")
    public R<StorePlatformMenu> getMenuById(@RequestBody StorePlatformMenu storePlatformMenu) {
        log.info("StorePlatformMenuController.getMenuById(JSON)?menuId={}",
                storePlatformMenu != null ? storePlatformMenu.getMenuId() : null);
        if (storePlatformMenu == null || storePlatformMenu.getMenuId() == null) {
            log.warn("查询/编辑当前菜单信息失败：菜单ID为空");
            return R.fail("菜单ID不能为空");
        }
        
        R<StorePlatformMenu> result = storePlatformMenuService.getByMenuId(storePlatformMenu);
        if (result.isSuccess()) {
            StorePlatformMenu menu = result.getData();
            log.info("查询/编辑当前菜单信息成功，菜单ID={}, 菜单名称={}, 菜单路径={}, 菜单状态={}, 菜单层级={}, 一级菜单名称={}, 二级菜单名称={}",
                    menu != null ? menu.getMenuId() : null,
                    menu != null ? menu.getMenuName() : null,
                    menu != null ? menu.getPath() : null,
                    menu != null ? menu.getStatus() : null,
                    menu != null ? menu.getLevel() : null,
                    menu != null ? menu.getFirstLevelMenuName() : null,
                    menu != null ? menu.getSecondLevelMenuName() : null);
        } else {
            log.warn("查询/编辑当前菜单信息失败，菜单ID={}", storePlatformMenu.getMenuId());
        }
        return result;
    }


    @ApiOperation("删除当前菜单（GET方式，URL参数）")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单ID", dataType = "long", paramType = "query", required = true)
    })
    @GetMapping("/deleteMenuDetails")
    public R<StorePlatformMenu> deleteMenuDetailsByIdByGet(
            @RequestParam Long menuId) {
        log.info("StorePlatformMenuController.deleteMenuDetailsByIdByGet? menuId={}",  menuId);
        
        StorePlatformMenu storePlatformMenu = new StorePlatformMenu();
        storePlatformMenu.setMenuId(menuId);
        return storePlatformMenuService.deleteMenuDetails(storePlatformMenu);
    }


    @ApiOperation("启用禁用状态")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单ID", dataType = "long", paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "是否显示（0显示 1隐藏）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name ="level" , value = "菜单层级（1一级菜单 2二级菜单 3三级菜单）",dataType = "int", paramType = "query")
    })
    @GetMapping("/updateMenuStatus")
    public R<StorePlatformMenu> updateMenuStatus(
            @RequestParam Long menuId,
            @RequestParam String status,
            @RequestParam Integer level) {
        log.info("StorePlatformMenuController.updateMenuStatus? menuId={},status={},level={}",  menuId,status,level);

        StorePlatformMenu storePlatformMenu = new StorePlatformMenu();
        storePlatformMenu.setMenuId(menuId);
        storePlatformMenu.setStatus(status);
        storePlatformMenu.setLevel(level);
        return storePlatformMenuService.editUpdateMenuStatus(menuId,status,level);
    }

    @ApiOperation("新增菜单")
    @ApiOperationSupport(order = 5)
    @PostMapping("/addMenuStatus")
    public R<StorePlatformMenu> addMenuStatus(@RequestBody StorePlatformMenu store) {
        log.info("StorePlatformMenuController.addMenuStatus?菜单名称={}, 一级分类名称={}, 二级分类名称={}",
                store != null ? store.getMenuName() : null,
                store != null ? store.getFirstLevelMenuName() : null,
                store != null ? store.getSecondLevelMenuName() : null);
        
        if (store == null) {
            log.warn("新增菜单失败：菜单信息为空");
            return R.fail("菜单信息不能为空");
        }

        R<StorePlatformMenu> result = storePlatformMenuService.addByMenu(store);
        if (result.isSuccess()) {
            StorePlatformMenu menu = result.getData();
            log.info("新增菜单成功，菜单ID={}, 菜单名称={}, 菜单路径={}, 菜单状态={}, 菜单层级={}, 一级菜单名称={}, 二级菜单名称={}",
                    menu != null ? menu.getMenuId() : null,
                    menu != null ? menu.getMenuName() : null,
                    menu != null ? menu.getPath() : null,
                    menu != null ? menu.getStatus() : null,
                    menu != null ? menu.getLevel() : null,
                    menu != null ? menu.getFirstLevelMenuName() : null,
                    menu != null ? menu.getSecondLevelMenuName() : null);
        } else {
            log.warn("新增菜单失败，菜单名称={}", store.getMenuName());
        }
        return result;
    }

}
