/**
 * 
 */
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.tool.api.R;
import org.springblade.modules.system.entity.DataPermission;
import org.springblade.modules.system.entity.DataPermissionType;
import org.springblade.modules.system.service.normal.DataPermissionService;
import org.springblade.modules.system.service.normal.DataPermissionTypeService;
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.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

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

/**
 * @author wookvn
 *
 */
@RestController
@RequestMapping("/dataPermType")
@Api(value = "权限组接口", tags = "权限组分类接口")
public class DataPermissionTypeController extends BladeController {

	@Autowired
	private DataPermissionTypeService dataPermissionTypeService;
	
	@Autowired
	private DataPermissionService dataPermissionService;
	
	
	/**
	 * 
	 * @param query
	 * @param name
	 * @return
	 */
	@GetMapping("/list")
	@ApiOperation(value = "数据列表")
	public R<List<DataPermissionType>> list(DataPermissionType dataPermType){
		QueryWrapper<DataPermissionType> queryWrapper = Wrappers.query(dataPermType);
		return R.data(dataPermissionTypeService.list(queryWrapper.lambda().orderByDesc(DataPermissionType::getSortNum)));
	}
	
	/**
	 * 
	 * @return
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除数据")
	public R<String> delete(@RequestParam Integer id){
		//判断是否有权限组在使用该分类
		long count = dataPermissionService.count(Wrappers.<DataPermission>lambdaQuery().eq(DataPermission::getTypeId, id));
		if(count > 0) {
			return R.fail("有权限组正在使用该分类");
		}
		return R.status(dataPermissionTypeService.removeById(id));
	}
	
	/**
	 * 
	 * @return
	 */
	@PostMapping("/submit")
	@ApiOperation(value = "添加或修改数据")
	public R<Integer> doSubmit(@Validated @RequestBody DataPermissionType dataPermType){
		if(dataPermType.getId() == null) {
			dataPermType.setCreateUserId(getUser().getUserId());
			dataPermType.setCreateBy(getUser().getNickName());
			dataPermType.setCreateTime(new Date());
		}
		boolean ret = dataPermissionTypeService.saveOrUpdate(dataPermType);
		return ret ? R.data(dataPermType.getId()) : R.fail("操作失败");
	}
	
	/**
	 * 
	 * @return
	 */
	@PostMapping("/adjustSort")
	@ApiOperation(value = "调整顺序", notes = "只传递ID和sortNum")
	public R<String> adjustSort(@RequestBody List<DataPermissionType> dataPermTypeList) {
		return R.status(dataPermissionTypeService.updateBatchById(dataPermTypeList));
	}
	
}
