package com.lutao.cloud.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lutao.cloud.admin.api.entity.SysDict;
import com.lutao.cloud.admin.api.entity.SysDictItem;
import com.lutao.cloud.admin.service.SysDictItemService;
import com.lutao.cloud.admin.service.SysDictService;
import com.lutao.cloud.common.core.constant.CacheConstants;
import com.lutao.cloud.common.core.util.MessageInfo;
import com.lutao.cloud.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *

 * @since 2019-03-19
 */
@Api(value = "/dict", tags ={"字典表"})
@RestController
@AllArgsConstructor
@RequestMapping("/dict")
public class SysDictController{

	private final SysDictService sysDictService;

	private final SysDictItemService sysDictItemService;

	/**
	 * 通过ID查询字典信息
	 * @param id ID
	 * @return 字典信息
	 */
	@ApiOperation(value = "通过ID查询字典信息", notes = "通过ID查询字典信息", httpMethod = "GET")
	@GetMapping("/{id}")
	public MessageInfo<SysDict> getById(@PathVariable Integer id){
		return MessageInfo.ok(sysDictService.getById(id));
	}

	/**
	 * 分页查询字典信息
	 * @param page 分页对象
	 * @return 分页对象
	 */
	@ApiOperation(value = "分页查询字典信息", notes = "分页查询字典信息", httpMethod = "GET")
	@GetMapping("/page")
	public MessageInfo<IPage> getDictPage(Page page, SysDict sysDict){
		return MessageInfo.ok(sysDictService.page(page, Wrappers.query(sysDict)));
	}

	/**
	 * 通过字典类型查找字典
	 * @param type 类型
	 * @return 同类型字典
	 */
	@ApiOperation(value = "通过字典类型查找字典", notes = "通过字典类型查找字典", httpMethod = "GET")
	@GetMapping("/type/{type}")
	@Cacheable(value = CacheConstants.DICT_DETAILS, key = "#type", unless = "#result.data.isEmpty()")
	public MessageInfo<List<SysDictItem>> getDictByType(@PathVariable String type){
		return MessageInfo.ok(sysDictItemService.list(Wrappers.<SysDictItem>query().lambda().eq(SysDictItem::getType, type)));
	}

	/**
	 * 添加字典
	 * @param sysDict 字典信息
	 * @return success、false
	 */
	@ApiOperation(value = "添加字典", notes = "添加字典", httpMethod = "POST")
	@SysLog("添加字典")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_dict_add')")
	public MessageInfo<Boolean> save(@Valid @RequestBody SysDict sysDict){
		return MessageInfo.ok(sysDictService.save(sysDict));
	}

	/**
	 * 删除字典，并且清除字典缓存
	 * @param id ID
	 * @return R
	 */
	@ApiOperation(value = "删除字典，并且清除字典缓存", notes = "删除字典，并且清除字典缓存", httpMethod = "DELETE")
	@SysLog("删除字典")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_dict_del')")
	public MessageInfo<Boolean> removeById(@PathVariable Integer id){
		return sysDictService.removeDict(id);
	}

	/**
	 * 修改字典
	 * @param sysDict 字典信息
	 * @return success/false
	 */
	@ApiOperation(value = "修改字典", notes = "修改字典", httpMethod = "PUT")
	@PutMapping
	@SysLog("修改字典")
	@PreAuthorize("@pms.hasPermission('sys_dict_edit')")
	public MessageInfo<Boolean> updateById(@Valid @RequestBody SysDict sysDict){
		return sysDictService.updateDict(sysDict);
	}

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param sysDictItem 字典项
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询", httpMethod = "GET")
	@GetMapping("/item/page")
	public MessageInfo<IPage> getSysDictItemPage(Page page, SysDictItem sysDictItem){
		return MessageInfo.ok(sysDictItemService.page(page, Wrappers.query(sysDictItem)));
	}

	/**
	 * 通过id查询字典项
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询字典项", notes = "通过id查询字典项", httpMethod = "GET")
	@GetMapping("/item/{id}")
	public MessageInfo<SysDictItem> getDictItemById(@PathVariable("id") Integer id){
		return MessageInfo.ok(sysDictItemService.getById(id));
	}

	/**
	 * 新增字典项
	 * @param sysDictItem 字典项
	 * @return R
	 */
	@ApiOperation(value = "新增字典项", notes = "新增字典项", httpMethod = "POST")
	@SysLog("新增字典项")
	@PostMapping("/item")
	@CacheEvict(value = CacheConstants.DICT_DETAILS, allEntries = true)
	public MessageInfo<Boolean> save(@RequestBody SysDictItem sysDictItem){
		return MessageInfo.ok(sysDictItemService.save(sysDictItem));
	}

	/**
	 * 修改字典项
	 * @param sysDictItem 字典项
	 * @return R
	 */
	@ApiOperation(value = "修改字典项", notes = "修改字典项", httpMethod = "PUT")
	@SysLog("修改字典项")
	@PutMapping("/item")
	public MessageInfo<Boolean> updateById(@RequestBody SysDictItem sysDictItem){
		return sysDictItemService.updateDictItem(sysDictItem);
	}

	/**
	 * 通过id删除字典项
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除字典项", notes = "通过id删除字典项", httpMethod = "DELETE")
	@SysLog("删除字典项")
	@DeleteMapping("/item/{id}")
	public MessageInfo<Boolean> removeDictItemById(@PathVariable Integer id){
		return sysDictItemService.removeDictItem(id);
	}

}
