RepurchaseMarketing.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace common\logic\order\marketing;
  3. use common\enums\AddonsEnum;
  4. use common\enums\StatusEnum;
  5. use common\models\goods\GoodsExtra;
  6. use common\models\mall\MallAddons;
  7. use common\models\order\OrderDetail;
  8. /**
  9. * 复购处理
  10. */
  11. class RepurchaseMarketing extends BaseMarketing
  12. {
  13. public function execute($form)
  14. {
  15. }
  16. public static function compute($item, &$form)
  17. {
  18. $goods_ids = array_keys($item['goods']);
  19. $user_id = $form['user_id'];
  20. $mall_id = $item['mch_order_info']['mall_id'];
  21. $setting = GoodsExtra::lists([
  22. 'where' => [['goods_id' => $goods_ids], ['is_repurchase' => StatusEnum::ENABLED], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]],
  23. 'select' => 'goods_id, repurchase_setting',
  24. 'index' => 'goods_id'
  25. ]);
  26. // $order_details = OrderDetail::find()->where([
  27. // 'and', ['mall_id' => $mall_id], ['user_id' => $user_id], ['goods_id' => $goods_ids], ['>', 'order_status', 0]
  28. // ])->select('user_id, goods_id, SUM(num) as total_num')->groupBy('goods_id')->asArray()->all();
  29. // $purchase_info = array_column($order_details, 'total_num', 'goods_id') ?? [];
  30. $purchase_num = OrderDetail::find()->where([
  31. 'and', ['mall_id' => $mall_id], ['user_id' => $user_id], ['>', 'order_status', 0]
  32. ])->sum('num') ?? 0;
  33. if ($purchase_num == 0 && MallAddons::isInstall($mall_id, AddonsEnum::ADDONS_NAME_STORE)) {
  34. /** @var \addons\Store\common\models\StoreOrderDetail $store_order_detail_class */
  35. $store_order_detail_class = \addons\Store\common\models\StoreOrderDetail::class;
  36. $purchase_num = $store_order_detail_class::find()->where([
  37. 'and', ['mall_id' => $mall_id], ['user_id' => $user_id], ['>', 'order_status', 0]
  38. ])->sum('num') ?? 0;
  39. }
  40. $pay_money = 0;
  41. foreach ($item['goods'] as $goods_id => &$goods) {
  42. $pay_money += $goods['goods_price'];
  43. if (!isset($setting[$goods_id])) continue;
  44. $repurchase = json_decode($setting[$goods_id]['repurchase_setting'], true);
  45. // $purchase_num = $purchase_info[$goods_id] ?? 0;
  46. //第一次购买
  47. if ($purchase_num <= 0) continue;
  48. $repurchase_num = $goods['num'];
  49. $is_match = false;
  50. $total_repurchase_price = 0;
  51. foreach ($goods['goods_details'] as $key => &$value) {
  52. if ($is_match) {
  53. $value['goods_price'] = $total_repurchase_price;
  54. } else {
  55. foreach ($repurchase as $val) {
  56. if ($repurchase_num >= $val['start'] && $repurchase_num <= $val['end']) {
  57. $is_match = true;
  58. $total_repurchase_price += bcmul($val['price'], $value['num'], 2);
  59. break;
  60. }
  61. }
  62. if ($is_match) {
  63. $value['goods_price'] = $total_repurchase_price;
  64. }
  65. }
  66. }
  67. if ($is_match) {
  68. $pay_money -= $goods['goods_price'];
  69. $goods['goods_price'] = $total_repurchase_price;
  70. $pay_money += $total_repurchase_price;
  71. }
  72. }
  73. $item['mch_order_info']['pay_money'] = $pay_money;
  74. $item['mch_order_info']['goods_price'] = $pay_money;
  75. return $item;
  76. }
  77. }