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

import cn.hutool.core.collection.CollectionUtil;
import com.starry.admin.modules.system.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 org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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


/**
 * 字典数据表Controller
 *
 * @author admin
 */
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController {
    @Resource
    private ISysDictDataService xlDictDataService;

    /**
     * 查询字典数据表列表
     */
    @PreAuthorize("@customSs.hasPermission('system:dict:list')")
    @GetMapping("/list")
    public R list(SysDictDataVo sysDictDataEntity) {
        return R.ok(xlDictDataService.selectXlDictDataList(sysDictDataEntity));
    }


    /**
     * 获取字典数据表详细信息
     */
    @PreAuthorize("@customSs.hasPermission('system:dict:query')")
    @GetMapping(value = "/{dictDataId}")
    public R getInfo(@PathVariable("dictDataId") Long dictDataId) {
        return R.ok(xlDictDataService.selectXlDictDataByDictDataId(dictDataId));
    }

    /**
     * 新增字典数据表
     */
    @PreAuthorize("@customSs.hasPermission('system:dict:create')")
    @Log(title = "字典数据表", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public R create(@RequestBody SysDictDataEntity sysDictDataEntity) {
        boolean success = xlDictDataService.create(sysDictDataEntity);
        if (success) {
            return R.ok();
        }
        return R.error("添加失败");
    }

    /**
     * 修改字典数据表
     */
    @PreAuthorize("@customSs.hasPermission('system:dict:edit')")
    @Log(title = "字典数据表", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update/{dictDataId}")
    public R update(@PathVariable Long dictDataId, @RequestBody SysDictDataEntity sysDictDataEntity) {
        sysDictDataEntity.setDictDataId(dictDataId);
        boolean success = xlDictDataService.update(sysDictDataEntity);
        if (success) {
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 删除字典数据表
     */
    @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));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @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);
    }
}
