DictionaryController.java 2.86 KB
package vion.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import vion.dto.DictionaryDTO;
import vion.dto.DictionaryTypeDTO;
import vion.model.Dictionary;
import vion.model.DictionaryType;
import vion.service.IDictionaryService;
import vion.service.IDictionaryTypeService;

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
@Slf4j
public class DictionaryController {

    private final IDictionaryService dictionaryService;
    private final IDictionaryTypeService dictionaryTypeService;
    private final Converter converter;

    @GetMapping("/dictionarys")
    @SaCheckPermission(value = "dict:list", orRole = "admin")
    public Page<Dictionary> getDictionaryList(DictionaryDTO data) {
        return dictionaryService.lambdaQuery(converter.convert(data, Dictionary.class))
                .page(Page.of(data.getPageNum(), data.getPageSize()));
    }

    @GetMapping("/dictionary")
    @SaCheckPermission(value = "dict:query", orRole = "admin")
    public Object getDictionaryById(Long id) {
        return dictionaryService.getById(id);
    }

    @PostMapping("/dictionary")
    @SaCheckPermission(value = "dict:editAndSave", orRole = "admin")
    public String saveOrUpdateDictionary(@RequestBody DictionaryDTO data) {
        Dictionary dictionary = converter.convert(data, Dictionary.class);
        return dictionaryService.saveOrUpdate(dictionary) ? "成功" : "失败";
    }

    @GetMapping("/dictionaryTypes")
    @SaCheckPermission(value = "dict:type:list", orRole = "admin")
    public Page<DictionaryType> getDictionaryTypeList(DictionaryTypeDTO data) {
        return dictionaryTypeService.lambdaQuery(converter.convert(data, DictionaryType.class))
                .page(Page.of(data.getPageNum(), data.getPageSize()));
    }

    @GetMapping("/dictionaryType")
    @SaCheckPermission(value = "dict:type:query", orRole = "admin")
    public Object getDictionaryTypeById(@RequestParam Integer id) {
        return dictionaryTypeService.getById(id);
    }

    @PostMapping("/dictionaryType")
    @SaCheckPermission(value = "dict:type:editAndSave", orRole = "admin")
    public String saveOrUpdateDictionaryType(@RequestBody DictionaryTypeDTO data) {
        DictionaryType dictionaryType = converter.convert(data, DictionaryType.class);
        return dictionaryTypeService.saveOrUpdate(dictionaryType) ? "成功" : "失败";
    }


    /**
     * 同步字典到 Redis
     *
     * @return java.lang.String
     */
    @GetMapping("/dict/sync")
    @SaCheckPermission(value = "dict:sync", orRole = "admin")
    // todo 未加权限
    public String syncDict() {
        dictionaryService.syncDict();
        return "同步成功";
    }
}