sys_dict_data.go 4.9 KB

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