Favorites.php 7.3 KB

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