Store.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <?php
  2. /**
  3. * 店铺设置
  4. *
  5. */
  6. namespace app\common\model;
  7. use think\facade\Db;
  8. /**
  9. * ============================================================================
  10. * DSMall多用户商城
  11. * ============================================================================
  12. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  13. * 网站地址: http://www.csdeshang.com
  14. * ----------------------------------------------------------------------------
  15. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  16. * 不允许对程序代码以任何形式任何目的的再发布。
  17. * ============================================================================
  18. * 数据层模型
  19. */
  20. class Store extends BaseModel {
  21. public $page_info;
  22. /**
  23. * 自营店铺的ID
  24. * @access protected
  25. * @author csdeshang
  26. * array(
  27. * '店铺ID(int)' => '是否绑定了全部商品类目(boolean)',
  28. * // ..
  29. * )
  30. */
  31. protected $ownShopIds;
  32. /**
  33. * 删除缓存自营店铺的ID
  34. * @access public
  35. * @author csdeshang
  36. */
  37. public function dropCachedOwnShopIds() {
  38. $this->ownShopIds = null;
  39. dkcache('own_shop_ids');
  40. }
  41. /**
  42. * 获取自营店铺的ID
  43. * @access public
  44. * @author csdeshang
  45. * @param boolean $bind_all_gc = false 是否只获取绑定全部类目的自营店 默认否(即全部自营店)
  46. * @return int
  47. */
  48. public function getOwnShopIds($bind_all_gc = false) {
  49. $data = $this->ownShopIds;
  50. // 属性为空则取缓存
  51. if (!$data) {
  52. $data = rkcache('own_shop_ids');
  53. // 缓存为空则查库
  54. if (!$data) {
  55. $data = array();
  56. $all_own_shops = Db::name('store')->field('store_id,bind_all_gc')->where(array('is_platform_store' => 1,))->select()->toArray();
  57. foreach ((array) $all_own_shops as $v) {
  58. $data[$v['store_id']] = (int) (bool) $v['bind_all_gc'];
  59. }
  60. // 写入缓存
  61. wkcache('own_shop_ids', $data);
  62. }
  63. // 写入属性
  64. $this->ownShopIds = $data;
  65. }
  66. return array_keys($bind_all_gc ? array_filter($data) : $data);
  67. }
  68. /**
  69. * 查询店铺列表
  70. * @access public
  71. * @author csdeshang
  72. * @param array $condition 查询条件
  73. * @param int $pagesize 分页数
  74. * @param string $order 排序
  75. * @param string $field 字段
  76. * @param string $limit 限制条数
  77. * @return array
  78. */
  79. public function getStoreList($condition, $pagesize = null, $order = '', $field = '*', $limit = 0) {
  80. if ($pagesize) {
  81. $result = Db::name('store')->field($field)->where($condition)->order($order)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  82. $this->page_info = $result;
  83. return $result->items();
  84. } else {
  85. $result = Db::name('store')->field($field)->where($condition)->order($order)->limit($limit)->select()->toArray();
  86. return $result;
  87. }
  88. }
  89. /**
  90. * 查询有效店铺列表
  91. * @access public
  92. * @author csdeshang
  93. * @param array $condition 查询条件
  94. * @param int $pagesize 分页数
  95. * @param string $order 排序
  96. * @param string $field 字段
  97. * @return array
  98. */
  99. public function getStoreOnlineList($condition, $pagesize = null, $order = '', $field = '*') {
  100. $condition[]=array('store_state','=',1);
  101. return $this->getStoreList($condition, $pagesize, $order, $field);
  102. }
  103. /**
  104. * 店铺数量
  105. * @access public
  106. * @author csdeshang
  107. * @param type $condition 条件
  108. * @return type
  109. */
  110. public function getStoreCount($condition) {
  111. return Db::name('store')->where($condition)->count();
  112. }
  113. /**
  114. * 按店铺编号查询店铺的信息
  115. * @access public
  116. * @author csdeshang
  117. * @param type $storeid_array 店铺ID编号
  118. * @param type $field 字段
  119. * @return type
  120. */
  121. public function getStoreMemberIDList($storeid_array, $field = 'store_id,member_id,store_name') {
  122. $store_list = Db::name('store')->where('store_id','in',$storeid_array)->field($field)->select()->toArray();
  123. $store_list = ds_change_arraykey($store_list, 'store_id');
  124. return $store_list;
  125. }
  126. /**
  127. * 查询店铺信息
  128. * @access public
  129. * @author csdeshang
  130. * @param array $condition 查询条件
  131. * @return array
  132. */
  133. public function getStoreInfo($condition) {
  134. $store_info = Db::name('store')->where($condition)->find();
  135. if (!empty($store_info)) {
  136. if (!empty($store_info['store_presales']))
  137. $store_info['store_presales'] = unserialize($store_info['store_presales']);
  138. if (!empty($store_info['store_aftersales']))
  139. $store_info['store_aftersales'] = unserialize($store_info['store_aftersales']);
  140. //商品数
  141. $goods_model = model('goods');
  142. $store_info['goods_count'] = $goods_model->getGoodsCommonOnlineCount(array(array('store_id' ,'=', $store_info['store_id'])));
  143. //店铺评价
  144. $evaluatestore_model = model('evaluatestore');
  145. $store_evaluate_info = $evaluatestore_model->getEvaluatestoreInfoByStoreID($store_info['store_id'], $store_info['storeclass_id']);
  146. $store_info = array_merge($store_info, $store_evaluate_info);
  147. }
  148. return $store_info;
  149. }
  150. /**
  151. * 通过店铺编号查询店铺信息
  152. * @access public
  153. * @author csdeshang
  154. * @param int $store_id 店铺编号
  155. * @return array
  156. */
  157. public function getStoreInfoByID($store_id) {
  158. $prefix = 'store_info';
  159. $store_info = rcache($store_id, $prefix);
  160. if (empty($store_info)) {
  161. $store_info = $this->getStoreInfo(array('store_id' => $store_id));
  162. $cache = array();
  163. $cache['store_info'] = serialize($store_info);
  164. wcache($store_id, $cache, $prefix, 60 * 24);
  165. } else {
  166. $store_info = unserialize($store_info['store_info']);
  167. }
  168. return $store_info;
  169. }
  170. /**
  171. * 获取店铺信息根据店铺id
  172. * @access public
  173. * @author csdeshang
  174. * @param type $store_id 店铺ID
  175. * @return type
  176. */
  177. public function getStoreOnlineInfoByID($store_id) {
  178. $store_info = $this->getStoreInfoByID($store_id);
  179. if (empty($store_info) || $store_info['store_state'] == '0') {
  180. return array();
  181. } else {
  182. return $store_info;
  183. }
  184. }
  185. /**
  186. * 获取店铺ID字符串
  187. * @access public
  188. * @author csdeshang
  189. * @param array $condition 条件
  190. * @return string
  191. */
  192. public function getStoreIDString($condition) {
  193. $condition[]=array('store_state','=',1);
  194. $store_list = $this->getStoreList($condition);
  195. $store_id_string = '';
  196. foreach ($store_list as $value) {
  197. $store_id_string .= $value['store_id'] . ',';
  198. }
  199. return $store_id_string;
  200. }
  201. /**
  202. * 添加店铺
  203. * @access public
  204. * @author csdeshang
  205. * @param type $data 店铺数据
  206. * @return type
  207. */
  208. public function addStore($data) {
  209. return Db::name('store')->insertGetId($data);
  210. }
  211. /**
  212. * 编辑店铺
  213. * @access public
  214. * @author csdeshang
  215. * @param type $update 更新数据
  216. * @param type $condition 条件
  217. * @return type
  218. */
  219. public function editStore($update, $condition) {
  220. //清空缓存
  221. $store_list = $this->getStoreList($condition);
  222. foreach ($store_list as $value) {
  223. dcache($value['store_id'], 'store_info');
  224. }
  225. return Db::name('store')->where($condition)->update($update);
  226. }
  227. /**
  228. * 删除店铺
  229. * @access public
  230. * @author csdeshang
  231. * @param array $condition 条件
  232. * @return bool
  233. */
  234. public function delStore($condition) {
  235. $store_info = $this->getStoreInfo($condition);
  236. //删除店铺相关图片
  237. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_STORE . DIRECTORY_SEPARATOR . $store_info['store_logo']);
  238. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_STORE . DIRECTORY_SEPARATOR . $store_info['store_banner']);
  239. if (isset($store_info['store_slide'])&&$store_info['store_slide'] != '') {
  240. foreach (explode(',', $store_info['store_slide']) as $val) {
  241. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_SLIDE . DIRECTORY_SEPARATOR . $val);
  242. }
  243. }
  244. //清空缓存
  245. dcache($store_info['store_id'], 'store_info');
  246. return Db::name('store')->where($condition)->delete();
  247. }
  248. /**
  249. * 完全删除店铺 包括店主账号、店铺的管理员账号、店铺相册、店铺扩展
  250. * @access public
  251. * @author csdeshang
  252. * @param type $condition 条件
  253. */
  254. public function delStoreEntirely($condition) {
  255. $this->delStore($condition);
  256. model('seller')->delSeller($condition);
  257. model('sellergroup')->delSellergroup($condition);
  258. model('album')->delAlbum($condition['store_id']);
  259. model('storeextend')->delStoreextend($condition);
  260. model('storegoodsclass')->delStoregoodsclass($condition,$condition['store_id']);
  261. model('storemsg')->delStoremsg($condition);
  262. model('storenavigation')->delStorenavigation(array('storenav_store_id'=>$condition['store_id']));
  263. model('storeplate')->delStoreplate($condition);
  264. model('storereopen')->delStorereopen(array('storereopen_store_id'=>$condition['store_id']));
  265. model('storewatermark')->delStorewatermark($condition);
  266. }
  267. /**
  268. * 获取商品销售排行(每天更新一次)
  269. * @access public
  270. * @author csdeshang
  271. * @param int $store_id 店铺编号
  272. * @param int $limit 限制数量
  273. * @return array
  274. */
  275. public function getHotSalesList($store_id, $limit = 5) {
  276. $prefix = 'store_hot_sales_list_' . $limit;
  277. $hot_sales_list = rcache($store_id, $prefix);
  278. if (empty($hot_sales_list)) {
  279. $goods_model = model('goods');
  280. $hot_sales_list = $goods_model->getGoodsOnlineList(array(array('store_id' ,'=', $store_id)), '*', 0, 'goods_salenum desc', $limit);
  281. $cache = array();
  282. $cache['hot_sales'] = serialize($hot_sales_list);
  283. wcache($store_id, $cache, $prefix, 60 * 24);
  284. } else {
  285. $hot_sales_list = unserialize($hot_sales_list['hot_sales']);
  286. }
  287. return $hot_sales_list;
  288. }
  289. /**
  290. * 获取商品收藏排行(每天更新一次)
  291. * @access public
  292. * @author csdeshang
  293. * @param int $store_id 店铺编号
  294. * @param int $limit 限制数量
  295. * @return array 商品信息
  296. */
  297. public function getHotCollectList($store_id, $limit = 5) {
  298. $prefix = 'store_collect_sales_list_' . $limit;
  299. $hot_collect_list = rcache($store_id, $prefix);
  300. if (empty($hot_collect_list)) {
  301. $goods_model = model('goods');
  302. $hot_collect_list = $goods_model->getGoodsOnlineList(array(array('store_id' ,'=', $store_id)), '*', 0, 'goods_collect desc', $limit);
  303. $cache = array();
  304. $cache['collect_sales'] = serialize($hot_collect_list);
  305. wcache($store_id, $cache, $prefix, 60 * 24);
  306. } else {
  307. $hot_collect_list = unserialize($hot_collect_list['collect_sales']);
  308. }
  309. return $hot_collect_list;
  310. }
  311. /**
  312. * 获取店铺列表页附加信息
  313. * @access public
  314. * @author csdeshang
  315. * @param array $store_array 店铺数组
  316. * @return array 包含近期销量和8个推荐商品的店铺数组
  317. */
  318. public function getStoreSearchList($store_array) {
  319. $store_array_new = array();
  320. if (!empty($store_array)) {
  321. $no_cache_store = array();
  322. foreach ($store_array as $value) {
  323. //$store_search_info = rcache($value['store_id']);
  324. //print_r($store_array);exit();
  325. //if($store_search_info !== FALSE) {
  326. // $store_array_new[$value['store_id']] = $store_search_info;
  327. //} else {
  328. // $no_cache_store[$value['store_id']] = $value;
  329. //}
  330. $no_cache_store[$value['store_id']] = $value;
  331. }
  332. if (!empty($no_cache_store)) {
  333. //获取店铺商品数
  334. $no_cache_store = $this->getStoreInfoBasic($no_cache_store);
  335. //获取店铺近期销量
  336. $no_cache_store = $this->getGoodsCountJq($no_cache_store);
  337. //获取店铺推荐商品
  338. $no_cache_store = $this->getGoodsListBySales($no_cache_store);
  339. //写入缓存
  340. foreach ($no_cache_store as $value) {
  341. wcache($value['store_id'], $value, 'store_search_info');
  342. }
  343. $store_array_new = array_merge($store_array_new, $no_cache_store);
  344. }
  345. }
  346. return $store_array_new;
  347. }
  348. /**
  349. * 获得店铺标志、信用、商品数量、店铺评分等信息
  350. * @access public
  351. * @author csdeshang
  352. * @param type $list 店铺数组
  353. * @param type $day 天数
  354. * @return type
  355. */
  356. public function getStoreInfoBasic($list, $day = 0) {
  357. $list_new = array();
  358. if (!empty($list) && is_array($list)) {
  359. foreach ($list as $key => $value) {
  360. if (!empty($value)) {
  361. $value['store_logo'] = get_store_logo($value['store_logo']);
  362. //店铺评价
  363. $evaluatestore_model = model('evaluatestore');
  364. $store_evaluate_info = $evaluatestore_model->getEvaluatestoreInfoByStoreID($value['store_id'], $value['storeclass_id']);
  365. $value = array_merge($value, $store_evaluate_info);
  366. if (!empty($value['store_presales']))
  367. $value['store_presales'] = unserialize($value['store_presales']);
  368. if (!empty($value['store_aftersales']))
  369. $value['store_aftersales'] = unserialize($value['store_aftersales']);
  370. $list_new[$value['store_id']] = $value;
  371. $list_new[$value['store_id']]['goods_count'] = 0;
  372. }
  373. }
  374. //全部商品数直接读取缓存
  375. if ($day > 0) {
  376. $store_id_string = implode(',', array_keys($list_new));
  377. //指定天数直接查询数据库
  378. $condition = array();
  379. $condition[] = array('goods_show','=',1);
  380. $condition[] = array('store_id','in',$store_id_string);
  381. $condition[] = array('goods_addtime','>',strtotime("-{$day} day"));
  382. $goods_count_array = Db::name('goods')->field('store_id,count(*) as goods_count')->where($condition)->group('store_id')->select()->toArray();
  383. if (!empty($goods_count_array)) {
  384. foreach ($goods_count_array as $value) {
  385. $list_new[$value['store_id']]['goods_count'] = $value['goods_count'];
  386. }
  387. }
  388. } else {
  389. $list_new = $this->getGoodsCountByStoreArray($list_new);
  390. }
  391. }
  392. return $list_new;
  393. }
  394. /**
  395. * 获取店铺商品数
  396. * @access public
  397. * @author csdeshang
  398. * @param type $store_array 店铺数组
  399. * @return type
  400. */
  401. public function getGoodsCountByStoreArray($store_array) {
  402. $store_array_new = array();
  403. $no_cache_store = '';
  404. foreach ($store_array as $value) {
  405. $goods_count = rcache($value['store_id'], 'store_goods_count');
  406. if (!empty($goods_count) && $goods_count !== FALSE) {
  407. //有缓存的直接赋值
  408. $value['goods_count'] = $goods_count;
  409. } else {
  410. //没有缓存记录store_id,统计从数据库读取
  411. $no_cache_store .= $value['store_id'] . ',';
  412. $value['goods_count'] = '0';
  413. }
  414. $store_array_new[$value['store_id']] = $value;
  415. }
  416. if (!empty($no_cache_store)) {
  417. //从数据库读取店铺商品数赋值并缓存
  418. $no_cache_store = rtrim($no_cache_store, ',');
  419. $condition = array();
  420. $condition[] = array('goods_state','=',1);
  421. $condition[] = array('store_id','in', $no_cache_store);
  422. $goods_count_array = Db::name('goods')->field('store_id,count(*) as goods_count')->where($condition)->group('store_id')->select()->toArray();
  423. if (!empty($goods_count_array)) {
  424. foreach ($goods_count_array as $value) {
  425. $store_array_new[$value['store_id']]['goods_count'] = $value['goods_count'];
  426. wcache($value['store_id'], $value['goods_count'], 'store_goods_count');
  427. }
  428. }
  429. }
  430. return $store_array_new;
  431. }
  432. /**
  433. * 获取近期销量
  434. * @access public
  435. * @author csdeshang
  436. * @param type $store_array 店铺数组
  437. * @return type
  438. */
  439. private function getGoodsCountJq($store_array) {
  440. $order_count_array = Db::name('order')->field('store_id,count(*) as order_count')->where('store_id','in',implode(',', array_keys($store_array)))->where('order_state','<>','0')->where('add_time','>',TIMESTAMP - 3600 * 24 * 90)->group('store_id')->select()->toArray();
  441. foreach ((array) $order_count_array as $value) {
  442. $store_array[$value['store_id']]['num_sales_jq'] = $value['order_count'];
  443. }
  444. return $store_array;
  445. }
  446. /**
  447. * 获取店铺8个销量最高商品
  448. * @access public
  449. * @author csdeshang
  450. * @param type $store_array 店铺数组
  451. * @return type
  452. */
  453. private function getGoodsListBySales($store_array) {
  454. $field = 'goods_id,store_id,goods_name,goods_image,goods_price,goods_salenum';
  455. foreach ($store_array as $value) {
  456. $store_array[$value['store_id']]['search_list_goods'] = Db::name('goods')->field($field)->where(array('store_id' => $value['store_id'], 'goods_state' => 1))->order('goods_salenum desc')->limit(8)->select()->toArray();
  457. }
  458. return $store_array;
  459. }
  460. /**
  461. * 编辑
  462. * @param type $condition
  463. * @param type $data
  464. * @return type
  465. */
  466. public function editGoodscommon($condition,$data){
  467. return Db::name('goodscommon')->where($condition)->update($data);
  468. }
  469. /**
  470. * 编辑商品
  471. * @param type $condition
  472. * @param type $data
  473. * @return type
  474. */
  475. public function editGoods($condition,$data){
  476. return Db::name('goods')->where($condition)->update($data);
  477. }
  478. /**
  479. * 插入店铺扩展表
  480. * @param type $condition
  481. * @return type
  482. */
  483. public function addStoreextend($condition){
  484. return Db::name('storeextend')->insert($condition);
  485. }
  486. /**
  487. * 获取单个店铺
  488. * @param type $condition
  489. * @param type $field
  490. * @return type
  491. */
  492. public function getOneStore($condition,$field){
  493. return Db::name('store')->field($field)->where($condition)->find();
  494. }
  495. }