package com.starry.admin.modules.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.starry.admin.modules.system.module.entity.SysDictEntity;
import com.starry.admin.modules.system.service.ISysDictService;
import com.starry.admin.modules.system.vo.SysDictVo;
import com.starry.common.annotation.Log;
import com.starry.common.enums.BusinessType;
import com.starry.common.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 字典表Controller
 *
 * @author admin
 */
@Api(tags = "字典类型管理", description = "系统字典类型相关接口")
@RestController
@RequestMapping("/system/dict")
public class SysDictController {
    @Resource
    private ISysDictService xlDictService;

    /**
     * 查询字典表列表
     */
    @ApiOperation(value = "查询字典类型列表", notes = "分页查询字典类型列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = SysDictEntity.class, responseContainer = "Page")})
    @PreAuthorize("@customSs.hasPermission('system:dict:list')")
    @GetMapping("/list")
    public R list(@ApiParam(value = "查询条件") SysDictVo dictVo) {
        Page<SysDictEntity> list = xlDictService.selectXlDictList(dictVo);
        return R.ok(list);
    }

    /**
     * 获取字典表详细信息
     */
    @ApiOperation(value = "获取字典类型详情", notes = "根据ID获取字典类型详细信息")
    @ApiImplicitParam(name = "dictId", value = "字典ID", required = true, dataType = "Long", paramType = "path")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功", response = SysDictEntity.class)})
    @PreAuthorize("@customSs.hasPermission('system:dict:query')")
    @GetMapping(value = "/{dictId}")
    public R getInfo(@PathVariable("dictId") Long dictId) {
        return R.ok(xlDictService.selectXlDictByDictId(dictId));
    }

    /**
     * 新增字典表
     */
    @ApiOperation(value = "新增字典类型", notes = "创建新的字典类型")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 500, message = "添加失败")})
    @PreAuthorize("@customSs.hasPermission('system:dict:create')")
    @Log(title = "字典表", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public R create(@ApiParam(value = "字典类型信息", required = true) @RequestBody SysDictEntity sysDictEntity) {
        boolean success = xlDictService.create(sysDictEntity);
        if (success) {
            return R.ok();
        }
        return R.error("添加失败");
    }

    /**
     * 修改字典表
     */
    @ApiOperation(value = "修改字典类型", notes = "根据ID修改字典类型")
    @ApiImplicitParam(name = "dictId", value = "字典ID", required = true, dataType = "Long", paramType = "path")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 500, message = "修改失败")})
    @PreAuthorize("@customSs.hasPermission('system:dict:edit')")
    @Log(title = "字典表", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update/{dictId}")
    public R update(@PathVariable Long dictId,
            @ApiParam(value = "字典类型信息", required = true) @RequestBody SysDictEntity sysDictEntity) {
        sysDictEntity.setDictId(dictId);
        boolean success = xlDictService.update(sysDictEntity);
        if (success) {
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 删除字典表
     */
    @ApiOperation(value = "删除字典类型", notes = "根据ID批量删除字典类型")
    @ApiImplicitParam(name = "dictIds", value = "字典ID数组", required = true, dataType = "Long[]", paramType = "path")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功", response = Boolean.class)})
    @PreAuthorize("@customSs.hasPermission('system:dict:remove')")
    @Log(title = "字典表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public R remove(@PathVariable Long[] dictIds) {
        return R.ok(xlDictService.deleteXlDictByDictIds(dictIds));
    }

    @ApiOperation(value = "获取字典选择列表", notes = "获取所有字典类型的下拉选择数据")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = SysDictEntity.class, responseContainer = "List")})
    @GetMapping("/optionselect")
    public R optionselect() {
        List<SysDictEntity> list = xlDictService.selectDictTypeAll();
        return R.ok(list);
    }

    /**
     * 刷新字典缓存
     */
    @ApiOperation(value = "刷新字典缓存", notes = "清除并重新加载字典缓存")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @PreAuthorize("@customSs.hasPermission('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public R refreshCache() {
        xlDictService.resetDictCache();
        return R.ok();
    }
}
