courses.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package courses
  2. import (
  3. "errors"
  4. "zhiyuan/models"
  5. "zhiyuan/pkg/app"
  6. "zhiyuan/pkg/db"
  7. "zhiyuan/pkg/utils"
  8. "zhiyuan/services/form"
  9. )
  10. var Courses models.Courses
  11. func Add(form form.CoursesAdd) (int64, error) {
  12. coursesMap := map[string]interface{}{
  13. "`course_id`": form.CourseId,
  14. "`name`": form.Name,
  15. "`type`": form.Type,
  16. "`content`": form.Content,
  17. "`orders`": form.Orders,
  18. }
  19. coursesID, err := db.InsertOne(Courses.TableName(), coursesMap)
  20. if err != nil {
  21. return 0, nil
  22. }
  23. return coursesID, nil
  24. }
  25. func EditByID(form form.CoursesAdd, id int) error {
  26. info, err := GetInfoByID(id, nil, nil)
  27. if info == nil {
  28. return errors.New("invalid id")
  29. }
  30. coursesMap := map[string]interface{}{
  31. "`course_id`": form.CourseId,
  32. "`name`": form.Name,
  33. "`type`": form.Type,
  34. "`content`": form.Content,
  35. "`orders`": form.Orders,
  36. }
  37. _, err = db.Update(Courses.TableName(), map[string]interface{}{"id": id}, coursesMap)
  38. return err
  39. }
  40. func DeleteByID(id int) error {
  41. info, _ := GetInfoByID(id, nil, nil)
  42. if info == nil {
  43. return errors.New("invalid id")
  44. }
  45. _, err := db.DeleteSoft(Courses.TableName(), map[string]interface{}{"id": id})
  46. return err
  47. }
  48. func Count(where map[string]interface{}) (int64, error) {
  49. return db.Count(Courses.TableName(), where)
  50. }
  51. func CountRaw(where string, param map[string]interface{}) (int64, error) {
  52. query := "`zy_courses` WHERE " + where
  53. return db.CountRaw(query, param)
  54. }
  55. func GetList(where map[string]interface{}, fields []string, page app.Page, retVal interface{}) ([]*models.Courses, error) {
  56. if page.PageNum > 0 && page.PageSize > 0 {
  57. where["_limit"] = db.GetOffset(uint(page.PageNum), uint(page.PageSize))
  58. }
  59. return Courses.GetMulti(where, fields, retVal)
  60. }
  61. func GetCoursessRaw(where map[string]string, param map[string]interface{}, retVal interface{}) error {
  62. field := "SELECT `zy_courses`.* FROM `zy_courses` "
  63. return db.GetMultiRaw(field, where, param, retVal)
  64. }
  65. func GetOne(where map[string]interface{}, fields []string, retVal interface{}) (*models.Courses, error) {
  66. return Courses.GetOne(where, fields, retVal)
  67. }
  68. func GetCoursesOne(where map[string]string, param map[string]interface{}, retVal interface{}) error {
  69. field := "SELECT `zy_courses`.* FROM `zy_courses` "
  70. where["_page_size"] = utils.ToStr(1)
  71. where["_page_num"] = utils.ToStr(1)
  72. return db.GetMultiRaw(field, where, param, retVal)
  73. }
  74. func GetCoursesByID(id int, retVal interface{}) error {
  75. where := map[string]string{
  76. "where": "`zy_courses`.`id` = {{id}}",
  77. }
  78. param := map[string]interface{}{"id": id}
  79. return GetCoursesOne(where, param, retVal)
  80. }
  81. func GetInfoByID(id int, fields []string, retVal interface{}) (*models.Courses, error) {
  82. return GetOne(map[string]interface{}{"id": id}, fields, retVal)
  83. }