package cn.gov.customs.wxjy.system.controller;

import cn.gov.customs.cacp.sdks.core.result.Result;
import cn.gov.customs.wxjy.common.annotation.Log;
import cn.gov.customs.wxjy.common.core.controller.BaseController;
import cn.gov.customs.wxjy.common.core.domain.AjaxResult;
import cn.gov.customs.wxjy.common.enums.BusinessType;
import cn.gov.customs.wxjy.common.utils.StringUtils;
import cn.gov.customs.wxjy.system.pojo.DictData;
import cn.gov.customs.wxjy.system.pojo.DictDataQuery;
import cn.gov.customs.wxjy.system.service.DictDataService;
import cn.gov.customs.wxjy.system.service.DictTypeService;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 字典数据Controller
 *
 * @author xiong
 * @date 2025-11-07
 */
@RestController
@RequestMapping("/dictData")
@RequiredArgsConstructor
public class DictDataController extends BaseController {
    @Autowired
    private DictDataService dictDataService;

    @Autowired
    private DictTypeService dictTypeService;

    //    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @PostMapping("/list")
    public Result<PageInfo<DictData>> list(@RequestBody DictDataQuery dictData) {
        PageInfo<DictData> list = dictDataService.selectDictDataList(dictData);
        return Result.success(list);
    }

//    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
////    @PreAuthorize("@ss.hasPermi('system:dict:export')")
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, DictData dictData)
//    {
//        List<DictData> list = dictDataService.selectDictDataList(dictData);
//        ExcelUtil<DictData> util = new ExcelUtil<DictData>(DictData.class);
//        util.exportExcel(response, list, "字典数据");
//    }

    /**
     * 查询字典数据详细
     */
//    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictCode}")
    public Result<DictData> getInfo(@PathVariable Long dictCode)
    {
        DictData info = dictDataService.selectDictDataById(dictCode);
        return Result.success(info);
    }

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

    /**
     * 新增字典类型
     */
//    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody DictData dict) {
        if (!dictDataService.checkDictDataUnique(dict))
        {
            return error("新增字典'" + dict.getDictValue() + "'失败，字典键值已存在");
        }
        dict.setCreateUser(getUsername());
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
//    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody DictData dict)
    {
        if (!dictDataService.checkDictDataUnique(dict))
        {
            return error("修改字典'" + dict.getDictValue() + "'失败，字典键值已存在");
        }
        dict.setUpdateUser(getUsername());
        return toAjax(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
//    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public AjaxResult remove(@PathVariable Long[] dictCodes)
    {
        dictDataService.deleteDictDataByIds(dictCodes);
        return success();
    }
}
