exam_record.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. if req.UserId != 0 {
  41. m = m.Where("a.UserId = ?", req.UserId)
  42. }
  43. if req.SkillId != 0 {
  44. m = m.Where("a.SkillId = ?", req.SkillId)
  45. }
  46. if req.TestpaperId != 0 {
  47. m = m.Where("a.TestpaperId = ?", req.TestpaperId)
  48. }
  49. if req.Status != 0 {
  50. m = m.Where("a.Status = ?", req.Status)
  51. }
  52. total, err := m.Count()
  53. if err != nil {
  54. return 0, nil, err
  55. }
  56. if req.Page != nil {
  57. if req.Page.Current == 0 {
  58. req.Page.Current = 1
  59. }
  60. if req.Page.Size == 0 {
  61. req.Page.Size = 10
  62. }
  63. m = m.Page(req.Page.Current, req.Page.Size)
  64. }
  65. if req.OrderBy != nil && req.OrderBy.Value != "" {
  66. order := "asc"
  67. if req.OrderBy.Type == "desc" {
  68. order = "desc"
  69. }
  70. m = m.Order(req.OrderBy.Value, order)
  71. }
  72. records := []*learning.LearningExamRecordGetRsp{}
  73. err = m.Fields("a.*", "b.Name as SkillName", "c.Realname as UserName").
  74. Structs(&records)
  75. if err != nil && err != sql.ErrNoRows {
  76. return 0, nil, err
  77. }
  78. return total, records, err
  79. }
  80. func (s LearningExamRecordService) Add(ctx context.Context, req *learning.LearningExamRecordAddReq) (int, error) {
  81. validErr := gvalid.CheckStruct(ctx, req, nil)
  82. if validErr != nil {
  83. return 0, myerrors.NewMsgError(nil, validErr.Current().Error())
  84. }
  85. r, err := s.Dao.DB.Table("base_user").Where("Id = ?", req.UserId).One()
  86. if err != nil {
  87. return 0, err
  88. }
  89. if r.IsEmpty() {
  90. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("用户不存在: %d", req.UserId))
  91. }
  92. r, err = s.Dao.DB.Table("learning_skill").Where("Id = ?", req.SkillId).One()
  93. if err != nil {
  94. return 0, err
  95. }
  96. if r.IsEmpty() {
  97. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("技能不存在: %d", req.SkillId))
  98. }
  99. r, err = s.Dao.DB.Table("learning_testpaper").Where("Id = ?", req.TestpaperId).One()
  100. if err != nil {
  101. return 0, err
  102. }
  103. if r.IsEmpty() {
  104. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("试卷不存在: %d", req.TestpaperId))
  105. }
  106. id, err := s.Dao.InsertAndGetId(learning.LearningExamRecord{
  107. UserId: req.UserId,
  108. SkillId: req.SkillId,
  109. TestpaperId: req.TestpaperId,
  110. Status: req.Status,
  111. CreatedAt: gtime.Now(),
  112. UpdatedAt: gtime.Now(),
  113. })
  114. return int(id), err
  115. }