sys_dict_data.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * @desc:字典数据管理
  3. * @company:云南奇讯科技有限公司
  4. * @Author: yixiaohu<yxh669@qq.com>
  5. * @Date: 2022/9/28 9:22
  6. */
  7. package sysDictData
  8. import (
  9. "context"
  10. "github.com/gogf/gf/v2/frame/g"
  11. "github.com/gogf/gf/v2/text/gstr"
  12. "github.com/gogf/gf/v2/util/gconv"
  13. "github.com/tiger1103/gfast/v3/api/v1/system"
  14. "github.com/tiger1103/gfast/v3/internal/app/common/consts"
  15. "github.com/tiger1103/gfast/v3/internal/app/common/dao"
  16. "github.com/tiger1103/gfast/v3/internal/app/common/model"
  17. "github.com/tiger1103/gfast/v3/internal/app/common/model/do"
  18. "github.com/tiger1103/gfast/v3/internal/app/common/service"
  19. systemConsts "github.com/tiger1103/gfast/v3/internal/app/system/consts"
  20. "github.com/tiger1103/gfast/v3/library/liberr"
  21. )
  22. func init() {
  23. service.RegisterSysDictData(New())
  24. }
  25. func New() *sSysDictData {
  26. return &sSysDictData{}
  27. }
  28. type sSysDictData struct {
  29. }
  30. // GetDictWithDataByType 通过字典键类型获取选项
  31. func (s *sSysDictData) GetDictWithDataByType(ctx context.Context, req *system.GetDictReq) (dict *system.GetDictRes,
  32. err error) {
  33. cache := service.Cache()
  34. cacheKey := consts.CacheSysDict + "_" + req.DictType
  35. //从缓存获取
  36. iDict := cache.GetOrSetFuncLock(ctx, cacheKey, func(ctx context.Context) (value interface{}, err error) {
  37. err = g.Try(ctx, func(ctx context.Context) {
  38. //从数据库获取
  39. dict = &system.GetDictRes{}
  40. //获取类型数据
  41. err = dao.SysDictType.Ctx(ctx).Where(dao.SysDictType.Columns().DictType, req.DictType).
  42. Where(dao.SysDictType.Columns().Status, 1).Fields(model.DictTypeRes{}).Scan(&dict.Info)
  43. liberr.ErrIsNil(ctx, err, "获取字典类型失败")
  44. err = dao.SysDictData.Ctx(ctx).Fields(model.DictDataRes{}).
  45. Where(dao.SysDictData.Columns().DictType, req.DictType).
  46. Order(dao.SysDictData.Columns().DictSort + " asc," +
  47. dao.SysDictData.Columns().DictCode + " asc").
  48. Scan(&dict.Values)
  49. liberr.ErrIsNil(ctx, err, "获取字典数据失败")
  50. })
  51. value = dict
  52. return
  53. }, 0, consts.CacheSysDictTag)
  54. if iDict != nil {
  55. err = gconv.Struct(iDict, &dict)
  56. if err != nil {
  57. return
  58. }
  59. }
  60. //设置给定的默认值
  61. for _, v := range dict.Values {
  62. if req.DefaultValue != "" {
  63. if gstr.Equal(req.DefaultValue, v.DictValue) {
  64. v.IsDefault = 1
  65. } else {
  66. v.IsDefault = 0
  67. }
  68. }
  69. }
  70. return
  71. }
  72. // List 获取字典数据
  73. func (s *sSysDictData) List(ctx context.Context, req *system.DictDataSearchReq) (res *system.DictDataSearchRes, err error) {
  74. res = new(system.DictDataSearchRes)
  75. err = g.Try(ctx, func(ctx context.Context) {
  76. m := dao.SysDictData.Ctx(ctx)
  77. if req != nil {
  78. if req.DictLabel != "" {
  79. m = m.Where(dao.SysDictData.Columns().DictLabel+" like ?", "%"+req.DictLabel+"%")
  80. }
  81. if req.Status != "" {
  82. m = m.Where(dao.SysDictData.Columns().Status+" = ", gconv.Int(req.Status))
  83. }
  84. if req.DictType != "" {
  85. m = m.Where(dao.SysDictData.Columns().DictType+" = ?", req.DictType)
  86. }
  87. res.Total, err = m.Count()
  88. liberr.ErrIsNil(ctx, err, "获取字典数据失败")
  89. if req.PageNum == 0 {
  90. req.PageNum = 1
  91. }
  92. res.CurrentPage = req.PageNum
  93. }
  94. if req.PageSize == 0 {
  95. req.PageSize = systemConsts.PageSize
  96. }
  97. err = m.Page(req.PageNum, req.PageSize).Order(dao.SysDictData.Columns().DictSort + " asc," +
  98. dao.SysDictData.Columns().DictCode + " asc").Scan(&res.List)
  99. liberr.ErrIsNil(ctx, err, "获取字典数据失败")
  100. })
  101. return
  102. }
  103. func (s *sSysDictData) Add(ctx context.Context, req *system.DictDataAddReq, userId uint64) (err error) {
  104. err = g.Try(ctx, func(ctx context.Context) {
  105. _, err = dao.SysDictData.Ctx(ctx).Insert(do.SysDictData{
  106. DictSort: req.DictSort,
  107. DictLabel: req.DictLabel,
  108. DictValue: req.DictValue,
  109. DictType: req.DictType,
  110. CssClass: req.CssClass,
  111. ListClass: req.ListClass,
  112. IsDefault: req.IsDefault,
  113. Status: req.Status,
  114. CreateBy: userId,
  115. Remark: req.Remark,
  116. })
  117. liberr.ErrIsNil(ctx, err, "添加字典数据失败")
  118. //清除缓存
  119. service.Cache().RemoveByTag(ctx, consts.CacheSysDictTag)
  120. })
  121. return
  122. }
  123. // Get 获取字典数据
  124. func (s *sSysDictData) Get(ctx context.Context, dictCode uint) (res *system.DictDataGetRes, err error) {
  125. res = new(system.DictDataGetRes)
  126. err = g.Try(ctx, func(ctx context.Context) {
  127. err = dao.SysDictData.Ctx(ctx).WherePri(dictCode).Scan(&res.Dict)
  128. liberr.ErrIsNil(ctx, err, "获取字典数据失败")
  129. })
  130. return
  131. }
  132. // Edit 修改字典数据
  133. func (s *sSysDictData) Edit(ctx context.Context, req *system.DictDataEditReq, userId uint64) (err error) {
  134. err = g.Try(ctx, func(ctx context.Context) {
  135. _, err = dao.SysDictData.Ctx(ctx).WherePri(req.DictCode).Update(do.SysDictData{
  136. DictSort: req.DictSort,
  137. DictLabel: req.DictLabel,
  138. DictValue: req.DictValue,
  139. DictType: req.DictType,
  140. CssClass: req.CssClass,
  141. ListClass: req.ListClass,
  142. IsDefault: req.IsDefault,
  143. Status: req.Status,
  144. UpdateBy: userId,
  145. Remark: req.Remark,
  146. })
  147. liberr.ErrIsNil(ctx, err, "修改字典数据失败")
  148. //清除缓存
  149. service.Cache().RemoveByTag(ctx, consts.CacheSysDictTag)
  150. })
  151. return
  152. }
  153. // Delete 删除字典数据
  154. func (s *sSysDictData) Delete(ctx context.Context, ids []int) (err error) {
  155. err = g.Try(ctx, func(ctx context.Context) {
  156. _, err = dao.SysDictData.Ctx(ctx).Where(dao.SysDictData.Columns().DictCode+" in(?)", ids).Delete()
  157. liberr.ErrIsNil(ctx, err, "删除字典数据失败")
  158. //清除缓存
  159. service.Cache().RemoveByTag(ctx, consts.CacheSysDictTag)
  160. })
  161. return
  162. }