| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- package system
- import (
- "fmt"
- "github.com/demdxx/gocast"
- "github.com/gin-gonic/gin"
- "go_server/model/common/response"
- system2 "go_server/model/system"
- "go_server/service/base"
- )
- type DictionaryService struct {
- base.SysCommonService
- }
- // 字典服务 增 删 改 查
- // 所有字典信息
- func (s *DictionaryService) DictionariesTree(c *gin.Context) {
- type Detail struct {
- DictionaryId int64 `json:"dictionaryId" gorm:"comment:字典ID"`
- Label string `json:"label" gorm:"type:varchar(50);comment:展示值"`
- Value string `json:"value" gorm:"type:varchar(50);comment:字典值"`
- Extend string `json:"extend" gorm:"type:varchar(100);comment:扩展值"`
- Sort int64 `json:"sort" gorm:"comment:排序"`
- }
- type DictionaryNode struct {
- Key string `json:"key"`
- Name string `json:"name"`
- Desc string `json:"desc"`
- Details []*Detail `json:"details"`
- }
- dictionaryMap := make(map[int64]*DictionaryNode)
- dictionaries, err := base.GetMore[system2.Dictionaries](s.DB(), "enable", true)
- if err != nil {
- response.Resp(c, err.Error())
- return
- }
- for _, item := range dictionaries {
- dictionaryMap[item.ID] = &DictionaryNode{
- Key: item.Key,
- Name: item.Name,
- Desc: item.Desc,
- Details: make([]*Detail, 0),
- }
- }
- dictionaryDetails, err := base.GetMore[system2.DictionaryDetail](s.DB().Order("sort"), "enable", true)
- if err != nil {
- response.Resp(c, err.Error())
- return
- }
- for _, item := range dictionaryDetails {
- if _, ok := dictionaryMap[item.DictionaryId]; ok {
- dictionaryMap[item.DictionaryId].Details = append(dictionaryMap[item.DictionaryId].Details, &Detail{
- DictionaryId: item.DictionaryId,
- Label: item.Label,
- Value: item.Value,
- Extend: item.Extend,
- Sort: item.Sort,
- })
- }
- }
- res := make(map[string]interface{})
- res["dictionaryMap"] = dictionaryMap
- response.Resp(c, res)
- return
- }
- // --finish
- func (s *DictionaryService) FindDictionaries(c *gin.Context) {
- type request[T any] struct {
- base.ListRequest[T]
- Id interface{} `form:"id"`
- Name string `json:"name" gorm:"column:name;type:varchar(50);comment:名称"`
- Key string `json:"key" gorm:"column:key;;unique;type:varchar(50);comment:关键词"`
- Enable interface{} `json:"enable" gorm:"column:enable;type:tinyint(1);comment:是否有效"`
- }
- req := new(request[system2.Dictionaries])
- if err := c.BindQuery(req); err != nil {
- response.Resp(c, err.Error())
- return
- }
- db := s.DB()
- if req.Id != nil && gocast.ToInt64(req.Id) != 0 {
- db = db.Where("id", req.Id)
- }
- if req.Name != "" {
- db = db.Where("label LIKE ?", "%"+req.Name+"%")
- }
- if req.Key != "" {
- db = db.Where("`key` LIKE ?", "%"+req.Key+"%")
- }
- if req.Enable != nil {
- db = db.Where("enable = ?", gocast.ToBool(req.Enable))
- }
- resp, err := base.NewQueryBaseHandler(system2.NewDictionaries()).List(db, req)
- if err != nil {
- response.Resp(c, err.Error())
- return
- }
- response.Resp(c, resp)
- }
- func (s *DictionaryService) CreateDictionary(c *gin.Context) {
- userId := c.GetInt64("userId")
- // 限制用户增加必须管理员才可以操作
- if userId != system2.AdminId {
- response.Resp(c, "不允许操作")
- return
- }
- type request struct {
- Name string `json:"name" gorm:"column:name;type:varchar(50);comment:名称"`
- Key string `json:"key" gorm:"column:key;;unique;type:varchar(50);comment:关键词"`
- Desc string `json:"desc" gorm:"column:desc;type:varchar(255);comment:说明"`
- }
- req := new(request)
- if err := c.BindJSON(req); err != nil {
- response.Resp(c, err.Error())
- return
- }
- if base.CountRow[system2.Dictionaries](s.DB(), "key", req.Key) > 0 {
- response.Resp(c, "key 不可重复")
- return
- }
- row := &system2.Dictionaries{
- Name: req.Name,
- Key: req.Key,
- Enable: true,
- Desc: req.Desc,
- }
- if err := s.DB().Create(&row).Error; err != nil {
- response.Resp(c, "添加失败")
- return
- }
- response.Resp(c)
- return
- }
- func (s *DictionaryService) UpdateDictionary(c *gin.Context) {
- userId := c.GetInt64("userId")
- // 限制用户增加必须管理员才可以操作
- if userId != system2.AdminId {
- response.Resp(c, "不允许操作")
- return
- }
- type request struct {
- Name string `json:"name" gorm:"column:name;type:varchar(50);comment:名称"`
- Key string `json:"key" gorm:"column:key;;unique;type:varchar(50);comment:关键词"`
- Enable interface{} `json:"enable"`
- Desc string `json:"desc" gorm:"column:desc;type:varchar(255);comment:说明"`
- }
- req := new(request)
- if err := c.BindJSON(req); err != nil {
- response.Resp(c, err.Error())
- return
- }
- if req.Key == "" || req.Name == "" {
- response.Resp(c, "key, name 必填")
- return
- }
- row, ok := base.GetOne[system2.Dictionaries](s.DB(), "key", req.Key)
- if !ok {
- response.Resp(c, fmt.Sprintf("字典:%s不存在", req.Key))
- return
- }
- row.Name = req.Name
- row.Desc = req.Desc
- row.Enable = gocast.ToBool(req.Enable)
- if err := s.DB().Save(&row).Error; err != nil {
- response.Resp(c, fmt.Sprintf("修改失败:%s", err.Error()))
- return
- }
- response.Resp(c)
- return
- }
- func (s *DictionaryService) DelDictionary(c *gin.Context) {
- userId := c.GetInt64("userId")
- // 限制用户增加必须管理员才可以操作
- if userId != system2.AdminId {
- response.Resp(c, "不允许操作")
- return
- }
- type request struct {
- Key string `json:"key" gorm:"column:key;;unique;type:varchar(50);comment:关键词"`
- }
- req := new(request)
- if err := c.BindJSON(req); err != nil {
- response.Resp(c, err.Error())
- return
- }
- row, ok := base.GetOne[system2.Dictionaries](s.DB(), "key", req.Key)
- if !ok {
- response.Resp(c, fmt.Sprintf("字典:%s不存在", req.Key))
- return
- }
- txDb := s.DB().Begin()
- if err := txDb.Delete(&row).Error; err != nil {
- response.Resp(c, "删除失败")
- txDb.Rollback()
- return
- }
- if err := txDb.
- Where("dictionary_id", row.ID).
- Delete(&system2.DictionaryDetail{}).Error; err != nil {
- response.Resp(c, "删除失败")
- txDb.Rollback()
- return
- }
- txDb.Commit()
- response.Resp(c)
- return
- }
- // --finish
- func (s *DictionaryService) FindDictionaryDetail(c *gin.Context) {
- type request[T any] struct {
- base.ListRequest[T]
- Id interface{} `form:"id"`
- Label string `json:"label" gorm:"type:varchar(50);comment:展示值"`
- Value string `json:"value" gorm:"type:varchar(50);comment:字典值"`
- Enable interface{} `json:"enable" gorm:"column:enable;type:tinyint(1);comment:是否有效"`
- }
- req := new(request[system2.DictionaryDetail])
- if err := c.BindQuery(req); err != nil {
- response.Resp(c, err.Error())
- return
- }
- db := s.DB()
- if req.Id != nil && gocast.ToInt64(req.Id) != 0 {
- db = db.Where("id", req.Id)
- }
- if req.Label != "" {
- db = db.Where("label LIKE ?", "%"+req.Label+"%")
- }
- if req.Value != "" {
- db = db.Where("value LIKE ?", "%"+req.Value+"%")
- }
- if req.Enable != nil {
- db = db.Where("enable = ?", gocast.ToBool(req.Enable))
- }
- resp, err := base.NewQueryBaseHandler(system2.NewDictionaryDetail()).List(db, req)
- if err != nil {
- response.Resp(c, err.Error())
- return
- }
- response.Resp(c, resp)
- }
- func (s *DictionaryService) CreateDictionaryDetail(c *gin.Context) {
- userId := c.GetInt64("userId")
- // 限制用户增加必须管理员才可以操作
- if userId != system2.AdminId {
- response.Resp(c, "不允许操作")
- return
- }
- type request struct {
- DictionaryId interface{} `json:"dictionaryId" gorm:"comment:字典ID"`
- Label string `json:"label" gorm:"type:varchar(50);comment:展示值"`
- Value string `json:"value" gorm:"type:varchar(50);comment:字典值"`
- Extend string `json:"extend" gorm:"type:varchar(100);comment:扩展值"`
- Enable interface{} `json:"enable" gorm:"column:enable;type:tinyint(1);comment:是否有效"`
- Sort interface{} `json:"sort" gorm:"comment:排序"`
- }
- req := new(request)
- if err := c.BindJSON(req); err != nil {
- response.Resp(c, err.Error())
- return
- }
- if req.DictionaryId == nil || req.Label == "" || req.Value == "" {
- response.Resp(c, "DictionaryId,Label,Value 必填")
- return
- }
- if base.CountRow[system2.Dictionaries](s.DB(), "id", req.DictionaryId) == 0 {
- response.Resp(c, "DictionaryId不存在")
- return
- }
- row := &system2.DictionaryDetail{
- DictionaryId: gocast.ToInt64(req.DictionaryId),
- Label: req.Label,
- Value: req.Value,
- Extend: req.Extend,
- Enable: gocast.ToBool(req.Enable),
- Sort: gocast.ToInt64(req.Sort),
- }
- if err := s.DB().Create(&row).Error; err != nil {
- response.Resp(c, "添加失败")
- return
- }
- response.Resp(c)
- return
- }
- func (s *DictionaryService) UpdateDictionaryDetail(c *gin.Context) {
- userId := c.GetInt64("userId")
- // 限制用户增加必须管理员才可以操作
- if err := s.CheckIsAdmin(userId); err != nil {
- response.Resp(c, err.Error())
- return
- }
- type request struct {
- Id interface{} `json:"id"`
- Label string `json:"label" gorm:"type:varchar(50);comment:展示值"`
- Value string `json:"value" gorm:"type:varchar(50);comment:字典值"`
- Extend string `json:"extend" gorm:"type:varchar(100);comment:扩展值"`
- Enable interface{} `json:"enable" gorm:"column:enable;type:tinyint(1);comment:是否有效"`
- Sort interface{} `json:"sort" gorm:"comment:排序"`
- }
- req := new(request)
- if err := c.BindJSON(req); err != nil {
- response.Resp(c, err.Error())
- return
- }
- if req.Id == nil || req.Id == 0 || req.Label == "" || req.Value == "" {
- response.Resp(c, "Id、label、value必填")
- return
- }
- row, ok := base.GetOne[system2.DictionaryDetail](s.DB(), "id", req.Id)
- if !ok {
- response.Resp(c, fmt.Sprintf("字典值:%s不存在", req.Id))
- return
- }
- row.Label = req.Label
- row.Value = req.Value
- row.Extend = req.Extend
- row.Sort = gocast.ToInt64(req.Sort)
- row.Enable = gocast.ToBool(req.Enable)
- if err := s.DB().Save(&row).Error; err != nil {
- response.Resp(c, fmt.Sprintf("修改失败:%s", err.Error()))
- return
- }
- response.Resp(c)
- return
- }
- func (s *DictionaryService) DelDictionaryDetail(c *gin.Context) {
- userId := c.GetInt64("userId")
- if err := s.CheckIsAdmin(userId); err != nil {
- response.Resp(c, err.Error())
- return
- }
- type request struct {
- Id interface{} `json:"id"`
- }
- req := new(request)
- if err := c.BindJSON(req); err != nil {
- response.Resp(c, err.Error())
- return
- }
- if req.Id == nil || gocast.ToInt64(req.Id) == 0 {
- response.Resp(c, "Id必填")
- return
- }
- row, ok := base.GetOne[system2.DictionaryDetail](s.DB(), "id", req.Id)
- if !ok {
- response.Resp(c, fmt.Sprintf("字典值:%s不存在", req.Id))
- return
- }
- if err := s.DB().Delete(&row).Error; err != nil {
- response.Resp(c, fmt.Sprintf("修改失败:%s", err.Error()))
- return
- }
- response.Resp(c)
- }
|