CommunityGoods.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. <?php
  2. namespace addons\Community\common\models;
  3. use addons\Community\common\enums\CommunityEnum;
  4. use addons\Community\common\helper\CommunityHelper;
  5. use addons\Community\common\service\CommunityService;
  6. use addons\Community\common\service\SettingService;
  7. use common\enums\GoodsTypeEnum;
  8. use common\enums\PreviewTypeEnum;
  9. use common\enums\ShippingTypeEnum;
  10. use common\enums\StatusEnum;
  11. use common\helpers\LocationHelper;
  12. use common\models\base\BaseActiveRecord;
  13. use common\models\common\Region;
  14. use common\models\goods\Goods;
  15. use common\models\goods\GoodsAttr;
  16. use common\models\goods\GoodsCateRelate;
  17. use Exception;
  18. use Yii;
  19. use yii\helpers\Json;
  20. use common\models\mall\MallAddons;
  21. use common\enums\AddonsEnum;
  22. /**
  23. * This is the model class for table "qimall_addons_community_head".
  24. *
  25. * @property int $id
  26. * @property int $mall_id
  27. * @property int $goods_id Goods ID
  28. * @property int $community_id Community ID
  29. * @property int $status 状态[0禁用 1启用 -1删除]
  30. * @property int $created_at 创建时间
  31. * @property int $updated_at 修改时间
  32. */
  33. class CommunityGoods extends BaseActiveRecord
  34. {
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public static function tableName()
  39. {
  40. return '{{%addons_community_goods}}';
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function rules()
  46. {
  47. return [
  48. [['mall_id', 'goods_id', 'community_id', 'status', 'created_at', 'updated_at',], 'integer'],
  49. ];
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function attributeLabels()
  55. {
  56. return [
  57. 'id' => 'ID',
  58. 'mall_id' => '商城id',
  59. 'goods_id' => '商品id',
  60. 'community_id' => '自提点id',
  61. 'status' => '状态[0禁用 1启用 -1删除]',
  62. 'created_at' => '创建时间',
  63. 'updated_at' => '修改时间',
  64. ];
  65. }
  66. public static function saveGoods($params)
  67. {
  68. // 后台页面组件展示
  69. if ($params['type'] == 'show_component') {
  70. return [
  71. 'component' => 'com-community-goods',
  72. 'path' => Yii::$app->basePath . '/../addons/Community/backend/views/components',
  73. ];
  74. } else {
  75. try {
  76. if (empty($params['setting'][CommunityEnum::ADDONS_NAME])) return true;
  77. $post = $params['setting'][CommunityEnum::ADDONS_NAME];
  78. $post['mall_id'] = $params['mall_id'];
  79. $mall_id = $params['mall_id'];
  80. $goods_id = $params['goods_id'];
  81. $enable = $post['enable'];
  82. $enable_refund = $post['enable_refund'];
  83. $supported_logistics = json_encode($post['supported_logistics']);
  84. $setting = CommunityGoodsSetting::setData(compact('mall_id', 'goods_id', 'enable', 'supported_logistics', 'enable_refund'));
  85. if ($setting === false) throw new Exception(CommunityGoodsSetting::getStaticError());
  86. if ($enable == StatusEnum::DISABLED) return true;
  87. $community_ids = $post['community_arr_ids'] ?? [];
  88. if (count($community_ids) <= 0) return true;
  89. $params = [
  90. 'where' => [['mall_id' => $mall_id], ['goods_id' => $goods_id], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]],
  91. 'select' => 'community_id'
  92. ];
  93. $community_arr = self::lists($params, true);
  94. $community_arr_ids = array_column($community_arr, 'community_id');
  95. $community_arr_ids = $community_arr_ids ?? [];
  96. $community_list_ids = array_unique(array_merge($community_arr_ids, $community_ids));
  97. // 需要删除的自提点id
  98. $del_community_ids = array_diff($community_list_ids, $community_ids);
  99. // 需要插入的自提点id
  100. $inc_community_ids = array_diff($community_ids, $community_arr_ids);
  101. if ($del_community_ids) self::batchDelCommunityGoods($del_community_ids, $mall_id, $goods_id);
  102. if ($inc_community_ids) {
  103. $data = [];
  104. foreach ($inc_community_ids as $community_id) {
  105. $data[] = [$mall_id, $goods_id, $community_id, time(), time()];
  106. }
  107. self::batchSetCommunityGoods($data);
  108. }
  109. return true;
  110. } catch (\Exception $e) {
  111. self::setStaticError($e->getMessage());
  112. return false;
  113. }
  114. }
  115. }
  116. public static function recursionRegions($regions, $deep = 1)
  117. {
  118. $ids = [];
  119. foreach ($regions as $region) {
  120. if ($deep == 4 && $region['checked']) {
  121. $ids[] = $region['id'];
  122. continue;
  123. }
  124. if (isset($region['children']) && $region['children']) {
  125. $new_ids = self::recursionRegions($region['children'], $deep + 1);
  126. $ids = array_merge($ids, $new_ids);
  127. }
  128. }
  129. return $ids;
  130. }
  131. public static function batchSetCommunityGoods($data)
  132. {
  133. $connection = Yii::$app->db;
  134. //数据批量入库
  135. $count = $connection->createCommand()->batchInsert(
  136. self::tableName(),
  137. ['mall_id', 'goods_id', 'community_id', 'created_at', 'updated_at'], //字段
  138. $data
  139. )->execute();
  140. return $count;
  141. }
  142. public static function batchDelCommunityGoods($community_ids, $mall_id, $goods_id)
  143. {
  144. $community_list = self::findAll(['goods_id' => $goods_id, 'status' => StatusEnum::ENABLED, 'mall_id' => $mall_id, 'community_id' => $community_ids]);
  145. foreach ($community_list as $community) {
  146. $community->status = StatusEnum::DELETE;
  147. $community->save();
  148. }
  149. return true;
  150. }
  151. public static function setGoodsSetting($mall_id, $community_id)
  152. {
  153. $enable_goods = CommunityGoodsSetting::lists([
  154. 'where' => [['mall_id' => $mall_id], ['status' => StatusEnum::ENABLED], ['enable' => StatusEnum::ENABLED]],
  155. 'select' => 'goods_id',
  156. ]);
  157. $enable_goods = array_column($enable_goods, 'goods_id');
  158. $enable_goods = $enable_goods ?? [];
  159. if (empty($enable_goods)) return true;
  160. $exsit = self::lists([
  161. 'where' => [['mall_id' => $mall_id], ['status' => StatusEnum::ENABLED], ['community_id' => $community_id]],
  162. 'select' => 'goods_id'
  163. ]);
  164. $exsit_goods_ids = array_column($exsit, 'goods_id');
  165. $exsit_goods_ids = $exsit_goods_ids ?? [];
  166. $insert_goods_ids = $enable_goods;
  167. if ($exsit_goods_ids) $insert_goods_ids = array_diff($enable_goods, $exsit_goods_ids);
  168. $data = [];
  169. foreach ($insert_goods_ids as $goods_id) {
  170. $data[] = [$mall_id, $goods_id, $community_id, time(), time()];
  171. }
  172. if ($data) self::batchSetCommunityGoods($data);
  173. return true;
  174. }
  175. public function filterGoodsId($params)
  176. {
  177. $mall_id = $params['mall_id'];
  178. if (!isset($params['location_style']) || $params['location_style'] <= 2) return $params['goods_id'];
  179. if (!isset($params['user_id']) || $params['user_id'] <= 0) return $params['goods_id'];
  180. $latest = self::getLatestCommunityOne($mall_id, $params['user_id']);
  181. if (empty($latest)) return $params['goods_id'];
  182. $goods = self::lists([
  183. 'where' => [['mall_id' => $mall_id], ['community_id' => $latest['community_id']], ['status' => StatusEnum::ENABLED]],
  184. 'select' => 'id, goods_id'
  185. ]);
  186. $exsit_goods_id = array_column($goods, 'goods_id');
  187. $exsit_goods_id = $exsit_goods_id ?? [];
  188. if ($exsit_goods_id) {
  189. $goods_setting = CommunityGoodsSetting::lists([
  190. 'where' => [['mall_id' => $mall_id], ['goods_id' => $exsit_goods_id], ['status' => StatusEnum::ENABLED], ['enable' => StatusEnum::ENABLED]],
  191. 'select' => 'goods_id'
  192. ]);
  193. $exsit_goods_id = array_column($goods_setting, 'goods_id');
  194. $exsit_goods_id = $exsit_goods_id ?? [];
  195. }
  196. return $exsit_goods_id;
  197. }
  198. /**
  199. * @Description: 订单预览追加字段
  200. * @Author: zsan
  201. * @DateTime 2023-02-16 09:36:11
  202. * @param array $params
  203. * @return array
  204. */
  205. public function orderPreviewAppendFields($params)
  206. {
  207. $input = $params['params'];
  208. $response = $params['data'];
  209. $mall_id = $params['mall_id'];
  210. $user_id = $params['user_id'];
  211. $latitude = $input['latitude'];
  212. $longitude = $input['longitude'];
  213. $type = $input['type'];
  214. $need_not_express = $input['need_not_express'];
  215. $location_style = $input['location_style'] ?? 1;
  216. if (!in_array($type, [PreviewTypeEnum::CART, PreviewTypeEnum::BUY_NOW])) return $response;
  217. if (!in_array($location_style, [3])) return $response;
  218. $attr_ids = [];
  219. if ($type == PreviewTypeEnum::CART) {
  220. // 购物车操作
  221. $attr_ids = $input['attr_ids'];
  222. } else {
  223. // 直接购买
  224. $attr_ids = $input['attr_id'];
  225. }
  226. $goods_attr = GoodsAttr::lists([
  227. 'where' => [
  228. ['status' => StatusEnum::ENABLED], ['id' => $attr_ids]
  229. ],
  230. 'select' => 'id, goods_id',
  231. 'with' => [
  232. 'goods' => function ($query) {
  233. $query->select('id, goods_type');
  234. }
  235. ],
  236. ]);
  237. $goods_type = GoodsTypeEnum::GoodsTypePhysical;
  238. foreach ($goods_attr as $v) {
  239. if ($v['goods']['goods_type'] == GoodsTypeEnum::GoodsTypeVirtual) {
  240. $goods_type = GoodsTypeEnum::GoodsTypeVirtual;
  241. break;
  242. }
  243. }
  244. if ($goods_type == GoodsTypeEnum::GoodsTypeVirtual) {
  245. return $response;
  246. }
  247. $response['option_list'] = ShippingTypeEnum::getBaseShippingType();
  248. $goods_ids = array_column($goods_attr, 'goods_id');
  249. $goods_ids = $goods_ids ?? [];
  250. if (empty($goods_ids)) return $response;
  251. $first_goods_id = $goods_ids[0];
  252. $goods_setting = self::lists([
  253. 'where' => [
  254. ['mall_id' => $mall_id], ['goods_id' => $goods_ids], ['status' => StatusEnum::ENABLED]
  255. ],
  256. 'select' => 'id, goods_id, community_id'
  257. ]);
  258. $community_ids = array_column($goods_setting, 'community_id');
  259. $supported_logistics = [];
  260. $enable_goods_setting = [];
  261. if ($goods_setting) {
  262. $enable_goods_setting = CommunityGoodsSetting::lists([
  263. 'where' => [['mall_id' => $mall_id], ['goods_id' => array_column($goods_setting, 'goods_id') ?? []], ['status' => StatusEnum::ENABLED], ['enable' => StatusEnum::ENABLED]],
  264. 'select' => 'id, supported_logistics, goods_id',
  265. ]);
  266. if ($enable_goods_setting) {
  267. foreach ($enable_goods_setting as $item) {
  268. $supported_logistics_item = json_decode($item['supported_logistics'], true);
  269. // 当前没有数据加入初始化数据
  270. if (empty($supported_logistics)) {
  271. $supported_logistics = $supported_logistics_item;
  272. } else {
  273. // 判断两个数组是否有交集
  274. $supported_logistics = array_intersect($supported_logistics, $supported_logistics_item);
  275. if (empty($supported_logistics)) {
  276. // 当前数组没有交集
  277. throw new Exception('部分商品属性不同无法一起下单,请您分开下单操作。');
  278. }
  279. }
  280. }
  281. $enable_goods_setting = array_column($enable_goods_setting, null, 'goods_id');
  282. }
  283. }
  284. if (empty($enable_goods_setting) && $need_not_express) {
  285. $response['option_list'] = [];
  286. $response['is_address'] = 0;
  287. }
  288. // 如果设置社区不为空并且其中有自提点
  289. if (!empty($supported_logistics) && in_array(ShippingTypeEnum::PICKUP, $supported_logistics)) {
  290. // 判断所选的多商品中社区地址是否有相同
  291. $community_unique = array_unique($community_ids); // 去除订单中重复地址ID
  292. $goods_ids_unique = array_unique($goods_ids);
  293. if ((count($community_unique) == count($community_ids)) && count($goods_ids_unique) > 1) {
  294. // 社区地址没有重复(那么多个社区团购订单无法进行一致地址则提示客户重新选择相同地址商品)
  295. throw new Exception('部分商品不支持当前自提点,切换自提点或单独下单。');
  296. }
  297. }
  298. $supported_logistics_methods = [
  299. ShippingTypeEnum::LOGISTICS => ['key' => ShippingTypeEnum::LOGISTICS, 'name' => '快递配送', 'enable' => 1],
  300. ShippingTypeEnum::PICKUP => ['key' => ShippingTypeEnum::PICKUP, 'name' => '站点自提', 'enable' => 1],
  301. ShippingTypeEnum::MERCHANT_DISTRIBUTION => ['key' => ShippingTypeEnum::MERCHANT_DISTRIBUTION, 'name' => '送货上门', 'enable' => 1]
  302. ];
  303. $latest = self::getLatestCommunityOne($mall_id, $user_id);
  304. $latest_logistics_methods = [];
  305. $community_goods_ids = array_column($goods_setting, 'goods_id');
  306. foreach ($supported_logistics_methods as $item) {
  307. if (in_array($item['key'], $supported_logistics)) {
  308. if (!in_array($first_goods_id, $community_goods_ids) || !isset($enable_goods_setting[$first_goods_id])) {
  309. $item['enable'] = 0;
  310. }
  311. if (!empty($latest) && $item['key'] != ShippingTypeEnum::LOGISTICS && !in_array($latest['community_id'], $community_ids)) {
  312. $item['enable'] = 0;
  313. }
  314. // 如果商品独立设置的自提区域有选中。但是社区团购区域配置已经去除该区域。则该区域不可用
  315. if (!empty($latest) && !empty($latest['community']) && !empty($item['key']) && $item['key'] == ShippingTypeEnum::PICKUP) {
  316. $setting_service = new SettingService();
  317. $area = $setting_service->getFieldSetting($mall_id, 'area', true);
  318. $area = json_decode($area, true);
  319. if (!empty($area) && !in_array($latest['community']['district_id'], $area)) {
  320. $item['enable'] = 0;
  321. }
  322. }
  323. if ($item['enable'] == 1) {
  324. if (!empty($latest) && $item['key'] == ShippingTypeEnum::MERCHANT_DISTRIBUTION && $latest['community']['is_home_delivery'] == 0) {
  325. $item['enable'] = 0;
  326. }
  327. }
  328. array_push($latest_logistics_methods, $item);
  329. }
  330. }
  331. if ($latest_logistics_methods) {
  332. unset($response['option_list']);
  333. $response['option_list'] = $latest_logistics_methods;
  334. }
  335. $extends_fields = [];
  336. $extends_fields[CommunityEnum::ADDONS_NAME] = [
  337. 'is_selected' => 0, //是否有选择自提点
  338. 'community_info' => [], //自提点信息
  339. 'distance' => 0, //距离
  340. 'pickup_time' => '', //提货时间文案
  341. ];
  342. if ($latest) $extends_fields[CommunityEnum::ADDONS_NAME]['is_selected'] = 1;
  343. if (isset($latest['community'])) {
  344. $extends_fields[CommunityEnum::ADDONS_NAME]['community_info'] = $latest['community'];
  345. $location_obj = new LocationHelper();
  346. $distance = $location_obj->getDistanceByGeo($latest['community']['latitude'], $latest['community']['longitude'], $latitude, $longitude);
  347. $extends_fields[CommunityEnum::ADDONS_NAME]['distance'] = bcmul(bcmul($distance, 1000, 4), 1, 1);
  348. $config = (new SettingService())->get($mall_id);
  349. $position_range = bcmul($config['position_range'], 1000, 1);
  350. $extends_fields[CommunityEnum::ADDONS_NAME]['distance'] = bcmul(bcmul($distance, 1000, 4), 1, 1);
  351. if ($position_range < $extends_fields[CommunityEnum::ADDONS_NAME]['distance']) {
  352. // 需要去掉 自提,上门两个选项
  353. foreach ($response['option_list'] as &$vItem) {
  354. if (in_array($vItem['key'], [2,3])) {
  355. $vItem['enable'] = 0;
  356. }
  357. }
  358. }
  359. $extends_fields[CommunityEnum::ADDONS_NAME]['pickup_time'] = CommunityHelper::getPickUpAt($mall_id, time()) . '可取货';
  360. }
  361. $response['extends_fields'] = array_merge($response['extends_fields'] ?? [], $extends_fields);
  362. return $response;
  363. }
  364. /**
  365. * 创建订单时检查对应商品自提点是否有相同门店
  366. * @param array $params
  367. * @return mixed
  368. * @throws Exception
  369. */
  370. public static function checkCommunityGoods(array $params)
  371. {
  372. try {
  373. $paramsInput = $params['params'];
  374. $mall_id = $params['mall_id'];
  375. $user_id = $params['user_id'];
  376. $type = $paramsInput['type'];
  377. if ($type == PreviewTypeEnum::CART) {
  378. // 购物车操作
  379. $attr_ids = $paramsInput['attr_ids'];
  380. } else {
  381. // 直接购买
  382. $attr_ids = $paramsInput['attr_id'];
  383. }
  384. $goods_attr = GoodsAttr::lists([
  385. 'where' => [
  386. ['status' => StatusEnum::ENABLED], ['id' => $attr_ids]
  387. ],
  388. 'select' => 'id, goods_id',
  389. 'with' => [
  390. 'goods' => function ($query) {
  391. $query->select('id, goods_type');
  392. }
  393. ],
  394. ]);
  395. $goods_ids = array_column($goods_attr, 'goods_id') ?? [];
  396. if (!empty($goods_ids)) {
  397. $goods_setting = self::lists([
  398. 'where' => [
  399. ['mall_id' => $mall_id], ['goods_id' => $goods_ids], ['status' => StatusEnum::ENABLED]
  400. ],
  401. 'select' => 'id, goods_id, community_id'
  402. ]);
  403. $community_ids = array_column($goods_setting, 'community_id');
  404. $latest = self::getLatestCommunityOne($mall_id, $user_id);
  405. // 当选择自提点时如果选中的商品没有在当前自提点门店则提示客户不支持自提
  406. $shipping_type = $paramsInput['shipping_type'];
  407. if ($shipping_type == ShippingTypeEnum::PICKUP && !empty($goods_setting)) {
  408. // 获取当前商品自提点
  409. $is_pickup_point = false;
  410. // 当为多商品时
  411. if (count($goods_ids) > 1 && !empty($community_ids)) {
  412. $goods_unique_ids = array_unique($goods_ids);
  413. foreach (array_count_values($community_ids) as $key => $item) {
  414. if (($item > 1 || count($goods_unique_ids) == 1) && $key == $latest['community_id']) {
  415. $is_pickup_point = true;
  416. break;
  417. }
  418. }
  419. } else {// 如果为单商品
  420. $is_pickup_point = in_array($latest['community_id'], $community_ids);
  421. }
  422. if (!$is_pickup_point) {
  423. self::$static_error = '部分商品不支持当前自提点,切换自提点或单独下单。';
  424. return false;
  425. }
  426. }
  427. }
  428. } catch (Exception $e) {
  429. self::$static_error = $e->getMessage();
  430. return false;
  431. }
  432. return true;
  433. }
  434. public function cartListAppendFields($params)
  435. {
  436. $data = $params['data'];
  437. if (empty($data)) return $data;
  438. $mall_id = $params['mall_id'];
  439. $user_id = $params['user_id'];
  440. $location_style = $params['location_style'] ?? 1;
  441. $latest_one = self::getLatestCommunityOne($mall_id, $user_id);
  442. foreach ($data as $k => $v) {
  443. $list = $v['list'];
  444. // 商城订单
  445. if ($k == 0) {
  446. $goods_ids = array_column($list, 'goods_id');
  447. $goods_setting = CommunityGoodsSetting::lists(['where' => [['mall_id' => $mall_id], ['goods_id' => $goods_ids], ['status' => StatusEnum::ENABLED], ['enable' => StatusEnum::ENABLED]], 'select' => 'supported_logistics, goods_id', 'index' => 'goods_id']);
  448. $goods_ids = array_column($goods_setting, 'goods_id') ?? [];
  449. $community_goods = CommunityGoods::lists(
  450. [
  451. 'where' => [['mall_id' => $mall_id], ['goods_id' => $goods_ids], ['status' => StatusEnum::ENABLED], ['community_id' => $latest_one['community_id'] ?? 0],],
  452. 'select' => 'goods_id',
  453. 'index' => 'goods_id'
  454. ]
  455. );
  456. foreach ($list as &$val) {
  457. $params['mall_id'] = $mall_id;
  458. $params['user']['id'] = $user_id;
  459. $params['goods']['id'] = $val['goods_id'];
  460. $result = self::getApiGoodsDetail($params);
  461. $val['is_community'] = $result['goods']['is_community'];
  462. $val['supported_logistics'] = [1];
  463. $val['is_supported_pickup'] = 0;
  464. if ($location_style == 3) {
  465. isset($goods_setting[$val['goods_id']]) && $val['supported_logistics'] = json_decode($goods_setting[$val['goods_id']]['supported_logistics'], true);
  466. isset($community_goods[$val['goods_id']]) && $val['is_supported_pickup'] = 1;
  467. }
  468. }
  469. unset($val);
  470. } else {
  471. // 多商户插件订单
  472. foreach ($list as &$val) {
  473. $val['supported_logistics'] = [1];
  474. $val['is_supported_pickup'] = 0;
  475. }
  476. unset($val);
  477. }
  478. $data[$k]['list'] = $list;
  479. }
  480. return $data;
  481. }
  482. public static function getComponentData($params, $mall_id)
  483. {
  484. $params['type'] = $params['type'] ?? 'goods';
  485. // 导航风格,3表示自提点风格
  486. $location_style = $params['location_style'] ?? 1;
  487. $community_id = $params['community_id'] ?? 0;
  488. switch ($params['type']) {
  489. case 'goods':
  490. $goods_ids = [];
  491. if ($location_style != 3) {
  492. $goods_ids = Json::decode($params['id'], true);
  493. } else {
  494. if (!empty($community_id)) {
  495. $goods_setting = CommunityGoods::lists(['where' => [['mall_id' => $mall_id], ['community_id' => $params['community_id']], ['status' => StatusEnum::ENABLED]], 'select' => 'goods_id']);
  496. $goods_ids = array_column($goods_setting, 'goods_id') ?? [];
  497. if ($goods_ids) {
  498. $enable_goods_setting = CommunityGoodsSetting::lists(['where' => [['mall_id' => $mall_id], ['goods_id' => $goods_ids], ['status' => StatusEnum::ENABLED], ['enable' => StatusEnum::ENABLED]], 'select' => 'goods_id']);
  499. $goods_ids = array_column($enable_goods_setting, 'goods_id') ?? [];
  500. }
  501. }
  502. $origin_goods_ids = Json::decode($params['id'], true);
  503. $goods_ids = array_intersect($origin_goods_ids, $goods_ids);
  504. }
  505. break;
  506. case 'goods_cate':
  507. $cate_id_arr = [$params['id']];
  508. $goods_cate_relate_list = GoodsCateRelate::findAll(['cate_id' => $cate_id_arr]);
  509. $goods_ids = array_column($goods_cate_relate_list, 'goods_id') ?? [];
  510. if ($location_style == 3 && !empty($community_id)) {
  511. $goods_setting = CommunityGoods::lists(['where' => [['mall_id' => $mall_id], ['goods_id' => $goods_ids], ['community_id' => $params['community_id']], ['status' => StatusEnum::ENABLED]], 'select' => 'goods_id']);
  512. $goods_ids = array_column($goods_setting, 'goods_id') ?? [];
  513. if ($goods_ids) {
  514. $enable_goods_setting = CommunityGoodsSetting::lists([
  515. 'where' => [['mall_id' => $mall_id], ['goods_id' => $goods_ids], ['status' => StatusEnum::ENABLED], ['enable' => StatusEnum::ENABLED]],
  516. 'select' => 'goods_id'
  517. ]);
  518. $goods_ids = array_column($enable_goods_setting, 'goods_id') ?? [];
  519. }
  520. }
  521. break;
  522. default:
  523. break;
  524. }
  525. $goods_params = ['where' => [['=', 'mall_id', $mall_id], ['in', 'id', $goods_ids], ['=', 'status', StatusEnum::ENABLED], ['=', 'is_on_sale', StatusEnum::ENABLED]], 'select' => 'id,goods_name,subtitle,price,original_price,cover_pic,(sales_num+virtual_sales) as sales_num, is_attr', 'index' => 'id'];
  526. if (isset($params['limit'])) {
  527. $goods_params['limit'] = $params['limit'];
  528. $goods_params['page'] = 1;
  529. }
  530. return Goods::lists($goods_params, true);
  531. }
  532. public static function getLatestCommunityOne($mall_id, $user_id)
  533. {
  534. return CommunityUser::getOne(
  535. [['mall_id' => $mall_id], ['user_id' => $user_id], ['status' => StatusEnum::ENABLED], ['is_checked' => StatusEnum::ENABLED]],
  536. true,
  537. ['community' => function ($query) {
  538. return $query->select('id,address,latitude,longitude,name,logo,is_home_delivery, province_id, city_id, district_id')->andWhere(['status' => StatusEnum::ENABLED]);
  539. }],
  540. 'id desc',
  541. 'id,user_id,community_id'
  542. );
  543. }
  544. // 是否社区团购
  545. public static function getApiGoodsDetail($params)
  546. {
  547. $is_community = 0;
  548. if (MallAddons::isInstall($params['mall_id'], AddonsEnum::ADDONS_NAME_COMMUNITY) && !empty($params['user']['id']) && !empty($params['goods']['id'])) {
  549. $CommunityInfo = CommunityGoodsSetting::findOne(['mall_id' => $params['mall_id'], 'goods_id' => $params['goods']['id'], 'status' => [StatusEnum::ENABLED], 'enable' => [StatusEnum::ENABLED]]);
  550. if (!empty($CommunityInfo)) {
  551. $is_community = 1;
  552. }
  553. }
  554. $params['goods']['is_community'] = $is_community;
  555. return $params;
  556. }
  557. }