question.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package question
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "zhiyuan/models"
  6. "zhiyuan/pkg/app"
  7. "zhiyuan/pkg/db"
  8. "zhiyuan/pkg/utils"
  9. "zhiyuan/services/form"
  10. )
  11. var Question models.Question
  12. func Add(form form.QuestionAdd) (int64, error) {
  13. options, err := json.Marshal(form.Options)
  14. if err != nil {
  15. return 0, errors.New("invalid json")
  16. }
  17. questionMap := map[string]interface{}{
  18. "`course_id`": form.CourseId,
  19. "`type`": form.Type,
  20. "`content`": form.Content,
  21. "`options`": options,
  22. "`answer`": form.Answer,
  23. "`show`": form.Show,
  24. }
  25. questionID, err := db.InsertOne(Question.TableName(), questionMap)
  26. if err != nil {
  27. return 0, nil
  28. }
  29. return questionID, nil
  30. }
  31. func EditByID(form form.QuestionAdd, id int) error {
  32. info, err := GetInfoByID(id, nil, nil)
  33. if info == nil {
  34. return errors.New("invalid id")
  35. }
  36. options, err := json.Marshal(form.Options)
  37. if err != nil {
  38. return errors.New("invalid json")
  39. }
  40. questionMap := map[string]interface{}{
  41. "`course_id`": form.CourseId,
  42. "`type`": form.Type,
  43. "`content`": form.Content,
  44. "`options`": options,
  45. "`answer`": form.Answer,
  46. "`show`": form.Show,
  47. }
  48. _, err = db.Update(Question.TableName(), map[string]interface{}{"id": id}, questionMap)
  49. return err
  50. }
  51. func DeleteByID(id int) error {
  52. info, _ := GetInfoByID(id, nil, nil)
  53. if info == nil {
  54. return errors.New("invalid id")
  55. }
  56. _, err := db.DeleteSoft(Question.TableName(), map[string]interface{}{"id": id})
  57. return err
  58. }
  59. func Count(where map[string]interface{}) (int64, error) {
  60. return db.Count(Question.TableName(), where)
  61. }
  62. func CountRaw(where string, param map[string]interface{}) (int64, error) {
  63. query := "`zy_question` WHERE " + where
  64. return db.CountRaw(query, param)
  65. }
  66. func GetList(where map[string]interface{}, fields []string, page app.Page, retVal interface{}) ([]*models.Question, error) {
  67. if page.PageNum > 0 && page.PageSize > 0 {
  68. where["_limit"] = db.GetOffset(uint(page.PageNum), uint(page.PageSize))
  69. }
  70. return Question.GetMulti(where, fields, retVal)
  71. }
  72. func GetQuestionsRaw(where map[string]string, param map[string]interface{}, retVal interface{}) error {
  73. field := "SELECT `zy_question`.* FROM `zy_question` "
  74. return db.GetMultiRaw(field, where, param, retVal)
  75. }
  76. func GetOne(where map[string]interface{}, fields []string, retVal interface{}) (*models.Question, error) {
  77. return Question.GetOne(where, fields, retVal)
  78. }
  79. func GetQuestionOne(where map[string]string, param map[string]interface{}, retVal interface{}) error {
  80. field := "SELECT `zy_question`.* FROM `zy_question` "
  81. where["_page_size"] = utils.ToStr(1)
  82. where["_page_num"] = utils.ToStr(1)
  83. return db.GetMultiRaw(field, where, param, retVal)
  84. }
  85. func GetQuestionByID(id int, retVal interface{}) error {
  86. where := map[string]string{
  87. "where": "`zy_question`.`id` = {{id}}",
  88. }
  89. param := map[string]interface{}{"id": id}
  90. return GetQuestionOne(where, param, retVal)
  91. }
  92. func GetInfoByID(id int, fields []string, retVal interface{}) (*models.Question, error) {
  93. return GetOne(map[string]interface{}{"id": id}, fields, retVal)
  94. }