/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.sys.controller;

import java.util.List;
import java.util.Objects;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.util.R;
import com.yami.shop.sys.model.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import com.yami.shop.common.util.PageParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.sys.service.SysMenuService;
import com.yami.shop.sys.service.SysRoleService;
import com.yami.shop.common.annotation.SysLog;

import cn.hutool.core.util.StrUtil;



/**
 * 系统角色信息管理实现
 *
 * @author lgh
 * @version 1.0.0
 * @since 2018-10-30
 */
@RestController
@RequestMapping("/sys/role")
public class SysRoleController{
	@Autowired
	private SysRoleService sysRoleService;

	@Autowired
	private SysMenuService sysMenuService;

	/**
	 * 角色列表
	 */
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('sys:role:page')")
	public R<IPage<SysRole>> page(String roleName,PageParam<SysRole> page){
		IPage<SysRole> sysRoles = sysRoleService.page(page,new LambdaQueryWrapper<SysRole>()
				.like(StrUtil.isNotBlank(roleName),SysRole::getRoleName,roleName)
				.orderByDesc(SysRole::getCreateTime));
		return R.SUCCESS(sysRoles);
	}

	/**
	 * 角色列表
	 */
	@GetMapping("/list")
	@PreAuthorize("@pms.hasPermission('sys:role:list')")
	public R<List<SysRole>> list(){
		List<SysRole> list = sysRoleService.list();
		return R.SUCCESS(list);
	}

	/**
	 * 角色信息
	 */
	@GetMapping("/info/{roleId}")
	@PreAuthorize("@pms.hasPermission('sys:role:info')")
	public R<SysRole> info(@PathVariable("roleId") Long roleId){
		SysRole role = sysRoleService.getById(roleId);
		//查询角色对应的菜单
		List<Long> menuList = sysMenuService.listMenuIdByRoleId(roleId);
		role.setMenuIdList(menuList);
		return R.SUCCESS(role);
	}

	/**
	 * 保存角色
	 */
	@SysLog("保存角色")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys:role:save')")
	public R<Void> save(@RequestBody SysRole role){
		if (sysRoleService.count(new LambdaQueryWrapper<SysRole>()
				.eq(SysRole::getRoleName, role.getRoleName())) > 0) {
			throw new GlobalException("系统已存在相同角色名称");
		}

		sysRoleService.saveRoleAndRoleMenu(role);
		return R.SUCCESS();
	}

	/**
	 * 修改角色
	 */
	@SysLog("修改角色")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys:role:update')")
	public R<Void> update(@RequestBody SysRole role){

		SysRole dbSysRole = sysRoleService.getOne(new LambdaQueryWrapper<SysRole>()
				.eq(SysRole::getRoleName, role.getRoleName()));

		if (dbSysRole != null && !Objects.equals(dbSysRole.getRoleId(),dbSysRole.getRoleId())) {
			throw new GlobalException("系统已存在相同角色名称");
		}

		sysRoleService.updateRoleAndRoleMenu(role);
		return R.SUCCESS();
	}

	/**
	 * 删除角色
	 */
	@SysLog("删除角色")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('sys:role:delete')")
	public R<Void> delete(@RequestBody Long[] roleIds){
		sysRoleService.deleteBatch(roleIds);
		return R.SUCCESS();
	}
}
