package courses import ( "errors" "zhiyuan/models" "zhiyuan/pkg/app" "zhiyuan/pkg/db" "zhiyuan/pkg/utils" "zhiyuan/services/form" ) var Courses models.Courses func Add(form form.CoursesAdd) (int64, error) { coursesMap := map[string]interface{}{ "`course_id`": form.CourseId, "`name`": form.Name, "`type`": form.Type, "`content`": form.Content, "`orders`": form.Orders, } coursesID, err := db.InsertOne(Courses.TableName(), coursesMap) if err != nil { return 0, nil } return coursesID, nil } func EditByID(form form.CoursesAdd, id int) error { info, err := GetInfoByID(id, nil, nil) if info == nil { return errors.New("invalid id") } coursesMap := map[string]interface{}{ "`course_id`": form.CourseId, "`name`": form.Name, "`type`": form.Type, "`content`": form.Content, "`orders`": form.Orders, } _, err = db.Update(Courses.TableName(), map[string]interface{}{"id": id}, coursesMap) return err } func DeleteByID(id int) error { info, _ := GetInfoByID(id, nil, nil) if info == nil { return errors.New("invalid id") } _, err := db.DeleteSoft(Courses.TableName(), map[string]interface{}{"id": id}) return err } func Count(where map[string]interface{}) (int64, error) { return db.Count(Courses.TableName(), where) } func CountRaw(where string, param map[string]interface{}) (int64, error) { query := "`zy_courses` WHERE " + where return db.CountRaw(query, param) } func GetList(where map[string]interface{}, fields []string, page app.Page, retVal interface{}) ([]*models.Courses, error) { if page.PageNum > 0 && page.PageSize > 0 { where["_limit"] = db.GetOffset(uint(page.PageNum), uint(page.PageSize)) } return Courses.GetMulti(where, fields, retVal) } func GetCoursessRaw(where map[string]string, param map[string]interface{}, retVal interface{}) error { field := "SELECT `zy_courses`.* FROM `zy_courses` " return db.GetMultiRaw(field, where, param, retVal) } func GetOne(where map[string]interface{}, fields []string, retVal interface{}) (*models.Courses, error) { return Courses.GetOne(where, fields, retVal) } func GetCoursesOne(where map[string]string, param map[string]interface{}, retVal interface{}) error { field := "SELECT `zy_courses`.* FROM `zy_courses` " where["_page_size"] = utils.ToStr(1) where["_page_num"] = utils.ToStr(1) return db.GetMultiRaw(field, where, param, retVal) } func GetCoursesByID(id int, retVal interface{}) error { where := map[string]string{ "where": "`zy_courses`.`id` = {{id}}", } param := map[string]interface{}{"id": id} return GetCoursesOne(where, param, retVal) } func GetInfoByID(id int, fields []string, retVal interface{}) (*models.Courses, error) { return GetOne(map[string]interface{}{"id": id}, fields, retVal) }