Favorites.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  9. * 网站地址: https://www.valimart.net/
  10. * ----------------------------------------------------------------------------
  11. *
  12. * ============================================================================
  13. * 数据层模型
  14. */
  15. class Favorites extends BaseModel
  16. {
  17. public $page_info;
  18. /**
  19. * 收藏列表
  20. * @access public
  21. * @author csdeshang
  22. * @param array $condition 查询条件
  23. * @param string $field 查询字段
  24. * @param int $pagesize 分页信息
  25. * @param string $order 排序
  26. * @return array
  27. */
  28. public function getFavoritesList($condition, $field = '*', $pagesize = 0, $order = 'favlog_id desc')
  29. {
  30. if ($pagesize) {
  31. $res= Db::name('favorites')->where($condition)->field($field)->order($order)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  32. $this->page_info=$res;
  33. return $res->items();
  34. } else {
  35. return Db::name('favorites')->where($condition)->field($field)->order($order)->select()->toArray();
  36. }
  37. }
  38. /**
  39. * 收藏商品列表
  40. * @access public
  41. * @author csdeshang
  42. * @param array $condition 检索条件
  43. * @param string $field 字段
  44. * @param int $pagesize 分页信息
  45. * @param string $order 排序
  46. * @return array
  47. */
  48. public function getGoodsFavoritesList($condition, $field = '*', $pagesize = 0, $order = 'favlog_id desc')
  49. {
  50. $condition[]=array('fav_type','=','goods');
  51. return $this->getFavoritesList($condition, $field, $pagesize, $order);
  52. }
  53. /**
  54. * 收藏店铺列表
  55. * @access public
  56. * @author csdeshang
  57. * @param array $condition 查询条件
  58. * @param string $field 字段
  59. * @param int $pagesize 分页信息
  60. * @param string $order 排序
  61. * @return array
  62. */
  63. public function getStoreFavoritesList($condition, $field = '*', $pagesize = 0, $order = 'favlog_id desc')
  64. {
  65. $condition[]=array('fav_type','=','store');
  66. return $this->getFavoritesList($condition, $field, $pagesize, $order);
  67. }
  68. /**
  69. * 取单个收藏的内容
  70. * @access public
  71. * @author csdeshang
  72. * @param array $condition 查询条件
  73. * @return array 数组类型的返回结果
  74. */
  75. public function getOneFavorites($condition)
  76. {
  77. return Db::name('favorites')->where($condition)->find();
  78. }
  79. /**
  80. * 获取店铺收藏数
  81. * @access public
  82. * @author csdeshang
  83. * @param int $storeId 店铺ID
  84. * @param int $memberId 会员ID
  85. * @return int
  86. */
  87. public function getStoreFavoritesCountByStoreId($storeId, $memberId = 0)
  88. {
  89. $condition = array();
  90. $condition[] = array('fav_type','=','store');
  91. $condition[] = array('fav_id','=',$storeId);
  92. if ($memberId > 0) {
  93. $condition[] = array('member_id','=',$memberId);
  94. }
  95. return Db::name('favorites')->where($condition)->count();
  96. }
  97. /**
  98. * 获取商品收藏数
  99. * @access public
  100. * @author csdeshang
  101. * @param int $goodsId 商品ID
  102. * @param int $memberId 会员ID
  103. * @return int
  104. */
  105. public function getGoodsFavoritesCountByGoodsId($goodsId, $memberId = 0)
  106. {
  107. $condition = array();
  108. $condition[] = array('fav_type','=','goods');
  109. $condition[] = array('fav_id','=',$goodsId);
  110. if ($memberId > 0) {
  111. $condition[] = array('member_id','=',$memberId);
  112. }
  113. return Db::name('favorites')->where($condition)->count();
  114. }
  115. /**
  116. * 新增收藏
  117. * @access public
  118. * @author csdeshang
  119. * @param array $data 参数内容
  120. * @return bool 布尔类型的返回结果
  121. */
  122. public function addFavorites($data)
  123. {
  124. if (empty($data)) {
  125. return false;
  126. }
  127. if ($data['fav_type'] == 'store') {
  128. $store_id = intval($data['fav_id']);
  129. $store_model = model('store');
  130. $store = $store_model->getStoreInfoByID($store_id);
  131. $data['store_name'] = $store['store_name'];
  132. $data['store_id'] = $store['store_id'];
  133. $data['storeclass_id'] = $store['storeclass_id'];
  134. }
  135. if ($data['fav_type'] == 'goods') {
  136. $goods_id = intval($data['fav_id']);
  137. $goods_model = model('goods');
  138. $goods = $goods_model->getGoodsInfoByID($goods_id);
  139. $data['goods_name'] = $goods['goods_name'];
  140. $data['goods_image'] = $goods['goods_image'];
  141. $data['favlog_price'] = $goods['goods_promotion_price'];//商品收藏时价格
  142. $data['favlog_msg'] = $goods['goods_promotion_price'];//收藏备注,默认为收藏时价格,可修改
  143. $data['gc_id'] = $goods['gc_id'];
  144. $store_id = intval($goods['store_id']);
  145. $store_model = model('store');
  146. $store = $store_model->getStoreInfoByID($store_id);
  147. $data['store_name'] = $store['store_name'];
  148. $data['store_id'] = $store['store_id'];
  149. $data['storeclass_id'] = $store['storeclass_id'];
  150. }
  151. return Db::name('favorites')->insertGetId($data);
  152. }
  153. /**
  154. * 修改记录
  155. * @access public
  156. * @author csdeshang
  157. * @param type $condition 修改条件
  158. * @param type $data 修改数据
  159. * @return boolean
  160. */
  161. public function editFavorites($condition, $data)
  162. {
  163. if (empty($condition)) {
  164. return false;
  165. }
  166. if (is_array($data)) {
  167. $result = Db::name('favorites')->where($condition)->update($data);
  168. return $result;
  169. }
  170. else {
  171. return false;
  172. }
  173. }
  174. /**
  175. * 删除
  176. * @access public
  177. * @author csdeshang
  178. * @param array $condition 查询条件
  179. * @return bool 布尔类型的返回结果
  180. */
  181. public function delFavorites($condition)
  182. {
  183. if (empty($condition)) {
  184. return false;
  185. }
  186. return Db::name('favorites')->where($condition)->delete();
  187. }
  188. /**
  189. * 获取店铺收藏数
  190. * @access public
  191. * @author csdeshang
  192. * @param int $id 会员ID
  193. * @return int
  194. */
  195. public function getStoreFavoritesCountByMemberId($member_id)
  196. {
  197. $condition = array();
  198. $condition[] = array('fav_type','=','store');
  199. if ($member_id > 0) {
  200. $condition[] = array('member_id','=',$member_id);
  201. }
  202. return Db::name('favorites')->where($condition)->count();
  203. }
  204. /**
  205. * 获取商品收藏数
  206. * @access public
  207. * @author csdeshang
  208. * @param int $id 会员ID
  209. * @return int
  210. */
  211. public function getGoodsFavoritesCountByMemberId($member_id)
  212. {
  213. $condition = array();
  214. $condition[] = array('fav_type','=','goods');
  215. if ($member_id > 0) {
  216. $condition[] = array('member_id','=',$member_id);
  217. }
  218. return Db::name('favorites')->where($condition)->count();
  219. }
  220. }