Favorites.php 7.1 KB

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