6
0

learning_record.go 3.6 KB

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