package com.taais.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.taais.common.core.core.domain.CommonResult;
import com.taais.common.core.core.page.PageResult;
import com.taais.common.excel.utils.ExcelUtil;
import com.taais.common.log.annotation.Log;
import com.taais.common.log.enums.BusinessType;
import com.taais.common.web.core.BaseController;
import com.taais.system.domain.bo.SysDictTypeBo;
import com.taais.system.domain.vo.SysDictTypeVo;
import com.taais.system.service.ISysDictTypeService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 数据字典信息
 *
 * @author wgk
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController {
    @Resource
    private ISysDictTypeService dictTypeService;

    @SaCheckPermission("system:dict:list")
    @GetMapping("/list")
    public CommonResult<PageResult<SysDictTypeVo>> list(SysDictTypeBo dictTypeBo) {
        return CommonResult.success(dictTypeService.selectPage(dictTypeBo));
    }

    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @SaCheckPermission("system:dict:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictTypeBo dictTypeBo) {
        List<SysDictTypeVo> list = dictTypeService.selectDictTypeList(dictTypeBo);
        ExcelUtil.exportExcel(list, "数据字典类型信息数据", SysDictTypeVo.class, response);
    }

    /**
     * 查询字典类型详细
     */
    @SaCheckPermission("system:dict:query")
    @GetMapping(value = "/{dictId}")
    public CommonResult<SysDictTypeVo> getInfo(@PathVariable Long dictId) {
        return CommonResult.success(dictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */
    @SaCheckPermission("system:dict:add")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody SysDictTypeBo dictTypeBo) {
        if (!dictTypeService.checkDictTypeUnique(dictTypeBo)) {
            return CommonResult.fail("新增字典'" + dictTypeBo.getDictName() + "'失败，字典类型已存在");
        }
        dictTypeService.insertDictType(dictTypeBo);
        return CommonResult.success();
    }

    /**
     * 修改字典类型
     */
    @SaCheckPermission("system:dict:edit")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody SysDictTypeBo dictTypeBo) {
        if (!dictTypeService.checkDictTypeUnique(dictTypeBo)) {
            return CommonResult.fail("修改字典'" + dictTypeBo.getDictName() + "'失败，字典类型已存在");
        }
        dictTypeService.updateDictType(dictTypeBo);
        return CommonResult.success();
    }

    /**
     * 删除字典类型
     */
    @SaCheckPermission("system:dict:remove")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public CommonResult<Void> remove(@PathVariable Long[] dictIds) {
        boolean deleted = dictTypeService.deleteDictTypeByIds(dictIds);
        if (!deleted) {
            return CommonResult.fail("删除字典类型记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 刷新字典缓存
     */
    @SaCheckPermission("system:dict:remove")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public CommonResult<Void> refreshCache() {
        dictTypeService.resetDictCache();
        return CommonResult.success();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/option/select")
    public CommonResult<List<SysDictTypeVo>> optionSelect() {
        List<SysDictTypeVo> dictTypes = dictTypeService.selectDictTypeAll();
        return CommonResult.success(dictTypes);
    }
}
