/**
 * 
 */
package org.springblade.modules.system.controller;

import java.util.Date;
import java.util.List;

import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.system.entity.DataPermission;
import org.springblade.modules.system.service.normal.DataPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author wookvn
 *
 */
@RestController
@RequestMapping("/dataPerm")
@Api(value = "权限组接口", tags = "权限组相关接口")
public class DataPermissionController extends BladeController {

	@Autowired
	private DataPermissionService dataPermissionService;
	
	/**
	 * 
	 * @param query
	 * @param name
	 * @return
	 */
	@GetMapping("/list")
	@ApiOperation(value = "分页数据列表")
	public R<IPage<DataPermission>> list(DataPermission dataPerm, Query query){
		QueryWrapper<DataPermission> queryWrapper = Wrappers.query(dataPerm);
		return R.data(dataPermissionService.page(Condition.getPage(query), queryWrapper.lambda().orderByDesc(DataPermission::getSuperStatus).orderByDesc(DataPermission::getId)));
	}
	
	/**
	 * 
	 * @return
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除数据")
	public R<String> delete(@RequestParam Integer permId){
		//TODO 删除之前校验是否有使用
		DataPermission dataPermission = dataPermissionService.getById(permId);
		if(dataPermission.getSuperStatus() == 1) {
			return R.fail("超级权限组不能删除。");
		}
		return R.status(dataPermissionService.doRemove(permId));
	}
	
	/**
	 * 
	 * @return
	 */
	@PostMapping("/submit")
	@ApiOperation(value = "添加或修改数据")
	public R<String> doSubmit(@Validated @RequestBody DataPermission dataPerm){
		if(dataPerm.getId() == null) {
			dataPerm.setCreateUserId(getUser().getUserId());
			dataPerm.setCreateTime(new Date());
		}else {
//			DataPermission dataPermission = dataPermissionService.getById(dataPerm.getId());
//			if(dataPermission.getSuperStatus() == 1) {
//				return R.fail("超级权限组不能编辑。");
//			}
		}
		return R.status(dataPermissionService.doSaveOrUpdate(dataPerm));
	}
	
	/**
	 * 
	 * @return
	 */
	@GetMapping("/ownerList")
	@ApiOperation(value = "查询当前登录用户的权限组列表")
	public R<List<DataPermission>> ownerList(){
		if(AuthUtil.isAdmin()) {
			return R.data(dataPermissionService.list());
		}else {
			//去掉超级权限组
			return R.data(dataPermissionService.listOwner(getUser().getUserId()));
		}
	}
	
	/**
	 * 
	 * @return
	 */
	@PostMapping("/tagList")
	@ApiOperation(value = "标签列表")
	@Deprecated
	public R<List<String>> tagList(){
		LambdaQueryWrapper<DataPermission> queryWrapper = Wrappers.<DataPermission>lambdaQuery();
		queryWrapper.select(DataPermission::getPermTag).groupBy(DataPermission::getPermTag);
		return R.data(dataPermissionService.listObjs(queryWrapper, t -> t.toString()));
	}
}
