6
0

exam_record.go 3.4 KB

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