feat: 更新多个模块以支持新的数据结构和日志格式
This commit is contained in:
@@ -3,33 +3,30 @@ package controller
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/utils/ctx"
|
||||
"be.ems/src/framework/reqctx"
|
||||
"be.ems/src/framework/resp"
|
||||
"be.ems/src/framework/utils/date"
|
||||
"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"
|
||||
)
|
||||
|
||||
// 实例化控制层 SysPostController 结构体
|
||||
var NewSysPost = &SysPostController{
|
||||
sysPostService: service.NewSysPostImpl,
|
||||
sysPostService: service.NewSysPost,
|
||||
}
|
||||
|
||||
// 岗位信息
|
||||
//
|
||||
// PATH /system/post
|
||||
type SysPostController struct {
|
||||
// 岗位服务
|
||||
sysPostService service.ISysPost
|
||||
sysPostService *service.SysPost // 岗位服务
|
||||
}
|
||||
|
||||
// 岗位列表
|
||||
@@ -48,15 +45,14 @@ type SysPostController struct {
|
||||
// @Description Post Information List
|
||||
// @Router /system/post/list [get]
|
||||
func (s *SysPostController) List(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
querys := ctx.QueryMap(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
query := reqctx.QueryMap(c)
|
||||
// 多语言值转key查询
|
||||
if v, ok := querys["postName"]; ok && v != "" {
|
||||
querys["postName"] = i18n.TFindKeyPrefix(language, "post", v.(string))
|
||||
if v, ok := query["postName"]; ok && v != "" {
|
||||
query["postName"] = i18n.TFindKeyPrefix(language, "post", v)
|
||||
}
|
||||
|
||||
data := s.sysPostService.SelectPostPage(querys)
|
||||
rows := data["rows"].([]model.SysPost)
|
||||
rows, total := s.sysPostService.FindByPage(query)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
converI18n := func(language string, arr *[]model.SysPost) {
|
||||
@@ -67,171 +63,184 @@ func (s *SysPostController) List(c *gin.Context) {
|
||||
}
|
||||
converI18n(language, &rows)
|
||||
|
||||
c.JSON(200, result.Ok(data))
|
||||
c.JSON(200, resp.OkData(map[string]any{"rows": rows, "total": total}))
|
||||
}
|
||||
|
||||
// 岗位信息
|
||||
// Info 岗位信息
|
||||
//
|
||||
// GET /:postId
|
||||
func (s *SysPostController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
postId := c.Param("postId")
|
||||
if postId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
postId := parse.Number(c.Param("postId"))
|
||||
if postId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: postId is empty"))
|
||||
return
|
||||
}
|
||||
data := s.sysPostService.SelectPostById(postId)
|
||||
if data.PostID == postId {
|
||||
data := s.sysPostService.FindById(postId)
|
||||
if data.PostId == postId {
|
||||
// 处理多语言
|
||||
data.PostName = i18n.TKey(language, data.PostName)
|
||||
data.Remark = i18n.TKey(language, data.Remark)
|
||||
c.JSON(200, result.OkData(data))
|
||||
c.JSON(200, resp.OkData(data))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 岗位新增
|
||||
// Add 岗位新增
|
||||
//
|
||||
// POST /
|
||||
func (s *SysPostController) Add(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.SysPost
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.PostID != "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(40422, errMsgs))
|
||||
return
|
||||
}
|
||||
if body.PostId > 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: postId not is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查名称唯一
|
||||
uniqueuPostName := s.sysPostService.CheckUniquePostName(body.PostName, "")
|
||||
if !uniqueuPostName {
|
||||
// 岗位新增【%s】失败,岗位名称已存在
|
||||
uniqueuName := s.sysPostService.CheckUniqueByName(body.PostName, 0)
|
||||
if !uniqueuName {
|
||||
// msg := fmt.Sprintf("岗位新增【%s】失败,岗位名称已存在", body.PostName)
|
||||
msg := i18n.TTemplate(language, "post.errNameExists", map[string]any{"name": body.PostName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查编码属性值唯一
|
||||
uniquePostCode := s.sysPostService.CheckUniquePostCode(body.PostCode, "")
|
||||
if !uniquePostCode {
|
||||
// 岗位新增【%s】失败,岗位编码已存在
|
||||
uniqueCode := s.sysPostService.CheckUniqueByCode(body.PostCode, 0)
|
||||
if !uniqueCode {
|
||||
// msg := fmt.Sprintf("岗位新增【%s】失败,岗位编码已存在", body.PostCode)
|
||||
msg := i18n.TTemplate(language, "post.errCodeExists", map[string]any{"name": body.PostCode})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
body.CreateBy = ctx.LoginUserToUserName(c)
|
||||
insertId := s.sysPostService.InsertPost(body)
|
||||
if insertId != "" {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
body.CreateBy = reqctx.LoginUserToUserName(c)
|
||||
insertId := s.sysPostService.Insert(body)
|
||||
if insertId > 0 {
|
||||
c.JSON(200, resp.OkData(insertId))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 岗位修改
|
||||
//
|
||||
// PUT /
|
||||
func (s *SysPostController) Edit(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.SysPost
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.PostID == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(40422, errMsgs))
|
||||
return
|
||||
}
|
||||
if body.PostId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: postId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
postInfo := s.sysPostService.SelectPostById(body.PostID)
|
||||
if postInfo.PostID != body.PostID {
|
||||
// 没有可访问岗位数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "post.noData")))
|
||||
postInfo := s.sysPostService.FindById(body.PostId)
|
||||
if postInfo.PostId != body.PostId {
|
||||
// c.JSON(200, resp.ErrMsg("没有权限访问岗位数据!"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "post.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查名称唯一
|
||||
uniqueuPostName := s.sysPostService.CheckUniquePostName(body.PostName, body.PostID)
|
||||
if !uniqueuPostName {
|
||||
// 岗位修改【%s】失败,岗位名称已存在
|
||||
uniquePostName := s.sysPostService.CheckUniqueByName(body.PostName, body.PostId)
|
||||
if !uniquePostName {
|
||||
// msg := fmt.Sprintf("岗位修改【%s】失败,岗位名称已存在", body.PostName)
|
||||
msg := i18n.TTemplate(language, "post.errNameExists", map[string]any{"name": body.PostName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查编码属性值唯一
|
||||
uniquePostCode := s.sysPostService.CheckUniquePostCode(body.PostCode, body.PostID)
|
||||
uniquePostCode := s.sysPostService.CheckUniqueByCode(body.PostCode, body.PostId)
|
||||
if !uniquePostCode {
|
||||
// 岗位修改【%s】失败,岗位编码已存在
|
||||
// msg := fmt.Sprintf("岗位修改【%s】失败,岗位编码已存在", body.PostCode)
|
||||
msg := i18n.TTemplate(language, "post.errCodeExists", map[string]any{"name": body.PostCode})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 多语言非原始值
|
||||
i18nValue := i18n.TKey(language, postInfo.PostName)
|
||||
if i18nValue != postInfo.PostName {
|
||||
i18n.UpdateKeyValue(language, postInfo.PostName, body.PostName)
|
||||
service.NewSysI18n.UpdateKeyValue(language, postInfo.PostName, body.PostName)
|
||||
body.PostName = postInfo.PostName
|
||||
}
|
||||
// 多语言非原始值
|
||||
i18nValue2 := i18n.TKey(language, postInfo.Remark)
|
||||
if i18nValue2 != postInfo.Remark {
|
||||
i18n.UpdateKeyValue(language, postInfo.Remark, body.Remark)
|
||||
service.NewSysI18n.UpdateKeyValue(language, postInfo.Remark, body.Remark)
|
||||
body.Remark = postInfo.Remark
|
||||
}
|
||||
|
||||
body.UpdateBy = ctx.LoginUserToUserName(c)
|
||||
rows := s.sysPostService.UpdatePost(body)
|
||||
postInfo.PostCode = body.PostCode
|
||||
postInfo.PostName = body.PostName
|
||||
postInfo.PostSort = body.PostSort
|
||||
postInfo.StatusFlag = body.StatusFlag
|
||||
postInfo.Remark = body.Remark
|
||||
postInfo.UpdateBy = reqctx.LoginUserToUserName(c)
|
||||
rows := s.sysPostService.Update(postInfo)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 岗位删除
|
||||
// Remove 岗位删除
|
||||
//
|
||||
// DELETE /:postIds
|
||||
func (s *SysPostController) Remove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
postIds := c.Param("postIds")
|
||||
if postIds == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
// DELETE /:postId
|
||||
func (s SysPostController) Remove(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
postId := c.Param("postId")
|
||||
if postId == "" {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: postId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 处理字符转id数组后去重
|
||||
ids := strings.Split(postIds, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
uniqueIDs := parse.RemoveDuplicatesToArray(postId, ",")
|
||||
// 转换成int64数组类型
|
||||
ids := make([]int64, 0)
|
||||
for _, v := range uniqueIDs {
|
||||
ids = append(ids, parse.Number(v))
|
||||
}
|
||||
rows, err := s.sysPostService.DeletePostByIds(uniqueIDs)
|
||||
|
||||
rows, err := s.sysPostService.DeleteByIds(ids)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
// msg := fmt.Sprintf("删除成功:%d", rows)
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
c.JSON(200, resp.OkMsg(msg))
|
||||
}
|
||||
|
||||
// 导出岗位信息
|
||||
//
|
||||
// POST /export
|
||||
func (s *SysPostController) Export(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
// 查询结果,根据查询条件结果,单页最大值限制
|
||||
querys := ctx.BodyJSONMap(c)
|
||||
querys["pageNum"] = 1
|
||||
querys["pageSize"] = 10000
|
||||
data := s.sysPostService.SelectPostPage(querys)
|
||||
if parse.Number(data["total"]) == 0 {
|
||||
// 导出数据记录为空
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
||||
query := reqctx.QueryMap(c)
|
||||
rows, total := s.sysPostService.FindByPage(query)
|
||||
if total == 0 {
|
||||
// c.JSON(200, resp.CodeMsg(40016, "export data record as empty"))
|
||||
c.JSON(200, resp.CodeMsg(40016, i18n.TKey(language, "app.common.exportEmpty")))
|
||||
return
|
||||
}
|
||||
rows := data["rows"].([]model.SysPost)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
converI18n := func(language string, arr *[]model.SysPost) {
|
||||
@@ -258,11 +267,11 @@ func (s *SysPostController) Export(c *gin.Context) {
|
||||
for i, row := range rows {
|
||||
idx := strconv.Itoa(i + 2)
|
||||
statusValue := i18n.TKey(language, "dictData.disable")
|
||||
if row.Status == "1" {
|
||||
if row.StatusFlag == "1" {
|
||||
statusValue = i18n.TKey(language, "dictData.normal")
|
||||
}
|
||||
dataCells = append(dataCells, map[string]any{
|
||||
"A" + idx: row.PostID,
|
||||
"A" + idx: row.PostId,
|
||||
"B" + idx: row.PostName,
|
||||
"C" + idx: row.PostCode,
|
||||
"D" + idx: row.PostSort,
|
||||
@@ -274,7 +283,7 @@ func (s *SysPostController) Export(c *gin.Context) {
|
||||
// 导出数据表格
|
||||
saveFilePath, err := file.WriteSheet(headerCells, dataCells, fileName, "")
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user