goods.model.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  1. <?php
  2. /**
  3. * 商品管理
  4. *
  5. *好商城V3 BY 33ha o.c om
  6. */
  7. defined('InShopNC') or exit('Access Invalid!');
  8. class goodsModel extends Model
  9. {
  10. public function __construct()
  11. {
  12. parent::__construct('goods');
  13. }
  14. const STATE1 = 1; // 出售中
  15. const STATE0 = 0; // 下架
  16. const STATE10 = 10; // 违规
  17. const VERIFY1 = 1; // 审核通过
  18. const VERIFY0 = 0; // 审核失败
  19. const VERIFY10 = 10; // 等待审核
  20. /**
  21. * 新增商品数据
  22. *
  23. * @param array $insert 数据
  24. * @param string $table 表名
  25. */
  26. public function addGoods($insert)
  27. {
  28. $result = $this->table('goods')->insert($insert);
  29. if ($result) {
  30. $this->_dGoodsCache($result);
  31. $this->_dGoodsCommonCache($insert['goods_commonid']);
  32. $this->_dGoodsSpecCache($insert['goods_commonid']);
  33. }
  34. return $result;
  35. }
  36. /**
  37. * 新增商品公共数据
  38. *
  39. * @param array $insert 数据
  40. * @param string $table 表名
  41. */
  42. public function addGoodsCommon($insert)
  43. {
  44. return $this->table('goods_common')->insert($insert);
  45. }
  46. /**
  47. * 新增多条商品数据
  48. *
  49. * @param unknown $insert
  50. */
  51. public function addGoodsImagesAll($insert)
  52. {
  53. $result = $this->table('goods_images')->insertAll($insert);
  54. if ($result) {
  55. foreach ($insert as $val) {
  56. $this->_dGoodsImageCache($val['goods_commonid'] . '|' . $val['color_id']);
  57. }
  58. }
  59. return $result;
  60. }
  61. /**
  62. * 商品SKU列表
  63. *
  64. * @param array $condition 条件
  65. * @param string $field 字段
  66. * @param string $group 分组
  67. * @param string $order 排序
  68. * @param int $limit 限制
  69. * @param int $page 分页
  70. * @param boolean $lock 是否锁定
  71. * @return array 二维数组
  72. */
  73. public function getGoodsList($condition, $field = '*', $group = '', $order = '', $limit = 0, $page = 0, $lock = false, $count = 0)
  74. {
  75. $start = microtime(true);
  76. $condition = $this->_getRecursiveClass($condition);
  77. perfor_period("goods.model",$start,"getGoodsList");
  78. return $this->table('goods')->field($field)->where($condition)->group($group)->page($page, $count)->order($order)->limit($limit)->lock($lock)->select();
  79. }
  80. /**
  81. * 出售中的商品SKU列表(只显示不同颜色的商品,前台商品索引,店铺也商品列表等使用)
  82. * @param array $condition
  83. * @param string $field
  84. * @param string $order
  85. * @param number $page
  86. * @return array
  87. */
  88. public function getGoodsListByColorDistinct($condition, $field = '*', $order = 'goods_id asc', $page = 0)
  89. {
  90. $condition['goods_state'] = self::STATE1;
  91. $condition['goods_verify'] = self::VERIFY1;
  92. $condition = $this->_getRecursiveClass($condition);
  93. //去掉多规格颜色显示 好商城V3-B11
  94. // $field = "CONCAT(goods_commonid,',',color_id) as nc_distinct ," . $field;
  95. // $count = $this->getGoodsOnlineCount($condition, "distinct CONCAT(goods_commonid,',',color_id)");
  96. $fieldex = "CONCAT(goods_commonid) as nc_distinct, max(goods_storage) as max_goods_storage, " . $field;
  97. $count = $this->getGoodsOnlineCount($condition, "distinct CONCAT(goods_commonid)");
  98. $goods_list = array();
  99. if ($count != 0) {
  100. $goods_list = $this->getGoodsOnlineList($condition, $fieldex, $page, $order, 0, 'nc_distinct', false, $count);
  101. }
  102. $goods_ret = array();
  103. $goods_cids = array();
  104. foreach($goods_list as $goods)
  105. {
  106. $g_storage = intval($goods['goods_storage']);
  107. if($g_storage > 0 || $g_storage == intval($goods['max_goods_storage'])) {
  108. array_push($goods_ret,$goods);
  109. } else {
  110. array_push($goods_cids,intval($goods['goods_commonid']));
  111. }
  112. }
  113. if(!empty($goods_cids))
  114. {
  115. arsort($goods_cids);
  116. $condition['goods_commonid'] = array('in', $goods_cids);
  117. $items = $this->cls()->table('goods')->field($field)->where($condition)->order('goods_commonid desc,goods_storage desc')->limit(false)->select();
  118. foreach($goods_cids as $commonid)
  119. {
  120. $fAdd = true;
  121. foreach($items as $key => $goods)
  122. {
  123. if($commonid == intval($goods['goods_commonid'])) {
  124. if($fAdd) {
  125. array_push($goods_ret,$goods);
  126. $fAdd = false;
  127. }
  128. unset($items[$key]);
  129. }
  130. else {
  131. break;
  132. }
  133. }
  134. }
  135. }
  136. return $goods_ret;
  137. }
  138. /**
  139. * 在售商品SKU列表
  140. *
  141. * @param array $condition 条件
  142. * @param string $field 字段
  143. * @param string $group 分组
  144. * @param string $order 排序
  145. * @param int $limit 限制
  146. * @param int $page 分页
  147. * @param boolean $lock 是否锁定
  148. * @return array
  149. */
  150. public function getGeneralGoodsList($condition, $field = '*', $page = 0, $order = 'goods_id desc')
  151. {
  152. $condition['is_virtual'] = 0;
  153. $condition['is_fcode'] = 0;
  154. $condition['is_presell'] = 0;
  155. return $this->getGoodsList($condition, $field, '', $order, 0, $page, false, 0);
  156. }
  157. /**
  158. * 在售商品SKU列表
  159. *
  160. * @param array $condition 条件
  161. * @param string $field 字段
  162. * @param string $group 分组
  163. * @param string $order 排序
  164. * @param int $limit 限制
  165. * @param int $page 分页
  166. * @param boolean $lock 是否锁定
  167. * @return array
  168. */
  169. public function getGoodsOnlineList($condition, $field = '*', $page = 0, $order = 'goods_id desc', $limit = 0, $group = '', $lock = false, $count = 0)
  170. {
  171. $condition['goods_state'] = self::STATE1;
  172. $condition['goods_verify'] = self::VERIFY1;
  173. return $this->getGoodsList($condition, $field, $group, $order, $limit, $page, $lock, $count);
  174. }
  175. /**
  176. * 出售中的普通商品列表,即不包括虚拟商品、F码商品、预售商品
  177. */
  178. public function getGoodsListForPromotion($condition, $field = '*', $page = 0, $type = '')
  179. {
  180. switch ($type) {
  181. case 'xianshi':
  182. case 'bundling':
  183. case 'combo':
  184. $condition['is_virtual'] = 0;
  185. $condition['is_fcode'] = 0;
  186. $condition['is_presell'] = 0;
  187. $condition['goods_state'] = self::STATE1;
  188. $condition['goods_verify'] = self::VERIFY1;
  189. break;
  190. case 'gift':
  191. $condition['is_virtual'] = 0;
  192. break;
  193. default:
  194. break;
  195. }
  196. return $this->getGoodsList($condition, $field, '', '', 0, $page);
  197. }
  198. /**
  199. * 商品列表 卖家中心使用
  200. *
  201. * @param array $condition 条件
  202. * @param array $field 字段
  203. * @param string $page 分页
  204. * @param string $order 排序
  205. * @return array
  206. */
  207. public function getGoodsCommonList($condition, $field = '*', $page = 10, $order = 'goods_commonid desc')
  208. {
  209. $condition = $this->_getRecursiveClass($condition);
  210. return $this->table('goods_common')->field($field)->where($condition)->order($order)->page($page)->select();
  211. }
  212. /**
  213. * 出售中的商品列表 卖家中心使用
  214. *
  215. * @param array $condition 条件
  216. * @param array $field 字段
  217. * @param string $page 分页
  218. * @param string $order 排序
  219. * @return array
  220. */
  221. public function getGoodsCommonOnlineList($condition, $field = '*', $page = 10, $order = "goods_commonid desc")
  222. {
  223. $condition['goods_state'] = self::STATE1;
  224. $condition['goods_verify'] = self::VERIFY1;
  225. return $this->getGoodsCommonList($condition, $field, $page, $order);
  226. }
  227. /**
  228. * 出售中的普通商品列表,即不包括虚拟商品、F码商品、预售商品
  229. */
  230. public function getGoodsCommonListForPromotion($condition, $field = '*', $page = 10, $type)
  231. {
  232. if ($type == 'groupbuy') {
  233. $condition['is_virtual'] = 0;
  234. $condition['is_fcode'] = 0;
  235. $condition['is_presell'] = 0;
  236. $condition['goods_state'] = self::STATE1;
  237. $condition['goods_verify'] = self::VERIFY1;
  238. }
  239. return $this->getGoodsCommonList($condition, $field, $page);
  240. }
  241. /**
  242. * 出售中的未参加促销的虚拟商品列表
  243. */
  244. public function getGoodsCommonListForVrPromotion($condition, $field = '*', $page = 10)
  245. {
  246. $condition['is_virtual'] = 1;
  247. $condition['is_fcode'] = 0;
  248. $condition['is_presell'] = 0;
  249. $condition['goods_state'] = self::STATE1;
  250. $condition['goods_verify'] = self::VERIFY1;
  251. return $this->getGoodsCommonList($condition, $field, $page);
  252. }
  253. /**
  254. * 仓库中的商品列表 卖家中心使用
  255. *
  256. * @param array $condition 条件
  257. * @param array $field 字段
  258. * @param string $page 分页
  259. * @param string $order 排序
  260. * @return array
  261. */
  262. public function getGoodsCommonOfflineList($condition, $field = '*', $page = 10, $order = "goods_commonid desc")
  263. {
  264. $condition['goods_state'] = self::STATE0;
  265. $condition['goods_verify'] = self::VERIFY1;
  266. return $this->getGoodsCommonList($condition, $field, $page, $order);
  267. }
  268. /**
  269. * 违规的商品列表 卖家中心使用
  270. *
  271. * @param array $condition 条件
  272. * @param array $field 字段
  273. * @param string $page 分页
  274. * @param string $order 排序
  275. * @return array
  276. */
  277. public function getGoodsCommonLockUpList($condition, $field = '*', $page = 10, $order = "goods_commonid desc")
  278. {
  279. $condition['goods_state'] = self::STATE10;
  280. $condition['goods_verify'] = self::VERIFY1;
  281. return $this->getGoodsCommonList($condition, $field, $page, $order);
  282. }
  283. /**
  284. * 等待审核或审核失败的商品列表 卖家中心使用
  285. *
  286. * @param array $condition 条件
  287. * @param array $field 字段
  288. * @param string $page 分页
  289. * @param string $order 排序
  290. * @return array
  291. */
  292. public function getGoodsCommonWaitVerifyList($condition, $field = '*', $page = 10, $order = "goods_commonid desc")
  293. {
  294. if (!isset($condition['goods_verify'])) {
  295. $condition['goods_verify'] = array('neq', self::VERIFY1);
  296. }
  297. return $this->getGoodsCommonList($condition, $field, $page, $order);
  298. }
  299. /**
  300. * 查询商品SUK及其店铺信息
  301. *
  302. * @param array $condition
  303. * @param string $field
  304. * @return array
  305. */
  306. public function getGoodsStoreList($condition, $field = '*')
  307. {
  308. $condition = $this->_getRecursiveClass($condition);
  309. return $this->table('goods,store')->field($field)->join('inner')->on('goods.store_id = store.store_id')->where($condition)->select();
  310. }
  311. /**
  312. * 计算商品库存
  313. *
  314. * @param array $goods_list
  315. * @return array|boolean
  316. */
  317. public function calculateStorage($goods_list)
  318. {
  319. // 计算库存
  320. if (!empty($goods_list)) {
  321. $goodsid_array = array();
  322. foreach ($goods_list as $value) {
  323. $goodscommonid_array[] = $value['goods_commonid'];
  324. }
  325. $goods_storage = $this->getGoodsList(array('goods_commonid' => array('in', $goodscommonid_array)), 'goods_storage,goods_commonid,goods_id,goods_storage_alarm');
  326. $storage_array = array();
  327. foreach ($goods_storage as $val) {
  328. if ($val['goods_storage_alarm'] != 0 && $val['goods_storage'] <= $val['goods_storage_alarm']) {
  329. $storage_array[$val['goods_commonid']]['alarm'] = true;
  330. }
  331. $storage_array[$val['goods_commonid']]['sum'] += $val['goods_storage'];
  332. $storage_array[$val['goods_commonid']]['goods_id'] = $val['goods_id'];
  333. }
  334. return $storage_array;
  335. } else {
  336. return false;
  337. }
  338. }
  339. /**
  340. * 更新商品SUK数据
  341. *
  342. * @param array $update 更新数据
  343. * @param array $condition 条件
  344. * @return boolean
  345. */
  346. public function editGoods($update, $condition)
  347. {
  348. $goods_list = $this->getGoodsList($condition, 'goods_id');
  349. if (empty($goods_list)) {
  350. return true;
  351. }
  352. $goodsid_array = array();
  353. foreach ($goods_list as $value) {
  354. $goodsid_array[] = $value['goods_id'];
  355. }
  356. return $this->editGoodsById($update, $goodsid_array);
  357. }
  358. /**
  359. * 更新商品SUK数据
  360. * @param array $update
  361. * @param int|array $goodsid_array
  362. * @return boolean|unknown
  363. */
  364. public function editGoodsById($update, $goodsid_array)
  365. {
  366. if (empty($goodsid_array)) {
  367. return true;
  368. }
  369. $condition['goods_id'] = array('in', $goodsid_array);
  370. $update['goods_edittime'] = time();
  371. $result = $this->table('goods')->where($condition)->update($update);
  372. if ($result)
  373. {
  374. foreach ((array)$goodsid_array as $value) {
  375. $this->_dGoodsCache($value);
  376. }
  377. $items = $this->table('goods')->field('goods_commonid')->where($condition)->limit(false)->select();
  378. $commonids = array();
  379. foreach($items as $key => $commid) {
  380. array_push($commonids,intval($commid['goods_commonid']));
  381. }
  382. $commonids = array_unique($commonids);
  383. foreach($commonids as $commid) {
  384. $this->_dGoodsCommonCache($commid);
  385. $this->_dGoodsSpecCache($commid);
  386. }
  387. }
  388. return $result;
  389. }
  390. /**
  391. * 更新商品促销价 (需要验证抢购和限时折扣是否进行)
  392. *
  393. * @param array $update 更新数据
  394. * @param array $condition 条件
  395. * @return boolean
  396. */
  397. public function editGoodsPromotionPrice($condition)
  398. {
  399. $goods_list = $this->getGoodsList($condition, 'goods_id,goods_commonid');
  400. $goods_array = array();
  401. foreach ($goods_list as $val) {
  402. $goods_array[$val['goods_commonid']][$val['goods_id']] = $val;
  403. }
  404. $model_groupbuy = Model('groupbuy');
  405. $model_xianshigoods = Model('p_xianshi_goods');
  406. foreach ($goods_array as $key => $val) {
  407. // 查询抢购时候进行
  408. $groupbuy = $model_groupbuy->getGroupbuyOnlineInfo(array('goods_commonid' => $key));
  409. if (!empty($groupbuy)) {
  410. // 更新价格
  411. $this->editGoods(array('goods_promotion_price' => $groupbuy['groupbuy_price'], 'goods_promotion_type' => 1), array('goods_commonid' => $key));
  412. continue;
  413. }
  414. foreach ($val as $k => $v) {
  415. // 查询限时折扣时候进行
  416. $xianshigoods = $model_xianshigoods->getXianshiGoodsInfo(array('goods_id' => $k, 'start_time' => array('lt', time()), 'end_time' => array('gt', time())));
  417. if (!empty($xianshigoods)) {
  418. // 更新价格
  419. $this->editGoodsById(array('goods_promotion_price' => $xianshigoods['xianshi_price'], 'goods_promotion_type' => 2), $k);
  420. continue;
  421. }
  422. // 没有促销使用原价
  423. $this->editGoodsById(array('goods_promotion_price' => array('exp', 'goods_price'), 'goods_promotion_type' => 0), $k);
  424. }
  425. }
  426. return true;
  427. }
  428. /**
  429. * 更新商品数据
  430. * @param array $update 更新数据
  431. * @param array $condition 条件
  432. * @return boolean
  433. */
  434. public function editGoodsCommon($update, $condition)
  435. {
  436. $common_list = $this->getGoodsCommonList($condition, 'goods_commonid', 0);
  437. if (empty($common_list)) {
  438. return false;
  439. }
  440. $commonid_array = array();
  441. foreach ($common_list as $val) {
  442. $commonid_array[] = $val['goods_commonid'];
  443. }
  444. return $this->editGoodsCommonById($update, $commonid_array);
  445. }
  446. /**
  447. * 更新商品数据
  448. * @param array $update
  449. * @param int|array $commonid_array
  450. * @return boolean|unknown
  451. */
  452. public function editGoodsCommonById($update, $commonid_array)
  453. {
  454. if (empty($commonid_array)) {
  455. return true;
  456. }
  457. $condition['goods_commonid'] = array('in', $commonid_array);
  458. $result = $this->table('goods_common')->where($condition)->update($update);
  459. if ($result) {
  460. foreach ((array)$commonid_array as $val) {
  461. $this->_dGoodsCommonCache($val);
  462. }
  463. }
  464. return $result;
  465. }
  466. /**
  467. * 锁定商品
  468. * @param unknown $condition
  469. * @return boolean
  470. */
  471. public function editGoodsCommonLock($condition)
  472. {
  473. $update = array('goods_lock' => 1);
  474. return $this->editGoodsCommon($update, $condition);
  475. }
  476. /**
  477. * 解锁商品
  478. * @param unknown $condition
  479. * @return boolean
  480. */
  481. public function editGoodsCommonUnlock($condition)
  482. {
  483. $update = array('goods_lock' => 0);
  484. return $this->editGoodsCommon($update, $condition);
  485. }
  486. /**
  487. * 更新商品信息
  488. *
  489. * @param array $condition
  490. * @param array $update1
  491. * @param array $update2
  492. * @return boolean
  493. */
  494. public function editProduces($condition, $update1, $update2 = array())
  495. {
  496. $update2 = empty($update2) ? $update1 : $update2;
  497. $goods_array = $this->getGoodsCommonList($condition, 'goods_commonid', 0);
  498. if (empty($goods_array)) {
  499. return true;
  500. }
  501. $commonid_array = array();
  502. foreach ($goods_array as $val) {
  503. $commonid_array[] = $val['goods_commonid'];
  504. }
  505. $return1 = $this->editGoodsCommonById($update1, $commonid_array);
  506. $return2 = $this->editGoods($update2, array('goods_commonid' => array('in', $commonid_array)));
  507. if ($return1 && $return2) {
  508. return true;
  509. } else {
  510. return false;
  511. }
  512. }
  513. /**
  514. * 更新商品信息(审核失败)
  515. *
  516. * @param array $condition
  517. * @param array $update1
  518. * @param array $update2
  519. * @return boolean
  520. */
  521. public function editProducesVerifyFail($condition, $update1, $update2 = array())
  522. {
  523. $result = $this->editProduces($condition, $update1, $update2);
  524. if ($result) {
  525. $commonlist = $this->getGoodsCommonList($condition, 'goods_commonid,store_id,goods_verifyremark', 0);
  526. foreach ($commonlist as $val) {
  527. $param = array();
  528. $param['common_id'] = $val['goods_commonid'];
  529. $param['remark'] = $val['goods_verifyremark'];
  530. $this->_sendStoreMsg('goods_verify', $val['store_id'], $param);
  531. }
  532. }
  533. }
  534. /**
  535. * 更新未锁定商品信息
  536. *
  537. * @param array $condition
  538. * @param array $update1
  539. * @param array $update2
  540. * @return boolean
  541. */
  542. public function editProducesNoLock($condition, $update1, $update2 = array())
  543. {
  544. $condition['goods_lock'] = 0;
  545. return $this->editProduces($condition, $update1, $update2);
  546. }
  547. /**
  548. * 商品下架
  549. * @param array $condition 条件
  550. * @return boolean
  551. */
  552. public function editProducesOffline($condition)
  553. {
  554. $update = array('goods_state' => self::STATE0);
  555. return $this->editProducesNoLock($condition, $update);
  556. }
  557. /**
  558. * 商品上架
  559. * @param array $condition 条件
  560. * @return boolean
  561. */
  562. public function editProducesOnline($condition)
  563. {
  564. $update = array('goods_state' => self::STATE1);
  565. // 禁售商品、审核失败商品不能上架。
  566. $condition['goods_state'] = self::STATE0;
  567. $condition['goods_verify'] = array('neq', self::VERIFY0);
  568. // 修改预约商品状态
  569. $update['is_appoint'] = 0;
  570. return $this->editProduces($condition, $update);
  571. }
  572. /**
  573. * 违规下架
  574. *
  575. * @param array $update
  576. * @param array $condition
  577. * @return boolean
  578. */
  579. public function editProducesLockUp($update, $condition)
  580. {
  581. $update_param['goods_state'] = self::STATE10;
  582. $update = array_merge($update, $update_param);
  583. $return = $this->editProduces($condition, $update, $update_param);
  584. if ($return) {
  585. // 商品违规下架发送店铺消息
  586. $common_list = $this->getGoodsCommonList($condition, 'goods_commonid,store_id,goods_stateremark', 0);
  587. foreach ($common_list as $val) {
  588. $param = array();
  589. $param['remark'] = $val['goods_stateremark'];
  590. $param['common_id'] = $val['goods_commonid'];
  591. $this->_sendStoreMsg('goods_violation', $val['store_id'], $param);
  592. }
  593. return true;
  594. } else {
  595. return false;
  596. }
  597. }
  598. /**
  599. * 获取单条商品SKU信息
  600. *
  601. * @param array $condition
  602. * @param string $field
  603. * @return array
  604. */
  605. public function getGoodsInfo($condition, $field = '*')
  606. {
  607. return $this->table('goods')->field($field)->where($condition)->find();
  608. }
  609. /**
  610. * 获取单条商品SKU信息及其促销信息
  611. *
  612. * @param int $goods_id
  613. * @param string $field
  614. * @return array
  615. */
  616. public function getGoodsOnlineInfoForShare($goods_id)
  617. {
  618. $goods_info = $this->getGoodsOnlineInfoAndPromotionById($goods_id);
  619. if (empty($goods_info)) {
  620. return array();
  621. }
  622. //抢购
  623. if (isset($goods_info['groupbuy_info'])) {
  624. $goods_info['promotion_type'] = '抢购';
  625. $goods_info['promotion_price'] = $goods_info['groupbuy_info']['groupbuy_price'];
  626. }
  627. if (isset($goods_info['xianshi_info'])) {
  628. $goods_info['promotion_type'] = '限时折扣';
  629. $goods_info['promotion_price'] = $goods_info['xianshi_info']['xianshi_price'];
  630. }
  631. return $goods_info;
  632. }
  633. /**
  634. * 查询出售中的商品详细信息及其促销信息
  635. * @param int $goods_id
  636. * @return array
  637. */
  638. public function getGoodsOnlineInfoAndPromotionById($goods_id)
  639. {
  640. $goods_info = $this->getGoodsInfoAndPromotionById($goods_id);
  641. if (empty($goods_info) || $goods_info['goods_state'] != self::STATE1 || $goods_info['goods_verify'] != self::VERIFY1) {
  642. return array();
  643. }
  644. return $goods_info;
  645. }
  646. /**
  647. * 查询商品详细信息及其促销信息
  648. * @param int $goods_id
  649. * @return array
  650. */
  651. public function getGoodsInfoAndPromotionById($goods_id)
  652. {
  653. $goods_info = $this->getGoodsInfoByID($goods_id);
  654. if (empty($goods_info)) {
  655. return array();
  656. }
  657. //抢购
  658. if (C('groupbuy_allow'))
  659. {
  660. $goods_info['groupbuy_info'] = Model('groupbuy')->getGroupbuyInfoByGoodsCommonID($goods_info['goods_commonid']);
  661. }
  662. //限时折扣
  663. if (C('promotion_allow') && empty($goods_info['groupbuy_info']))
  664. {
  665. $goods_info['xianshi_info'] = Model('p_xianshi_goods')->getXianshiGoodsInfoByGoodsID($goods_info['goods_id']);
  666. }
  667. return $goods_info;
  668. }
  669. public function getGoodsStorageById($goods_id)
  670. {
  671. $goods_info = $this->getGoodsInfoByID($goods_id);
  672. if (empty($goods_info)) {
  673. return "0";
  674. }
  675. return $goods_info['goods_storage'];
  676. }
  677. /**
  678. * 查询出售中的商品列表及其促销信息
  679. * @param array $goodsid_array
  680. * @return array
  681. */
  682. public function getGoodsOnlineListAndPromotionByIdArray($goodsid_array)
  683. {
  684. if (empty($goodsid_array) || !is_array($goodsid_array)) return array();
  685. $goods_list = array();
  686. foreach ($goodsid_array as $goods_id) {
  687. $goods_info = $this->getGoodsOnlineInfoAndPromotionById($goods_id);
  688. if (!empty($goods_info)) $goods_list[] = $goods_info;
  689. }
  690. return $goods_list;
  691. }
  692. /**
  693. * 获取单条商品信息
  694. *
  695. * @param array $condition
  696. * @param string $field
  697. * @return array
  698. */
  699. public function getGoodeCommonInfo($condition, $field = '*')
  700. {
  701. return $this->table('goods_common')->field($field)->where($condition)->find();
  702. }
  703. /**
  704. * 取得商品详细信息(优先查询缓存)
  705. * 如果未找到,则缓存所有字段
  706. * @param int $goods_commonid
  707. * @param string $fields 需要取得的缓存键值, 例如:'*','goods_name,store_name'
  708. * @return array
  709. */
  710. public function getGoodeCommonInfoByID($goods_commonid, $fields = '*')
  711. {
  712. $common_info = $this->_rGoodsCommonCache($goods_commonid, $fields);
  713. if (empty($common_info)) {
  714. $common_info = $this->getGoodeCommonInfo(array('goods_commonid' => $goods_commonid));
  715. $this->_wGoodsCommonCache($goods_commonid, $common_info);
  716. }
  717. return $common_info;
  718. }
  719. /**
  720. * 获得商品SKU某字段的和
  721. *
  722. * @param array $condition
  723. * @param string $field
  724. * @return boolean
  725. */
  726. public function getGoodsSum($condition, $field)
  727. {
  728. return $this->table('goods')->where($condition)->sum($field);
  729. }
  730. /**
  731. * 获得商品SKU数量
  732. *
  733. * @param array $condition
  734. * @param string $field
  735. * @return int
  736. */
  737. public function getGoodsCount($condition)
  738. {
  739. return $this->table('goods')->where($condition)->count();
  740. }
  741. /**
  742. * 获得出售中商品SKU数量
  743. *
  744. * @param array $condition
  745. * @param string $field
  746. * @return int
  747. */
  748. public function getGoodsOnlineCount($condition, $field = '*')
  749. {
  750. $condition['goods_state'] = self::STATE1;
  751. $condition['goods_verify'] = self::VERIFY1;
  752. return $this->table('goods')->where($condition)->group('')->count1($field);
  753. }
  754. /**
  755. * 获得商品数量
  756. *
  757. * @param array $condition
  758. * @param string $field
  759. * @return int
  760. */
  761. public function getGoodsCommonCount($condition)
  762. {
  763. return $this->table('goods_common')->where($condition)->count();
  764. }
  765. /**
  766. * 出售中的商品数量
  767. *
  768. * @param array $condition
  769. * @return int
  770. */
  771. public function getGoodsCommonOnlineCount($condition)
  772. {
  773. $condition['goods_state'] = self::STATE1;
  774. $condition['goods_verify'] = self::VERIFY1;
  775. return $this->getGoodsCommonCount($condition);
  776. }
  777. /**
  778. * 仓库中的商品数量
  779. *
  780. * @param array $condition
  781. * @return int
  782. */
  783. public function getGoodsCommonOfflineCount($condition)
  784. {
  785. $condition['goods_state'] = self::STATE0;
  786. $condition['goods_verify'] = self::VERIFY1;
  787. return $this->getGoodsCommonCount($condition);
  788. }
  789. /**
  790. * 等待审核的商品数量
  791. *
  792. * @param array $condition
  793. * @return int
  794. */
  795. public function getGoodsCommonWaitVerifyCount($condition)
  796. {
  797. $condition['goods_verify'] = self::VERIFY10;
  798. return $this->getGoodsCommonCount($condition);
  799. }
  800. /**
  801. * 审核失败的商品数量
  802. *
  803. * @param array $condition
  804. * @return int
  805. */
  806. public function getGoodsCommonVerifyFailCount($condition)
  807. {
  808. $condition['goods_verify'] = self::VERIFY0;
  809. return $this->getGoodsCommonCount($condition);
  810. }
  811. /**
  812. * 违规下架的商品数量
  813. *
  814. * @param array $condition
  815. * @return int
  816. */
  817. public function getGoodsCommonLockUpCount($condition)
  818. {
  819. $condition['goods_state'] = self::STATE10;
  820. $condition['goods_verify'] = self::VERIFY1;
  821. return $this->getGoodsCommonCount($condition);
  822. }
  823. /**
  824. * 商品图片列表
  825. *
  826. * @param array $condition
  827. * @param array $order
  828. * @param string $field
  829. * @return array
  830. */
  831. public function getGoodsImageList($condition, $field = '*', $order = 'is_default desc,goods_image_sort asc')
  832. {
  833. return $this->table('goods_images')->field($field)->where($condition)->order($order)->select();
  834. }
  835. public function getGoodsImageListEx($condition, $field = '*', $order = 'is_default desc,goods_image_sort asc')
  836. {
  837. return $this->table('goods_images')->field($field)->where($condition)->group('filesig')->order($order)->select();
  838. }
  839. /**
  840. * 删除商品SKU信息
  841. *
  842. * @param array $condition
  843. * @return boolean
  844. */
  845. public function delGoods($condition)
  846. {
  847. $goods_list = $this->getGoodsList($condition, 'goods_id,goods_commonid,store_id');
  848. if (!empty($goods_list)) {
  849. $goodsid_array = array();
  850. // 删除商品二维码
  851. foreach ($goods_list as $val) {
  852. $goodsid_array[] = $val['goods_id'];
  853. @unlink(BASE_UPLOAD_PATH . DS . ATTACH_STORE . DS . $val['store_id'] . DS . $val['goods_id'] . '.png');
  854. // 删除商品缓存
  855. $this->_dGoodsCache($val['goods_id']);
  856. // 删除商品规格缓存
  857. $this->_dGoodsSpecCache($val['goods_commonid']);
  858. }
  859. // 删除属性关联表数据
  860. $this->table('goods_attr_index')->where(array('goods_id' => array('in', $goodsid_array)))->delete();
  861. // 删除优惠套装商品
  862. Model('p_bundling')->delBundlingGoods(array('goods_id' => array('in', $goodsid_array)));
  863. // 优惠套餐活动下架
  864. Model('p_bundling')->editBundlingCloseByGoodsIds(array('goods_id' => array('in', $goodsid_array)));
  865. // 推荐展位商品
  866. Model('p_booth')->delBoothGoods(array('goods_id' => array('in', $goodsid_array)));
  867. // 限时折扣
  868. Model('p_xianshi_goods')->delXianshiGoods(array('goods_id' => array('in', $goodsid_array)));
  869. //删除商品浏览记录
  870. Model('goods_browse')->delGoodsbrowse(array('goods_id' => array('in', $goodsid_array)));
  871. // 删除买家收藏表数据
  872. $this->table('favorites')->where(array('fav_id' => array('in', $goodsid_array), 'fav_type' => 'goods'))->delete();
  873. // 删除商品赠品
  874. Model('goods_gift')->delGoodsGift(array('goods_id' => array('in', $goodsid_array), 'gift_goodsid' => array('in', $goodsid_array), '_op' => 'or'));
  875. // 删除推荐组合
  876. Model('goods_combo')->delGoodsCombo(array('goods_id' => array('in', $goodsid_array), 'combo_goodsid' => array('in', $goodsid_array), '_op' => 'or'));
  877. }
  878. return $this->table('goods')->where($condition)->delete();
  879. }
  880. /**
  881. * 删除商品图片表信息
  882. *
  883. * @param array $condition
  884. * @return boolean
  885. */
  886. public function delGoodsImages($condition)
  887. {
  888. $image_list = $this->getGoodsImageList($condition, 'goods_commonid,color_id');
  889. if (empty($image_list)) {
  890. return true;
  891. }
  892. $result = $this->table('goods_images')->where($condition)->delete();
  893. if ($result) {
  894. foreach ($image_list as $val) {
  895. $this->_dGoodsImageCache($val['goods_commonid'] . '|' . $val['color_id']);
  896. }
  897. }
  898. return $result;
  899. }
  900. /**
  901. * 商品删除及相关信息
  902. *
  903. * @param array $condition 列表条件
  904. * @return boolean
  905. */
  906. public function delGoodsAll($condition)
  907. {
  908. $goods_list = $this->getGoodsList($condition, 'goods_id,goods_commonid,store_id');
  909. if (empty($goods_list)) {
  910. return false;
  911. }
  912. $goodsid_array = array();
  913. $commonid_array = array();
  914. foreach ($goods_list as $val) {
  915. $goodsid_array[] = $val['goods_id'];
  916. $commonid_array[] = $val['goods_commonid'];
  917. // 商品公共缓存
  918. $this->_dGoodsCommonCache($val['goods_commonid']);
  919. // 商品规格缓存
  920. $this->_dGoodsSpecCache($val['goods_commonid']);
  921. }
  922. $commonid_array = array_unique($commonid_array);
  923. // 删除商品表数据
  924. $this->delGoods(array('goods_id' => array('in', $goodsid_array)));
  925. // 删除商品公共表数据
  926. $this->table('goods_common')->where(array('goods_commonid' => array('in', $commonid_array)))->delete();
  927. // 删除商品图片表数据
  928. $this->delGoodsImages(array('goods_commonid' => array('in', $commonid_array)));
  929. // 删除商品F码
  930. Model('goods_fcode')->delGoodsFCode(array('goods_commonid' => array('in', $commonid_array)));
  931. return true;
  932. }
  933. /**
  934. * 删除未锁定商品
  935. * @param unknown $condition
  936. */
  937. public function delGoodsNoLock($condition)
  938. {
  939. $condition['goods_lock'] = 0;
  940. $common_array = $this->getGoodsCommonList($condition, 'goods_commonid', 0);
  941. $common_array = array_under_reset($common_array, 'goods_commonid');
  942. $commonid_array = array_keys($common_array);
  943. return $this->delGoodsAll(array('goods_commonid' => array('in', $commonid_array)));
  944. }
  945. /**
  946. * 发送店铺消息
  947. * @param string $code
  948. * @param int $store_id
  949. * @param array $param
  950. */
  951. private function _sendStoreMsg($code, $store_id, $param)
  952. {
  953. QueueClient::push('sendStoreMsg', array('code' => $code, 'store_id' => $store_id, 'param' => $param));
  954. }
  955. /**
  956. * 获得商品子分类的ID
  957. * @param array $condition
  958. * @return array
  959. */
  960. private function _getRecursiveClass($condition)
  961. {
  962. if (isset($condition['gc_id']) && !is_array($condition['gc_id'])) {
  963. $gc_list = Model('goods_class')->getGoodsClassForCacheModel();
  964. if (!empty($gc_list[$condition['gc_id']])) {
  965. $gc_id[] = $condition['gc_id'];
  966. $gcchild_id = empty($gc_list[$condition['gc_id']]['child']) ? array() : explode(',', $gc_list[$condition['gc_id']]['child']);
  967. $gcchildchild_id = empty($gc_list[$condition['gc_id']]['childchild']) ? array() : explode(',', $gc_list[$condition['gc_id']]['childchild']);
  968. $gc_id = array_merge($gc_id, $gcchild_id, $gcchildchild_id);
  969. $condition['gc_id'] = array('in', $gc_id);
  970. }
  971. }
  972. return $condition;
  973. }
  974. /**
  975. * 由ID取得在售单个虚拟商品信息
  976. * @param unknown $goods_id
  977. * @param string $field 需要取得的缓存键值, 例如:'*','goods_name,store_name'
  978. * @return array
  979. */
  980. public function getVirtualGoodsOnlineInfoByID($goods_id)
  981. {
  982. $goods_info = $this->getGoodsInfoByID($goods_id, '*');
  983. return $goods_info['is_virtual'] == 1 && $goods_info['virtual_indate'] >= time() ? $goods_info : array();
  984. }
  985. /**
  986. * 取得商品详细信息(优先查询缓存)(在售)
  987. * 如果未找到,则缓存所有字段
  988. * @param int $goods_id
  989. * @param string $field 需要取得的缓存键值, 例如:'*','goods_name,store_name'
  990. * @return array
  991. */
  992. public function getGoodsOnlineInfoByID($goods_id, $field = '*')
  993. {
  994. if ($field != '*') {
  995. $field .= ',goods_state,goods_verify';
  996. }
  997. $goods_info = $this->getGoodsInfoByID($goods_id, trim($field, ','));
  998. if ($goods_info['goods_state'] != self::STATE1 || $goods_info['goods_verify'] != self::VERIFY1) {
  999. $goods_info = array();
  1000. }
  1001. return $goods_info;
  1002. }
  1003. /**
  1004. * 取得商品详细信息(优先查询缓存)
  1005. * 如果未找到,则缓存所有字段
  1006. * @param int $goods_id
  1007. * @param string $fields 需要取得的缓存键值, 例如:'*','goods_name,store_name'
  1008. * @return array
  1009. */
  1010. public function getGoodsInfoByID($goods_id, $fields = '*')
  1011. {
  1012. $goods_info = $this->_rGoodsCache($goods_id, $fields);
  1013. if (empty($goods_info)) {
  1014. $goods_info = $this->getGoodsInfo(array('goods_id' => $goods_id));
  1015. $this->_wGoodsCache($goods_id, $goods_info);
  1016. }
  1017. return $goods_info;
  1018. }
  1019. /**
  1020. * 验证是否为普通商品
  1021. * @param array $goods 商品数组
  1022. * @return boolean
  1023. */
  1024. public function checkIsGeneral($goods)
  1025. {
  1026. if ($goods['is_virtual'] == 1 || $goods['is_fcode'] == 1 || $goods['is_presell'] == 1) {
  1027. return false;
  1028. }
  1029. return true;
  1030. }
  1031. /**
  1032. * 验证是否允许送赠品
  1033. * @param unknown $goods
  1034. * @return boolean
  1035. */
  1036. public function checkGoodsIfAllowGift($goods)
  1037. {
  1038. if ($goods['is_virtual'] == 1) {
  1039. return false;
  1040. }
  1041. return true;
  1042. }
  1043. public function checkGoodsIfAllowCombo($goods)
  1044. {
  1045. if ($goods['is_virtual'] == 1 || $goods['is_fcode'] == 1 || $goods['is_presell'] == 1 || $goods['is_appoint'] == 1) {
  1046. return false;
  1047. }
  1048. return true;
  1049. }
  1050. /**
  1051. * 获得商品规格数组
  1052. * @param unknown $common_id
  1053. */
  1054. public function getGoodsSpecListByCommonId($common_id)
  1055. {
  1056. $spec_list = $this->_rGoodsSpecCache($common_id);
  1057. if (empty($spec_list)) {
  1058. $spec_array = $this->getGoodsList(array('goods_commonid' => $common_id), 'goods_spec,goods_id,store_id,goods_image,color_id,goods_price,goods_promotion_price,goods_serial,goods_storage,goods_storage_alarm');
  1059. $spec_list['spec'] = serialize($spec_array);
  1060. $this->_wGoodsSpecCache($common_id, $spec_list);
  1061. }
  1062. $spec_array = unserialize($spec_list['spec']);
  1063. return $spec_array;
  1064. }
  1065. /**
  1066. * 获得商品图片数组
  1067. * @param int $goods_id
  1068. * @param array $condition
  1069. */
  1070. public function getGoodsImageByKey($key)
  1071. {
  1072. $image_list = $this->_rGoodsImageCache($key);
  1073. if (empty($image_list)) {
  1074. $array = explode('|', $key);
  1075. list($common_id, $color_id) = $array;
  1076. $image_more = $this->getGoodsImageList(array('goods_commonid' => $common_id, 'color_id' => $color_id), 'goods_image');
  1077. $image_list['image'] = serialize($image_more);
  1078. $this->_wGoodsImageCache($key, $image_list);
  1079. }
  1080. $image_more = unserialize($image_list['image']);
  1081. return $image_more;
  1082. }
  1083. /**
  1084. * 读取商品缓存
  1085. * @param int $goods_id
  1086. * @param string $fields
  1087. * @return array
  1088. */
  1089. private function _rGoodsCache($goods_id, $fields)
  1090. {
  1091. return rcache($goods_id, 'goods', $fields);
  1092. }
  1093. /**
  1094. * 写入商品缓存
  1095. * @param int $goods_id
  1096. * @param array $goods_info
  1097. * @return boolean
  1098. */
  1099. private function _wGoodsCache($goods_id, $goods_info)
  1100. {
  1101. return wcache($goods_id, $goods_info, 'goods');
  1102. }
  1103. /**
  1104. * 删除商品缓存
  1105. * @param int $goods_id
  1106. * @return boolean
  1107. */
  1108. private function _dGoodsCache($goods_id)
  1109. {
  1110. return dcache($goods_id, 'goods');
  1111. }
  1112. /**
  1113. * 读取商品公共缓存
  1114. * @param int $goods_commonid
  1115. * @param string $fields
  1116. * @return array
  1117. */
  1118. private function _rGoodsCommonCache($goods_commonid, $fields)
  1119. {
  1120. return rcache($goods_commonid, 'goods_common', $fields);
  1121. }
  1122. /**
  1123. * 写入商品公共缓存
  1124. * @param int $goods_commonid
  1125. * @param array $common_info
  1126. * @return boolean
  1127. */
  1128. private function _wGoodsCommonCache($goods_commonid, $common_info)
  1129. {
  1130. return wcache($goods_commonid, $common_info, 'goods_common');
  1131. }
  1132. /**
  1133. * 删除商品公共缓存
  1134. * @param int $goods_commonid
  1135. * @return boolean
  1136. */
  1137. private function _dGoodsCommonCache($goods_commonid)
  1138. {
  1139. return dcache($goods_commonid, 'goods_common');
  1140. }
  1141. /**
  1142. * 读取商品规格缓存
  1143. * @param int $goods_commonid
  1144. * @param string $fields
  1145. * @return array
  1146. */
  1147. private function _rGoodsSpecCache($goods_commonid)
  1148. {
  1149. return rcache($goods_commonid, 'goods_spec');
  1150. }
  1151. /**
  1152. * 写入商品规格缓存
  1153. * @param int $goods_commonid
  1154. * @param array $spec_list
  1155. * @return boolean
  1156. */
  1157. private function _wGoodsSpecCache($goods_commonid, $spec_list)
  1158. {
  1159. return wcache($goods_commonid, $spec_list, 'goods_spec');
  1160. }
  1161. /**
  1162. * 删除商品规格缓存
  1163. * @param int $goods_commonid
  1164. * @return boolean
  1165. */
  1166. private function _dGoodsSpecCache($goods_commonid)
  1167. {
  1168. return dcache($goods_commonid, 'goods_spec');
  1169. }
  1170. /**
  1171. * 读取商品图片缓存
  1172. * @param int $key ($goods_commonid .'|'. $color_id)
  1173. * @param string $fields
  1174. * @return array
  1175. */
  1176. private function _rGoodsImageCache($key)
  1177. {
  1178. return rcache($key, 'goods_image');
  1179. }
  1180. /**
  1181. * 写入商品图片缓存
  1182. * @param int $key ($goods_commonid .'|'. $color_id)
  1183. * @param array $image_list
  1184. * @return boolean
  1185. */
  1186. private function _wGoodsImageCache($key, $image_list)
  1187. {
  1188. return wcache($key, $image_list, 'goods_image');
  1189. }
  1190. /**
  1191. * 删除商品图片缓存
  1192. * @param int $key ($goods_commonid .'|'. $color_id)
  1193. * @return boolean
  1194. */
  1195. private function _dGoodsImageCache($key)
  1196. {
  1197. return dcache($key, 'goods_image');
  1198. }
  1199. private function _formatGoodsAttr($goods_attr)
  1200. {
  1201. $result_attr = array();
  1202. foreach ($goods_attr as $key => $val) {
  1203. $attr_item['goods_attr_id'] = intval($key);
  1204. $attr_item['goods_attr_name'] = $val['name'];
  1205. if ($attr_item['goods_attr_name'] === '功效' ||
  1206. $attr_item['goods_attr_name'] === '品牌' ||
  1207. $attr_item['goods_attr_name'] === '保质期'
  1208. ) {
  1209. $goods_attr_value = array();
  1210. foreach ($val as $id => $name) {
  1211. if ($id !== 'name') {
  1212. $val_item['id'] = intval($id);
  1213. $val_item['name'] = $name;
  1214. array_push($goods_attr_value, $val_item);
  1215. }
  1216. }
  1217. $attr_item['goods_attr_value'] = $goods_attr_value;
  1218. array_push($result_attr, $attr_item);
  1219. }
  1220. }
  1221. return $result_attr;
  1222. }
  1223. public function getGoodsSku($goods_id)
  1224. {
  1225. Log::record(__FUNCTION__ . " goods_id={$goods_id}.", Log::DEBUG);
  1226. if (empty($goods_id) || $goods_id <= 0) {
  1227. Log::record(" return null 1.", Log::DEBUG);
  1228. return null;
  1229. }
  1230. // get goods_commonid
  1231. $goods_info = $this->getGoodsInfoAndPromotionById($goods_id);
  1232. if (empty($goods_info)) {
  1233. Log::record(" return null 2.", Log::DEBUG);
  1234. return null;
  1235. }
  1236. $goods_common_info = $this->getGoodeCommonInfoByID($goods_info['goods_commonid']);
  1237. if (empty($goods_common_info)) {
  1238. Log::record(" return null 3.", Log::DEBUG);
  1239. return null;
  1240. }
  1241. Log::record("goods_common_info = " . (string)$goods_common_info, Log::DEBUG);
  1242. $goods_common_info['goods_body'] = '';//base64_encode($goods_common_info['goods_body']);
  1243. $goods_common_info['goods_attr'] = $this->_formatGoodsAttr(unserialize($goods_common_info['goods_attr']));
  1244. // sku
  1245. $goods_spec_list = $this->getGoodsSpecListByCommonId($goods_info['goods_commonid']);
  1246. $spec = Model('spec');
  1247. foreach ($goods_spec_list as $key => &$value) {
  1248. $value['goods_image'] = cthumb($value['goods_image'], 1280);
  1249. $goods_spec = unserialize($value['goods_spec']);
  1250. $value['goods_spec'] = array();
  1251. if (!empty($goods_spec)) {
  1252. foreach ($goods_spec as $key => $val) {
  1253. $spec_id = $spec->getSpecValueList(array('sp_value_id' => $key), 'sp_id');
  1254. $spec_name = $spec->specList(array('sp_id' => $spec_id[0]['sp_id']), '', 'sp_name');
  1255. $spec_value = array('key' => $key, 'name' => $spec_name[0]['sp_name'], 'value' => $goods_spec[$key]);
  1256. array_push($value['goods_spec'], $spec_value);
  1257. }
  1258. }
  1259. // xianshi
  1260. $xianshi_item = Model('p_xianshi_goods')->getXianshiGoodsInfoByGoodsID($value['goods_id']);
  1261. if (!empty($xianshi_item)) {
  1262. $value['promotion_type'] = 'xianshi';
  1263. $value['title'] = $xianshi_item['xianshi_title'];
  1264. $value['remark'] = $xianshi_item['xianshi_title'];
  1265. $value['promotion_price'] = $xianshi_item['xianshi_price'];
  1266. $value['down_price'] = ncPriceFormat($value['goods_price'] - $xianshi_item['xianshi_price']);
  1267. $value['lower_limit'] = $xianshi_item['lower_limit'];
  1268. $value['explain'] = $xianshi_item['xianshi_explain'];
  1269. }
  1270. }
  1271. // images
  1272. $goods_images = $this->getGoodsImageListEx(array('goods_commonid' => $goods_info['goods_commonid']));
  1273. foreach ($goods_images as $key => &$value) {
  1274. $value['goods_image'] = cthumb($value['goods_image'], 1280);
  1275. }
  1276. $goods_common_info['skus'] = $goods_spec_list;
  1277. $goods_common_info['images'] = $goods_images;
  1278. $result = array();
  1279. $result['request_goods_id'] = $goods_id;
  1280. $result['goods_info'] = $goods_common_info;
  1281. //$result['goods_info']['goods_id'] = $goods_id;
  1282. Log::record(" return 4 result = {$result}.", Log::DEBUG);
  1283. return $result;
  1284. }
  1285. /**
  1286. * 获取单条商品信息
  1287. *
  1288. * @param int $goods_id
  1289. * @return array
  1290. */
  1291. public function getGoodsDetail($goods_id)
  1292. {
  1293. if ($goods_id <= 0) {
  1294. return null;
  1295. }
  1296. $result1 = $this->getGoodsInfoAndPromotionById($goods_id);
  1297. if (empty($result1)) {
  1298. return null;
  1299. }
  1300. $result2 = $this->getGoodeCommonInfoByID($result1['goods_commonid']);
  1301. $goods_info = array_merge($result2, $result1);
  1302. $goods_info['spec_value'] = unserialize($goods_info['spec_value']);
  1303. $goods_info['spec_name'] = unserialize($goods_info['spec_name']);
  1304. $goods_info['goods_spec'] = unserialize($goods_info['goods_spec']);
  1305. $goods_info['goods_attr'] = unserialize($goods_info['goods_attr']);
  1306. // 手机商品描述
  1307. if ($goods_info['mobile_body'] != '') {
  1308. $mobile_body_array = unserialize($goods_info['mobile_body']);
  1309. if (is_array($mobile_body_array)) {
  1310. $mobile_body = '';
  1311. foreach ($mobile_body_array as $val) {
  1312. switch ($val['type']) {
  1313. case 'text':
  1314. $mobile_body .= '<div>' . $val['value'] . '</div>';
  1315. break;
  1316. case 'image':
  1317. $mobile_body .= '<img src="' . $val['value'] . '">';
  1318. break;
  1319. }
  1320. }
  1321. $goods_info['mobile_body'] = $mobile_body;
  1322. }
  1323. }
  1324. // 查询所有规格商品
  1325. $spec_array = $this->getGoodsSpecListByCommonId($goods_info['goods_commonid']);
  1326. $spec_list = array(); // 各规格商品地址,js使用
  1327. $spec_list_mobile = array(); // 各规格商品地址,js使用
  1328. $spec_image = array(); // 各规格商品主图,规格颜色图片使用
  1329. foreach ($spec_array as $key => $value) {
  1330. $s_array = unserialize($value['goods_spec']);
  1331. $tmp_array = array();
  1332. if (!empty($s_array) && is_array($s_array)) {
  1333. foreach ($s_array as $k => $v) {
  1334. $tmp_array[] = $k;
  1335. }
  1336. }
  1337. sort($tmp_array);
  1338. $spec_sign = implode('|', $tmp_array);
  1339. $tpl_spec = array();
  1340. $tpl_spec['sign'] = $spec_sign;
  1341. $tpl_spec['url'] = urlShop('goods', 'index', array('goods_id' => $value['goods_id']));
  1342. $spec_list[] = $tpl_spec;
  1343. //$spec_list_mobile[$spec_sign] = $value['goods_id'];
  1344. $spec_list_mobile[$spec_sign] = $value;
  1345. $spec_image[$value['color_id']] = thumb($value, 60);
  1346. }
  1347. $spec_list = json_encode($spec_list);
  1348. // 商品多图
  1349. $image_more = $this->getGoodsImageByKey($goods_info['goods_commonid'] . '|' . $goods_info['color_id']);
  1350. $goods_image = array();
  1351. $goods_image_mobile = array();
  1352. if (!empty($image_more)) {
  1353. foreach ($image_more as $val) {
  1354. $goods_image[] = "{ title : '', levelA : '" . cthumb($val['goods_image'], 60, $goods_info['store_id']) . "', levelB : '" . cthumb($val['goods_image'], 360, $goods_info['store_id']) . "', levelC : '" . cthumb($val['goods_image'], 360, $goods_info['store_id']) . "', levelD : '" . cthumb($val['goods_image'], 1280, $goods_info['store_id']) . "'}";
  1355. $goods_image_mobile[] = cthumb($val['goods_image'], 360, $goods_info['store_id']);
  1356. }
  1357. } else {
  1358. $goods_image[] = "{ title : '', levelA : '" . thumb($goods_info, 60) . "', levelB : '" . thumb($goods_info, 360) . "', levelC : '" . thumb($goods_info, 360) . "', levelD : '" . thumb($goods_info, 1280) . "'}";
  1359. $goods_image_mobile[] = thumb($goods_info, 360);
  1360. }
  1361. //抢购
  1362. if (!empty($goods_info['groupbuy_info'])) {
  1363. $goods_info['promotion_type'] = 'groupbuy';
  1364. $goods_info['title'] = '抢购';
  1365. $goods_info['remark'] = $goods_info['groupbuy_info']['remark'];
  1366. $goods_info['promotion_price'] = $goods_info['groupbuy_info']['groupbuy_price'];
  1367. $goods_info['down_price'] = ncPriceFormat($goods_info['goods_price'] - $goods_info['groupbuy_info']['groupbuy_price']);
  1368. $goods_info['upper_limit'] = $goods_info['groupbuy_info']['upper_limit'];
  1369. unset($goods_info['groupbuy_info']);
  1370. }
  1371. //限时折扣
  1372. if (!empty($goods_info['xianshi_info'])) {
  1373. $goods_info['promotion_type'] = 'xianshi';
  1374. $goods_info['title'] = $goods_info['xianshi_info']['xianshi_title'];
  1375. $goods_info['remark'] = $goods_info['xianshi_info']['xianshi_title'];
  1376. $goods_info['promotion_price'] = $goods_info['xianshi_info']['xianshi_price'];
  1377. $goods_info['down_price'] = ncPriceFormat($goods_info['goods_price'] - $goods_info['xianshi_info']['xianshi_price']);
  1378. $goods_info['lower_limit'] = $goods_info['xianshi_info']['lower_limit'];
  1379. $goods_info['explain'] = $goods_info['xianshi_info']['xianshi_explain'];
  1380. unset($goods_info['xianshi_info']);
  1381. }
  1382. // 验证是否允许送赠品
  1383. if ($this->checkGoodsIfAllowGift($goods_info)) {
  1384. $gift_array = Model('goods_gift')->getGoodsGiftListByGoodsId($goods_id);
  1385. if (!empty($gift_array)) {
  1386. $goods_info['have_gift'] = 'gift';
  1387. }
  1388. }
  1389. // 加入购物车按钮
  1390. $goods_info['cart'] = true;
  1391. //虚拟、F码、预售不显示加入购物车
  1392. if ($goods_info['is_virtual'] == 1 || $goods_info['is_fcode'] == 1 || $goods_info['is_presell'] == 1) {
  1393. $goods_info['cart'] = false;
  1394. }
  1395. // 立即购买文字显示
  1396. $goods_info['buynow_text'] = '立即购买';
  1397. if ($goods_info['is_presell'] == 1) {
  1398. $goods_info['buynow_text'] = '预售购买';
  1399. } elseif ($goods_info['is_fcode'] == 1) {
  1400. $goods_info['buynow_text'] = 'F码购买';
  1401. }
  1402. //满即送
  1403. $mansong_info = ($goods_info['is_virtual'] == 1) ? array() : Model('p_mansong')->getMansongInfoByStoreID($goods_info['store_id']);
  1404. // 商品受关注次数加1
  1405. $goods_info['goods_click'] = intval($goods_info['goods_click']) + 1;
  1406. if (C('cache_open')) {
  1407. $this->_wGoodsCache($goods_id, array('goods_click' => $goods_info['goods_click']));
  1408. wcache('updateRedisDate', array($goods_id => $goods_info['goods_click']), 'goodsClick');
  1409. } else {
  1410. $this->editGoodsById(array('goods_click' => array('exp', 'goods_click + 1')), $goods_id);
  1411. }
  1412. $result = array();
  1413. $result['goods_info'] = $goods_info;
  1414. $result['spec_list'] = $spec_list;
  1415. $result['spec_list_mobile'] = $spec_list_mobile;
  1416. $result['spec_image'] = $spec_image;
  1417. $result['goods_image'] = $goods_image;
  1418. $result['goods_image_mobile'] = $goods_image_mobile;
  1419. $result['mansong_info'] = $mansong_info;
  1420. $result['gift_array'] = $gift_array;
  1421. return $result;
  1422. }
  1423. public function getMobileBodyByCommonID($goods_commonid, $fields = 'mobile_body')
  1424. {
  1425. $common_info = $this->_rGoodsCommonCache($goods_commonid, $fields);
  1426. if (empty($common_info)) {
  1427. $common_info = $this->getGoodeCommonInfo(array('goods_commonid' => $goods_commonid));
  1428. $this->_wGoodsCommonCache($goods_commonid, $common_info);
  1429. }
  1430. // 手机商品描述
  1431. if ($common_info['mobile_body'] != '') {
  1432. $mobile_body_array = unserialize($common_info['mobile_body']);
  1433. if (is_array($mobile_body_array)) {
  1434. $mobile_body = '';
  1435. foreach ($mobile_body_array as $val) {
  1436. switch ($val['type']) {
  1437. case 'text':
  1438. $mobile_body .= '<div>' . $val['value'] . '</div>';
  1439. break;
  1440. case 'image':
  1441. $mobile_body .= '<img src="' . $val['value'] . '">';
  1442. break;
  1443. }
  1444. }
  1445. $common_info['mobile_body'] = $mobile_body;
  1446. }
  1447. }
  1448. return $common_info;
  1449. }
  1450. public function click_goods($goods_id)
  1451. {
  1452. QueueClient::push('click_goods',['goods_id' => $goods_id]);
  1453. }
  1454. }