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

import cn.gov.customs.cacp.sdks.core.result.CacpResultCodeMessage;
import cn.gov.customs.cacp.sdks.core.result.Result;
import cn.gov.customs.cacp.sdks.core.result.ResultCodeMessage;
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.system.pojo.DictType;
import cn.gov.customs.wxjy.system.pojo.DictTypeQuery;
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.List;

/**
 * 字典类型Controller
 *
 * @author xiong
 * @date 2025-11-07
 */
@RestController
@RequestMapping("/dictType")
@RequiredArgsConstructor
public class DictTypeController extends BaseController {
    @Autowired
    private DictTypeService dictTypeService;
    @Autowired
    private DictDataService dictDataService;

    //    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @PostMapping("/list")
    public Result<PageInfo<DictType>> list(@RequestBody DictTypeQuery dictType) {
        PageInfo<DictType> list = dictTypeService.selectDictTypeList(dictType);
        return Result.success(list);
    }

//    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
////    @PreAuthorize("@ss.hasPermi('system:dict:export')")
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, DictType dictType)
//    {
//        List<DictType> list = dictTypeService.selectDictTypeList(dictType);
//        ExcelUtil<DictType> util = new ExcelUtil<DictType>(DictType.class);
//        util.exportExcel(response, list, "字典类型");
//    }

    /**
     * 查询字典类型详细
     */
//    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/{PkDictType}")
    public AjaxResult getInfo(@PathVariable Long PkDictType)
    {
        return success(dictTypeService.selectDictTypeById(PkDictType));
    }

    /**
     * 新增字典类型
     */
//    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DictType dict)
    {
        if (!dictTypeService.checkDictTypeUnique(dict))
        {
            return error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setCreateDept(getUserDeptGuid());
        dict.setCreateUser(getUsername());
        return toAjax(dictTypeService.insertDictType(dict));
    }

    /**
     * 修改字典类型
     */
//    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody DictType dict)
    {
        if (!dictTypeService.checkDictTypeUnique(dict))
        {
            return error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateUser(getUsername());
        return toAjax(dictTypeService.updateDictType(dict));
    }

    /**
     * 删除字典类型
     */
    @PostMapping("/delete-dictTypes")
    public Result<Integer> remove(@RequestBody Long[] ids) {
        for (Long PkDictType : ids)
        {
            DictType dictType = dictTypeService.selectDictTypeById(PkDictType);
            if (dictDataService.countDictDataByType(dictType.getDictType()) > 0)
            {
                return Result.fail(CacpResultCodeMessage.WARNING,"字典类型'" + dictType.getDictType() + "'已被使用,不能删除,请先删除字典项值");
            }
        }
        return Result.success(dictTypeService.deleteDictTypeByIds(ids));
    }
//    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
/*    @PostMapping("/{PkDictTypes}")
    public AjaxResult remove(@PathVariable Long[] PkDictTypes)
    {
        for (Long PkDictType : PkDictTypes)
        {
            DictType dictType = dictTypeService.selectDictTypeById(PkDictType);
            if (dictDataService.countDictDataByType(dictType.getDictType()) > 0)
            {
                return error("字典类型'" + dictType.getDictType() + "'已被使用,不能删除,请先删除字典项值");
            }
        }
        dictTypeService.deleteDictTypeByIds(PkDictTypes);
        return success();
    }*/

    /**
     * 刷新字典缓存
     */
//    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public AjaxResult refreshCache()
    {
        dictTypeService.resetDictCache();
        return success();
    }

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