check.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package check
  2. import (
  3. "errors"
  4. "time"
  5. "zhiyuan/models"
  6. "zhiyuan/pkg/app"
  7. "zhiyuan/pkg/db"
  8. "zhiyuan/pkg/utils"
  9. "zhiyuan/services/train/course"
  10. "zhiyuan/services/train/question"
  11. )
  12. var Check models.UserCheck
  13. const courseId = 13
  14. func CheckCourse(userID int) (int64, error) {
  15. courseInfo, _ := course.GetInfoByID(courseId, nil, nil)
  16. if courseInfo == nil {
  17. return 0, errors.New("invalid id")
  18. }
  19. now := time.Now()
  20. today0 := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).Unix()
  21. test, _ := GetOne(map[string]interface{}{"course_id": courseInfo.ID, "user_id": userID, "created_at >=": today0}, nil, nil)
  22. if test != nil {
  23. return 0, errors.New("今天已答题")
  24. }
  25. qWhere := map[string]string{
  26. "where": "`zy_question`.`id`>0 AND `zy_question`.`deleted_at`=0 AND `zy_question`.`show`=1 AND `zy_question`.`course_id` = {{course_id}}",
  27. "_order_by": "rand()",
  28. "_page_size": utils.ToStr(courseInfo.Checks),
  29. "_page_num": utils.ToStr(1),
  30. }
  31. qParam := map[string]interface{}{
  32. "course_id": courseInfo.ID,
  33. }
  34. questionList := make([]models.Question, 0)
  35. if err := question.GetQuestionsRaw(qWhere, qParam, &questionList); err != nil {
  36. return 0, nil
  37. }
  38. if len(questionList) == 0 {
  39. return 0, errors.New("无考核试题")
  40. }
  41. checkMap := map[string]interface{}{
  42. "`user_id`": userID,
  43. "`total`": len(questionList),
  44. }
  45. checkID, err := db.InsertOne(Check.TableName(), checkMap)
  46. if err != nil {
  47. return 0, nil
  48. }
  49. for _, v := range questionList {
  50. checksMap := map[string]interface{}{
  51. "`check_id`": checkID,
  52. "`question_id`": v.ID,
  53. }
  54. _, err := db.InsertOne(models.UserChecks{}.TableName(), checksMap)
  55. if err != nil {
  56. db.DeleteSoft(models.UserChecks{}.TableName(), map[string]interface{}{"check_id": checkID})
  57. db.DeleteSoft(Check.TableName(), map[string]interface{}{"id": checkID})
  58. return 0, nil
  59. }
  60. }
  61. return checkID, nil
  62. }
  63. func SubmitCheck(check_id int, userID int) error {
  64. checkInfo, _ := GetInfoByID(check_id, nil, nil)
  65. if checkInfo == nil {
  66. return errors.New("invalid id")
  67. }
  68. if checkInfo.UserId != userID {
  69. return errors.New("权限错误")
  70. }
  71. if checkInfo.State != 0 {
  72. return errors.New("考核状态错误")
  73. }
  74. if checkInfo.Total != checkInfo.Progress {
  75. return errors.New("存在未作答的试题")
  76. }
  77. right, err := db.GetOneMapRaw("select count(distinct `zy_user_checks`.`id`) as `count` from `zy_user_checks` left join `zy_question` on `zy_question`.`id` = `zy_user_checks`.`question_id` WHERE `zy_user_checks`.`check_id` = {{check_id}} AND `zy_user_checks`.`answer` = `zy_question`.`answer`", map[string]interface{}{"check_id": checkInfo.ID})
  78. if err != nil {
  79. return err
  80. }
  81. rightCount := utils.ToInt64(right["count"])
  82. integral := rightCount * 5 / int64(checkInfo.Total)
  83. db.InsertModel(db.Type(models.UserIntegral{}), map[string]interface{}{
  84. "user_id": userID,
  85. "num": integral,
  86. "explain": "每日答题",
  87. })
  88. checkMap := map[string]interface{}{
  89. "`right`": utils.ToInt64(right["count"]),
  90. "`state`": 1,
  91. }
  92. _, err = db.Update(Check.TableName(), map[string]interface{}{"id": checkInfo.ID}, checkMap)
  93. return err
  94. }
  95. func Count(where map[string]interface{}) (int64, error) {
  96. return db.Count(Check.TableName(), where)
  97. }
  98. func CountRaw(where string, param map[string]interface{}) (int64, error) {
  99. query := "`zy_user_check` WHERE " + where
  100. return db.CountRaw(query, param)
  101. }
  102. func GetList(where map[string]interface{}, fields []string, page app.Page, retVal interface{}) ([]*models.UserCheck, error) {
  103. if page.PageNum > 0 && page.PageSize > 0 {
  104. where["_limit"] = db.GetOffset(uint(page.PageNum), uint(page.PageSize))
  105. }
  106. return Check.GetMulti(where, fields, retVal)
  107. }
  108. func GetChecksRaw(where map[string]string, param map[string]interface{}, retVal interface{}) error {
  109. field := "SELECT `zy_user_check`.* FROM `zy_user_check` "
  110. return db.GetMultiRaw(field, where, param, retVal)
  111. }
  112. func GetOne(where map[string]interface{}, fields []string, retVal interface{}) (*models.UserCheck, error) {
  113. return Check.GetOne(where, fields, retVal)
  114. }
  115. func GetCheckOne(where map[string]string, param map[string]interface{}, retVal interface{}) error {
  116. field := "SELECT `zy_user_check`.* FROM `zy_user_check` "
  117. where["_page_size"] = utils.ToStr(1)
  118. where["_page_num"] = utils.ToStr(1)
  119. return db.GetMultiRaw(field, where, param, retVal)
  120. }
  121. func GetCheckByID(id int, retVal interface{}) error {
  122. where := map[string]string{
  123. "where": "`zy_user_check`.`id` = {{id}}",
  124. }
  125. param := map[string]interface{}{"id": id}
  126. return GetCheckOne(where, param, retVal)
  127. }
  128. func GetInfoByID(id int, fields []string, retVal interface{}) (*models.UserCheck, error) {
  129. return GetOne(map[string]interface{}{"id": id}, fields, retVal)
  130. }