Files
be.ems/src/modules/system/controller/sys_post.go
T

271 lines
7.4 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/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,
}
// 岗位信息
//
// PATH /system/post
type SysPostController struct {
// 岗位服务
sysPostService service.ISysPost
}
// 岗位列表
//
// GET /list
func (s *SysPostController) List(c *gin.Context) {
language := ctx.AcceptLanguage(c)
querys := ctx.QueryMap(c)
// 多语言值转key查询
if v, ok := querys["postName"]; ok && v != "" {
querys["postName"] = i18n.TFindKeyPrefix(language, "post", v.(string))
}
data := s.sysPostService.SelectPostPage(querys)
rows := data["rows"].([]model.SysPost)
// 闭包函数处理多语言
converI18n := func(language string, arr *[]model.SysPost) {
for i := range *arr {
(*arr)[i].PostName = i18n.TKey(language, (*arr)[i].PostName)
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
}
}
converI18n(language, &rows)
c.JSON(200, result.Ok(data))
}
// 岗位信息
//
// 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")))
return
}
data := s.sysPostService.SelectPostById(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))
return
}
c.JSON(200, result.Err(nil))
}
// 岗位新增
//
// POST /
func (s *SysPostController) Add(c *gin.Context) {
language := ctx.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")))
return
}
// 检查名称唯一
uniqueuPostName := s.sysPostService.CheckUniquePostName(body.PostName, "")
if !uniqueuPostName {
// 岗位新增【%s】失败,岗位名称已存在
msg := i18n.TTemplate(language, "post.errNameExists", map[string]any{"name": body.PostName})
c.JSON(200, result.ErrMsg(msg))
return
}
// 检查编码属性值唯一
uniquePostCode := s.sysPostService.CheckUniquePostCode(body.PostCode, "")
if !uniquePostCode {
// 岗位新增【%s】失败,岗位编码已存在
msg := i18n.TTemplate(language, "post.errCodeExists", map[string]any{"name": body.PostCode})
c.JSON(200, result.ErrMsg(msg))
return
}
body.CreateBy = ctx.LoginUserToUserName(c)
insertId := s.sysPostService.InsertPost(body)
if insertId != "" {
c.JSON(200, result.Ok(nil))
return
}
c.JSON(200, result.Err(nil))
}
// 岗位修改
//
// PUT /
func (s *SysPostController) Edit(c *gin.Context) {
language := ctx.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")))
return
}
// 检查是否存在
postInfo := s.sysPostService.SelectPostById(body.PostID)
if postInfo.PostID != body.PostID {
// 没有可访问岗位数据!
c.JSON(200, result.ErrMsg(i18n.TKey(language, "post.noData")))
return
}
// 检查名称唯一
uniqueuPostName := s.sysPostService.CheckUniquePostName(body.PostName, body.PostID)
if !uniqueuPostName {
// 岗位修改【%s】失败,岗位名称已存在
msg := i18n.TTemplate(language, "post.errNameExists", map[string]any{"name": body.PostName})
c.JSON(200, result.ErrMsg(msg))
return
}
// 检查编码属性值唯一
uniquePostCode := s.sysPostService.CheckUniquePostCode(body.PostCode, body.PostID)
if !uniquePostCode {
// 岗位修改【%s】失败,岗位编码已存在
msg := i18n.TTemplate(language, "post.errCodeExists", map[string]any{"name": body.PostCode})
c.JSON(200, result.ErrMsg(msg))
return
}
// 多语言非原始值
i18nValue := i18n.TKey(language, postInfo.PostName)
if i18nValue != postInfo.PostName {
i18n.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)
body.Remark = postInfo.Remark
}
body.UpdateBy = ctx.LoginUserToUserName(c)
rows := s.sysPostService.UpdatePost(body)
if rows > 0 {
c.JSON(200, result.Ok(nil))
return
}
c.JSON(200, result.Err(nil))
}
// 岗位删除
//
// 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")))
return
}
// 处理字符转id数组后去重
ids := strings.Split(postIds, ",")
uniqueIDs := parse.RemoveDuplicates(ids)
if len(uniqueIDs) <= 0 {
c.JSON(200, result.Err(nil))
return
}
rows, err := s.sysPostService.DeletePostByIds(uniqueIDs)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
c.JSON(200, result.OkMsg(msg))
}
// 导出岗位信息
//
// POST /export
func (s *SysPostController) Export(c *gin.Context) {
language := ctx.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")))
return
}
rows := data["rows"].([]model.SysPost)
// 闭包函数处理多语言
converI18n := func(language string, arr *[]model.SysPost) {
for i := range *arr {
(*arr)[i].PostName = i18n.TKey(language, (*arr)[i].PostName)
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
}
}
converI18n(language, &rows)
// 导出文件名称
fileName := fmt.Sprintf("post_export_%d_%d.xlsx", len(rows), time.Now().UnixMilli())
// 第一行表头标题
headerCells := map[string]string{
"A1": i18n.TKey(language, "post.export.id"),
"B1": i18n.TKey(language, "post.export.name"),
"C1": i18n.TKey(language, "post.export.code"),
"D1": i18n.TKey(language, "post.export.sort"),
"E1": i18n.TKey(language, "post.export.status"),
"F1": i18n.TKey(language, "post.export.time"),
}
// 从第二行开始的数据
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.PostID,
"B" + idx: row.PostName,
"C" + idx: row.PostCode,
"D" + idx: row.PostSort,
"E" + idx: statusValue,
"F" + idx: date.ParseDateToStr(row.CreateTime, date.YYYY_MM_DDTHH_MM_SSZ),
})
}
// 导出数据表格
saveFilePath, err := file.WriteSheet(headerCells, dataCells, fileName, "")
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
c.FileAttachment(saveFilePath, fileName)
}