|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- package db
-
- import (
- "applet/app/db/model"
- "applet/app/utils"
- "applet/app/utils/logx"
- "errors"
- "fmt"
- "reflect"
- "time"
- "xorm.io/xorm"
- )
-
- // 获取所有类目原始数据
- func GetAllCategory(Db *xorm.Engine) (GoodsCategoryList *[]model.GoodsCategory, err error) {
- var categoryList []model.GoodsCategory
- err = Db.Find(&categoryList)
- GoodsCategoryList = &categoryList
- return
- }
-
- func GetCategoryList(Db *xorm.Engine, level string) (GoodsCategoryList *[]model.GoodsCategory, err error) {
- var categoryList []model.GoodsCategory
- if level == "" {
- err = Db.Find(&categoryList)
- } else {
- err = Db.Where("level = ?", level).Find(&categoryList)
- }
-
- GoodsCategoryList = &categoryList
- return
- }
-
- // CategoryListFindByParams 通过传入的参数查询数据(多条)
- func CategoryListFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.GoodsCategory, error) {
- var m []model.GoodsCategory
- if params["value"] == nil {
- return nil, errors.New("参数有误")
- }
- if params["key"] == nil {
- //查询全部数据
- err := Db.Find(&m)
- if err != nil {
- return nil, logx.Error(err)
- }
- return &m, nil
- } else {
- if reflect.TypeOf(params["value"]).Kind() == reflect.Slice {
- //指定In查询
- if err := Db.In(utils.AnyToString(params["key"]), params["value"]).Find(&m); err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- } else {
- var query = fmt.Sprintf("%s =?", params["key"])
- err := Db.Where(query, params["value"]).Find(&m)
- if err != nil {
- return nil, logx.Error(err)
- }
- return &m, nil
- }
-
- }
- }
-
- func GetCategoryById(Db *xorm.Engine, pid string) (GoodsCategoryList *model.GoodsCategory, err error) {
- var m model.GoodsCategory
- has, err := Db.Where("id=?", pid).Get(&m)
- if has == false || err != nil {
- return nil, err
- }
- return &m, nil
- }
-
- func UpdateCategoryById(eg *xorm.Engine, m *model.GoodsCategory, columns ...string) error {
- var (
- affected int64
- err error
- )
- if columns == nil || len(columns) == 0 {
- affected, err = eg.Where("id = ?", m.Id).AllCols().Update(m)
- } else {
- affected, err = eg.Where("id = ?", m.Id).Cols(columns...).Update(m)
- }
- if err != nil {
- return err
- }
- if affected == 0 {
- return errors.New("更新类目数据失败,稍后再试")
- }
- return nil
- }
-
- func CategoryInsert(Db *xorm.Engine, m *model.GoodsCategory) (int, error) {
- _, err := Db.InsertOne(m)
- if err != nil {
- return 0, err
- }
- return m.Id, nil
- }
-
- func UpdateOrInsert(Db *xorm.Engine, m *model.GoodsCategory) error {
- if m.Id != 0 {
- err := UpdateCategoryById(Db, m)
- if err != nil {
- return err
- }
- } else {
- m.CreateAt = time.Now().Format("2006-01-02 15:04:05")
- _, err := CategoryInsert(Db, m)
- if err != nil {
- return err
- }
- }
- return nil
- }
|