store.model.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. /**
  3. * 店铺模型管理
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class storeModel extends Model {
  11. /**
  12. * 自营店铺的ID
  13. *
  14. * array(
  15. * '店铺ID(int)' => '是否绑定了全部商品类目(boolean)',
  16. * // ..
  17. * )
  18. */
  19. protected $ownShopIds;
  20. public function __construct() {
  21. parent::__construct('store');
  22. }
  23. /**
  24. * 删除缓存自营店铺的ID
  25. */
  26. public function dropCachedOwnShopIds() {
  27. $this->ownShopIds = null;
  28. dkcache('own_shop_ids');
  29. }
  30. /**
  31. * 获取自营店铺的ID
  32. *
  33. * @param boolean $bind_all_gc = false 是否只获取绑定全部类目的自营店 默认否(即全部自营店)
  34. * @return array
  35. */
  36. public function getOwnShopIds($bind_all_gc = false) {
  37. $data = $this->ownShopIds;
  38. // 属性为空则取缓存
  39. if (!$data) {
  40. $data = rkcache('own_shop_ids');
  41. // 缓存为空则查库
  42. if (!$data) {
  43. $data = array();
  44. $all_own_shops = $this->table('store')->field('store_id,bind_all_gc')->where(array(
  45. 'is_own_shop' => 1,
  46. ))->select();
  47. foreach ((array) $all_own_shops as $v) {
  48. $data[$v['store_id']] = (int) (bool) $v['bind_all_gc'];
  49. }
  50. // 写入缓存
  51. wkcache('own_shop_ids', $data);
  52. }
  53. // 写入属性
  54. $this->ownShopIds = $data;
  55. }
  56. return array_keys($bind_all_gc ? array_filter($data) : $data);
  57. }
  58. /**
  59. * 查询店铺列表
  60. *
  61. * @param array $condition 查询条件
  62. * @param int $page 分页数
  63. * @param string $order 排序
  64. * @param string $field 字段
  65. * @param string $limit 取多少条
  66. * @return array
  67. */
  68. public function getStoreList($condition, $page = null, $order = '', $field = '*', $limit = '') {
  69. $result = $this->field($field)->where($condition)->order($order)->limit($limit)->page($page)->select();
  70. return $result;
  71. }
  72. /**
  73. * 查询有效店铺列表
  74. *
  75. * @param array $condition 查询条件
  76. * @param int $page 分页数
  77. * @param string $order 排序
  78. * @param string $field 字段
  79. * @return array
  80. */
  81. public function getStoreOnlineList($condition, $page = null, $order = '', $field = '*') {
  82. $condition['store_state'] = 1;
  83. return $this->getStoreList($condition, $page, $order, $field);
  84. }
  85. /**
  86. * 店铺数量
  87. * @param array $condition
  88. * @return int
  89. */
  90. public function getStoreCount($condition) {
  91. return $this->where($condition)->count();
  92. }
  93. /**
  94. * 按店铺编号查询店铺的信息
  95. *
  96. * @param array $storeid_array 店铺编号
  97. * @return array
  98. */
  99. public function getStoreMemberIDList($storeid_array, $field = 'store_id,member_id,store_domain') {
  100. $store_list = $this->table('store')->where(array('store_id'=> array('in', $storeid_array)))->field($field)->key('store_id')->select();
  101. return $store_list;
  102. }
  103. /**
  104. * 查询店铺信息
  105. *
  106. * @param array $condition 查询条件
  107. * @return array
  108. */
  109. public function getStoreInfo($condition) {
  110. $store_info = $this->where($condition)->find();
  111. if(!empty($store_info)) {
  112. if(!empty($store_info['store_presales'])) $store_info['store_presales'] = unserialize($store_info['store_presales']);
  113. if(!empty($store_info['store_aftersales'])) $store_info['store_aftersales'] = unserialize($store_info['store_aftersales']);
  114. //商品数
  115. $model_goods = Model('goods');
  116. $store_info['goods_count'] = $model_goods->getGoodsCommonOnlineCount(array('store_id' => $store_info['store_id']));
  117. //店铺评价
  118. $model_evaluate_store = Model('evaluate_store');
  119. $store_evaluate_info = $model_evaluate_store->getEvaluateStoreInfoByStoreID($store_info['store_id'], $store_info['sc_id']);
  120. $store_info = array_merge($store_info, $store_evaluate_info);
  121. }
  122. return $store_info;
  123. }
  124. /**
  125. * 通过店铺编号查询店铺信息
  126. *
  127. * @param int $store_id 店铺编号
  128. * @return array
  129. */
  130. public function getStoreInfoByID($store_id) {
  131. $prefix = 'store_info';
  132. $store_info = rcache($store_id, $prefix);
  133. if(empty($store_info)) {
  134. $store_info = $this->table('store')->getStoreInfo(array('store_id' => $store_id));
  135. $cache = array();
  136. $cache['store_info'] = serialize($store_info);
  137. wcache($store_id, $cache, $prefix, 60 * 24);
  138. } else {
  139. $store_info = unserialize($store_info['store_info']);
  140. }
  141. return $store_info;
  142. }
  143. public function getStoreOnlineInfoByID($store_id) {
  144. $store_info = $this->getStoreInfoByID($store_id);
  145. if(empty($store_info) || $store_info['store_state'] == '0') {
  146. return array();
  147. } else {
  148. return $store_info;
  149. }
  150. }
  151. public function getStoreIDString($condition) {
  152. $condition['store_state'] = 1;
  153. $store_list = $this->getStoreList($condition);
  154. $store_id_string = '';
  155. foreach ($store_list as $value) {
  156. $store_id_string .= $value['store_id'].',';
  157. }
  158. return $store_id_string;
  159. }
  160. /*
  161. * 添加店铺
  162. *
  163. * @param array $param 店铺信息
  164. * @return bool
  165. */
  166. public function addStore($param){
  167. return $this->insert($param);
  168. }
  169. /*
  170. * 编辑店铺
  171. *
  172. * @param array $update 更新信息
  173. * @param array $condition 条件
  174. * @return bool
  175. */
  176. public function editStore($update, $condition){
  177. //清空缓存
  178. $store_list = $this->getStoreList($condition);
  179. foreach ($store_list as $value) {
  180. dcache($value['store_id'], 'store_info');
  181. }
  182. return $this->where($condition)->update($update);
  183. }
  184. /*
  185. * 删除店铺
  186. *
  187. * @param array $condition 条件
  188. * @return bool
  189. */
  190. public function delStore($condition){
  191. $store_info = $this->getStoreInfo($condition);
  192. //删除店铺相关图片
  193. @unlink(BASE_UPLOAD_PATH.DS.ATTACH_STORE.DS.$store_info['store_label']);
  194. @unlink(BASE_UPLOAD_PATH.DS.ATTACH_STORE.DS.$store_info['store_banner']);
  195. if($store_info['store_slide'] != ''){
  196. foreach(explode(',', $store_info['store_slide']) as $val){
  197. @unlink(BASE_UPLOAD_PATH.DS.ATTACH_SLIDE.DS.$val);
  198. }
  199. }
  200. //清空缓存
  201. dcache($store_info['store_id'], 'store_info');
  202. return $this->where($condition)->delete();
  203. }
  204. /**
  205. * 完全删除店铺 包括店主账号、店铺的管理员账号、店铺相册、店铺扩展
  206. */
  207. public function delStoreEntirely($condition)
  208. {
  209. $this->delStore($condition);
  210. Model('seller')->delSeller($condition);
  211. Model('seller_group')->delSellerGroup($condition);
  212. Model('album')->delAlbum($condition['store_id']);
  213. Model('store_extend')->delStoreExtend($condition);
  214. }
  215. /**
  216. * 获取商品销售排行(每天更新一次)
  217. *
  218. * @param int $store_id 店铺编号
  219. * @param int $limit 数量
  220. * @return array 商品信息
  221. */
  222. public function getHotSalesList($store_id, $limit = 5) {
  223. $prefix = 'store_hot_sales_list_' . $limit;
  224. $hot_sales_list = rcache($store_id, $prefix);
  225. if(empty($hot_sales_list)) {
  226. $model_goods = Model('goods');
  227. $hot_sales_list = $model_goods->getGoodsOnlineList(array('store_id' => $store_id), '*', 0, 'goods_salenum desc', $limit);
  228. $cache = array();
  229. $cache['hot_sales'] = serialize($hot_sales_list);
  230. wcache($store_id, $cache, $prefix, 60 * 24);
  231. } else {
  232. $hot_sales_list = unserialize($hot_sales_list['hot_sales']);
  233. }
  234. return $hot_sales_list;
  235. }
  236. /**
  237. * 获取商品收藏排行(每天更新一次)
  238. *
  239. * @param int $store_id 店铺编号
  240. * @param int $limit 数量
  241. * @return array 商品信息
  242. */
  243. public function getHotCollectList($store_id, $limit = 5) {
  244. $prefix = 'store_collect_sales_list_' . $limit;
  245. $hot_collect_list = rcache($store_id, $prefix);
  246. if(empty($hot_collect_list)) {
  247. $model_goods = Model('goods');
  248. $hot_collect_list = $model_goods->getGoodsOnlineList(array('store_id' => $store_id), '*', 0, 'goods_collect desc', $limit);
  249. $cache = array();
  250. $cache['collect_sales'] = serialize($hot_collect_list);
  251. wcache($store_id, $cache, $prefix, 60 * 24);
  252. } else {
  253. $hot_collect_list = unserialize($hot_collect_list['collect_sales']);
  254. }
  255. return $hot_collect_list;
  256. }
  257. //by abc.com 店铺列表新增项
  258. /**
  259. * 获取店铺列表页附加信息
  260. *
  261. * @param array $store_array 店铺数组
  262. * @return array $store_array 包含近期销量和8个推荐商品的店铺数组
  263. */
  264. public function getStoreSearchList($store_array) {
  265. $store_array_new = array();
  266. if(!empty($store_array)){
  267. $model = Model();
  268. $no_cache_store = array();
  269. foreach ($store_array as $value) {
  270. //$store_search_info = rcache($value['store_id'],'store_search_info');
  271. //print_r($store_array);exit();
  272. //if($store_search_info !== FALSE) {
  273. // $store_array_new[$value['store_id']] = $store_search_info;
  274. //} else {
  275. // $no_cache_store[$value['store_id']] = $value;
  276. //}
  277. $no_cache_store[$value['store_id']] = $value;
  278. }
  279. if(!empty($no_cache_store)) {
  280. //获取店铺商品数
  281. $no_cache_store = $this->getStoreInfoBasic($no_cache_store);
  282. //获取店铺近期销量
  283. $no_cache_store = $this->getGoodsCountJq($no_cache_store);
  284. //获取店铺推荐商品
  285. $no_cache_store = $this->getGoodsListBySales($no_cache_store);
  286. //写入缓存
  287. foreach ($no_cache_store as $value) {
  288. wcache($value['store_id'],$value,'store_search_info');
  289. }
  290. $store_array_new = array_merge($store_array_new,$no_cache_store);
  291. }
  292. }
  293. return $store_array_new;
  294. }
  295. /**
  296. * 获得店铺标志、信用、商品数量、店铺评分等信息
  297. *
  298. * @param array $param 店铺数组
  299. * @return array 数组格式的返回结果
  300. */
  301. public function getStoreInfoBasic($list,$day = 0){
  302. $list_new = array();
  303. if (!empty($list) && is_array($list)){
  304. foreach ($list as $key=>$value) {
  305. if(!empty($value)) {
  306. $value['store_logo'] = getStoreLogo($value['store_logo']);
  307. //店铺评价
  308. $model_evaluate_store = Model('evaluate_store');
  309. $store_evaluate_info = $model_evaluate_store->getEvaluateStoreInfoByStoreID($value['store_id'], $value['sc_id']);
  310. $value = array_merge($value, $store_evaluate_info);
  311. if(!empty($value['store_presales'])) $value['store_presales'] = unserialize($value['store_presales']);
  312. if(!empty($value['store_aftersales'])) $value['store_aftersales'] = unserialize($value['store_aftersales']);
  313. $list_new[$value['store_id']] = $value;
  314. $list_new[$value['store_id']]['goods_count'] = 0;
  315. }
  316. }
  317. //全部商品数直接读取缓存
  318. if($day > 0) {
  319. $store_id_string = implode(',',array_keys($list_new));
  320. //指定天数直接查询数据库
  321. $condition = array();
  322. $condition['goods_show'] = '1';
  323. $condition['store_id'] = array('in',$store_id_string);
  324. $condition['goods_add_time'] = array('gt',strtotime("-{$day} day"));
  325. $model = Model();
  326. $goods_count_array = $model->table('goods')->field('store_id,count(*) as goods_count')->where($condition)->group('store_id')->select();
  327. if (!empty($goods_count_array)){
  328. foreach ($goods_count_array as $value){
  329. $list_new[$value['store_id']]['goods_count'] = $value['goods_count'];
  330. }
  331. }
  332. } else {
  333. $list_new = $this->getGoodsCountByStoreArray($list_new);
  334. }
  335. }
  336. return $list_new;
  337. }
  338. /**
  339. * 获取店铺商品数
  340. *
  341. * @param array $store_array 店铺数组
  342. * @return array $store_array 包含商品数goods_count的店铺数组
  343. */
  344. public function getGoodsCountByStoreArray($store_array) {
  345. $store_array_new = array();
  346. $model = Model();
  347. $no_cache_store = '';
  348. foreach ($store_array as $value) {
  349. $goods_count = rcache($value['store_id'],'store_goods_count');
  350. if(!empty($goods_count)&&$goods_count !== FALSE) {
  351. //有缓存的直接赋值
  352. $value['goods_count'] = $goods_count;
  353. } else {
  354. //没有缓存记录store_id,统计从数据库读取
  355. $no_cache_store .= $value['store_id'].',';
  356. $value['goods_count'] = '0';
  357. }
  358. $store_array_new[$value['store_id']] = $value;
  359. }
  360. if(!empty($no_cache_store)) {
  361. //从数据库读取店铺商品数赋值并缓存
  362. $no_cache_store = rtrim($no_cache_store,',');
  363. $condition = array();
  364. $condition['goods_state'] = '1';
  365. $condition['store_id'] = array('in',$no_cache_store);
  366. $goods_count_array = $model->table('goods')->field('store_id,count(*) as goods_count')->where($condition)->group('store_id')->select();
  367. if (!empty($goods_count_array)){
  368. foreach ($goods_count_array as $value){
  369. $store_array_new[$value['store_id']]['goods_count'] = $value['goods_count'];
  370. wcache($value['store_id'],$value['goods_count'],'store_goods_count');
  371. }
  372. }
  373. }
  374. return $store_array_new;
  375. }
  376. //获取近期销量
  377. private function getGoodsCountJq($store_array) {
  378. $model = Model();
  379. $order_count_array = $model->table('order')->field('store_id,count(*) as order_count')->where(array('store_id'=>array('in',implode(',',array_keys($store_array))),'add_time'=>array('gt',time()-3600*24*90)))->group('store_id')->select();
  380. foreach ((array)$order_count_array as $value) {
  381. $store_array[$value['store_id']]['num_sales_jq'] = $value['order_count'];
  382. }
  383. return $store_array;
  384. }
  385. //获取店铺8个销量最高商品
  386. private function getGoodsListBySales($store_array) {
  387. $model = Model();
  388. $field = 'goods_id,store_id,goods_name,goods_image,goods_price,goods_salenum';
  389. foreach ($store_array as $value) {
  390. $store_array[$value['store_id']]['search_list_goods'] = $model->table('goods')->field($field)->where(array('store_id'=>$value['store_id'],'goods_state'=>1))->order('goods_salenum desc')->limit(8)->select();
  391. }
  392. return $store_array;
  393. }
  394. }