package com.css.simulation.resource.server.adapter.controller;

import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.param.system.DictParam;
import api.common.pojo.po.system.DictPO;
import api.common.pojo.vo.system.DictVO;
import com.css.simulation.resource.server.app.service.DictService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 字典数据管理
 */
@RestController
@RequestMapping("/dict")
public class DictController {

    @Resource
    private DictService dictService;

    /**
     * 通过字典类型dictTypes将字典数据刷新至缓存，当dictTypes值为all时，将刷新所有字典。
     */
    @RequestMapping("/refreshDictsByTypes")
    @PreAuthorize("@AuthorityCheck.admin()")
    public ResponseBodyVO<String> refreshDictsByTypes(@RequestBody @Validated DictParam param) {
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, dictService.refreshDicts(param));
    }

    /**
     * 通过字典类型dictTypes从缓存中查询字典数据lists
     */
    @RequestMapping("/getDictListsByTypes")
    public ResponseBodyVO<Map<String, List<DictVO>>> getDictListsByTypes(@RequestBody @Validated DictParam param) {
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, dictService.getDictListsByTypes(param));
    }

    /**
     * 通过字典类型dictTypes从缓存中查询字典数据trees
     */
    @RequestMapping("/getDictTreesByTypes")
    public ResponseBodyVO<Map<String, List<DictVO>>> getDictTreesByTypes(@RequestBody @Validated DictParam param) {
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, dictService.getDictTreesByTypes(param));
    }

    /**
     * 通过字典类型dictTypes从缓存中查询字典数据maps
     */
    @RequestMapping("/getDictMapsByTypes")
    public ResponseBodyVO<Map<String, Map<String, String>>> getDictMapsByTypes(@RequestBody @Validated DictParam param) {
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, dictService.getDictMapsByTypes(param));
    }

    /**
     * 通过 id 删除 dict
     */
    @RequestMapping("/deleteDictById")
    @PreAuthorize("@AuthorityCheck.admin()")
    public ResponseBodyVO<String> deleteDictById(@RequestBody @Validated DictPO param) {
        return dictService.deleteDictById(param);
    }

    /**
     * 新增 dict
     */
    @RequestMapping("/createDictByType")
    @PreAuthorize("@AuthorityCheck.admin()")
    public ResponseBodyVO<String> createDictByType(@RequestBody @Validated DictPO param) {
        return dictService.createDictByType(param);
    }

}
