package com.zcloud.modules.sys.controller;

import com.zcloud.common.annotation.SysLog;
import com.zcloud.common.exception.ZException;
import com.zcloud.common.utils.Constant;
import com.zcloud.common.utils.PageUtils;
import com.zcloud.common.utils.R;
import com.zcloud.modules.sys.entity.SysDictionariesEntity;
import com.zcloud.modules.sys.service.ShiroService;
import com.zcloud.modules.sys.service.SysDictionariesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 数据字典管理
 *
 */
@RestController
@RequestMapping("/sys/dictionaries")
@Api(tags="数据字典管理")
public class SysDictionariesController extends AbstractController {
	@Autowired
	private SysDictionariesService sysDictionariesService;

	/**
	 * 保存
	 */
	@PostMapping("/save")
	//@RequiresPermission("sys:dictionaries:save")
	@ApiOperation(value = "保存字典")
	public R save(@RequestBody SysDictionariesEntity dictionaries){

		sysDictionariesService.save(dictionaries);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	//@RequiresPermission("sys:dictionaries:update")
	@ApiOperation(value = "修改字典")
	public R update(@RequestBody SysDictionariesEntity dictionaries){

		sysDictionariesService.updateById(dictionaries);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping("/delete")
	//@RequiresPermission("sys:dictionaries:delete")
	@ApiOperation(value = "删除字典")
	public R delete(@RequestBody SysDictionariesEntity dictionaries){
		//判断是否有下级字典
		List<SysDictionariesEntity> dictionariesList = sysDictionariesService.listAllCte(dictionaries.getDictionariesId());
		if(dictionariesList.size() > 0){
			return R.error(200,"请先删除下级字典");
		}

		sysDictionariesService.delete(dictionaries.getDictionariesId());

		return R.ok();
	}

	/**
	 * 所有字典列表
	 */
	@PostMapping("/findByBianma")
	@ApiOperation(value = "按编码查询")
	public R findByBianma(@RequestBody SysDictionariesEntity dictionaries){
		dictionaries = sysDictionariesService.queryByBianma(dictionaries.getBianma());
		return R.ok().put("dictionaries", dictionaries);
	}

	/**
	 * 分页字典列表
	 */
	@PostMapping("/listPage")
	//@RequiresPermission("sys:dictionaries:list")
	@ApiOperation(value = "分页查询字典列表")
	public R listPage(@RequestBody Map<String, Object> params){

		PageUtils page = sysDictionariesService.queryPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 查询字典列表
	 */
	@PostMapping("/list")
	//@RequiresPermission("sys:dictionaries:list")
	@ApiOperation(value = "查询字典列表")
	public R list(@RequestBody SysDictionariesEntity dictionaries){
		List<SysDictionariesEntity> dictionariesList = sysDictionariesService.listTreeCte(dictionaries.getParentId());
		return R.ok().put("dictionariesList", dictionariesList);
	}

	/**
	 * 查询字典列表(树)
	 */
	@PostMapping("/listTree")
	//@RequiresPermission("sys:dictionaries:list")
	@ApiOperation(value = "查询字典列表(树)")
	public R listTree(@RequestBody SysDictionariesEntity dictionaries){
		List<SysDictionariesEntity> dictionariesList = sysDictionariesService.listTreeCte(dictionaries.getParentId());

		return R.ok().put("dictionariesList", dictionariesList);
	}

	/**
	 * 字典信息
	 */
	@PostMapping("/info")
	//@RequiresPermission("sys:dictionaries:info")
	@ApiOperation(value = "查看字典详情")
	public R info(
			@ApiParam(name = "dictionariesId", value = "主键", required = true) @RequestBody SysDictionariesEntity dictionaries){
		dictionaries = sysDictionariesService.getById(dictionaries.getDictionariesId());
		return R.ok().put("dictionaries", dictionaries);
	}
}

