shop_imp.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package imp
  2. import (
  3. "fmt"
  4. "lot_interlligentControl/app/e"
  5. "lot_interlligentControl/global"
  6. "lot_interlligentControl/models"
  7. "lot_interlligentControl/unity"
  8. )
  9. type Shop struct{}
  10. func (s Shop) AuditState(uid, id, state int) e.Rescode {
  11. //TODO implement me
  12. tx := global.DBLink.Model(models.Shop{}).
  13. Where("id = ?", id).
  14. Updates(&models.Shop{
  15. ProductState: state,
  16. CreateBy: uid,
  17. })
  18. if tx.RowsAffected > 0 {
  19. return e.SUCCESS
  20. }
  21. return e.ERROR
  22. }
  23. func (s Shop) GetShopByName(uid int, productName string, params unity.PageParams) ([]models.Shop, int64, error) {
  24. //TODO implement me
  25. var shop []models.Shop
  26. var count int64
  27. if err := global.DBLink.Model(models.Shop{}).
  28. Where("product_name LIKE ?", fmt.Sprintf("%s%s%s", "%", productName, "%")).
  29. Where("create_by = ?", uid).
  30. Count(&count).Error; err != nil {
  31. return nil, 0, err
  32. }
  33. // 计算偏移量并设置分页大小
  34. offset := (params.Page - 1) * params.Size
  35. // 执行实际分页查询
  36. if err := global.DBLink.
  37. Where("product_name LIKE ?", fmt.Sprintf("%s%s%s", "%", productName, "%")).
  38. Where("create_by = ?", uid).
  39. Offset(offset).Limit(params.Size).
  40. Order(params.Desc).
  41. Find(&shop).Error; err != nil {
  42. return nil, 0, err
  43. }
  44. return shop, count, nil
  45. }
  46. func (s Shop) UpdateShop(uid, id int, shop models.ShopDto) e.Rescode {
  47. //TODO implement me
  48. tx := global.DBLink.Where("id = ?", id).Model(&models.Shop{}).Updates(map[string]interface{}{
  49. "product_avatar": shop.ProductAvatar,
  50. "product_name": shop.ProductName,
  51. "product_price": shop.ProductPrice,
  52. "product_description": shop.ProductDescription,
  53. "product_type": shop.ProductType,
  54. "create_by": uid,
  55. "product_state": 0,
  56. })
  57. if tx.RowsAffected > 0 {
  58. return e.SUCCESS
  59. }
  60. return e.ERROR
  61. }
  62. func (s Shop) AddShop(uid int, shop models.ShopDto) e.Rescode {
  63. //TODO implement me
  64. var shops models.Shop
  65. shops.ProductAvatar = shop.ProductAvatar
  66. shops.ProductType = shop.ProductType
  67. shops.ProductDescription = shop.ProductDescription
  68. shops.ProductPrice = shop.ProductPrice
  69. shops.ProductName = shop.ProductName
  70. shops.CreateBy = uid
  71. shops.ProductState = 0
  72. tx := global.DBLink.Create(&shops)
  73. if tx.RowsAffected > 0 {
  74. return e.SUCCESS
  75. }
  76. return e.ERROR
  77. }