package cn.qinys.platform.upms.api.controller;


import cn.qinys.platform.base.response.Result;
import cn.qinys.platform.base.security.annotation.HasPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.qinys.platform.entity.upms.Dictionary;
import cn.qinys.platform.entity.upms.DictionaryOption;
import cn.qinys.platform.upms.api.req.*;
import cn.qinys.platform.upms.api.service.IDictionaryService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

/**
 * Description: 字典
 *
 * @author tanlie
 * Date: 2024/2/8 16:48
 */
@RestController
@RequestMapping("/upms/dictionary")
public class DictionaryController {

    @Resource
    IDictionaryService dictionaryService;

    /**
     * 字典列表
     *
     * @return
     */
    @GetMapping("/query-dictionaries")
    public Result<Map<Object, Object>> queryDictionaries() {
        Map<Object, Object> dict = dictionaryService.getDictionaries();
        return new Result<>(dict);
    }

    /**
     * 字典分页
     *
     * @param req
     * @return
     */
    @GetMapping("/page")
    public Result<Page<Dictionary>> queryDictionariesPage(DictionariesPageReq req) {
        Page<Dictionary> dict = dictionaryService.getDictionaryPage(req);
        return new Result<>(dict);
    }

    /**
     * 字典新增
     *
     * @param req
     * @return
     */
    @HasPermission({"dev.dictionary"})
    @PostMapping("/create")
    public Result<String> createDictionary(@RequestBody @Valid DictionaryCreateReq req) {
        dictionaryService.createDictionary(req);
        return new Result<>("操作成功");
    }

    /**
     * 字典刷新
     *
     * @return
     */
    @HasPermission({"dev.dictionary"})
    @PostMapping("/refresh")
    public Result<String> refreshDictionary() {
        dictionaryService.refreshDictionary();
        return new Result<>("操作成功");
    }

    /**
     * 字典刷新
     *
     * @param req
     * @return
     */
    @HasPermission({"dev.dictionary"})
    @PostMapping("/update")
    public Result<String> updateDictionary(@RequestBody @Valid DictionaryUpdateReq req) {
        dictionaryService.updateDictionary(req);
        return new Result<>("操作成功");
    }

    /**
     * 字典删除
     *
     * @param req
     * @return
     */
    @HasPermission({"dev.dictionary"})
    @PostMapping("/remove")
    public Result<String> removeDictionary(@RequestBody @Valid DictionaryRemoveReq req) {
        dictionaryService.removeDictionary(req);
        return new Result<>("操作成功");
    }

    /**
     * 字典选项列表
     *
     * @param req
     * @return
     */
    @GetMapping("/options")
    public Result<List<DictionaryOption>> getOptionsList(@Valid DictionaryOptionListReq req) {
        List<DictionaryOption> options = dictionaryService.getOptionList(req);
        return new Result<>(options);
    }


    /**
     * 字典选项新增
     *
     * @param req
     * @return
     */
    @PostMapping("/option/create")
    public Result<String> createOption(@RequestBody @Valid DictionaryOptionCreateReq req) {
        dictionaryService.optionCreate(req);
        return new Result<>("操作成功");
    }

    /**
     * 字典选项更新
     *
     * @param req
     * @return
     */
    @PostMapping("/option/update")
    public Result<String> updateOption(@RequestBody @Valid DictionaryOptionUpdateReq req) {
        dictionaryService.optionUpdate(req);
        return new Result<>("操作成功");
    }

    /**
     * 字典删除
     *
     * @param req
     * @return
     */
    @PostMapping("/option/remove")
    public Result<String> removeOption(@RequestBody @Valid DictionaryOptionRemoveReq req) {
        dictionaryService.optionRemove(req);
        return new Result<>("操作成功");
    }

}
