package com.chushang.system.controller;

import com.chushang.common.core.exception.utils.AssertUtil;
import com.chushang.common.core.web.AjaxResult;
import com.chushang.common.excel.utils.ExcelUtils;
import com.chushang.common.log.annotation.SysLog;
import com.chushang.common.log.constant.LogApplicationName;
import com.chushang.common.log.enums.BusinessType;
import com.chushang.common.mybatis.page.CommonParam;
import com.chushang.common.mybatis.utils.PageResult;
import com.chushang.security.annotation.RequiresPermissions;
import com.chushang.security.utils.SecurityUtils;
import com.chushang.system.entity.po.SysDictType;
import com.chushang.system.service.ISysDictTypeService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @auther: zhao
 * @date: 2024/5/9 15:54
 * todo 加入缓存
 */
@RestController
@RequestMapping(value = "/dict/type")
public class DictTypeController {
    @Resource
    ISysDictTypeService dictTypeService;

    @RequiresPermissions("system:dict:list")
    @GetMapping("/list")
    public AjaxResult list(SysDictType dictType)
    {
        CommonParam commonParam = CommonParam.buildPageRequest();
        PageResult pageResult = dictTypeService.pageDictTypeList(dictType, commonParam);
        return AjaxResult.success(pageResult);
    }

    @SysLog(value = "字典类型", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:dict:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictType dictType)
    {
        CommonParam commonParam = CommonParam.buildAllRequest();
        List<SysDictType> list = dictTypeService.allDictTypeList(dictType, commonParam);
        ExcelUtils.exportList(response, SysDictType.class, list, "导出字典类型数据");
    }

    /**
     * 查询字典类型详细
     */
    @RequiresPermissions("system:dict:query")
    @GetMapping(value = "/{dictId}")
    public AjaxResult getInfo(@PathVariable Long dictId)
    {
        return AjaxResult.success(dictTypeService.getById(dictId));
    }

    /**
     * 新增字典类型
     */
    @RequiresPermissions("system:dict:add")
    @SysLog(value = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDictType dict)
    {
        CommonParam commonParam = CommonParam.buildAllRequest();

        AssertUtil.invalidate(dictTypeService.checkDictTypeUnique(dict, commonParam),"新增字典'" + dict.getDictName() + "'失败，字典类型已存在" );

        dict.setCreateBy(SecurityUtils.getUsername());
        dictTypeService.saveOrUpdate(dict);
        return AjaxResult.success();
    }

    /**
     * 修改字典类型
     */
    @RequiresPermissions("system:dict:edit")
    @SysLog(value = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysDictType dict)
    {
        CommonParam commonParam = CommonParam.buildAllRequest();

        AssertUtil.invalidate(dictTypeService.checkDictTypeUnique(dict, commonParam),"修改字典'" + dict.getDictName() + "'失败，字典类型已存在" );

        dict.setUpdateBy(SecurityUtils.getUsername());
        dictTypeService.saveOrUpdate(dict);
        return AjaxResult.success();
    }

    /**
     * 删除字典类型
     */
    @RequiresPermissions("system:dict:remove")
    @SysLog(value = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public AjaxResult remove(@PathVariable Long[] dictIds)
    {
        dictTypeService.removeBatchByIds(Arrays.asList(dictIds));
        return AjaxResult.success();
    }

    /**
     * TODO 刷新字典缓存
     */
    @RequiresPermissions("system:dict:refresh")
    @SysLog(value = "字典类型", businessType = BusinessType.CLEAR)
    @DeleteMapping("/refreshCache")
    public AjaxResult refreshCache()
    {
        dictTypeService.resetDictCache();
        return AjaxResult.success();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/option/select")
    public AjaxResult optionSelect()
    {
        CommonParam commonParam = CommonParam.buildAllRequest();
        List<SysDictType> list = dictTypeService.allDictTypeList(new SysDictType(), commonParam);
        return AjaxResult.success(list);
    }
}
