favorites.model.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * 买家收藏
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class favoritesModel extends Model
  11. {
  12. public function __construct() {
  13. parent::__construct('favorites');
  14. }
  15. /**
  16. * 取数量
  17. * @param unknown $condition
  18. */
  19. public function getFavoritesCount($condition = array()) {
  20. return $this->where($condition)->count();
  21. }
  22. /**
  23. * 收藏列表
  24. *
  25. * @param array $condition
  26. * @param treing $field
  27. * @param int $page
  28. * @param string $order
  29. * @return array
  30. */
  31. public function getFavoritesList($condition, $field = '*', $page = 0 , $order = 'fav_time desc', $limit = 0) {
  32. return $this->where($condition)->order($order)->page($page)->limit($limit)->select();
  33. }
  34. /**
  35. * 收藏商品列表
  36. * @param array $condition
  37. * @param treing $field
  38. * @param int $page
  39. * @param string $order
  40. * @return array
  41. */
  42. public function getGoodsFavoritesList($condition, $field = '*', $page = 0, $order = 'fav_time desc') {
  43. $condition['fav_type'] = 'goods';
  44. return $this->getFavoritesList($condition, $field, $page, $order);
  45. }
  46. /**
  47. * 收藏店铺列表
  48. * @param array $condition
  49. * @param treing $field
  50. * @param int $page
  51. * @param string $order
  52. * @return array
  53. */
  54. public function getStoreFavoritesList($condition, $field = '*', $page = 0, $order = 'fav_time desc', $limit = 0) {
  55. $condition['fav_type'] = 'store';
  56. return $this->getFavoritesList($condition, $field, $page, $order, $limit);
  57. }
  58. // /**
  59. // * 收藏列表
  60. // *
  61. // * @param array $condition 检索条件
  62. // * @param obj $obj_page 分页对象
  63. // * @return array 数组类型的返回结果
  64. // */
  65. // public function getFavoritesList($condition,$page = ''){
  66. // $condition_str = $this->_condition($condition);
  67. // $param = array(
  68. // 'table'=>'favorites',
  69. // 'where'=>$condition_str,
  70. // 'order'=>$condition['order'] ? $condition['order'] : 'fav_time desc'
  71. // );
  72. // $result = Db::select($param,$page);
  73. // return $result;
  74. // }
  75. /**
  76. * 取单个收藏的内容
  77. *
  78. * @param array $condition 查询条件
  79. * @param string $field 查询字段
  80. * @return array 数组类型的返回结果
  81. */
  82. public function getOneFavorites($condition,$field='*'){
  83. $param = array();
  84. $param['table'] = 'favorites';
  85. $param['field'] = array_keys($condition);
  86. $param['value'] = array_values($condition);
  87. return Db::getRow($param,$field);
  88. }
  89. /**
  90. * 新增收藏
  91. *
  92. * @param array $param 参数内容
  93. * @return bool 布尔类型的返回结果
  94. */
  95. public function addFavorites($param){
  96. if (empty($param)){
  97. return false;
  98. }
  99. return Db::insert('favorites',$param);
  100. }
  101. /**
  102. * 更新收藏数量
  103. *
  104. *
  105. * @param string $table 表名
  106. * @param array $update 更新内容
  107. * @param array $param 相应参数
  108. * @return bool 布尔类型的返回结果
  109. */
  110. public function updateFavoritesNum($table, $update, $param){
  111. $where = $this->_condition($param);
  112. return Db::update($table,$update,$where);
  113. }
  114. /**
  115. * 验证是否为当前用户收藏
  116. *
  117. * @param array $param 条件数据
  118. * @return bool 布尔类型的返回结果
  119. */
  120. public function checkFavorites($fav_id,$fav_type,$member_id){
  121. if (intval($fav_id) == 0 || empty($fav_type) || intval($member_id) == 0){
  122. return true;
  123. }
  124. $result = self::getOneFavorites($fav_id,$fav_type,$member_id);
  125. if ($result['member_id'] == $member_id){
  126. return true;
  127. }else {
  128. return false;
  129. }
  130. }
  131. /**
  132. * 删除
  133. *
  134. * @param int $id 记录ID
  135. * @return array $rs_row 返回数组形式的查询结果
  136. */
  137. public function delFavorites($condition){
  138. if (empty($condition)){
  139. return false;
  140. }
  141. $condition_str = '';
  142. if ($condition['fav_id'] != ''){
  143. $condition_str .= " and fav_id='{$condition['fav_id']}' ";
  144. }
  145. if ($condition['member_id'] != ''){
  146. $condition_str .= " and member_id='{$condition['member_id']}' ";
  147. }
  148. if ($condition['fav_type'] != ''){
  149. $condition_str .= " and fav_type='{$condition['fav_type']}' ";
  150. }
  151. if ($condition['fav_id_in'] !=''){
  152. $condition_str .= " and fav_id in({$condition['fav_id_in']}) ";
  153. }
  154. return Db::delete('favorites',$condition_str);
  155. }
  156. /**
  157. * 构造检索条件
  158. *
  159. * @param array $condition 检索条件
  160. * @return string 字符串类型的返回结果
  161. */
  162. public function _condition($condition){
  163. $condition_str = '';
  164. if ($condition['member_id'] != ''){
  165. $condition_str .= " and member_id = '".$condition['member_id']."'";
  166. }
  167. if ($condition['fav_type'] != ''){
  168. $condition_str .= " and fav_type = '".$condition['fav_type']."'";
  169. }
  170. if ($condition['goods_id'] != ''){
  171. $condition_str .= " and goods_id = '".$condition['goods_id']."'";
  172. }
  173. if ($condition['store_id'] != ''){
  174. $condition_str .= " and store_id = '".$condition['store_id']."'";
  175. }
  176. if ($condition['fav_id_in'] !=''){
  177. $condition_str .= " and favorites.fav_id in({$condition['fav_id_in']}) ";
  178. }
  179. return $condition_str;
  180. }
  181. }