cart_service.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package service
  2. import (
  3. "database/sql"
  4. "errors"
  5. "xiaoniaokuaiyan.com/xiaoniao/entity"
  6. "xiaoniaokuaiyan.com/xiaoniao/dal"
  7. "xiaoniaokuaiyan.com/xiaoniao/util"
  8. )
  9. type CartService struct {
  10. dal.ICart
  11. }
  12. func (srv *CartService) CartList(customId int) (interface{}, error) {
  13. return srv.ICart.Get(customId)
  14. }
  15. func (srv *CartService) CartAdd(customId int, productId int, quantity int, isAdd bool) (interface{}, error) {
  16. plist, err := dal.DefaultProductDal.Get([]int{productId})
  17. if err != nil {
  18. return nil, err
  19. }
  20. if len(plist) == 0 {
  21. return nil, errors.New("1::wrong product id")
  22. }
  23. db := util.GetSqlDB()
  24. var cartNum int
  25. var strSql string
  26. if isAdd {
  27. strSql = "select quantity from t_cart where cid = ? and pid =?;"
  28. err = db.Get(&cartNum, strSql, customId, productId)
  29. if err != nil && err != sql.ErrNoRows {
  30. return nil, err
  31. }
  32. }
  33. if plist[0].StockSwitch == "ON" && plist[0].Stock < quantity+cartNum {
  34. return nil, errors.New("3::out of stock")
  35. }
  36. strSql = "SELECT sum(t2.quantity) as bought_num from t_order t1 left join t_order_product t2 on t1.id = t2.order_id left join t_product t3 on t2.product_id = t3.id where custom_id = ? and t3.id =? and t1.status not in(7,4,9) and t1.retype in ('100','110');"
  37. var bnum int
  38. db.Get(&bnum, strSql, customId, productId)
  39. if plist[0].PstockSwitch == "ON" && bnum+quantity+cartNum > plist[0].Pstock {
  40. return nil, errors.New("4::upper limit")
  41. }
  42. data, err := srv.ICart.Update(customId, productId, quantity, isAdd)
  43. if err != nil {
  44. return nil, errors.New("2::购物车已存在该产品")
  45. }
  46. return data, nil
  47. }
  48. func (srv *CartService) CartRemove(customId int, productIds []int) (interface{}, error) {
  49. return srv.ICart.Del(customId, productIds)
  50. }
  51. func (srv *CartService) CartClear(customId int) (interface{}, error) {
  52. return srv.ICart.Clear(customId)
  53. }
  54. func (srv *CartService) ZFB_CartList(customId int) (interface{}, error) {
  55. db := util.GetSqlDB()
  56. strSql := "select t1.quantity, t2.* from t_cart t1 left join t_product t2 on t1.pid = t2.id where cid = ? and t2.is_zfb=1"
  57. plist := []*entity.ProductDB{}
  58. err := db.Select(&plist, strSql, customId)
  59. if err != nil {
  60. return nil, err
  61. }
  62. entity.ZFBWash(plist)
  63. return plist, err
  64. }