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

import cn.hutool.core.collection.CollectionUtil;
import com.starry.admin.modules.system.module.entity.SysDictDataEntity;
import com.starry.admin.modules.system.service.ISysDictDataService;
import com.starry.admin.modules.system.vo.SysDictDataVo;
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.ArrayList;
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/data")
public class SysDictDataController {
    @Resource
    private ISysDictDataService xlDictDataService;

    /**
     * 查询字典数据表列表
     */
    @ApiOperation(value = "查询字典数据列表", notes = "根据条件查询字典数据项列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = SysDictDataEntity.class, responseContainer = "Page")})
    @PreAuthorize("@customSs.hasPermission('system:dict:list')")
    @GetMapping("/list")
    public R list(@ApiParam(value = "查询条件") SysDictDataVo sysDictDataEntity) {
        return R.ok(xlDictDataService.selectXlDictDataList(sysDictDataEntity));
    }

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

    /**
     * 新增字典数据表
     */
    @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 SysDictDataEntity sysDictDataEntity) {
        boolean success = xlDictDataService.create(sysDictDataEntity);
        if (success) {
            return R.ok();
        }
        return R.error("添加失败");
    }

    /**
     * 修改字典数据表
     */
    @ApiOperation(value = "修改字典数据", notes = "根据ID修改字典数据项")
    @ApiImplicitParam(name = "dictDataId", 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/{dictDataId}")
    public R update(@PathVariable Long dictDataId,
            @ApiParam(value = "字典数据信息", required = true) @RequestBody SysDictDataEntity sysDictDataEntity) {
        sysDictDataEntity.setDictDataId(dictDataId);
        boolean success = xlDictDataService.update(sysDictDataEntity);
        if (success) {
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 删除字典数据表
     */
    @ApiOperation(value = "删除字典数据", notes = "根据ID批量删除字典数据项")
    @ApiImplicitParam(name = "dictDataIds", 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("/{dictDataIds}")
    public R remove(@PathVariable Long[] dictDataIds) {
        return R.ok(xlDictDataService.deleteXlDictDataByDictDataIds(dictDataIds));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @ApiOperation(value = "根据类型查询字典数据", notes = "根据字典类型查询对应的字典数据项列表")
    @ApiImplicitParam(name = "dictType", value = "字典类型", required = true, dataType = "String", paramType = "path")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = SysDictDataEntity.class, responseContainer = "List")})
    @GetMapping(value = "/type/{dictType}")
    public R getDictDataByType(@PathVariable String dictType) {
        List<SysDictDataEntity> list = xlDictDataService.selectDictDataByType(dictType);
        if (CollectionUtil.isEmpty(list)) {
            list = new ArrayList<>();
        }
        return R.ok(list);
    }
}
