learning_record.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.Status != 0 {
  48. m = m.Where("a.Status = ?", req.Status)
  49. }
  50. total, err := m.Count()
  51. if err != nil {
  52. return 0, nil, err
  53. }
  54. if req.Page != nil {
  55. if req.Page.Current == 0 {
  56. req.Page.Current = 1
  57. }
  58. if req.Page.Size == 0 {
  59. req.Page.Size = 10
  60. }
  61. m = m.Page(req.Page.Current, req.Page.Size)
  62. }
  63. if req.OrderBy != nil && req.OrderBy.Value != "" {
  64. order := "asc"
  65. if req.OrderBy.Type == "desc" {
  66. order = "desc"
  67. }
  68. m = m.Order(req.OrderBy.Value, order)
  69. }
  70. records := []*learning.LearningLearningRecordGetRsp{}
  71. err = m.Fields(
  72. "a.*",
  73. "b.Name as MaterialName",
  74. "c.Name as SkillName",
  75. "d.Realname as UserName").
  76. Structs(&records)
  77. if err != nil && err != sql.ErrNoRows {
  78. return 0, nil, err
  79. }
  80. return total, records, err
  81. }
  82. func (s LearningLearningRecordService) Add(ctx context.Context, req *learning.LearningLearningRecordAddReq) (int, error) {
  83. validErr := gvalid.CheckStruct(ctx, req, nil)
  84. if validErr != nil {
  85. return 0, myerrors.NewMsgError(nil, validErr.Current().Error())
  86. }
  87. r, err := s.Dao.DB.Table("base_user").Where("Id = ?", req.UserId).One()
  88. if err != nil {
  89. return 0, err
  90. }
  91. if r.IsEmpty() {
  92. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("用户不存在: %d", req.UserId))
  93. }
  94. r, err = s.Dao.DB.Table("learning_material").Where("Id = ?", req.MaterialId).One()
  95. if err != nil {
  96. return 0, err
  97. }
  98. if r.IsEmpty() {
  99. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("资料不存在: %d", req.MaterialId))
  100. }
  101. id, err := s.Dao.InsertAndGetId(learning.LearningLearningRecord{
  102. UserId: req.UserId,
  103. MaterialId: req.MaterialId,
  104. Status: req.Status,
  105. CreatedAt: gtime.Now(),
  106. UpdatedAt: gtime.Now(),
  107. })
  108. return int(id), err
  109. }
  110. func (s LearningLearningRecordService) LearntMaterialIds(ctx context.Context, userId int) ([]int, error) {
  111. _, records, err := s.List(ctx, &learning.LearningLearningRecordListReq{
  112. UserId: userId,
  113. Status: 1,
  114. })
  115. if err != nil {
  116. return nil, err
  117. }
  118. ids := []int{}
  119. for _, r := range records {
  120. ids = append(ids, r.MaterialId)
  121. }
  122. return ids, err
  123. }