check.go 4.4 KB

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