PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/core/service/shop.service.php

https://github.com/alin40404/FanweShare
PHP | 200 lines | 164 code | 19 blank | 17 comment | 12 complexity | e2ef8cd9350e0a108a97f42c3dc5950f MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * 店铺服务类
  4. * @author awfigq
  5. *
  6. */
  7. class ShopService
  8. {
  9. public function getShopExistsByUrl($url)
  10. {
  11. $sql = 'SELECT COUNT(shop_id) FROM '.FDB::table('shop')." WHERE shop_url = '$url'";
  12. return (int)FDB::resultFirst($sql) > 0;
  13. }
  14. public function updateShopStatistic($shop_ids)
  15. {
  16. if(is_array($shop_ids))
  17. {
  18. $shop_ids = array_unique($shop_ids);
  19. foreach($shop_ids as $shop_id)
  20. {
  21. $shop_id = (int)$shop_id;
  22. if($shop_id > 0)
  23. {
  24. $data = array();
  25. $data['recommend_count'] = ShopService::getShopUserCount($shop_id);
  26. $temp = array();
  27. $temp['tags'] = ShopService::getShopTags($shop_id);
  28. $temp['goods'] = ShopService::getShopGoods($shop_id);
  29. $data['data'] = addslashes(serialize($temp));
  30. FDB::update('shop',$data,'shop_id = '.$shop_id);
  31. }
  32. }
  33. }
  34. }
  35. public function formatShopList($shop_ids)
  36. {
  37. $shop_ids = implode(',',$shop_ids);
  38. if(empty($shop_ids))
  39. return array();
  40. $sql = 'SELECT goods_id FROM '.FDB::table('share_goods').'
  41. WHERE shop_id = '.$shop_id.' ORDER BY goods_id DESC LIMIT 0,'.$num;
  42. $list = array();
  43. $res = FDB::query($sql);
  44. while($data = FDB::fetch($res))
  45. {
  46. $list[] = $data['goods_id'];
  47. }
  48. return $list;
  49. }
  50. public function getShopGoods($shop_id,$num = 10)
  51. {
  52. $sql = 'SELECT goods_id FROM '.FDB::table('share_goods').'
  53. WHERE shop_id = '.$shop_id.' ORDER BY goods_id DESC LIMIT 0,'.$num;
  54. $list = array();
  55. $res = FDB::query($sql);
  56. while($data = FDB::fetch($res))
  57. {
  58. $list[] = $data['goods_id'];
  59. }
  60. return $list;
  61. }
  62. public function getShopUserCount($shop_id)
  63. {
  64. $sql = 'SELECT COUNT(DISTINCT uid) AS user_count FROM '.FDB::table('share_goods').' WHERE shop_id = '.$shop_id;
  65. return FDB::resultFirst($sql);
  66. }
  67. public function getShopTags($shop_id,$num = 20)
  68. {
  69. $sql = 'SELECT st.tag_name,COUNT(st.tag_name) AS tag_count FROM '.FDB::table('share_goods').' AS sg
  70. INNER JOIN '.FDB::table('share_tags').' AS st ON st.share_id = sg.share_id
  71. WHERE sg.shop_id = '.$shop_id.' GROUP BY st.tag_name ORDER BY tag_count DESC LIMIT 0,'.$num;
  72. $list = array();
  73. $res = FDB::query($sql);
  74. while($data = FDB::fetch($res))
  75. {
  76. $list[] = $data['tag_name'];
  77. }
  78. return $list;
  79. }
  80. public function getUserOtherShopAndTags($shop_id,$shop_num = 4,$tag_num = 12)
  81. {
  82. $sql = 'SELECT DISTINCT uid FROM '.FDB::table('share_goods').'
  83. WHERE shop_id = '.$shop_id.' ORDER BY goods_id DESC LIMIT 0,5000';
  84. $uids = array();
  85. $res = FDB::query($sql);
  86. while($data = FDB::fetch($res))
  87. {
  88. $uids[] = $data['uid'];
  89. }
  90. $uids = implode(',',$uids);
  91. if(!$uids)
  92. return ;
  93. $shop_count = FDB::resultFirst('SELECT COUNT(DISTINCT shop_id) FROM '.FDB::table('share_goods').'
  94. WHERE uid IN ('.$uids.') AND shop_id <> '.$shop_id);
  95. $list['shops'] = array();
  96. $list['tags'] = array();
  97. if($shop_count > 0)
  98. {
  99. $begin = 0;
  100. if($shop_count > $shop_num)
  101. $begin = mt_rand(0,$shop_count - $shop_num);
  102. $sql = 'SELECT DISTINCT shop_id FROM '.FDB::table('share_goods').'
  103. WHERE uid IN ('.$uids.') AND shop_id <> '.$shop_id.' ORDER BY shop_id DESC LIMIT '.$begin.','.$shop_num;
  104. $shop_ids = array();
  105. $res = FDB::query($sql);
  106. while($data = FDB::fetch($res))
  107. {
  108. $shop_ids[] = $data['shop_id'];
  109. }
  110. $shop_ids = implode(',',$shop_ids);
  111. $sql = 'SELECT * FROM '.FDB::table('shop').' WHERE shop_id IN ('.$shop_ids.')';
  112. $res = FDB::query($sql);
  113. while($data = FDB::fetch($res))
  114. {
  115. $cache_data = fStripslashes(unserialize($data['data']));
  116. $data['tags'] = array();
  117. if($cache_data)
  118. {
  119. if($cache_data['tags'] && is_array($cache_data['tags']))
  120. $data['tags'] = $cache_data['tags'];
  121. }
  122. unset($data['data']);
  123. $list['tags'] = array_merge($list['tags'],$data['tags']);
  124. $data['url'] = FU('shop/show',array('id'=>$data['shop_id']));
  125. $list['shops'][] = $data;
  126. }
  127. $list['tags'] = array_slice(array_unique($list['tags']),0,$tag_num);
  128. }
  129. return $list;
  130. }
  131. /**
  132. * 获取会员分享的店铺信息
  133. * @param int $uid 会员编号
  134. * @param int $num
  135. * @return array
  136. */
  137. public function getUserShareShops($uid,$num = 10)
  138. {
  139. $list = array();
  140. $shop_count = FDB::resultFirst('SELECT COUNT(goods_id) FROM '.FDB::table('share_goods').' WHERE shop_id > 0 AND uid = '.$uid);
  141. if($shop_count > 0)
  142. {
  143. $total = FDB::resultFirst('SELECT COUNT(DISTINCT shop_id) FROM '.FDB::table('share_goods').' WHERE shop_id > 0 AND uid = '.$uid);
  144. $sql = 'SELECT sg.shop_id,COUNT(sg.shop_id) AS shop_count,s.shop_name,s.shop_url,s.shop_logo,s.taoke_url
  145. FROM '.FDB::table('share_goods').' AS sg
  146. INNER JOIN '.FDB::table('shop').' AS s ON s.shop_id = sg.shop_id
  147. WHERE sg.uid = '.$uid.'
  148. GROUP BY sg.shop_id ORDER BY shop_count DESC LIMIT 0,'.$num;
  149. $res = FDB::query($sql);
  150. while($shop = FDB::fetch($res))
  151. {
  152. if(empty($shop['taoke_url']))
  153. $shop['to_url'] = FU('tgo',array('url'=>$shop['shop_url']));
  154. else
  155. $shop['to_url'] = FU('tgo',array('url'=>$shop['taoke_url']));
  156. $shop['percent'] = round($shop['shop_count'] / $shop_count * 100,1).'%';
  157. $list[] = $shop;
  158. }
  159. }
  160. return array('total'=>$total,'list'=>$list);
  161. }
  162. /**
  163. * 获取会员分享的店铺百分比信息HTML
  164. * @param int $uid 会员编号
  165. * @param int $num 显示数量
  166. * @return string
  167. */
  168. public function getUserShareShopHtml($uid,$num = 10)
  169. {
  170. $user = FS('User')->getUserById($uid);
  171. $user_lang = $_FANWE['uid'] == $uid ? lang('user','me') : lang('user','ta_'.$user['gender']);
  172. $shops = ShopService::getUserShareShops($uid,$num);
  173. $args = array(
  174. 'shops'=>&$shops,
  175. 'user'=>&$user,
  176. 'user_lang'=>&$user_lang,
  177. );
  178. return tplFetch("inc/shop/user_shop_percent",$args);
  179. }
  180. }
  181. ?>