package com.ruoyi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDictType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cs.domain.CsResourceDictType;
import com.ruoyi.cs.service.ICsResourceDictTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 资源字典类型Controller
 * 
 * @author ruoyi
 * @date 2024-04-29
 */
@RestController
@RequestMapping("/cs/csResourceDictType")
public class CsResourceDictTypeController extends BaseController
{
    @Autowired
    private ICsResourceDictTypeService csResourceDictTypeService;

    /**
     * 查询资源字典类型列表
     */
    @PreAuthorize("@ss.hasPermi('cs:csResourceDictType:list')")
    @GetMapping("/list")
    public TableDataInfo list(CsResourceDictType csResourceDictType)
    {
        startPage();
        List<CsResourceDictType> list = csResourceDictTypeService.selectCsResourceDictTypeList(csResourceDictType);
        return getDataTable(list);
    }

    /**
     * 导出资源字典类型列表
     */
    @PreAuthorize("@ss.hasPermi('cs:csResourceDictType:export')")
    @Log(title = "资源字典类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CsResourceDictType csResourceDictType)
    {
        List<CsResourceDictType> list = csResourceDictTypeService.selectCsResourceDictTypeList(csResourceDictType);
        ExcelUtil<CsResourceDictType> util = new ExcelUtil<CsResourceDictType>(CsResourceDictType.class);
        util.exportExcel(response, list, "资源字典类型数据");
    }

    /**
     * 获取资源字典类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('cs:csResourceDictType:query')")
    @GetMapping(value = "/{dictId}")
    public AjaxResult getInfo(@PathVariable("dictId") Long dictId)
    {
        return AjaxResult.success(csResourceDictTypeService.selectCsResourceDictTypeByDictId(dictId));
    }

    /**
     * 新增资源字典类型
     */
    @PreAuthorize("@ss.hasPermi('cs:csResourceDictType:add')")
    @Log(title = "资源字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CsResourceDictType csResourceDictType)
    {
        if (UserConstants.NOT_UNIQUE.equals(csResourceDictTypeService.checkDictTypeUnique(csResourceDictType)))
        {
            return AjaxResult.error("新增字典'" + csResourceDictType.getDictName() + "'失败，字典类型已存在");
        }
        csResourceDictType.setCreateBy(getUsername());
        return toAjax(csResourceDictTypeService.insertCsResourceDictType(csResourceDictType));
    }

    /**
     * 修改资源字典类型
     */
    @PreAuthorize("@ss.hasPermi('cs:csResourceDictType:edit')")
    @Log(title = "资源字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CsResourceDictType csResourceDictType)
    {
        if (UserConstants.NOT_UNIQUE.equals(csResourceDictTypeService.checkDictTypeUnique(csResourceDictType)))
        {
            return AjaxResult.error("修改字典'" + csResourceDictType.getDictName() + "'失败，字典类型已存在");
        }
        csResourceDictType.setUpdateBy(getUsername());
        return toAjax(csResourceDictTypeService.updateCsResourceDictType(csResourceDictType));
    }

    /**
     * 删除资源字典类型
     */
    @PreAuthorize("@ss.hasPermi('cs:csResourceDictType:remove')")
    @Log(title = "资源字典类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dictIds}")
    public AjaxResult remove(@PathVariable Long[] dictIds)
    {
        csResourceDictTypeService.deleteCsResourceDictTypeByDictIds(dictIds);
        return success();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<CsResourceDictType> dictTypes = csResourceDictTypeService.selectDictTypeAll();
        return AjaxResult.success(dictTypes);
    }
}
