course.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package course
  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 Course models.Course
  11. func Add(form form.CourseAdd) (int64, error) {
  12. courseMap := map[string]interface{}{
  13. "`name`": form.Name,
  14. "`intro`": form.Intro,
  15. "`type`": form.Type,
  16. "`role_ids`": utils.JoinIntSlice(form.RoleIds, ","),
  17. "`checks`": form.Checks,
  18. "`show`": form.Show,
  19. }
  20. courseID, err := db.InsertOne(Course.TableName(), courseMap)
  21. if err != nil {
  22. return 0, nil
  23. }
  24. return courseID, nil
  25. }
  26. func EditByID(form form.CourseAdd, id int) error {
  27. info, err := GetInfoByID(id, nil, nil)
  28. if info == nil {
  29. return errors.New("invalid id")
  30. }
  31. courseMap := map[string]interface{}{
  32. "`name`": form.Name,
  33. "`intro`": form.Intro,
  34. "`type`": form.Type,
  35. "`role_ids`": utils.JoinIntSlice(form.RoleIds, ","),
  36. "`checks`": form.Checks,
  37. "`show`": form.Show,
  38. }
  39. _, err = db.Update(Course.TableName(), map[string]interface{}{"id": id}, courseMap)
  40. return err
  41. }
  42. func DeleteByID(id int) error {
  43. info, _ := GetInfoByID(id, nil, nil)
  44. if info == nil {
  45. return errors.New("invalid id")
  46. }
  47. _, err := db.DeleteSoft(Course.TableName(), map[string]interface{}{"id": id})
  48. return err
  49. }
  50. func Count(where map[string]interface{}) (int64, error) {
  51. return db.Count(Course.TableName(), where)
  52. }
  53. func CountRaw(where string, param map[string]interface{}) (int64, error) {
  54. query := "`zy_course` WHERE " + where
  55. return db.CountRaw(query, param)
  56. }
  57. func GetList(where map[string]interface{}, fields []string, page app.Page, retVal interface{}) ([]*models.Course, error) {
  58. if page.PageNum > 0 && page.PageSize > 0 {
  59. where["_limit"] = db.GetOffset(uint(page.PageNum), uint(page.PageSize))
  60. }
  61. return Course.GetMulti(where, fields, retVal)
  62. }
  63. func GetCoursesRaw(where map[string]string, param map[string]interface{}, retVal interface{}) error {
  64. field := "SELECT `zy_course`.*, group_concat(`zy_role`.`name` separator ',') as role_names FROM `zy_course` left join `zy_role` on FIND_IN_SET(`zy_role`.`id`, `zy_course`.`role_ids`) "
  65. return db.GetMultiRaw(field, where, param, retVal)
  66. }
  67. func GetOne(where map[string]interface{}, fields []string, retVal interface{}) (*models.Course, error) {
  68. return Course.GetOne(where, fields, retVal)
  69. }
  70. func GetCourseOne(where map[string]string, param map[string]interface{}, retVal interface{}) error {
  71. field := "SELECT `zy_course`.*, group_concat(`zy_role`.`name` separator ',') as role_names FROM `zy_course` left join `zy_role` on FIND_IN_SET(`zy_role`.`id`, `zy_course`.`role_ids`) "
  72. where["_page_size"] = utils.ToStr(1)
  73. where["_page_num"] = utils.ToStr(1)
  74. return db.GetMultiRaw(field, where, param, retVal)
  75. }
  76. func GetCourseByID(id int, retVal interface{}) error {
  77. where := map[string]string{
  78. "where": "`zy_course`.`id` = {{id}}",
  79. }
  80. param := map[string]interface{}{"id": id}
  81. return GetCourseOne(where, param, retVal)
  82. }
  83. func GetInfoByID(id int, fields []string, retVal interface{}) (*models.Course, error) {
  84. return GetOne(map[string]interface{}{"id": id}, fields, retVal)
  85. }