GoodsService.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. <?php
  2. namespace addons\Happiness\common\service;
  3. use addons\Happiness\common\enums\HappinessEnum;
  4. use addons\Happiness\common\enums\HappinessVoucherEnum;
  5. use addons\Happiness\common\logic\voucher\WalletLogic;
  6. use addons\Happiness\common\models\HappinessGoods;
  7. use addons\Happiness\common\models\HappinessStoreGoods;
  8. use addons\Happiness\common\models\HappinessStoreGoodsAttr;
  9. use addons\Happiness\common\models\HappinessVoucherWallet;
  10. use common\models\common\CapitalLog;
  11. use common\models\user\User;
  12. use common\models\goods\Goods;
  13. use common\models\goods\GoodsAttr;
  14. use common\enums\StatusEnum;
  15. use common\helpers\ApiCode;
  16. use common\enums\AddonsEnum;
  17. use common\enums\WhetherEnum;
  18. use common\models\cart\Cart;
  19. use common\models\goods\GoodsCate;
  20. use common\models\goods\GoodsCateRelate;
  21. use yii\helpers\Json;
  22. use Exception;
  23. use Money\Exchange;
  24. use Swoole\Http\Status;
  25. use Yii;
  26. class GoodsService extends BaseService
  27. {
  28. public function getGoodsLabelName($params)
  29. {
  30. $label_list = \Yii::$app->services->setting->addons->get($params['mall_id'], HappinessEnum::ADDONS_NAME, 'basic.goods_label');
  31. if (!empty($label_list)) {
  32. $label_list = Json::decode($label_list, true);
  33. if (!empty($label_list)) {
  34. foreach ($label_list as $key => $value) {
  35. if ($value['level'] == $params['label_id']) {
  36. return $value['name'];
  37. }
  38. }
  39. }
  40. }
  41. return '';
  42. }
  43. //获取商品列表
  44. public function getGoodsList($params)
  45. {
  46. try {
  47. $where = [
  48. ['bg.mall_id' => $this->mall_id],
  49. ['bg.status' => StatusEnum::ENABLED],
  50. ];
  51. if (!empty($params['store_id'])) $where[] = ['bg.store_id' => $params['store_id']];
  52. if (!empty($params['goods_name'])) {
  53. $where[] = ['like' ,'g.goods_name', $params['goods_name']];
  54. }
  55. isset($params['goods_id']) && $where[] = ['=', 'bg.goods_id', $params['goods_id']];
  56. if (!empty($params['goods_type'])) $where[] = ['g.goods_type' => $params['goods_type']];
  57. isset($params['start']) && $where[] = ['>=', 'g.created_at', strtotime($params['start'])];
  58. isset($params['end']) && $where[] = ['<=', 'g.created_at', strtotime($params['end'])];
  59. isset($params['goods_name']) && $where[] = ['like', 'g.goods_name', trim($params['goods_name']) . '%', false];
  60. if (isset($params['type'])) {
  61. switch ($params['type']) {
  62. case 'onsale':
  63. $where[] = ['=', 'bg.is_on_sale', WhetherEnum::ENABLED];
  64. $where[] = ['>', 'bg.stock', 0];
  65. break;
  66. case 'offline':
  67. $where[] = ['=', 'bg.is_on_sale', WhetherEnum::DISABLED];
  68. break;
  69. case 'sell-out':
  70. $where[] = ['=', 'bg.stock', 0];
  71. break;
  72. }
  73. }
  74. $goods_params = [
  75. 'alias' => 'bg',
  76. 'where' => $where,
  77. 'order' => 'id desc',
  78. 'join' => [['LEFT JOIN', Goods::tableName(). ' as g', "g.id = bg.goods_id"]],
  79. 'select' => 'bg.id,bg.goods_id, bg.sales_num, g.goods_name, g.freight_type, g.price, g.is_on_sale as goods_sale, g.cost_price, g.unit, g.cover_pic, bg.is_on_sale, g.is_attr, g.attr_groups, g.attr_groups, bg.status, bg.stock, g.virtual_sales, g.goods_type, g.services, g.goods_source, g.check_status, g.check_remark, g.sort, g.created_at, g.stock_warning',
  80. 'with' => ['cats', 'goods', 'attr','yunAttr', 'yunGoods'],
  81. ];
  82. $list = HappinessStoreGoods::listPage($goods_params);
  83. if (!empty($list['list'])) {
  84. foreach ($list['list'] as &$item) {
  85. $item['freight_type'] = explode(',', $item['freight_type']);
  86. $item['can_change_status'] = false;
  87. if (in_array(2, $item['freight_type']) || in_array(3, $item['freight_type'])) {
  88. $item['can_change_status'] = true;
  89. }
  90. if ($item['goods_sale'] == 0) $item['can_change_status'] = false;
  91. if ($item['yunGoods']['is_on_sale'] == 0) $item['can_change_status'] = false;
  92. // foreach ($item['yunAttr'] as $key => $value) {
  93. // $item['yunAttr'][$key]['name'] = $item['attr'][$value['attr_id']]['name'];
  94. // $item['yunAttr'][$key]['price'] = $item['attr'][$value['attr_id']]['price'];
  95. // }
  96. foreach ($item['attr'] as $key => $value) {
  97. if (empty($item['yunAttr'][$value['id']])) {
  98. $item['yunAttr'][$value['id']] = [
  99. 'attr_id' => $value['id'],
  100. 'goods_id' => $value['goods_id'],
  101. 'name' => $value['name'],
  102. 'price' => $value['price'],
  103. ];
  104. } else {
  105. $item['yunAttr'][$value['id']]['name'] = $value['name'];
  106. $item['yunAttr'][$value['id']]['price'] = $value['price'];
  107. }
  108. }
  109. $item['yunAttr'] = array_values($item['yunAttr']);
  110. }
  111. }
  112. return $list;
  113. } catch (\Exception $e) {
  114. throw new Exception($e->getMessage());
  115. }
  116. }
  117. //门店商品上下架
  118. public function storeGoodsMutiple($params)
  119. {
  120. try {
  121. if(empty($params['good_ids'])) throw new Exception('错误参数');
  122. $res = HappinessStoreGoods::updateAll(
  123. [
  124. 'is_on_sale' => $params['type'],
  125. 'updated_at' => time(),
  126. ],
  127. [
  128. 'mall_id' => $this->mall_id,
  129. 'store_id' => $params['store_id'],
  130. 'id' => $params['good_ids']
  131. ]
  132. );
  133. if (!$res) throw new Exception('操作失败');
  134. HappinessStoreGoodsAttr::updateAll(['is_on_sale' => $params['type']], ['mall_id' => $this->mall_id, 'store_id' => $params['store_id'], 'yun_goods_id' => $params['good_ids']]);
  135. return true;
  136. } catch (\Exception $e) {
  137. throw new Exception($e->getMessage());
  138. }
  139. }
  140. //获取商品详情
  141. public function getGoodsDetail($params)
  142. {
  143. try {
  144. $info = [];
  145. //存在id,获取详情数据
  146. if (!empty($params['id'])) {
  147. $detail = HappinessStoreGoods::getOne([
  148. ['mall_id' => $this->mall_id],
  149. ['status' => StatusEnum::ENABLED],
  150. ['id' => $params['id']],
  151. ], true, ['goods', 'attr', 'yunAttr']);
  152. foreach ($detail['yunAttr'] as &$item) {
  153. $item['name'] = $detail['attr'][$item['attr_id']]['name'];
  154. $item['price'] = $detail['attr'][$item['attr_id']]['price'];
  155. }
  156. foreach ($detail['attr'] as $key => $value) {
  157. if (empty($detail['yunAttr'][$value['id']])) {
  158. $detail['yunAttr'][$value['id']] = [
  159. 'attr_id' => $value['id'],
  160. 'goods_id' => $value['goods_id'],
  161. 'name' => $value['name'],
  162. 'price' => $value['price'],
  163. ];
  164. } else {
  165. $detail['yunAttr'][$value['id']]['name'] = $value['name'];
  166. $detail['yunAttr'][$value['id']]['price'] = $value['price'];
  167. }
  168. }
  169. $detail['yunAttr'] = array_values($detail['yunAttr']);
  170. $info['detail'] = $detail;
  171. }
  172. return $info;
  173. } catch (\Exception $e) {
  174. throw new Exception($e->getMessage());
  175. }
  176. }
  177. //添加、编辑商品
  178. public function addGoods($params)
  179. {
  180. $attr_params=[];
  181. $label=[];
  182. //获取商品价格
  183. $attr_ids=array_column($params, 'attr_id');
  184. $attrs=GoodsAttr::find()->where(['id' => $attr_ids,'status' => StatusEnum::ENABLED])->select('id,stock,price,goods_id')
  185. ->asArray()->indexBy('id')->all();
  186. $happinessGoodsList=HappinessGoods::find()->where(['goods_id' => array_column($params, 'goods_id'),'status'
  187. => StatusEnum::ENABLED,'mall_id' => $this->mall_id])->asArray()->indexBy('goods_id')->all();
  188. if(empty($happinessGoodsList))
  189. {
  190. throw new Exception('未检测到任何商品');
  191. }
  192. foreach ($params as &$item)
  193. {
  194. if(!isset($happinessGoodsList[$item['goods_id']]))
  195. {
  196. throw new Exception('选品库中不存在商品(ID:'.$item['goods_id'].')');
  197. }
  198. $label_id=$happinessGoodsList[$item['goods_id']]['label_id'];
  199. $attr_params[]=[
  200. 'mall_id' => $this->mall_id,
  201. 'goods_id' => $item['goods_id'],
  202. 'status' => StatusEnum::ENABLED,
  203. 'label_id'=>$label_id,
  204. 'store_id' => $this->store_id,
  205. 'buy_num' => $item['buy_num'],
  206. 'attr_id' => $item['attr_id'],
  207. 'voucher'=>bcmul($item['buy_num'], $attrs[$item['attr_id']]['price'], 2)
  208. ];
  209. if(!isset($label[$label_id]))
  210. {
  211. $label[$label_id]=bcmul($item['buy_num'], $attrs[$item['attr_id']]['price'], 2);
  212. }
  213. else
  214. {
  215. $label[$label_id] += bcmul($item['buy_num'], $attrs[$item['attr_id']]['price'], 2);
  216. }
  217. }
  218. //检测提货券数量
  219. $goods_label= \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic.goods_label');
  220. $goods_label = empty($goods_label) ? [] : json_decode($goods_label,true);
  221. $goods_label=array_column($goods_label,null,'level');
  222. $label_ids=array_column($params, 'label_id');
  223. $store_labels=HappinessVoucherWallet::find()->where(
  224. ['mall_id' => $this->mall_id,
  225. 'status' => StatusEnum::ENABLED,
  226. 'store_id'=>$this->store_id,
  227. 'level' => $label_ids
  228. ])->asArray()->indexBy('level')->all();
  229. $wallet_log=[];
  230. foreach ($label as $key => $item)
  231. {
  232. $label_name=isset($goods_label[$key])?$goods_label[$key]['name']:'未知';
  233. if(empty($store_labels[$key]))
  234. {
  235. throw new Exception($label_name.'提货券当前为0');
  236. }
  237. else
  238. {
  239. if($item > $store_labels[$key]['balance'])
  240. {
  241. throw new Exception($label_name.'提货券数量不足,当前只有'.$store_labels[$key]['balance'].',共需'.$item);
  242. }
  243. }
  244. $wallet_log[] = [
  245. 'mall_id' => $this->mall_id,
  246. 'store_id' => $this->store_id,
  247. 'level' => $key,
  248. 'change_type' => CapitalLog::CHANGE_TYPE_SUB,
  249. 'change_num' => -$item,
  250. 'desc' => '小店进货扣除',
  251. 'source_table' => HappinessVoucherWallet::tableName(),
  252. 'source_table_id' => $this->store_id,
  253. 'from_type' => HappinessVoucherEnum::FROM_STORE_BUY,
  254. 'wallet_type' => HappinessVoucherEnum::WALLET_VALUE,
  255. 'status' => 1,
  256. 'order_no' => ''
  257. ];
  258. }
  259. $tran=Yii::$app->db->beginTransaction();
  260. try {
  261. //添加提货单
  262. $goods_ids=[];
  263. // foreach ($goods_params as $key => $item)
  264. // {
  265. // $goods = HappinessStoreGoods::setData($item);
  266. // if (!$goods) throw new Exception(HappinessStoreGoods::$static_error);
  267. // $goods_ids[$item['goods_id']]=$goods->id;
  268. // }
  269. // foreach ($attr_params as $key => &$item)
  270. // {
  271. // $item['yun_goods_id']=$goods_ids[$item['goods_id']];
  272. // $attr = HappinessStoreGoodsAttr::setData($item);
  273. // if (!$attr) throw new Exception(HappinessStoreGoodsAttr::$static_error);
  274. // }
  275. $voucherService=new VoucherService;
  276. $res=$voucherService->addVoucher($attr_params,$this->mall_id, $this->store_id);
  277. if (!$res) throw new Exception($voucherService->getError());
  278. //扣除提货券
  279. if(!empty($wallet_log))
  280. {
  281. foreach ($wallet_log as $item)
  282. {
  283. if(abs($item['change_num'])>0)
  284. {
  285. $res=WalletLogic::handle($item);
  286. if (!$res) throw new Exception(WalletLogic::getStaticError());
  287. }
  288. }
  289. }
  290. $tran->commit();
  291. return true;
  292. } catch (\Exception $e) {
  293. $tran->rollBack();
  294. throw new Exception($e->getMessage().$e->getFile());
  295. }
  296. }
  297. //删除商品
  298. public function deleteGoods($params)
  299. {
  300. try {
  301. $check = HappinessStoreGoods::getOne([['mall_id' => $this->mall_id], ['id' => $params['id']], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]]);
  302. if (empty($check)) throw new Exception('查询不到该商品数据');
  303. $check->status = $params['status'];
  304. $res = $check->save();
  305. if (!$res) throw new Exception('操作失败');
  306. return true;
  307. } catch (\Exception $e) {
  308. throw new Exception($e->getMessage());
  309. }
  310. }
  311. //搜索商品信息
  312. public function searchGoodsData($params)
  313. {
  314. try {
  315. $where = [
  316. ['yg.mall_id' => $this->mall_id],
  317. ['yg.status' => StatusEnum::ENABLED],
  318. // ['>','g.stock',0],
  319. ['yg.is_on_sale'=>1],
  320. ];
  321. if (!empty($params['keyword'])) $where[] = ['or', ['like', 'g.id', $params['keyword']], ['like', 'g.goods_name', $params['keyword']]];
  322. if ($params['label_id']>0) $where[] = ['yg.label_id' => $params['label_id']];
  323. $with = ['cats', 'attr'];
  324. $params = [
  325. 'alias' => 'yg',
  326. 'where' => $where,
  327. 'with' => $with,
  328. 'limit' => 20,
  329. 'order' => 'id desc',
  330. 'join' => [['LEFT JOIN', Goods::tableName(). ' as g', "g.id = yg.goods_id"]],
  331. 'select' => 'yg.min_num,yg.store_scale,yg.label_id,yg.id,yg.mall_id,yg.sales_num,g.id as goods_id, g.goods_name, g.price, g.cost_price, g.unit, g.cover_pic, yg.is_on_sale, g.is_attr, g.attr_groups, g.attr_groups, g.status, g.stock, g.virtual_sales, g.goods_type, g.services, g.goods_source, g.check_remark, g.sort, g.created_at, g.stock_warning, g.freight_type',
  332. ];
  333. $list = HappinessGoods::listPage($params, false, true);
  334. return $list;
  335. } catch (\Exception $e) {
  336. throw new Exception($e->getMessage());
  337. }
  338. }
  339. public function searchStoreGoodsData($params)
  340. {
  341. try {
  342. $where = [
  343. ['yg.mall_id' => $this->mall_id],
  344. ['yg.status' => StatusEnum::ENABLED],
  345. // ['>','g.stock',0],
  346. ['sg.store_id' => $this->store_id],
  347. ['yg.is_on_sale'=>1],
  348. ];
  349. if (!empty($params['keyword'])) $where[] = ['or', ['like', 'g.id', $params['keyword']], ['like', 'g.goods_name', $params['keyword']]];
  350. if ($params['label_id']>0) $where[] = ['yg.label_id' => $params['label_id']];
  351. $with = ['yunGoods.cats', 'yunAttr.attr'];
  352. $params = [
  353. 'alias' => 'sg',
  354. 'where' => $where,
  355. 'with' => $with,
  356. 'limit' => 20,
  357. 'order' => 'id desc',
  358. 'join' => [['LEFT JOIN', Goods::tableName(). ' as g', "g.id = sg.goods_id and g.status=1"],
  359. ['LEFT JOIN', HappinessGoods::tableName(). ' as yg', "yg.goods_id = sg.goods_id and sg.status=1"]],
  360. 'select' => 'yg.min_num,yg.store_scale,yg.label_id,sg.id,yg.mall_id,yg.sales_num,g.id as goods_id, g.goods_name, g.price, g.cost_price, g.unit, g.cover_pic, yg.is_on_sale, g.is_attr, g.attr_groups, g.attr_groups, g.status, g.stock, g.virtual_sales, g.goods_type, g.services, g.goods_source, g.check_remark, g.sort, g.created_at, g.stock_warning, g.freight_type',
  361. ];
  362. $list = HappinessStoreGoods::listPage($params, false, true);
  363. if($list==false)
  364. {
  365. $error=HappinessStoreGoods::getStaticError();
  366. }
  367. return $list;
  368. } catch (\Exception $e) {
  369. throw new Exception($e->getMessage());
  370. }
  371. }
  372. public function cateGoodsList($params)
  373. {
  374. try {
  375. if (!empty($params['type']) && $params['type'] == 'all') {
  376. //查询三级的所有
  377. $goods_cate_list = GoodsCate::findAll(['parent_id' => $params['cate_id'], 'mall_id' => $this->mall_id, 'is_show' => 1]);
  378. $cate_id_arr = array_column($goods_cate_list, 'id');
  379. $cate_id_arr[] = $params['cate_id'];
  380. } else {
  381. if (empty($params['cate_id'])) { //顶级
  382. //$goods_cate_list = GoodsCate::findAll(['parent_id' => 0, 'mall_id' => $this->mall_id, 'is_show' => 1]);
  383. //查询所有分类的商品
  384. $goods_cate_list = GoodsCate::findAll(['mall_id' => $this->mall_id, 'is_show' => 1]);
  385. $cate_id_arr = array_column($goods_cate_list, 'id');
  386. } else {
  387. $goods_cate = GoodsCate::findOne(['id' => $params['cate_id'], 'mall_id' => $this->mall_id, 'is_show' => 1]);
  388. $c = [$params['cate_id']];
  389. $cate_id_arr = [];
  390. $cate_id_arr1 = [];
  391. GoodsCate::getAllCate($this->mall_id, $c, $cate_id_arr1);
  392. if (!empty($cate_id_arr1)) {
  393. foreach ($cate_id_arr1 as $item) {
  394. if (!empty($item)) {
  395. $cats = explode(",", $item);
  396. array_push($cate_id_arr, ...$cats);
  397. }
  398. }
  399. }
  400. if (!empty($goods_cate)) {
  401. if ($goods_cate['parent_id'] == 0) {
  402. $goods_cate_list = GoodsCate::findAll(['parent_id' => $params['cate_id'], 'mall_id' => $this->mall_id, 'is_show' => 1]);
  403. $cid_arr2 = array_column($goods_cate_list, 'id');
  404. $goods_cate_list = GoodsCate::findAll(['parent_id' => $cid_arr2, 'mall_id' => $this->mall_id, 'is_show' => 1]);
  405. $cid_arr3 = array_column($goods_cate_list, 'id');
  406. $cate_id_arr = array_merge($cate_id_arr, $cid_arr2, $cid_arr3);
  407. }
  408. }
  409. }
  410. }
  411. $goods_id_arr = GoodsCateRelate::find()->where(['cate_id' => $cate_id_arr])->groupBy('goods_id')->select('goods_id')->column();
  412. if (!empty($params['keyword'])) {
  413. $goods_info = Goods::lists([
  414. 'where' => [
  415. ['mall_id' => $this->mall_id],
  416. ['status' => StatusEnum::ENABLED],
  417. ['is_on_sale' => 1],
  418. ['id' => $goods_id_arr],
  419. ['like', 'goods_name', $params['keyword']],
  420. ]
  421. ]);
  422. if (!empty($goods_info)) {
  423. $goods_id_arr = array_column($goods_info, 'id');
  424. } else {
  425. $goods_id_arr = [];
  426. }
  427. }
  428. $where = [
  429. ['mall_id' => $this->mall_id],
  430. ['status' => StatusEnum::ENABLED],
  431. ['is_on_sale' => 1],
  432. ['store_id' => $params['store_id']],
  433. ['goods_id' => $goods_id_arr],
  434. ];
  435. $goods_params = [
  436. 'where' => $where,
  437. 'with' => ['good', 'attrs'],
  438. 'page' => $params['page'],
  439. 'limit' => $params['limit'],
  440. 'is_on_sale' => 1,
  441. 'order' => 'id desc',
  442. ];
  443. $list = HappinessStoreGoods::listPage($goods_params);
  444. if($list===false)
  445. {
  446. throw new Exception(HappinessStoreGoods::getStaticError());
  447. }
  448. $cart_list = Cart::getCartList($this->user_id);
  449. $goods_num_arr = [];
  450. foreach ($cart_list as $v) {
  451. $row = json_decode($v, true);
  452. if (isset($goods_num_arr[$row['goods_id']])) {
  453. $goods_num_arr[$row['goods_id']] += $row['num'];
  454. } else {
  455. $goods_num_arr[$row['goods_id']] = $row['num'];
  456. }
  457. }
  458. $pension_scale = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME,
  459. 'basic.pension_scale')??0;
  460. if (!empty($list['list'])) {
  461. foreach ($list['list'] as &$item) {
  462. $item['good']['num'] = !empty($goods_num_arr[$item['goods_id']]) ? $goods_num_arr[$item['goods_id']] : 0;
  463. $item['pension_price']= bcmul($item['good']['price'],bcdiv($pension_scale,100,4),2);
  464. if (!empty($item['attrs'])) {
  465. $item['good']['attr'] = $item['attrs'];
  466. $item['good']['attr_id'] = 0;
  467. foreach($item['attrs'] as $val){
  468. if($val['is_show'] == StatusEnum::ENABLED){
  469. $item['good']['attr_id'] = $val['id'];
  470. break;
  471. }
  472. }
  473. unset($item['attrs']);
  474. }
  475. }
  476. }
  477. return $list;
  478. } catch (\Exception $e) {
  479. throw new Exception($e->getMessage());
  480. }
  481. }
  482. public function cartListAppendFields($params)
  483. {
  484. if (empty($params)) return [];
  485. $data = $params['data'];
  486. foreach ($data as $k => $v) {
  487. $list = $v['list'];
  488. // 商城订单
  489. if ($k == 0) {
  490. //组合当前商品id
  491. $goods_id = array_column($list, 'goods_id');
  492. $attr_id = array_column($list, 'attr_id');
  493. //获取商品规格库存量
  494. $goods_attr_data = GoodsAttr::lists([
  495. 'where' => [
  496. ['status' => StatusEnum::ENABLED],
  497. ['id' => $attr_id],
  498. ],
  499. 'index' => 'id',
  500. ]);
  501. //获取商品是否幸福小店门店商品
  502. $happiness_goods = HappinessStoreGoods::lists([
  503. 'where' => [
  504. ['mall_id' => $params['mall_id']],
  505. ['status' => StatusEnum::ENABLED],
  506. ['goods_id' => $goods_id],
  507. ],
  508. 'index' => 'goods_id',
  509. ]);
  510. //获取幸福小店门店商品规格库存量
  511. $happiness_goods_attr = HappinessStoreGoodsAttr::lists([
  512. 'where' => [
  513. ['mall_id' => $params['mall_id']],
  514. ['status' => StatusEnum::ENABLED],
  515. ['attr_id' => $attr_id],
  516. ['goods_id' => $goods_id],
  517. ['is_on_sale' => 1],
  518. ['>', 'stock', 0],
  519. ],
  520. 'index' => 'attr_id',
  521. ]);
  522. foreach ($list as &$item) {
  523. if ($item['is_on_sale'] == 1) {
  524. $item['is_happiness_goods'] = 0;
  525. if (!empty($happiness_goods[$item['goods_id']])) {
  526. $item['is_happiness_goods'] = 1;
  527. //判断主商城该商品是否库存为0
  528. if (!empty($goods_attr_data[$item['attr_id']]) && $goods_attr_data[$item['attr_id']]['stock'] <= 0) {
  529. //库存为0则为失效商品
  530. $item['is_on_sale'] = 0;
  531. //如果已被设为幸福小店门店商品,且存在库存,则仍为可购买商品
  532. if (!empty($happiness_goods_attr[$item['attr_id']])) {
  533. $item['is_on_sale'] = 1;
  534. }
  535. }
  536. }
  537. }
  538. }
  539. $data[$k]['list'] = $list;
  540. }
  541. }
  542. return $data;
  543. }
  544. }