package com.ruoyi.controller;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysDictDataWithChildren;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.cs.domain.dto.DictParamDTO;
import com.ruoyi.cs.domain.vo.CsResourceDictDataVO;
import com.ruoyi.cs.domain.vo.CsResourceDictDataWithChildren;
import com.ruoyi.cs.service.ICsResourceDictTypeService;
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.CsResourceDictData;
import com.ruoyi.cs.service.ICsResourceDictDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 资源字典数据Controller
 *
 * @author ruoyi
 * @date 2024-04-29
 */
@RestController
@RequestMapping("/cs/csResourceDictData")
public class CsResourceDictDataController extends BaseController
{
    @Autowired
    private ICsResourceDictDataService csResourceDictDataService;

    @Resource
    private ICsResourceDictTypeService csResourceDictTypeService;

    @PreAuthorize("@ss.hasPermi('cs:csResourceDictData:list')")
    @GetMapping("/treelist")
    public TableDataInfo treelist(String dictType)
    {
        startPage();
        List<CsResourceDictDataWithChildren> list = csResourceDictDataService.selectDictDataTreeList(dictType);
        return getDataTable(list);
    }

    /**
     * 查询资源字典数据列表
     */
    @PreAuthorize("@ss.hasPermi('cs:csResourceDictData:list')")
    @GetMapping("/list")
    public TableDataInfo list(CsResourceDictData csResourceDictData)
    {
        startPage();
        List<CsResourceDictData> list = csResourceDictDataService.selectCsResourceDictDataList(csResourceDictData);
        return getDataTable(list);
    }

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

    /**
     * 获取资源字典数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('cs:csResourceDictData:query')")
    @GetMapping(value = "/{dictCode}")
    public AjaxResult getInfo(@PathVariable("dictCode") Long dictCode)
    {
        return AjaxResult.success(csResourceDictDataService.selectCsResourceDictDataByDictCode(dictCode));
    }

    /**
     * 根据dictType字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        List<CsResourceDictData> data = csResourceDictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<CsResourceDictData>();
        }
        return AjaxResult.success(data);
    }

    /**
     * 根据pCode父级字典编码查询字典数据信息
     */
    @GetMapping(value = "/dataListByPcode")
    public TableDataInfo dataListByPcode(CsResourceDictData csResourceDictData)
    {
        startPage();
        List<CsResourceDictData> list = csResourceDictDataService.selectDictDataByPcode(csResourceDictData.getDictPcode());
        if (StringUtils.isNull(list))
        {
            list = new ArrayList<CsResourceDictData>();
        }
        return getDataTable(list);
    }

    /**
     * 新增资源字典数据
     */
    @PreAuthorize("@ss.hasPermi('cs:csResourceDictData:add')")
    @Log(title = "资源字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CsResourceDictData csResourceDictData)
    {
        if(StringUtils.isBlank(csResourceDictData.getDictValue())){
            csResourceDictData.setDictValue(csResourceDictData.getDictLabel());
        }
        csResourceDictData.setCreateBy(getUsername());
        return toAjax(csResourceDictDataService.insertCsResourceDictData(csResourceDictData));
    }

    /**
     * 修改资源字典数据
     */
    @PreAuthorize("@ss.hasPermi('cs:csResourceDictData:edit')")
    @Log(title = "资源字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CsResourceDictData csResourceDictData)
    {
        if(StringUtils.isBlank(csResourceDictData.getDictValue())){
            csResourceDictData.setDictValue(csResourceDictData.getDictLabel());
        }
        //csResourceDictData.setUpdateBy(getUsername());
        return toAjax(csResourceDictDataService.updateCsResourceDictData(csResourceDictData));
    }

    /**
     * 删除资源字典数据
     */
    @PreAuthorize("@ss.hasPermi('cs:csResourceDictData:remove')")
    @Log(title = "资源字典数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dictCodes}")
    public AjaxResult remove(@PathVariable Long[] dictCodes)
    {
        csResourceDictDataService.deleteCsResourceDictDataByDictCodes(dictCodes);
        return success();
    }

    /**
     * 获取模板组件字典参数列表
     * @param param 字典参数
     * @return 结果
     */
    @GetMapping("/getCSDictParamList")
    public AjaxResult getCSDictParamList(DictParamDTO param){
        List<CsResourceDictDataVO> dictParamList = csResourceDictDataService.selectOptions(param);
        return AjaxResult.success(dictParamList);
    }
}
