package com.mosty.common.core.business.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.mosty.common.base.constant.enums.DeletedEnum;
import com.mosty.common.base.constant.enums.MenuTypeEnum;
import com.mosty.common.base.domain.ResponseResult;
import com.mosty.common.base.entity.log.BusinessType;
import com.mosty.common.base.entity.log.Log;
import com.mosty.common.base.exception.Asserts;
import com.mosty.common.base.util.IpUtil;
import com.mosty.common.config.entity.vo.DeleteVO;
import com.mosty.common.core.business.entity.SysMenu;
import com.mosty.common.core.business.entity.vo.AddSysMenuVO;
import com.mosty.common.core.business.entity.vo.EditSysMenuVO;
import com.mosty.common.core.business.entity.vo.MenuPage;
import com.mosty.common.core.business.entity.vo.MenuQuery;
import com.mosty.common.core.business.service.SysMenuService;
import com.mosty.common.token.JwtSysUser;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author kevin
 * @since 2022-02-12
 */
@Slf4j
@RestController
@AllArgsConstructor
@SuppressWarnings("unused")
@RequestMapping("/sysMenu")
@Api(tags = "菜单信息接口")
public class SysMenuController {

    private final SysMenuService sysMenuService;

    /**
     * 添加菜单
     * @param addSysMenuVo 添加菜单请求
     * @return 添加结果
     */
    @JwtSysUser
    @PostMapping("addSysMenu")
    @Log(title = "菜单功能",businessType = BusinessType.INSERT)
    @ApiOperation(value = "添加菜单", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> addSysMenu(@RequestBody @Valid AddSysMenuVO addSysMenuVo) {

        MenuTypeEnum menuTypeEnum = MenuTypeEnum.valueOf(addSysMenuVo.getMenuType());
        Asserts.check(menuTypeEnum == null, "菜单类型有误，请检查");
      /*  Asserts.check(menuTypeEnum == MenuTypeEnum.RESOURCE && StringUtils.isNotBlank(addSysMenuVo.getMenuUrl()),
                "资源类型菜单不能配置url地址");*/

        String menuCode = addSysMenuVo.getMenuCode();
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("menu_code", menuCode);
        queryWrapper.eq("xt_zxbz", DeletedEnum.NATURE.code);
        List<SysMenu> list = sysMenuService.list(queryWrapper);
        Asserts.check(CollectionUtils.isNotEmpty(list), "菜单编码%s已存在，请修改", menuCode);

        SysMenu parentMenu = null;
        if (addSysMenuVo.getParentId() != null) {
            parentMenu = sysMenuService.getById(addSysMenuVo.getParentId());
            Asserts.check(parentMenu == null || DeletedEnum.DELETED.code.equals(parentMenu.getXtZxbz()),
                    "父菜单不存在或已删除");
        }
        // 添加菜单信息
        sysMenuService.addSysMenuInfo(addSysMenuVo, parentMenu);
        return ResponseResult.success(Boolean.TRUE);
    }

    /**
     * 修改菜单
     * @param editSysMenuVo 修改菜单请求
     * @return 修改结果
     */
    @JwtSysUser
    @Log(title = "菜单功能",businessType = BusinessType.UPDATE)
    @PostMapping("updateSysMenu")
    @ApiOperation(value = "修改菜单", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> updateSysMenu(@RequestBody @Valid EditSysMenuVO editSysMenuVo) {
        Asserts.check(editSysMenuVo.getId() == null, "主键id不能为空！");
        SysMenu oldSysMenu = sysMenuService.getBaseMapper().selectById(editSysMenuVo.getId());
        Asserts.check(oldSysMenu == null, "主键为 %s 的菜单信息不存在", editSysMenuVo.getId());

        String menuCode = editSysMenuVo.getMenuCode();
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("id", editSysMenuVo.getId());
        queryWrapper.eq("menu_code", menuCode);
        queryWrapper.eq("xt_zxbz", DeletedEnum.NATURE.code);
        List<SysMenu> list = sysMenuService.list(queryWrapper);
        Asserts.check(CollectionUtils.isNotEmpty(list), "菜单编码%s已存在，请修改", menuCode);

        oldSysMenu.setMenuName(editSysMenuVo.getMenuName());
        oldSysMenu.setMenuCode(editSysMenuVo.getMenuCode());
        oldSysMenu.setMenuUrl(editSysMenuVo.getMenuUrl());

        oldSysMenu.setOrderNo(editSysMenuVo.getOrderNo());
        oldSysMenu.setShowMode(editSysMenuVo.getShowMode());
        oldSysMenu.setBz(editSysMenuVo.getBz());

        // 获取ip地址
        String ipAddress = IpUtil.getIpAddress();

        UserInfo userInfo = UserInfoManager.get();
        Asserts.checkAuth(userInfo == null, "用户信息为空，请登陆");
        oldSysMenu.setXtZhxgsj(new Date());
        oldSysMenu.setXtZhxgrid(userInfo.getUserId().toString());
        oldSysMenu.setXtZhxgrxm(userInfo.getUserName());
        oldSysMenu.setXtZhxgrbmid(userInfo.getDeptId().toString());
        oldSysMenu.setXtZhxgrbm(userInfo.getDeptName());
        oldSysMenu.setXtZhxgip(ipAddress);

        sysMenuService.getBaseMapper().updateById(oldSysMenu);
        return ResponseResult.success(Boolean.TRUE);
    }

    /**
     * 删除菜单信息
     * @param deleteVO 菜单id
     * @return 删除结果
     */
    @PostMapping("deleteSysMenu")
    @JwtSysUser
    @Log(title = "菜单功能",businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除菜单信息", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> deleteSysMenu(@RequestBody DeleteVO deleteVO) {
        SysMenu oldSysMenu = sysMenuService.getBaseMapper().selectById(deleteVO.getId());
        Asserts.check(oldSysMenu == null, "主键为 %s 的岗位信息不存在", deleteVO.getId());

        // 获取ip地址
        String ipAddress = IpUtil.getIpAddress();

        UserInfo userInfo = UserInfoManager.get();
        Asserts.checkAuth(userInfo == null, "用户信息为空，请登陆");
        oldSysMenu.setXtZhxgsj(new Date());
        oldSysMenu.setXtZhxgrid(userInfo.getUserId().toString());
        oldSysMenu.setXtZhxgrxm(userInfo.getUserName());
        oldSysMenu.setXtZhxgrbmid(userInfo.getDeptId().toString());
        oldSysMenu.setXtZhxgrbm(userInfo.getDeptName());
        oldSysMenu.setXtZhxgip(ipAddress);

        oldSysMenu.setXtZxbz(DeletedEnum.DELETED.code);
        oldSysMenu.setXtZxyy(deleteVO.getZxyy());

        sysMenuService.getBaseMapper().updateById(oldSysMenu);
        return ResponseResult.success(Boolean.TRUE);
    }

    @GetMapping("selectPage")
    @ApiOperation(value = "分页查询菜单信息", httpMethod = "GET", response = SysMenu.class)
    public ResponseResult<IPage<SysMenu>> selectPage(MenuPage positionPage) {
        IPage<SysMenu> positionIPage = sysMenuService.selectByPage(positionPage);
        return ResponseResult.success(positionIPage);
    }

    @GetMapping("getMenuChild/{menuId}")
    @ApiOperation(value = "根据菜单id查询菜单和子页面信息", httpMethod = "GET", response = SysMenu.class)
    public ResponseResult<SysMenu> getMenuChild(@PathVariable("menuId") Long menuId) {
       SysMenu menus= sysMenuService.getMenuChild(menuId);
        return ResponseResult.success(menus);
    }

    @GetMapping("selectList")
    @ApiOperation(value = "查询菜单信息树形", httpMethod = "GET", response = SysMenu.class)
    public ResponseResult<List<SysMenu>> selectList(MenuQuery menuQuery) {
        List<SysMenu> menus = sysMenuService.selectList(menuQuery);
        return ResponseResult.success(menus);
    }

}

