320 lines
9.2 KiB
Go
320 lines
9.2 KiB
Go
package controller
|
||
|
||
import (
|
||
"fmt"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
|
||
"be.ems/src/framework/i18n"
|
||
"be.ems/src/framework/utils/ctx"
|
||
"be.ems/src/framework/utils/file"
|
||
"be.ems/src/framework/utils/parse"
|
||
"be.ems/src/framework/vo/result"
|
||
"be.ems/src/modules/system/model"
|
||
"be.ems/src/modules/system/service"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/gin-gonic/gin/binding"
|
||
)
|
||
|
||
// 实例化控制层 SysDictDataController 结构体
|
||
var NewSysDictData = &SysDictDataController{
|
||
sysDictDataService: service.NewSysDictData,
|
||
sysDictTypeService: service.NewSysDictType,
|
||
}
|
||
|
||
// 字典类型对应的字典数据信息
|
||
//
|
||
// PATH /system/dict/data
|
||
type SysDictDataController struct {
|
||
sysDictDataService *service.SysDictData // 字典数据服务
|
||
sysDictTypeService *service.SysDictType // 字典类型服务
|
||
}
|
||
|
||
// 字典数据列表
|
||
//
|
||
// GET /list
|
||
//
|
||
// @Tags system/dict/data
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param dictLabel query string false "dictLabel"
|
||
// @Param pageNum query number true "pageNum" default(1)
|
||
// @Param pageSize query number true "pageSize" default(10)
|
||
// @Success 200 {object} object "Response Results"
|
||
// @Security TokenAuth
|
||
// @Summary Dictionary Data List
|
||
// @Description Dictionary Data List
|
||
// @Router /system/dict/data/list [get]
|
||
func (s *SysDictDataController) List(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
querys := ctx.QueryMap(c)
|
||
// 多语言值转key查询
|
||
if v, ok := querys["dictLabel"]; ok && v != "" {
|
||
querys["dictLabel"] = i18n.TFindKeyPrefix(language, "dictData", v.(string))
|
||
}
|
||
|
||
data := s.sysDictDataService.SelectDictDataPage(querys)
|
||
rows := data["rows"].([]model.SysDictData)
|
||
|
||
// 闭包函数处理多语言
|
||
converI18n := func(language string, arr *[]model.SysDictData) {
|
||
for i := range *arr {
|
||
if strings.Contains((*arr)[i].DictType, "i18n") {
|
||
continue
|
||
}
|
||
(*arr)[i].DictLabel = i18n.TKey(language, (*arr)[i].DictLabel)
|
||
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
|
||
}
|
||
}
|
||
converI18n(language, &rows)
|
||
|
||
c.JSON(200, result.Ok(data))
|
||
}
|
||
|
||
// 字典数据详情
|
||
//
|
||
// GET /:dictCode
|
||
func (s *SysDictDataController) Info(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
dictCode := c.Param("dictCode")
|
||
if dictCode == "" {
|
||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||
return
|
||
}
|
||
data := s.sysDictDataService.SelectDictDataByCode(dictCode)
|
||
if data.DictCode == dictCode {
|
||
// 处理多语言
|
||
if !strings.Contains(data.DictType, "i18n") {
|
||
data.DictLabel = i18n.TKey(language, data.DictLabel)
|
||
data.Remark = i18n.TKey(language, data.Remark)
|
||
}
|
||
c.JSON(200, result.OkData(data))
|
||
return
|
||
}
|
||
c.JSON(200, result.Err(nil))
|
||
}
|
||
|
||
// 字典数据新增
|
||
//
|
||
// POST /
|
||
func (s *SysDictDataController) Add(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
var body model.SysDictData
|
||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||
if err != nil || body.DictCode != "" {
|
||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||
return
|
||
}
|
||
|
||
// 检查字典类型是否存在
|
||
sysDictType := s.sysDictTypeService.SelectDictTypeByType(body.DictType)
|
||
if sysDictType.DictType != body.DictType {
|
||
// 没有可访问字典类型数据!
|
||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "dictType.noData")))
|
||
return
|
||
}
|
||
|
||
// 检查字典标签唯一
|
||
uniqueDictLabel := s.sysDictDataService.CheckUniqueDictLabel(body.DictType, body.DictLabel, "")
|
||
if !uniqueDictLabel {
|
||
// 数据新增【%s】失败,该字典类型下标签名已存在
|
||
msg := i18n.TTemplate(language, "dictType.errLabelExists", map[string]any{"name": body.DictLabel})
|
||
c.JSON(200, result.ErrMsg(msg))
|
||
return
|
||
}
|
||
|
||
body.CreateBy = ctx.LoginUserToUserName(c)
|
||
insertId := s.sysDictDataService.InsertDictData(body)
|
||
if insertId != "" {
|
||
c.JSON(200, result.Ok(nil))
|
||
return
|
||
}
|
||
c.JSON(200, result.Err(nil))
|
||
}
|
||
|
||
// 字典类型修改
|
||
//
|
||
// PUT /
|
||
func (s *SysDictDataController) Edit(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
var body model.SysDictData
|
||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||
if err != nil || body.DictCode == "" {
|
||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||
return
|
||
}
|
||
|
||
// 检查字典类型是否存在
|
||
sysDictType := s.sysDictTypeService.SelectDictTypeByType(body.DictType)
|
||
if sysDictType.DictType != body.DictType {
|
||
// 没有可访问字典类型数据!
|
||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "dictType.noData")))
|
||
return
|
||
}
|
||
|
||
// 检查字典编码是否存在
|
||
sysDictData := s.sysDictDataService.SelectDictDataByCode(body.DictCode)
|
||
if sysDictData.DictCode != body.DictCode {
|
||
// 没有可访问字典编码数据!
|
||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "dictData.noData")))
|
||
return
|
||
}
|
||
|
||
// 检查字典标签唯一
|
||
uniqueDictLabel := s.sysDictDataService.CheckUniqueDictLabel(body.DictType, body.DictLabel, body.DictCode)
|
||
if !uniqueDictLabel {
|
||
// 数据修改【%s】失败,该字典类型下标签名已存在
|
||
msg := i18n.TTemplate(language, "dictType.errLabelExists", map[string]any{"name": body.DictLabel})
|
||
c.JSON(200, result.ErrMsg(msg))
|
||
return
|
||
}
|
||
|
||
// 多语言非原始值
|
||
i18nValue := i18n.TKey(language, sysDictData.DictLabel)
|
||
if i18nValue != sysDictData.DictLabel {
|
||
i18n.UpdateKeyValue(language, sysDictData.DictLabel, body.DictLabel)
|
||
body.DictLabel = sysDictData.DictLabel
|
||
}
|
||
// 多语言非原始值
|
||
i18nValue2 := i18n.TKey(language, sysDictData.Remark)
|
||
if i18nValue2 != sysDictData.Remark {
|
||
i18n.UpdateKeyValue(language, sysDictData.Remark, body.Remark)
|
||
body.Remark = sysDictData.Remark
|
||
}
|
||
|
||
body.UpdateBy = ctx.LoginUserToUserName(c)
|
||
rows := s.sysDictDataService.UpdateDictData(body)
|
||
if rows > 0 {
|
||
c.JSON(200, result.Ok(nil))
|
||
return
|
||
}
|
||
c.JSON(200, result.Err(nil))
|
||
}
|
||
|
||
// 字典数据删除
|
||
//
|
||
// DELETE /:dictCodes
|
||
func (s *SysDictDataController) Remove(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
dictCodes := c.Param("dictCodes")
|
||
if dictCodes == "" {
|
||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||
return
|
||
}
|
||
// 处理字符转id数组后去重
|
||
ids := strings.Split(dictCodes, ",")
|
||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||
if len(uniqueIDs) <= 0 {
|
||
c.JSON(200, result.Err(nil))
|
||
return
|
||
}
|
||
rows, err := s.sysDictDataService.DeleteDictDataByCodes(uniqueIDs)
|
||
if err != nil {
|
||
c.JSON(200, result.ErrMsg(err.Error()))
|
||
return
|
||
}
|
||
// 删除成功:%d
|
||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||
c.JSON(200, result.OkMsg(msg))
|
||
}
|
||
|
||
// 字典数据列表(指定字典类型)
|
||
//
|
||
// GET /type/:dictType
|
||
func (s *SysDictDataController) DictType(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
dictType := c.Param("dictType")
|
||
if dictType == "" {
|
||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||
return
|
||
}
|
||
|
||
data := s.sysDictDataService.SelectDictDataByType(dictType)
|
||
|
||
// 闭包函数处理多语言
|
||
converI18n := func(language string, arr *[]model.SysDictData) {
|
||
for i := range *arr {
|
||
(*arr)[i].DictLabel = i18n.TKey(language, (*arr)[i].DictLabel)
|
||
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
|
||
}
|
||
}
|
||
converI18n(language, &data)
|
||
|
||
c.JSON(200, result.OkData(data))
|
||
}
|
||
|
||
// 字典数据列表导出
|
||
//
|
||
// POST /export
|
||
func (s *SysDictDataController) Export(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
// 查询结果,根据查询条件结果,单页最大值限制
|
||
querys := ctx.BodyJSONMap(c)
|
||
querys["pageNum"] = 1
|
||
querys["pageSize"] = 10000
|
||
data := s.sysDictDataService.SelectDictDataPage(querys)
|
||
if parse.Number(data["total"]) == 0 {
|
||
// 导出数据记录为空
|
||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
||
return
|
||
}
|
||
rows := data["rows"].([]model.SysDictData)
|
||
|
||
// rows := s.sysDictDataService.SelectDictDataList(model.SysDictData{})
|
||
if len(rows) <= 0 {
|
||
// 导出数据记录为空
|
||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
||
return
|
||
}
|
||
|
||
// 闭包函数处理多语言
|
||
converI18n := func(language string, arr *[]model.SysDictData) {
|
||
for i := range *arr {
|
||
if strings.Contains((*arr)[i].DictType, "i18n") {
|
||
continue
|
||
}
|
||
(*arr)[i].DictLabel = i18n.TKey(language, (*arr)[i].DictLabel)
|
||
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
|
||
}
|
||
}
|
||
converI18n(language, &rows)
|
||
|
||
// 导出文件名称
|
||
fileName := fmt.Sprintf("dict_data_export_%d_%d.xlsx", len(rows), time.Now().UnixMilli())
|
||
// 第一行表头标题
|
||
headerCells := map[string]string{
|
||
"A1": i18n.TKey(language, "dictData.export.code"),
|
||
"B1": i18n.TKey(language, "dictData.export.label"),
|
||
"C1": i18n.TKey(language, "dictData.export.value"),
|
||
"D1": i18n.TKey(language, "dictData.export.sort"),
|
||
"E1": i18n.TKey(language, "dictData.export.status"),
|
||
}
|
||
// 从第二行开始的数据
|
||
dataCells := make([]map[string]any, 0)
|
||
for i, row := range rows {
|
||
idx := strconv.Itoa(i + 2)
|
||
statusValue := i18n.TKey(language, "dictData.disable")
|
||
if row.Status == "1" {
|
||
statusValue = i18n.TKey(language, "dictData.normal")
|
||
}
|
||
dataCells = append(dataCells, map[string]any{
|
||
"A" + idx: row.DictCode,
|
||
"B" + idx: row.DictLabel,
|
||
"C" + idx: row.DictValue,
|
||
"D" + idx: row.DictSort,
|
||
"E" + idx: statusValue,
|
||
})
|
||
}
|
||
|
||
// 导出数据表格
|
||
saveFilePath, err := file.WriteSheet(headerCells, dataCells, fileName, "")
|
||
if err != nil {
|
||
c.JSON(200, result.ErrMsg(err.Error()))
|
||
return
|
||
}
|
||
|
||
c.FileAttachment(saveFilePath, fileName)
|
||
}
|