sys_dict_data.go 4.4 KB

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