exam_record.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 LearningExamRecordService struct {
  16. Dao *dao.LearningExamRecordDao
  17. Tenant string
  18. userInfo request.UserInfo
  19. }
  20. func NewLearningExamRecordService(ctx context.Context) (*LearningExamRecordService, 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 &LearningExamRecordService{
  31. Dao: dao.NewLearningExamRecordDao(tenant),
  32. Tenant: tenant,
  33. userInfo: userInfo,
  34. }, nil
  35. }
  36. func (s LearningExamRecordService) List(ctx context.Context, req *learning.LearningExamRecordListReq) (int, []*learning.LearningExamRecordGetRsp, error) {
  37. m := s.Dao.DB.
  38. Table("learning_exam_record a").
  39. LeftJoin("learning_skill b", "a.SkillId=b.Id").
  40. LeftJoin("base_user c", "a.UserId=c.Id").
  41. LeftJoin("learning_testpaper d", "a.TestpaperId=d.Id")
  42. if req.UserId != 0 {
  43. m = m.Where("a.UserId = ?", req.UserId)
  44. }
  45. if req.SkillId != 0 {
  46. m = m.Where("a.SkillId = ?", req.SkillId)
  47. }
  48. if req.TestpaperId != 0 {
  49. m = m.Where("a.TestpaperId = ?", req.TestpaperId)
  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.LearningExamRecordGetRsp{}
  82. err = m.Fields(
  83. "a.*",
  84. "b.Name as SkillName",
  85. "c.Realname as UserName",
  86. "d.Name as TestpaperName").
  87. Structs(&records)
  88. if err != nil && err != sql.ErrNoRows {
  89. return 0, nil, err
  90. }
  91. return total, records, err
  92. }
  93. func (s LearningExamRecordService) Add(ctx context.Context, req *learning.LearningExamRecordAddReq) (int, error) {
  94. validErr := gvalid.CheckStruct(ctx, req, nil)
  95. if validErr != nil {
  96. return 0, myerrors.NewMsgError(nil, validErr.Current().Error())
  97. }
  98. r, err := s.Dao.DB.Table("base_user").Where("Id = ?", req.UserId).One()
  99. if err != nil {
  100. return 0, err
  101. }
  102. if r.IsEmpty() {
  103. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("用户不存在: %d", req.UserId))
  104. }
  105. r, err = s.Dao.DB.Table("learning_skill").Where("Id = ?", req.SkillId).One()
  106. if err != nil {
  107. return 0, err
  108. }
  109. if r.IsEmpty() {
  110. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("技能不存在: %d", req.SkillId))
  111. }
  112. r, err = s.Dao.DB.Table("learning_testpaper").Where("Id = ?", req.TestpaperId).One()
  113. if err != nil {
  114. return 0, err
  115. }
  116. if r.IsEmpty() {
  117. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("试卷不存在: %d", req.TestpaperId))
  118. }
  119. id, err := s.Dao.InsertAndGetId(learning.LearningExamRecord{
  120. UserId: req.UserId,
  121. SkillId: req.SkillId,
  122. TestpaperId: req.TestpaperId,
  123. Status: req.Status,
  124. CreatedAt: gtime.Now(),
  125. UpdatedAt: gtime.Now(),
  126. })
  127. return int(id), err
  128. }