package com.elephant.framework.galaxy.aroad.module.system.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.elephant.framework.galaxy.aroad.module.common.annotation.SysLog;
import com.elephant.framework.galaxy.aroad.module.common.controller.BaseController;
import com.elephant.framework.galaxy.aroad.module.system.entity.Dict;
import com.elephant.framework.galaxy.aroad.module.system.service.DictService;
import com.elephant.framework.galaxy.aroad.response.R;
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;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author wangjian
 * @since 2019-02-21
 */
@RestController
@AllArgsConstructor
@RequestMapping("/admin/dict")
public class DictController extends BaseController {
    private final DictService dictService;

    /**
     * 通过ID查询字典信息
     *
     * @param id ID
     * @return 字典信息
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        return new R(dictService.getById(id));
    }

    /**
     * 分页查询字典信息
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("/page")
    public R<IPage> getDictPage(Page page, Dict dict) {
        return new R(dictService.page(page, Wrappers.query(dict)));
    }

    /**
     * 通过字典类型查找字典
     *
     * @param type 类型
     * @return 同类型字典
     */
    @GetMapping("/type/{type}")
    @Cacheable(value = "dict_details", key = "#type")
    public R getDictByType(@PathVariable String type) {
        return new R(dictService.list(Wrappers
                .<Dict>query().lambda()
                .eq(Dict::getType, type)));
    }

    /**
     * 添加字典
     *
     * @param dict 字典信息
     * @return success、false
     */
    @SysLog("添加字典")
    @PostMapping
    @CacheEvict(value = "dict_details", key = "#dict.type")
    @PreAuthorize("@pms.hasPermission('sys_dict_add')")
    public R save(@Valid @RequestBody Dict dict) {
        return new R(dictService.save(dict));
    }

    /**
     * 删除字典，并且清除字典缓存
     *
     * @param id   ID
     * @param type 类型
     * @return R
     */
    @SysLog("删除字典")
    @DeleteMapping("/{id}/{type}")
    @CacheEvict(value = "dict_details", key = "#type")
    @PreAuthorize("@pms.hasPermission('sys_dict_del')")
    public R removeById(@PathVariable Integer id, @PathVariable String type) {
        return new R(dictService.removeById(id));
    }

    /**
     * 修改字典
     *
     * @param dict 字典信息
     * @return success/false
     */
    @PutMapping
    @SysLog("修改字典")
    @CacheEvict(value = "dict_details", key = "#dict.type")
    @PreAuthorize("@pms.hasPermission('sys_dict_edit')")
    public R updateById(@Valid @RequestBody Dict dict) {
        return new R(dictService.updateById(dict));
    }

}

