exam_record.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. "reflect"
  10. "sort"
  11. "strings"
  12. "dashoo.cn/micro_libary/micro_srv"
  13. "dashoo.cn/micro_libary/myerrors"
  14. "dashoo.cn/micro_libary/request"
  15. "github.com/gogf/gf/os/gtime"
  16. "github.com/gogf/gf/util/gvalid"
  17. )
  18. type LearningExamRecordService struct {
  19. Dao *dao.LearningExamRecordDao
  20. TestpaperDao *dao.LearningTestpaperDao
  21. QuestionSrv *LearningQuestionService
  22. Tenant string
  23. userInfo request.UserInfo
  24. }
  25. func NewLearningExamRecordService(ctx context.Context) (*LearningExamRecordService, error) {
  26. tenant, err := micro_srv.GetTenant(ctx)
  27. if err != nil {
  28. return nil, fmt.Errorf("获取组合码异常:%s", err.Error())
  29. }
  30. // 获取用户信息
  31. userInfo, err := micro_srv.GetUserInfo(ctx)
  32. if err != nil {
  33. return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
  34. }
  35. tps, err := NewLearningQuestionService(ctx)
  36. if err != nil {
  37. return nil, err
  38. }
  39. return &LearningExamRecordService{
  40. Dao: dao.NewLearningExamRecordDao(tenant),
  41. TestpaperDao: dao.NewLearningTestpaperDao(tenant),
  42. QuestionSrv: tps,
  43. Tenant: tenant,
  44. userInfo: userInfo,
  45. }, nil
  46. }
  47. func (s LearningExamRecordService) List(ctx context.Context, req *learning.LearningExamRecordListReq) (int, []*learning.LearningExamRecordGetRsp, error) {
  48. m := s.Dao.DB.
  49. Table("learning_exam_record a").
  50. LeftJoin("learning_skill b", "a.SkillId=b.Id").
  51. LeftJoin("base_user c", "a.UserId=c.Id").
  52. LeftJoin("learning_testpaper d", "a.TestpaperId=d.Id")
  53. if req.UserId != 0 {
  54. m = m.Where("a.UserId = ?", req.UserId)
  55. }
  56. if req.SkillId != 0 {
  57. m = m.Where("a.SkillId = ?", req.SkillId)
  58. }
  59. if req.TestpaperId != 0 {
  60. m = m.Where("a.TestpaperId = ?", req.TestpaperId)
  61. }
  62. if req.Status != 0 {
  63. m = m.Where("a.Status = ?", req.Status)
  64. }
  65. total, err := m.Count()
  66. if err != nil {
  67. return 0, nil, err
  68. }
  69. if req.Page != nil {
  70. if req.Page.Current == 0 {
  71. req.Page.Current = 1
  72. }
  73. if req.Page.Size == 0 {
  74. req.Page.Size = 10
  75. }
  76. m = m.Page(req.Page.Current, req.Page.Size)
  77. }
  78. if req.OrderBy == nil {
  79. req.OrderBy = &model.OrderBy{}
  80. }
  81. if req.OrderBy.Value == "" {
  82. req.OrderBy.Value = "a.CreatedAt"
  83. req.OrderBy.Type = "desc"
  84. }
  85. if req.OrderBy != nil && req.OrderBy.Value != "" {
  86. order := "asc"
  87. if req.OrderBy.Type == "desc" {
  88. order = "desc"
  89. }
  90. m = m.Order(req.OrderBy.Value, order)
  91. }
  92. records := []*learning.LearningExamRecordGetRsp{}
  93. err = m.Fields(
  94. "a.*",
  95. "b.Name as SkillName",
  96. "c.Realname as UserName",
  97. "d.Name as TestpaperName").
  98. Structs(&records)
  99. if err != nil && err != sql.ErrNoRows {
  100. return 0, nil, err
  101. }
  102. for i := range records {
  103. if records[i].QuestionCount == 0 || records[i].CorrectCount == 0 {
  104. records[i].CorrectRate = "0%"
  105. }
  106. records[i].CorrectRate = fmt.Sprintf("%.f%%", float64(records[i].CorrectCount)/float64(records[i].QuestionCount)*100)
  107. }
  108. return total, records, err
  109. }
  110. func (s LearningExamRecordService) Add(ctx context.Context, req *learning.LearningExamRecordAddReq) (int, error) {
  111. validErr := gvalid.CheckStruct(ctx, req, nil)
  112. if validErr != nil {
  113. return 0, myerrors.NewMsgError(nil, validErr.Current().Error())
  114. }
  115. r, err := s.Dao.DB.Table("base_user").Where("Id = ?", req.UserId).One()
  116. if err != nil {
  117. return 0, err
  118. }
  119. if r.IsEmpty() {
  120. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("用户不存在: %d", req.UserId))
  121. }
  122. r, err = s.Dao.DB.Table("learning_skill").Where("Id = ?", req.SkillId).One()
  123. if err != nil {
  124. return 0, err
  125. }
  126. if r.IsEmpty() {
  127. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("技能不存在: %d", req.SkillId))
  128. }
  129. tp, err := s.TestpaperDao.Where("Id = ?", req.TestpaperId).One()
  130. if err != nil {
  131. return 0, err
  132. }
  133. if tp == nil {
  134. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("试卷不存在: %d", req.TestpaperId))
  135. }
  136. qustions, err := s.QuestionSrv.ListByTestpaperId(ctx, req.TestpaperId)
  137. if err != nil {
  138. return 0, err
  139. }
  140. questionCount := len(qustions)
  141. correctCount := 0
  142. userAnswerMap := map[int][]string{}
  143. for _, a := range req.Detail {
  144. userAnswerMap[a.QuestionId] = a.Answer
  145. }
  146. for _, q := range qustions {
  147. answer := strings.Split(strings.ToUpper(q.Answer), " ")
  148. userAnswer := userAnswerMap[q.Id]
  149. if len(answer) != len(userAnswer) {
  150. continue
  151. }
  152. sort.Strings(answer)
  153. sort.Strings(userAnswer)
  154. // fmt.Println(q, answer, userAnswer, reflect.DeepEqual(answer, userAnswer))
  155. if reflect.DeepEqual(answer, userAnswer) {
  156. correctCount++
  157. }
  158. }
  159. id, err := s.Dao.InsertAndGetId(learning.LearningExamRecord{
  160. UserId: req.UserId,
  161. SkillId: req.SkillId,
  162. TestpaperId: req.TestpaperId,
  163. QuestionCount: questionCount,
  164. CorrectCount: correctCount,
  165. Status: req.Status,
  166. CreatedAt: gtime.Now(),
  167. UpdatedAt: gtime.Now(),
  168. })
  169. return int(id), err
  170. }