sys_dict_data.go 4.2 KB

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