package com.haha.admin.controller;

import com.haha.admin.annotation.RequirePermission;
import com.haha.entity.dto.DictTypeQueryDTO;
import com.haha.entity.dto.DictDataQueryDTO;
import com.haha.common.annotation.Log;
import com.haha.common.enums.OperationType;
import com.haha.common.vo.Result;
import com.haha.entity.DictData;
import com.haha.entity.DictType;
import com.haha.service.DictService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典管理控制器
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/dict")
public class DictController {

    private final DictService dictService;

    @RequirePermission("dict:read")
    @PostMapping("/type/list")
    public Result<Map<String, Object>> getDictTypeList(@RequestBody DictTypeQueryDTO queryDTO) {
        return Result.success("查询成功", dictService.getDictTypeList(queryDTO));
    }

    @RequirePermission("dict:read")
    @GetMapping("/type/{id}")
    public Result<DictType> getDictTypeById(@PathVariable Long id) {
        return Result.success("查询成功", dictService.getDictTypeById(id));
    }

    @RequirePermission("dict:read")
    @GetMapping("/type/code/{dictCode}")
    public Result<DictType> getDictTypeByCode(@PathVariable String dictCode) {
        return Result.success("查询成功", dictService.getDictTypeByCode(dictCode));
    }

    @RequirePermission("dict:create")
    @Log(module = "字典管理", operation = OperationType.INSERT, summary = "新增字典类型")
    @PostMapping("/type/add")
    public Result<Void> addDictType(@RequestBody DictType dictType) {
        dictService.addDictType(dictType);
        return Result.success("添加成功");
    }

    @RequirePermission("dict:update")
    @Log(module = "字典管理", operation = OperationType.UPDATE, summary = "修改字典类型")
    @PostMapping("/type/update")
    public Result<Void> updateDictType(@RequestBody DictType dictType) {
        dictService.updateDictType(dictType);
        return Result.success("修改成功");
    }

    @RequirePermission("dict:delete")
    @Log(module = "字典管理", operation = OperationType.DELETE, summary = "删除字典类型")
    @DeleteMapping("/type/{id}")
    public Result<Void> deleteDictType(@PathVariable Long id) {
        dictService.deleteDictType(id);
        return Result.success("删除成功");
    }

    @RequirePermission("dict:delete")
    @Log(module = "字典管理", operation = OperationType.DELETE, summary = "批量删除字典类型")
    @PostMapping("/type/batch-delete")
    public Result<Void> batchDeleteDictType(@RequestBody List<Long> ids) {
        dictService.batchDeleteDictType(ids);
        return Result.success("批量删除成功");
    }

    @RequirePermission("dict:read")
    @GetMapping("/data/list/{dictCode}")
    public Result<List<DictData>> getDictDataList(@PathVariable String dictCode) {
        return Result.success("查询成功", dictService.getDictDataList(dictCode));
    }

    @RequirePermission("dict:read")
    @PostMapping("/data/page")
    public Result<Map<String, Object>> getDictDataPage(@RequestBody DictDataQueryDTO queryDTO) {
        return Result.success("查询成功", dictService.getDictDataPage(queryDTO));
    }

    @RequirePermission("dict:read")
    @GetMapping("/data/{id}")
    public Result<DictData> getDictDataById(@PathVariable Long id) {
        return Result.success("查询成功", dictService.getDictDataById(id));
    }

    @RequirePermission("dict:create")
    @Log(module = "字典管理", operation = OperationType.INSERT, summary = "新增字典数据")
    @PostMapping("/data/add")
    public Result<Void> addDictData(@RequestBody DictData dictData) {
        dictService.addDictData(dictData);
        return Result.success("添加成功");
    }

    @RequirePermission("dict:update")
    @Log(module = "字典管理", operation = OperationType.UPDATE, summary = "修改字典数据")
    @PostMapping("/data/update")
    public Result<Void> updateDictData(@RequestBody DictData dictData) {
        dictService.updateDictData(dictData);
        return Result.success("修改成功");
    }

    @RequirePermission("dict:delete")
    @Log(module = "字典管理", operation = OperationType.DELETE, summary = "删除字典数据")
    @DeleteMapping("/data/{id}")
    public Result<Void> deleteDictData(@PathVariable Long id) {
        dictService.deleteDictData(id);
        return Result.success("删除成功");
    }

    @RequirePermission("dict:delete")
    @Log(module = "字典管理", operation = OperationType.DELETE, summary = "批量删除字典数据")
    @PostMapping("/data/batch-delete")
    public Result<Void> batchDeleteDictData(@RequestBody List<Long> ids) {
        dictService.batchDeleteDictData(ids);
        return Result.success("批量删除成功");
    }

    @GetMapping("/all")
    public Result<Map<String, List<DictData>>> getAllDictData() {
        return Result.success("查询成功", dictService.getAllDictData());
    }

    @GetMapping("/label")
    public Result<String> getDictLabel(
            @RequestParam String dictCode,
            @RequestParam String itemValue) {
        return Result.success("查询成功", dictService.getDictLabel(dictCode, itemValue));
    }

    @GetMapping("/value")
    public Result<String> getDictValue(
            @RequestParam String dictCode,
            @RequestParam String itemLabel) {
        return Result.success("查询成功", dictService.getDictValue(dictCode, itemLabel));
    }

    @RequirePermission("dict:update")
    @Log(module = "字典管理", operation = OperationType.UPDATE, summary = "刷新字典缓存")
    @PostMapping("/refresh-cache")
    public Result<Void> refreshCache() {
        dictService.refreshCache();
        return Result.success("缓存刷新成功");
    }

    @RequirePermission("dict:export")
    @Log(module = "字典管理", operation = OperationType.EXPORT, summary = "导出字典数据")
    @GetMapping("/export/{dictCode}")
    public Result<Void> exportDictData(@PathVariable String dictCode) {
        dictService.exportDictData(dictCode);
        return Result.success("导出成功");
    }

    @RequirePermission("dict:import")
    @Log(module = "字典管理", operation = OperationType.IMPORT, summary = "导入字典数据")
    @PostMapping("/import")
    public Result<Void> importDictData(@RequestBody List<DictData> dataList) {
        dictService.importDictData(dataList);
        return Result.success("导入成功");
    }
}
