| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- namespace common\logic\order\marketing;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\models\goods\GoodsExtra;
- use common\models\mall\MallAddons;
- use common\models\order\OrderDetail;
- /**
- * 复购处理
- */
- class RepurchaseMarketing extends BaseMarketing
- {
- public function execute($form)
- {
- }
- public static function compute($item, &$form)
- {
- $goods_ids = array_keys($item['goods']);
- $user_id = $form['user_id'];
- $mall_id = $item['mch_order_info']['mall_id'];
- $setting = GoodsExtra::lists([
- 'where' => [['goods_id' => $goods_ids], ['is_repurchase' => StatusEnum::ENABLED], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]],
- 'select' => 'goods_id, repurchase_setting',
- 'index' => 'goods_id'
- ]);
- // $order_details = OrderDetail::find()->where([
- // 'and', ['mall_id' => $mall_id], ['user_id' => $user_id], ['goods_id' => $goods_ids], ['>', 'order_status', 0]
- // ])->select('user_id, goods_id, SUM(num) as total_num')->groupBy('goods_id')->asArray()->all();
- // $purchase_info = array_column($order_details, 'total_num', 'goods_id') ?? [];
- $purchase_num = OrderDetail::find()->where([
- 'and', ['mall_id' => $mall_id], ['user_id' => $user_id], ['>', 'order_status', 0]
- ])->sum('num') ?? 0;
- if ($purchase_num == 0 && MallAddons::isInstall($mall_id, AddonsEnum::ADDONS_NAME_STORE)) {
- /** @var \addons\Store\common\models\StoreOrderDetail $store_order_detail_class */
- $store_order_detail_class = \addons\Store\common\models\StoreOrderDetail::class;
- $purchase_num = $store_order_detail_class::find()->where([
- 'and', ['mall_id' => $mall_id], ['user_id' => $user_id], ['>', 'order_status', 0]
- ])->sum('num') ?? 0;
- }
- $pay_money = 0;
- foreach ($item['goods'] as $goods_id => &$goods) {
- $pay_money += $goods['goods_price'];
- if (!isset($setting[$goods_id])) continue;
- $repurchase = json_decode($setting[$goods_id]['repurchase_setting'], true);
- // $purchase_num = $purchase_info[$goods_id] ?? 0;
- //第一次购买
- if ($purchase_num <= 0) continue;
- $repurchase_num = $goods['num'];
- $is_match = false;
- $total_repurchase_price = 0;
- foreach ($goods['goods_details'] as $key => &$value) {
- if ($is_match) {
- $value['goods_price'] = $total_repurchase_price;
- } else {
- foreach ($repurchase as $val) {
- if ($repurchase_num >= $val['start'] && $repurchase_num <= $val['end']) {
- $is_match = true;
- $total_repurchase_price += bcmul($val['price'], $value['num'], 2);
- break;
- }
- }
- if ($is_match) {
- $value['goods_price'] = $total_repurchase_price;
- }
- }
- }
- if ($is_match) {
- $pay_money -= $goods['goods_price'];
- $goods['goods_price'] = $total_repurchase_price;
- $pay_money += $total_repurchase_price;
- }
- }
- $item['mch_order_info']['pay_money'] = $pay_money;
- $item['mch_order_info']['goods_price'] = $pay_money;
- return $item;
- }
- }
|