package com.taais.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.mybatisflex.core.query.QueryWrapper;
import com.taais.common.core.core.domain.CommonResult;
import com.taais.common.core.core.page.PageResult;
import com.taais.common.core.utils.StringUtils;
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.SysDictData;
import com.taais.system.domain.bo.SysDictDataBo;
import com.taais.system.domain.vo.SysDictDataVo;
import com.taais.system.service.ISysDictDataService;
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.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author wgk
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController {
    @Resource
    private ISysDictDataService dictDataService;

    @Resource
    private ISysDictTypeService dictTypeService;

    @SaCheckPermission("system:dict:list")
    @GetMapping("/list")
    public CommonResult<PageResult<SysDictDataVo>> list(SysDictDataBo dictDataBo) {
        return CommonResult.success(dictDataService.selectPage(dictDataBo));
    }

    /**
     * 导出字典数据列表
     */
    @SaCheckPermission("system:dict:export")
    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictDataBo dictDataBo) {
        List<SysDictDataVo> list = dictDataService.selectDictDataList(dictDataBo);
        ExcelUtil.exportExcel(list, "字典数据", SysDictDataVo.class, response);
    }

    /**
     * 查询字典数据详细
     */
    @SaCheckPermission("system:dict:query")
    @GetMapping(value = "/{dictCode}")
    public CommonResult<SysDictDataVo> getInfo(@PathVariable Long dictCode) {
        return CommonResult.success(dictDataService.selectDictDataById(dictCode));
    }

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

    /**
     * 新增字典类型
     */
    @SaCheckPermission("system:dict:add")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody SysDictDataBo dictDataBo) {
        boolean inserted = dictDataService.insertDictData(dictDataBo);
        if (!inserted) {
            return CommonResult.fail("新增字典数据记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改保存字典类型
     */
    @SaCheckPermission("system:dict:edit")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody SysDictDataBo dictDataBo) {
        boolean updated = dictDataService.updateDictData(dictDataBo);
        if (!updated) {
            return CommonResult.fail("修改字典数据记录失败！");
        }
        return CommonResult.success();
    }

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


}
