GoodsCollect.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app\model\goods;
  11. use app\model\BaseModel;
  12. use app\model\system\Stat;
  13. /**
  14. * 商品收藏
  15. */
  16. class GoodsCollect extends BaseModel
  17. {
  18. /**
  19. * 添加收藏
  20. * @param array $data
  21. */
  22. public function addCollect($data)
  23. {
  24. $res = model('goods_collect')->getInfo([ [ 'member_id', '=', $data[ 'member_id' ] ], [ 'goods_id', '=', $data[ 'goods_id' ] ] ], 'site_id, collect_id');
  25. if (empty($res)) {
  26. $data[ 'create_time' ] = time();
  27. $collect_id = model('goods_collect')->add($data);
  28. if ($collect_id) {
  29. model('goods_sku')->setInc([ [ 'goods_id', '=', $data[ 'goods_id' ] ] ], 'collect_num', 1);
  30. }
  31. //添加统计
  32. $stat = new Stat();
  33. // $stat->addShopStat(['collect_goods' => 1, 'site_id' => $data['site_id']]);
  34. $stat->switchStat([ 'type' => 'collect_goods', 'data' => [ 'collect_goods' => 1, 'site_id' => $data[ 'site_id' ] ] ]);
  35. return $this->success($collect_id);
  36. } else {
  37. return $this->error();
  38. }
  39. }
  40. /**
  41. * 取消收藏
  42. * @param int $member_id
  43. * @param int $goods_id
  44. */
  45. public function deleteCollect($member_id, $goods_id)
  46. {
  47. $res = model('goods_collect')->delete([ [ 'member_id', '=', $member_id ], [ 'goods_id', '=', $goods_id ] ]);
  48. if ($res) {
  49. model('goods_sku')->setDec([ [ 'goods_id', '=', $goods_id ] ], 'collect_num', 1);
  50. }
  51. return $this->success($res);
  52. }
  53. /**
  54. * 检测商品是否收藏
  55. * @param unknown $sku_id
  56. * @param unknown $member_id
  57. */
  58. public function getIsCollect($goods_id, $member_id)
  59. {
  60. $res = model('goods_collect')->getInfo([ [ 'member_id', '=', $member_id ], [ 'goods_id', '=', $goods_id ] ], 'collect_id');
  61. if (!empty($res)) {
  62. return $this->success(1);
  63. } else {
  64. return $this->success(0);
  65. }
  66. }
  67. /**
  68. * 获取收藏列表
  69. * @param array $condition
  70. * @param string $field
  71. * @param string $order
  72. * @param string $limit
  73. */
  74. public function getCollectList($condition = [], $field = 'collect_id, member_id, goods_id, sku_id, category_id, sku_name, sku_price, sku_image, create_time', $order = '', $limit = null)
  75. {
  76. $list = model('goods_collect')->getList($condition, $field, $order, '', '', '', $limit);
  77. return $this->success($list);
  78. }
  79. /**
  80. * 获取收藏分页列表
  81. * @param array $condition
  82. * @param number $page
  83. * @param string $page_size
  84. * @param string $order
  85. * @param string $field
  86. */
  87. public function getCollectPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'gc.create_time desc', $field = '')
  88. {
  89. if (empty($field)) {
  90. $field = 'gc.collect_id, gc.member_id, gc.goods_id, gc.sku_id,sku.sku_name, gc.sku_price, gc.sku_image,g.goods_name,g.is_free_shipping,sku.promotion_type,sku.member_price,sku.discount_price,g.sale_num,g.price,g.market_price,g.is_virtual, g.goods_image';
  91. }
  92. $join = [
  93. [ 'goods g', 'gc.goods_id = g.goods_id', 'inner' ],
  94. [ 'goods_sku sku', 'g.sku_id = sku.sku_id', 'inner' ]
  95. ];
  96. $alias = 'gc';
  97. $res = model('goods_collect')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
  98. foreach ($res[ 'list' ] as $k => $v) {
  99. if (isset($v[ 'sale_num' ])) {
  100. $res[ 'list' ][ $k ][ 'sale_num' ] = numberFormat($res[ 'list' ][ $k ][ 'sale_num' ]);
  101. }
  102. if (isset($v[ 'goods_stock' ])) {
  103. $res[ 'list' ][ $k ][ 'goods_stock' ] = numberFormat($res[ 'list' ][ $k ][ 'goods_stock' ]);
  104. }
  105. if (isset($v[ 'virtual_sale' ])) {
  106. $res[ 'list' ][ $k ][ 'virtual_sale' ] = numberFormat($res[ 'list' ][ $k ][ 'virtual_sale' ]);
  107. }
  108. if (isset($v[ 'stock' ])) {
  109. $res[ 'list' ][ $k ][ 'stock' ] = numberFormat($res[ 'list' ][ $k ][ 'stock' ]);
  110. }
  111. }
  112. return $this->success($res);
  113. }
  114. }