123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- package course
- import (
- "errors"
- "zhiyuan/models"
- "zhiyuan/pkg/app"
- "zhiyuan/pkg/db"
- "zhiyuan/pkg/utils"
- "zhiyuan/services/form"
- )
- var Course models.Course
- func Add(form form.CourseAdd) (int64, error) {
- courseMap := map[string]interface{}{
- "`name`": form.Name,
- "`intro`": form.Intro,
- "`type`": form.Type,
- "`role_ids`": utils.JoinIntSlice(form.RoleIds, ","),
- "`checks`": form.Checks,
- "`show`": form.Show,
- }
- courseID, err := db.InsertOne(Course.TableName(), courseMap)
- if err != nil {
- return 0, nil
- }
- return courseID, nil
- }
- func EditByID(form form.CourseAdd, id int) error {
- info, err := GetInfoByID(id, nil, nil)
- if info == nil {
- return errors.New("invalid id")
- }
- courseMap := map[string]interface{}{
- "`name`": form.Name,
- "`intro`": form.Intro,
- "`type`": form.Type,
- "`role_ids`": utils.JoinIntSlice(form.RoleIds, ","),
- "`checks`": form.Checks,
- "`show`": form.Show,
- }
- _, err = db.Update(Course.TableName(), map[string]interface{}{"id": id}, courseMap)
- return err
- }
- func DeleteByID(id int) error {
- info, _ := GetInfoByID(id, nil, nil)
- if info == nil {
- return errors.New("invalid id")
- }
- _, err := db.DeleteSoft(Course.TableName(), map[string]interface{}{"id": id})
- return err
- }
- func Count(where map[string]interface{}) (int64, error) {
- return db.Count(Course.TableName(), where)
- }
- func CountRaw(where string, param map[string]interface{}) (int64, error) {
- query := "`zy_course` WHERE " + where
- return db.CountRaw(query, param)
- }
- func GetList(where map[string]interface{}, fields []string, page app.Page, retVal interface{}) ([]*models.Course, error) {
- if page.PageNum > 0 && page.PageSize > 0 {
- where["_limit"] = db.GetOffset(uint(page.PageNum), uint(page.PageSize))
- }
- return Course.GetMulti(where, fields, retVal)
- }
- func GetCoursesRaw(where map[string]string, param map[string]interface{}, retVal interface{}) error {
- 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`) "
- return db.GetMultiRaw(field, where, param, retVal)
- }
- func GetOne(where map[string]interface{}, fields []string, retVal interface{}) (*models.Course, error) {
- return Course.GetOne(where, fields, retVal)
- }
- func GetCourseOne(where map[string]string, param map[string]interface{}, retVal interface{}) error {
- 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`) "
- where["_page_size"] = utils.ToStr(1)
- where["_page_num"] = utils.ToStr(1)
- return db.GetMultiRaw(field, where, param, retVal)
- }
- func GetCourseByID(id int, retVal interface{}) error {
- where := map[string]string{
- "where": "`zy_course`.`id` = {{id}}",
- }
- param := map[string]interface{}{"id": id}
- return GetCourseOne(where, param, retVal)
- }
- func GetInfoByID(id int, fields []string, retVal interface{}) (*models.Course, error) {
- return GetOne(map[string]interface{}{"id": id}, fields, retVal)
- }
|