package com.lantone.security.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lantone.common.api.CommonResult;
import com.lantone.security.entity.DictionaryInfo;
import com.lantone.security.facade.DictionaryManagementFacade;
import com.lantone.security.vo.GetDictionaryVO;
import com.lantone.security.vo.SaveDictionaryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

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

/**
 * @Description: 字典管理API
 * @author: cy
 * @time: 2021/7/20 12:39
 */
@RestController
@Api(value = "字典管理API", tags = { "字典管理API" })
@RequestMapping("/dictionaryManage")
public class DictionaryManagementController {
    @Autowired
    private DictionaryManagementFacade dictionaryManagementFacade;

    @ApiOperation(value = "新增字典[by:cy]")
    @PostMapping("/addDictionary")
    @Transactional
    public CommonResult<Object> addDictionary(@RequestBody @Valid SaveDictionaryVO saveDictionaryVO) {
        return CommonResult.success(dictionaryManagementFacade.addDictionary(saveDictionaryVO));
    }

    @ApiOperation(value = "修改字典[by:cy]")
    @PostMapping("/updateDictionary")
    @Transactional
    public CommonResult<Object> updateDictionary(@RequestBody @Valid SaveDictionaryVO saveDictionaryVO) {
        return CommonResult.success(dictionaryManagementFacade.updateDictionary(saveDictionaryVO));
    }

    @ApiOperation(value = "查看字典管理[by:cy]")
    @PostMapping("/getDictionary")
    public CommonResult<IPage<DictionaryInfo>> getDictionary(@RequestBody GetDictionaryVO getDictionaryVO) {
        return CommonResult.success(dictionaryManagementFacade.getDictionary(getDictionaryVO));
    }

    @ApiOperation(value = "删除字典管理[by:cy]")
    @PostMapping("/deleteDictionary")
    @Transactional
    public CommonResult<Boolean> deleteDictionary(@RequestParam @Valid @NotNull(message = "字典id为空") Integer id) {
        return CommonResult.success(dictionaryManagementFacade.deleteDictionary(id));
    }

    @ApiOperation(value = "查看字典管理代码类别[by:cy]")
    @PostMapping("/getCodeCategory")
    public CommonResult<List<String>> getCodeCategory(@RequestParam("groupType") String groupType) {
        return CommonResult.success(dictionaryManagementFacade.getCodeCategory(groupType));
    }

    @ApiOperation(value = "加载所有系统字典信息[by:cy]")
    @PostMapping("/initDicMap")
    @ApiIgnore
    public CommonResult<Boolean> initDicMap() {
        return CommonResult.success(dictionaryManagementFacade.initDicMap());
    }

}