面包店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

db_category.go 2.7 KiB

8 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils"
  5. "applet/app/utils/logx"
  6. "errors"
  7. "fmt"
  8. "reflect"
  9. "time"
  10. "xorm.io/xorm"
  11. )
  12. // 获取所有类目原始数据
  13. func GetAllCategory(Db *xorm.Engine) (GoodsCategoryList *[]model.GoodsCategory, err error) {
  14. var categoryList []model.GoodsCategory
  15. err = Db.Find(&categoryList)
  16. GoodsCategoryList = &categoryList
  17. return
  18. }
  19. func GetCategoryList(Db *xorm.Engine, level string) (GoodsCategoryList *[]model.GoodsCategory, err error) {
  20. var categoryList []model.GoodsCategory
  21. if level == "" {
  22. err = Db.Find(&categoryList)
  23. } else {
  24. err = Db.Where("level = ?", level).Find(&categoryList)
  25. }
  26. GoodsCategoryList = &categoryList
  27. return
  28. }
  29. // CategoryListFindByParams 通过传入的参数查询数据(多条)
  30. func CategoryListFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.GoodsCategory, error) {
  31. var m []model.GoodsCategory
  32. if params["value"] == nil {
  33. return nil, errors.New("参数有误")
  34. }
  35. if params["key"] == nil {
  36. //查询全部数据
  37. err := Db.Find(&m)
  38. if err != nil {
  39. return nil, logx.Error(err)
  40. }
  41. return &m, nil
  42. } else {
  43. if reflect.TypeOf(params["value"]).Kind() == reflect.Slice {
  44. //指定In查询
  45. if err := Db.In(utils.AnyToString(params["key"]), params["value"]).Find(&m); err != nil {
  46. return nil, logx.Warn(err)
  47. }
  48. return &m, nil
  49. } else {
  50. var query = fmt.Sprintf("%s =?", params["key"])
  51. err := Db.Where(query, params["value"]).Find(&m)
  52. if err != nil {
  53. return nil, logx.Error(err)
  54. }
  55. return &m, nil
  56. }
  57. }
  58. }
  59. func GetCategoryById(Db *xorm.Engine, pid string) (GoodsCategoryList *model.GoodsCategory, err error) {
  60. var m model.GoodsCategory
  61. has, err := Db.Where("id=?", pid).Get(&m)
  62. if has == false || err != nil {
  63. return nil, err
  64. }
  65. return &m, nil
  66. }
  67. func UpdateCategoryById(eg *xorm.Engine, m *model.GoodsCategory, columns ...string) error {
  68. var (
  69. affected int64
  70. err error
  71. )
  72. if columns == nil || len(columns) == 0 {
  73. affected, err = eg.Where("id = ?", m.Id).AllCols().Update(m)
  74. } else {
  75. affected, err = eg.Where("id = ?", m.Id).Cols(columns...).Update(m)
  76. }
  77. if err != nil {
  78. return err
  79. }
  80. if affected == 0 {
  81. return errors.New("更新类目数据失败,稍后再试")
  82. }
  83. return nil
  84. }
  85. func CategoryInsert(Db *xorm.Engine, m *model.GoodsCategory) (int, error) {
  86. _, err := Db.InsertOne(m)
  87. if err != nil {
  88. return 0, err
  89. }
  90. return m.Id, nil
  91. }
  92. func UpdateOrInsert(Db *xorm.Engine, m *model.GoodsCategory) error {
  93. if m.Id != 0 {
  94. err := UpdateCategoryById(Db, m)
  95. if err != nil {
  96. return err
  97. }
  98. } else {
  99. m.CreateAt = time.Now().Format("2006-01-02 15:04:05")
  100. _, err := CategoryInsert(Db, m)
  101. if err != nil {
  102. return err
  103. }
  104. }
  105. return nil
  106. }