learning_record.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package learning
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "lims_adapter/dao/learning"
  7. "lims_adapter/model"
  8. "lims_adapter/model/learning"
  9. "dashoo.cn/micro_libary/micro_srv"
  10. "dashoo.cn/micro_libary/myerrors"
  11. "dashoo.cn/micro_libary/request"
  12. "github.com/gogf/gf/os/gtime"
  13. "github.com/gogf/gf/util/gvalid"
  14. )
  15. type LearningLearningRecordService struct {
  16. Dao *dao.LearningLearningRecordDao
  17. Tenant string
  18. userInfo request.UserInfo
  19. }
  20. func NewLearningLearningRecordService(ctx context.Context) (*LearningLearningRecordService, error) {
  21. tenant, err := micro_srv.GetTenant(ctx)
  22. if err != nil {
  23. return nil, fmt.Errorf("获取组合码异常:%s", err.Error())
  24. }
  25. // 获取用户信息
  26. userInfo, err := micro_srv.GetUserInfo(ctx)
  27. if err != nil {
  28. return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
  29. }
  30. return &LearningLearningRecordService{
  31. Dao: dao.NewLearningLearningRecordDao(tenant),
  32. Tenant: tenant,
  33. userInfo: userInfo,
  34. }, nil
  35. }
  36. func (s LearningLearningRecordService) List(ctx context.Context, req *learning.LearningLearningRecordListReq) (int, []*learning.LearningLearningRecordGetRsp, error) {
  37. m := s.Dao.DB.
  38. Table("learning_learning_record a").
  39. LeftJoin("learning_material b", "a.MaterialId=b.Id").
  40. LeftJoin("learning_skill c", "b.SkillId=c.Id").
  41. LeftJoin("base_user d", "a.UserId=d.Id")
  42. if req.UserId != 0 {
  43. m = m.Where("a.UserId = ?", req.UserId)
  44. }
  45. if req.MaterialId != 0 {
  46. m = m.Where("a.MaterialId = ?", req.MaterialId)
  47. }
  48. if req.MaterialType != 0 {
  49. m = m.Where("b.Type = ?", req.MaterialType)
  50. }
  51. if req.Status != 0 {
  52. m = m.Where("a.Status = ?", req.Status)
  53. }
  54. total, err := m.Count()
  55. if err != nil {
  56. return 0, nil, err
  57. }
  58. if req.Page != nil {
  59. if req.Page.Current == 0 {
  60. req.Page.Current = 1
  61. }
  62. if req.Page.Size == 0 {
  63. req.Page.Size = 10
  64. }
  65. m = m.Page(req.Page.Current, req.Page.Size)
  66. }
  67. if req.OrderBy == nil {
  68. req.OrderBy = &model.OrderBy{}
  69. }
  70. if req.OrderBy.Value == "" {
  71. req.OrderBy.Value = "a.CreatedAt"
  72. req.OrderBy.Type = "desc"
  73. }
  74. if req.OrderBy != nil && req.OrderBy.Value != "" {
  75. order := "asc"
  76. if req.OrderBy.Type == "desc" {
  77. order = "desc"
  78. }
  79. m = m.Order(req.OrderBy.Value, order)
  80. }
  81. records := []*learning.LearningLearningRecordGetRsp{}
  82. err = m.Fields(
  83. "a.*",
  84. "b.Name as MaterialName",
  85. "b.Type as MaterialType",
  86. "c.Name as SkillName",
  87. "d.Realname as UserName").
  88. Structs(&records)
  89. if err != nil && err != sql.ErrNoRows {
  90. return 0, nil, err
  91. }
  92. return total, records, err
  93. }
  94. func (s LearningLearningRecordService) Add(ctx context.Context, req *learning.LearningLearningRecordAddReq) (int, error) {
  95. validErr := gvalid.CheckStruct(ctx, req, nil)
  96. if validErr != nil {
  97. return 0, myerrors.NewMsgError(nil, validErr.Current().Error())
  98. }
  99. r, err := s.Dao.DB.Table("base_user").Where("Id = ?", req.UserId).One()
  100. if err != nil {
  101. return 0, err
  102. }
  103. if r.IsEmpty() {
  104. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("用户不存在: %d", req.UserId))
  105. }
  106. r, err = s.Dao.DB.Table("learning_material").Where("Id = ?", req.MaterialId).One()
  107. if err != nil {
  108. return 0, err
  109. }
  110. if r.IsEmpty() {
  111. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("资料不存在: %d", req.MaterialId))
  112. }
  113. id, err := s.Dao.InsertAndGetId(learning.LearningLearningRecord{
  114. UserId: req.UserId,
  115. MaterialId: req.MaterialId,
  116. Status: req.Status,
  117. CreatedAt: gtime.Now(),
  118. UpdatedAt: gtime.Now(),
  119. })
  120. return int(id), err
  121. }
  122. func (s LearningLearningRecordService) LearntMaterialIds(ctx context.Context, userId int) ([]int, error) {
  123. _, records, err := s.List(ctx, &learning.LearningLearningRecordListReq{
  124. UserId: userId,
  125. Status: 1,
  126. })
  127. if err != nil {
  128. return nil, err
  129. }
  130. ids := []int{}
  131. for _, r := range records {
  132. ids = append(ids, r.MaterialId)
  133. }
  134. return ids, err
  135. }