package com.chushang.system.controller;

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.enums.BusinessType;
import com.chushang.common.mybatis.page.CommonParam;
import com.chushang.security.annotation.RequiresPermissions;
import com.chushang.security.utils.SecurityUtils;
import com.chushang.system.entity.dto.ListDictDTO;
import com.chushang.system.entity.po.SysDictData;
import com.chushang.system.service.ISysDictTypeService;
import com.chushang.system.service.SysDictDataService;
import org.aspectj.weaver.loadtime.Aj;
import org.checkerframework.checker.units.qual.A;
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;

/**
 * @author by zhaowenyuan create 2022/8/26 11:43
 * 获取字典数据 -- 添加直接入库
 */
@RestController
@RequestMapping(value = "/dict/data")
public class DictController {

    @Resource
    SysDictDataService dictDataService;

    @Resource
    ISysDictTypeService dictTypeService;
    /**
     * 获取字典数据值
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult getInfo(@PathVariable String dictType)
    {
        return AjaxResult.success(dictDataService.selectDictDataByType(dictType));
    }

    @RequiresPermissions("system:dict:list")
    @GetMapping("/list")
    public AjaxResult list(ListDictDTO dictData)
    {
        CommonParam pageParam = CommonParam.buildPageRequest();
        return AjaxResult.success(dictDataService.pageDictDataList(dictData, pageParam));
    }

    @SysLog(value = "字典数据", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:dict:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ListDictDTO dictData)
    {
        CommonParam commonParam = CommonParam.buildAllRequest();
        List<SysDictData> list = dictDataService.allDictDataList(dictData,commonParam);
        ExcelUtils.exportList(response, SysDictData.class, list, "导出字典数据");
    }

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

    /**
     * 新增字典类型
     */
    @RequiresPermissions("system:dict:add")
    @SysLog(value = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDictData dict)
    {
        dict.setCreateBy(SecurityUtils.getUsername());
        dictDataService.saveOrUpdate(dict);
        return AjaxResult.success();
    }

    /**
     * 修改保存字典类型
     */
    @RequiresPermissions("system:dict:edit")
    @SysLog(value = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysDictData dict)
    {
        dict.setUpdateBy(SecurityUtils.getUsername());
        dictDataService.saveOrUpdate(dict);
        return AjaxResult.success();
    }

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