sys_dict_data.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package service
  2. import (
  3. "context"
  4. "dashoo.cn/micro/app/common/global"
  5. comService "dashoo.cn/micro/app/common/service"
  6. "dashoo.cn/micro/app/dao"
  7. "dashoo.cn/micro/app/model"
  8. "database/sql"
  9. "github.com/gogf/gf/errors/gerror"
  10. "github.com/gogf/gf/frame/g"
  11. "github.com/gogf/gf/text/gstr"
  12. "github.com/gogf/gf/util/gconv"
  13. )
  14. type dictDataService struct {
  15. *contextService
  16. Dao *dao.SysDictDataDao
  17. }
  18. func NewDictDataService(ctx context.Context) (svc *dictDataService, err error) {
  19. svc = new(dictDataService)
  20. if svc.contextService, err = svc.Init(ctx); err != nil {
  21. return nil, err
  22. }
  23. svc.Dao = dao.NewSysDictDataDao(svc.Tenant)
  24. svc.Table = svc.Dao.Table
  25. return svc, nil
  26. }
  27. func (s *dictDataService) GetDictDataList(req *model.SelectDictPageReq) (total int, list []*model.SysDictData, err error) {
  28. db := s.Dao.Ctx(req.Ctx)
  29. if req != nil {
  30. if req.DictLabel != "" {
  31. db = db.Where(s.Dao.Columns.DictLabel+" like ?", "%"+req.DictLabel+"%")
  32. }
  33. if req.Status != "" {
  34. db = db.Where(s.Dao.Columns.Status+" = ", gconv.Int(req.Status))
  35. }
  36. if req.DictType != "" {
  37. db = db.Where(s.Dao.Columns.DictType+" = ?", req.DictType)
  38. }
  39. total, err = db.Count()
  40. if err != nil {
  41. g.Log().Error(err)
  42. err = gerror.New("获取总行数失败")
  43. return
  44. }
  45. }
  46. list, err = db.Page(req.GetPage()).Order(s.Dao.Columns.DictSort + " asc," + s.Dao.Columns.DictCode + " asc").All()
  47. if err != nil {
  48. g.Log().Error(err)
  49. err = gerror.New("获取数据失败")
  50. return
  51. }
  52. return
  53. }
  54. // GetDictDataById 通过字典数据id获取字典数据
  55. func (s *dictDataService) GetDictDataById(id int64) (data *model.SysDictData, err error) {
  56. data, err = s.Dao.FindOne(s.Dao.Columns.DictCode, id)
  57. if err != nil {
  58. g.Log().Error(err)
  59. err = gerror.New("获取字典数据失败")
  60. return
  61. }
  62. if data == nil {
  63. err = gerror.New("获取字典数据失败")
  64. }
  65. return
  66. }
  67. // GetDictWithDataByType 通过字典键类型获取选项
  68. func (s *dictDataService) GetDictWithDataByType(req *model.GetDictReq) (dict *model.DictRes, err error) {
  69. cache := comService.Cache.New()
  70. cacheKey := global.SysDict + "_" + req.DictType
  71. //从缓存获取
  72. iDict := cache.Get(cacheKey)
  73. if iDict != nil {
  74. err = gconv.Struct(iDict, &dict)
  75. if err != nil {
  76. return
  77. }
  78. } else {
  79. //从数据库获取
  80. dict = &model.DictRes{}
  81. //获取类型数据
  82. err = s.Dao.DB.Model("sys_dict_type").Where(dao.SysDictType.Columns.DictType, req.DictType).
  83. Where(dao.SysDictType.Columns.Status, "10").Fields(model.DictTypeRes{}).Scan(&dict.Info)
  84. if err != nil {
  85. g.Log().Error(err)
  86. err = gerror.New("获取字典类型失败")
  87. }
  88. err = s.Dao.Where(s.Dao.Columns.DictType, req.DictType).Where(s.Dao.Columns.Status, "10").
  89. Order(s.Dao.Columns.DictSort + " asc," + s.Dao.Columns.DictCode + " asc").
  90. Scan(&dict.Values)
  91. if err != nil {
  92. g.Log().Error(err)
  93. err = gerror.New("获取字典数据失败")
  94. }
  95. //缓存菜单
  96. if dict.Info != nil && dict.Values != nil {
  97. cache.Set(cacheKey, dict, 0, global.SysDictTag)
  98. }
  99. }
  100. //设置给定的默认值
  101. for _, v := range dict.Values {
  102. if req.DefaultValue != "" {
  103. if gstr.Equal(req.DefaultValue, v.DictValue) {
  104. v.IsDefault = 1
  105. } else {
  106. v.IsDefault = 0
  107. }
  108. }
  109. }
  110. return
  111. }
  112. // CheckDictTypeUniqueAll 检查字典类型是否唯一
  113. func (s *dictDataService) CheckDictTypeUniqueAll(dictType string) bool {
  114. dict, err := s.Dao.FindOne(s.Dao.Columns.DictType+"=?", dictType)
  115. if err != nil {
  116. g.Log().Error(err)
  117. return false
  118. }
  119. if dict != nil {
  120. return false
  121. }
  122. return true
  123. }
  124. // Create 添加保存字典数据
  125. func (s *dictDataService) Create(req *model.DictDataAddReq) (id int64, err error) {
  126. var res sql.Result
  127. data := new(model.SysDictData)
  128. if err := gconv.Struct(req, data); err != nil {
  129. return -1, err
  130. }
  131. SetCreatedInfo(data, s.GetCxtUserId(), s.GetCxtUserName())
  132. res, err = s.Dao.Data(data).Insert()
  133. if err != nil {
  134. g.Log().Error(err)
  135. err = gerror.New("添加字典数据失败")
  136. return
  137. }
  138. id, err = res.LastInsertId()
  139. return
  140. }
  141. // UpdateByDict 修改字典数据
  142. func (s *dictDataService) UpdateByDict(req *model.EditDictDataReq) (err error) {
  143. data := new(model.SysDictData)
  144. if err := gconv.Struct(req, data); err != nil {
  145. return err
  146. }
  147. SetUpdatedInfo(data, s.GetCxtUserId(), s.GetCxtUserName())
  148. updateFieldEx := append(CommonUpdateFieldEx, dao.SysDictData.Columns.DictCode)
  149. _, err = s.Dao.FieldsEx(updateFieldEx...).WherePri(req.DictCode).Update(data)
  150. return
  151. }
  152. // DeleteDictDataByIds 删除字典数据
  153. func (s *dictDataService) DeleteDictDataByIds(ids []int64) error {
  154. _, err := s.Dao.Where(s.Dao.Columns.DictCode+" in(?)", ids).Delete()
  155. if err != nil {
  156. g.Log().Error(err)
  157. return gerror.New("删除失败")
  158. }
  159. return nil
  160. }