package com.haha.admin.controller;

import com.haha.admin.annotation.RequirePermission;
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.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

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

    @Autowired
    private DictService dictService;

    @RequirePermission("dict:read")
    @PostMapping("/type/list")
    public Result<Map<String, Object>> getDictTypeList(@RequestBody Map<String, Object> params) {
        return dictService.getDictTypeList(params);
    }

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

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

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

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

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

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

    @GetMapping("/data/list/{dictCode}")
    public Result<List<DictData>> getDictDataList(@PathVariable String dictCode) {
        return dictService.getDictDataList(dictCode);
    }

    @RequirePermission("dict:read")
    @PostMapping("/data/page")
    public Result<Map<String, Object>> getDictDataPage(@RequestBody Map<String, Object> params) {
        return dictService.getDictDataPage(params);
    }

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

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

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

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

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

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

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

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

    @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) {
        return dictService.exportDictData(dictCode);
    }

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