brand.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package brand
  2. import (
  3. "errors"
  4. "zhiyuan/models"
  5. "zhiyuan/pkg/app"
  6. "zhiyuan/pkg/db"
  7. "zhiyuan/services/form"
  8. )
  9. var MatBrand models.MatBrand
  10. func Count(where map[string]interface{}) (int64, error) {
  11. return db.Count(MatBrand.TableName(), where)
  12. }
  13. func GetList(where map[string]interface{}, fields []string, page app.Page, retVal interface{}) ([]*models.MatBrand, error) {
  14. if page.PageNum > 0 && page.PageSize > 0 {
  15. where["_limit"] = db.GetOffset(uint(page.PageNum), uint(page.PageSize))
  16. }
  17. return MatBrand.GetMulti(where, fields, retVal)
  18. }
  19. func GetOne(where map[string]interface{}, fields []string, dest interface{}) (*models.MatBrand, error) {
  20. return MatBrand.GetOne(where, fields, dest)
  21. }
  22. func Add(form form.MaterialBrandAdd) (int64, error) {
  23. if CheckDuplicate(form.BrandName) {
  24. return 0, errors.New("品牌已存在")
  25. }
  26. brandMap := map[string]interface{}{
  27. "brand_name": form.BrandName,
  28. }
  29. if form.Deleted > 0 {
  30. brandMap["deleted_at"] = form.Deleted
  31. }
  32. pkgID, err := db.InsertOne(MatBrand.TableName(), brandMap)
  33. if err != nil {
  34. return 0, nil
  35. }
  36. return pkgID, nil
  37. }
  38. func Edit(form form.MaterialBrandAdd, id int) (int64, error) {
  39. brandInfo, _ := GetOne(map[string]interface{}{"id": id}, nil, nil)
  40. if brandInfo.BrandName != form.BrandName && CheckDuplicate(form.BrandName) {
  41. return 0, errors.New("品牌已存在")
  42. }
  43. brandMap := map[string]interface{}{
  44. "brand_name": form.BrandName,
  45. }
  46. return db.Update(MatBrand.TableName(), map[string]interface{}{"id": id}, brandMap)
  47. }
  48. func CheckDuplicate(name string) bool {
  49. brandInfo, err := GetOne(map[string]interface{}{"brand_name": name}, nil, nil)
  50. return brandInfo != nil && err == nil
  51. }