ShareGroupPurchaseParentLog.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. <?php
  2. namespace app\models;
  3. use yii\behaviors\TimestampBehavior;
  4. /**
  5. * This is the model class for table "{{%share}}".
  6. *
  7. * @property integer $id
  8. * @property integer $store_id
  9. * @property integer $user_id
  10. * @property integer $order_id
  11. * @property string $order_detail_id
  12. * @property string $goods_name
  13. * @property integer $parent_id
  14. * @property integer $parent_user_id
  15. * @property integer $child_level
  16. * @property integer $batch_id
  17. * @property integer $points
  18. * @property float $direct_price
  19. * @property float $support_price
  20. * @property float $group_price
  21. * @property integer $is_repeat
  22. * @property integer $is_auto_repeat
  23. * @property integer $created_at
  24. * @property integer $updated_at
  25. */
  26. class ShareGroupPurchaseParentLog extends \yii\db\ActiveRecord
  27. {
  28. //基本流程:购买指定商品后 判断是否是37拼购设置下的商品 判断上级是否存在拼购数据 如果上级存在数据且上级拼购数据信息中下级人数带当前购买人大于三人
  29. // 则第三人算做扶持区人员 前两人算是拼购区人员 以下结构展示(每个人的下级最多三个 且前两位是拼购区 第三位是扶持区)
  30. // /////////////////////
  31. // // A
  32. // // / | \
  33. // // B1 B2 B3
  34. // /////////////////////
  35. // 此时 B1 B2算作拼购区 B3以及下级算作扶持区
  36. // 拼购:如果B1 B2再发展下级那么带上A一共7人 算是已经拼购完成
  37. // 扶持区规则:A如果满三人(B1 B2 B3)再推荐人员的话 需要滑落给B3(B3也可以自己推荐人员)。
  38. // 佣金计算:
  39. // 直推佣金:只要是上级推荐的都需要先给上级发放直推佣金再进行滑落或者其他等操作。
  40. // 拼购佣金(成团奖):如果拼购完成后 需要给A发放 拼购区人数6 * 成团奖比例。
  41. // 帮扶佣金:扶持区每多一个人员都需要给A增加帮扶佣金。
  42. // /////////////////////
  43. // // |----------------|
  44. // // | 成团区 A |------------|
  45. // // | / | | \ 扶持区|
  46. // // | B1 B2 | B3 |
  47. // // | / \ / \ | / \ |
  48. // // | C1 C2 C3 C4 | C5 C6 |
  49. // // |----------------|------------|
  50. //成团区的下级是看订单的 比如B1不推荐下级进行下单 那么可以让B2或者B2下级推荐
  51. // /////////////////////
  52. /**
  53. * @inheritdoc
  54. */
  55. public static function tableName()
  56. {
  57. return '{{%share_group_purchase_parent_log}}';
  58. }
  59. public function behaviors()
  60. {
  61. return [
  62. [
  63. 'class' => TimestampBehavior::class
  64. ]
  65. ];
  66. }
  67. /**
  68. * @inheritdoc
  69. */
  70. public function rules()
  71. {
  72. return [
  73. [['id', 'user_id', 'order_id', 'parent_id', 'parent_user_id', 'child_level', 'points', 'created_at', 'updated_at', 'batch_id', 'is_repeat', 'is_auto_repeat'], 'integer'],
  74. [['direct_price', 'support_price', 'group_price'], 'number'],
  75. [['order_detail_id', 'goods_name'], 'string']
  76. ];
  77. }
  78. /**
  79. * @inheritdoc
  80. */
  81. public function attributeLabels()
  82. {
  83. return [
  84. 'id' => '',
  85. 'user_id' => '用户ID',
  86. 'order_id' => '入位订单ID',
  87. 'order_detail_id' => '',
  88. 'goods_name' => '商品名称 方便查询',
  89. 'parent_id' => '上级ID',
  90. 'parent_user_id' => '用户上级ID',
  91. 'child_level' => '当前层级(一个关系树内的层级)',
  92. 'batch_id' => '同一树标识ID',
  93. 'points' => '同一上级下的入位顺序',
  94. 'direct_price' => '当前点位直推佣金',
  95. 'support_price' => '当前点位扶持佣金',
  96. 'group_price' => '成团佣金',
  97. 'created_at' => '',
  98. 'updated_at' => '',
  99. ];
  100. }
  101. //
  102. public function afterSave($insert, $changedAttributes)
  103. {
  104. try {
  105. //添加成团佣金
  106. if ($insert) {
  107. //兼容一下之前的逻辑
  108. $quotient = ceil($this->points / 4) - 1;
  109. $array = range($quotient * 4 + 1, $this->points);
  110. foreach ($array as $item) {
  111. $quo_purchase_log = self::findOne(['batch_id' => $this->batch_id, 'points' => $item, 'child_level' => $this->child_level]);
  112. $prev_quo_purchase_log = self::findOne(['child_level' => $quo_purchase_log->child_level - 1, 'points' => ceil($item / 2), 'batch_id' => $this->batch_id]);
  113. if ($prev_quo_purchase_log) {
  114. //检查是否存在成团佣金构成
  115. $prev_quo_group_money = ShareGroupMoneySub::findOne(['order_id' => $quo_purchase_log->order_id]);
  116. if (!$prev_quo_group_money) {
  117. $result = self::addData($prev_quo_purchase_log->id);
  118. }
  119. }
  120. //检查是否存在成团佣金构成
  121. $quo_group_money = ShareGroupMoneySub::findOne(['order_id' => $quo_purchase_log->order_id]);
  122. if (!$quo_group_money) {
  123. $result = self::addData($quo_purchase_log->id);
  124. }
  125. }
  126. $parent_purchase_log = self::findOne($this->parent_id);
  127. if ($parent_purchase_log) {
  128. $group_parent_purchase_log = self::findOne($parent_purchase_log->parent_id);
  129. if ($group_parent_purchase_log) {
  130. if (self::checkUnfreeze($group_parent_purchase_log->user_id, $group_parent_purchase_log->batch_id ?: $group_parent_purchase_log->id)) {
  131. //释放之前冻结的 并且订单已经过售后期
  132. $freeze_amount_list = ShareGroupMoney::find()->where([
  133. 'is_freeze' => 1,
  134. 'is_send' => 1,
  135. 'is_send_freeze' => 0,
  136. 'user_id' => $group_parent_purchase_log->user_id
  137. ])->select('id')->column();
  138. foreach ($freeze_amount_list as $freeze_amount_item) {
  139. $freeze_amount_item_ = ShareGroupMoney::findOne($freeze_amount_item);
  140. $freeze_amount_item_->is_send_freeze = 1;
  141. $freeze_amount_item_->freeze_send_time = time();
  142. $freeze_amount_item_->save();
  143. $shareGroupPurchaseUser = ShareGroupPurchaseUser::findOne($freeze_amount_item_->user_id);
  144. if (!$shareGroupPurchaseUser) {
  145. continue;
  146. }
  147. $shareGroupPurchaseUser->support_price += $freeze_amount_item_->freeze_amount;
  148. $shareGroupPurchaseUser->save();
  149. //判断用户是否存在 存在则开始发放
  150. $user = User::findOne(['id' => $freeze_amount_item_->user_id, 'is_delete' => 0]);
  151. if ($user) {
  152. $user->price = bcadd($user->price, $freeze_amount_item_->freeze_amount);
  153. $user->total_price = bcadd($user->total_price, $freeze_amount_item_->freeze_amount);
  154. if (!$user->save()) {
  155. throw new \Exception(json_encode($user->errors, JSON_UNESCAPED_UNICODE));
  156. }
  157. $result = UserShareMoney::set($freeze_amount_item_->freeze_amount, $freeze_amount_item_->user_id, 0, 0, 12, $freeze_amount_item_->store_id, 0, '37拼购冻结佣金发放');
  158. if (!$result) {
  159. throw new \Exception('保存失败');
  160. }
  161. }
  162. }
  163. }
  164. }
  165. }
  166. $parent_user_id = $this->parent_user_id;
  167. $shareGroupSupportLog = ShareGroupSupportLog::findOne(['user_id' => $this->user_id]);
  168. if ($shareGroupSupportLog && $shareGroupSupportLog->parent_id) {
  169. $parentShareGroupSupportLog = ShareGroupSupportLog::findOne(['id' => $shareGroupSupportLog->parent_id]);
  170. $parent_user_id = $parentShareGroupSupportLog->user_id;
  171. }
  172. $shareGroupPurchaseUser = new ShareGroupPurchaseUser();
  173. $shareGroupPurchaseUser->user_id = $this->user_id;
  174. $shareGroupPurchaseUser->store_id = $this->store_id;
  175. $shareGroupPurchaseUser->direct_price = '0.00';
  176. $shareGroupPurchaseUser->support_price = '0.00';
  177. $shareGroupPurchaseUser->group_price = '0.00';
  178. $shareGroupPurchaseUser->parent_user_id = $parent_user_id;
  179. $shareGroupPurchaseUser->save();
  180. }
  181. parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
  182. } catch (\Exception $e) {
  183. return false;
  184. }
  185. }
  186. public static function addData($id) {
  187. try {
  188. $self_purchase_log = self::findOne($id);
  189. $share_group_setting = Option::get('share_group_setting', $self_purchase_log->store_id, 'share_group')['value'];
  190. $share_group_setting = json_decode($share_group_setting ?? '', true);
  191. if (empty($share_group_setting)) {
  192. throw new \Exception('未配置佣金设置');
  193. }
  194. $share_group_commission_profit = $share_group_setting['share_group_commission_profit'];
  195. $share_group_auto_switch = $share_group_setting['share_group_auto_switch'];
  196. $share_group_balance_profit = $share_group_setting['share_group_balance_profit'];
  197. $goods_ids = explode(',', $share_group_setting['goods_ids'] ?? '');
  198. $order = Order::findOne($self_purchase_log->order_id);
  199. $order_detail = OrderDetail::find()->where(['order_id' => $order->id])->select('goods_id, total_price, goods_name')
  200. ->asArray()->all();
  201. $item_radix = 0;
  202. $goods_name = '';
  203. foreach ($order_detail as $goods_item) {
  204. if (in_array($goods_item['goods_id'], $goods_ids)) {
  205. $item_radix = bcadd($goods_item['total_price'], $item_radix, 2);
  206. $goods_name .= $goods_item['goods_name'] . ';';
  207. }
  208. }
  209. $item_amount = bcdiv(bcmul($share_group_commission_profit, $item_radix, 2), 100, 2);
  210. $item_balance_amount = bcdiv(bcmul($item_radix, $share_group_balance_profit, 2), 100, 2);
  211. // $balance_amount = 0;
  212. //如果存在一层上级就添加一条成团记录
  213. //如果存在两层上级就添加两条成团记录
  214. // if ($this->points % 4 == 0) {
  215. $parent_purchase_log = self::findOne($self_purchase_log->parent_id);
  216. if ($parent_purchase_log) {
  217. $moneyForm = ShareGroupMoney::findOne([
  218. 'type' => ShareGroupMoney::TYPE_GROUP,
  219. 'type_id' => $parent_purchase_log->id,
  220. 'profit_type' => ShareGroupMoney::PROFIT_TYPE_COMMON
  221. ]);
  222. if (!$moneyForm) {
  223. $moneyForm = new ShareGroupMoney();
  224. $moneyForm->store_id = $self_purchase_log->store_id;
  225. $moneyForm->user_id = $parent_purchase_log->user_id;
  226. $moneyForm->type = ShareGroupMoney::TYPE_GROUP;
  227. $moneyForm->type_id = $parent_purchase_log->id;
  228. $moneyForm->amount = 0;
  229. $moneyForm->radix = 0;
  230. $moneyForm->profit = $share_group_commission_profit;
  231. $moneyForm->profit_type = ShareGroupMoney::PROFIT_TYPE_COMMON;
  232. $moneyForm->group_data = '';
  233. $moneyForm->save();
  234. }
  235. $money_sub_form = ShareGroupMoneySub::findOne(['order_id' => $self_purchase_log->order_id, 'share_money_id' => $moneyForm->id]);
  236. if (!$money_sub_form) {
  237. $money_sub_form = new ShareGroupMoneySub();
  238. $money_sub_form->order_id = $self_purchase_log->order_id;
  239. $money_sub_form->amount = $item_amount;
  240. $money_sub_form->radix = $item_radix;
  241. $money_sub_form->share_money_id = $moneyForm->id;
  242. $money_sub_form->goods_name = $goods_name;
  243. $money_sub_form->save();
  244. $group_data = json_decode($moneyForm->group_data, true) ?: [];
  245. array_push($group_data, [
  246. 'id' => $self_purchase_log->id,
  247. 'order_id' => $self_purchase_log->order_id,
  248. 'amount' => $item_amount,
  249. 'balance_amount' => $item_balance_amount,
  250. 'radix' => $item_radix,
  251. 'order_no' => $order->order_no,
  252. 'goods_name' => $goods_name
  253. ]);
  254. $moneyForm->group_data = json_encode($group_data, JSON_UNESCAPED_UNICODE);
  255. $moneyForm->radix = bcadd($moneyForm->radix, $item_radix, 2);
  256. $moneyForm->amount = bcadd($moneyForm->amount, $item_amount, 2);
  257. $moneyForm->save();
  258. }
  259. if (!intval($share_group_auto_switch)) {
  260. $moneyBalanceForm = ShareGroupMoney::findOne([
  261. 'type' => ShareGroupMoney::TYPE_GROUP,
  262. 'type_id' => $parent_purchase_log->id,
  263. 'profit_type' => ShareGroupMoney::PROFIT_TYPE_BALANCE
  264. ]);
  265. if (!$moneyBalanceForm) {
  266. $moneyBalanceForm = new ShareGroupMoney();
  267. $moneyBalanceForm->store_id = $self_purchase_log->store_id;
  268. $moneyBalanceForm->user_id = $parent_purchase_log->user_id;
  269. $moneyBalanceForm->type = ShareGroupMoney::TYPE_GROUP;
  270. $moneyBalanceForm->type_id = $parent_purchase_log->id;
  271. $moneyBalanceForm->amount = 0;
  272. $moneyBalanceForm->radix = 0;
  273. $moneyBalanceForm->profit = $share_group_commission_profit;
  274. $moneyBalanceForm->profit_type = ShareGroupMoney::PROFIT_TYPE_BALANCE;
  275. $moneyBalanceForm->group_data = '';
  276. $moneyBalanceForm->save();
  277. }
  278. $money_balance_sub_form = ShareGroupMoneySub::findOne(['order_id' => $self_purchase_log->order_id, 'share_money_id' => $moneyBalanceForm->id]);
  279. if (!$money_balance_sub_form) {
  280. $money_balance_sub_form = new ShareGroupMoneySub();
  281. $money_balance_sub_form->order_id = $self_purchase_log->order_id;
  282. $money_balance_sub_form->amount = $item_amount;
  283. $money_balance_sub_form->radix = $item_radix;
  284. $money_balance_sub_form->share_money_id = $moneyBalanceForm->id;
  285. $money_balance_sub_form->goods_name = $goods_name;
  286. $money_balance_sub_form->save();
  287. $group_data = json_decode($moneyBalanceForm->group_data, true) ?: [];
  288. array_push($group_data, [
  289. 'id' => $self_purchase_log->id,
  290. 'order_id' => $self_purchase_log->order_id,
  291. 'amount' => $item_amount,
  292. 'balance_amount' => $item_balance_amount,
  293. 'radix' => $item_radix,
  294. 'order_no' => $order->order_no,
  295. 'goods_name' => $goods_name
  296. ]);
  297. $moneyBalanceForm->group_data = json_encode($group_data, JSON_UNESCAPED_UNICODE);
  298. $moneyBalanceForm->radix = bcadd($moneyBalanceForm->radix, $item_radix, 2);
  299. $moneyBalanceForm->amount = bcadd($moneyBalanceForm->amount, $item_balance_amount, 2);
  300. $moneyBalanceForm->save();
  301. }
  302. }
  303. $group_parent_purchase_log = self::findOne($parent_purchase_log->parent_id);
  304. if ($group_parent_purchase_log) {
  305. $moneyParentForm = ShareGroupMoney::findOne([
  306. 'type' => ShareGroupMoney::TYPE_GROUP,
  307. 'type_id' => $group_parent_purchase_log->id,
  308. 'profit_type' => ShareGroupMoney::PROFIT_TYPE_COMMON
  309. ]);
  310. if (!$moneyParentForm) {
  311. $moneyParentForm = new ShareGroupMoney();
  312. $moneyParentForm->store_id = $self_purchase_log->store_id;
  313. $moneyParentForm->user_id = $group_parent_purchase_log->user_id;
  314. $moneyParentForm->type = ShareGroupMoney::TYPE_GROUP;
  315. $moneyParentForm->type_id = $group_parent_purchase_log->id;
  316. $moneyParentForm->amount = 0;
  317. $moneyParentForm->radix = 0;
  318. $moneyParentForm->profit = $share_group_commission_profit;
  319. $moneyParentForm->profit_type = ShareGroupMoney::PROFIT_TYPE_COMMON;
  320. $moneyParentForm->group_data = '';
  321. $moneyParentForm->save();
  322. }
  323. $money_parent_sub_form = ShareGroupMoneySub::findOne(['order_id' => $self_purchase_log->order_id, 'share_money_id' => $moneyParentForm->id]);
  324. if (!$money_parent_sub_form) {
  325. $money_parent_sub_form = new ShareGroupMoneySub();
  326. $money_parent_sub_form->order_id = $self_purchase_log->order_id;
  327. $money_parent_sub_form->amount = $item_amount;
  328. $money_parent_sub_form->radix = $item_radix;
  329. $money_parent_sub_form->share_money_id = $moneyParentForm->id;
  330. $money_parent_sub_form->goods_name = $goods_name;
  331. $money_parent_sub_form->save();
  332. $group_data = json_decode($moneyParentForm->group_data, true) ?: [];
  333. array_push($group_data, [
  334. 'id' => $self_purchase_log->id,
  335. 'order_id' => $self_purchase_log->order_id,
  336. 'amount' => $item_amount,
  337. 'balance_amount' => $item_balance_amount,
  338. 'radix' => $item_radix,
  339. 'order_no' => $order->order_no,
  340. 'goods_name' => $goods_name
  341. ]);
  342. $moneyParentForm->group_data = json_encode($group_data, JSON_UNESCAPED_UNICODE);
  343. $moneyParentForm->radix = bcadd($moneyParentForm->radix, $item_radix, 2);
  344. $moneyParentForm->amount = bcadd($moneyParentForm->amount, $item_amount, 2);
  345. $moneyParentForm->save();
  346. }
  347. if (!intval($share_group_auto_switch)) {
  348. $moneyParentBalanceForm = ShareGroupMoney::findOne([
  349. 'type' => ShareGroupMoney::TYPE_GROUP,
  350. 'type_id' => $group_parent_purchase_log->id,
  351. 'profit_type' => ShareGroupMoney::PROFIT_TYPE_BALANCE
  352. ]);
  353. if (!$moneyParentBalanceForm) {
  354. $moneyParentBalanceForm = new ShareGroupMoney();
  355. $moneyParentBalanceForm->store_id = $self_purchase_log->store_id;
  356. $moneyParentBalanceForm->user_id = $group_parent_purchase_log->user_id;
  357. $moneyParentBalanceForm->type = ShareGroupMoney::TYPE_GROUP;
  358. $moneyParentBalanceForm->type_id = $group_parent_purchase_log->id;
  359. $moneyParentBalanceForm->amount = 0;
  360. $moneyParentBalanceForm->radix = 0;
  361. $moneyParentBalanceForm->profit = $share_group_commission_profit;
  362. $moneyParentBalanceForm->profit_type = ShareGroupMoney::PROFIT_TYPE_BALANCE;
  363. $moneyParentBalanceForm->group_data = '';
  364. $moneyParentBalanceForm->save();
  365. }
  366. $money_parent_sub_balance_form = ShareGroupMoneySub::findOne(['order_id' => $self_purchase_log->order_id, 'share_money_id' => $moneyParentBalanceForm->id]);
  367. if (!$money_parent_sub_balance_form) {
  368. $money_parent_sub_balance_form = new ShareGroupMoneySub();
  369. $money_parent_sub_balance_form->order_id = $self_purchase_log->order_id;
  370. $money_parent_sub_balance_form->amount = $item_amount;
  371. $money_parent_sub_balance_form->radix = $item_radix;
  372. $money_parent_sub_balance_form->share_money_id = $moneyParentBalanceForm->id;
  373. $money_parent_sub_balance_form->goods_name = $goods_name;
  374. $money_parent_sub_balance_form->save();
  375. $group_data = json_decode($moneyParentBalanceForm->group_data, true) ?: [];
  376. array_push($group_data, [
  377. 'id' => $self_purchase_log->id,
  378. 'order_id' => $self_purchase_log->order_id,
  379. 'amount' => $item_amount,
  380. 'balance_amount' => $item_balance_amount,
  381. 'radix' => $item_radix,
  382. 'order_no' => $order->order_no,
  383. 'goods_name' => $goods_name
  384. ]);
  385. $moneyParentBalanceForm->group_data = json_encode($group_data, JSON_UNESCAPED_UNICODE);
  386. $moneyParentBalanceForm->radix = bcadd($moneyParentBalanceForm->radix, $item_radix, 2);
  387. $moneyParentBalanceForm->amount = bcadd($moneyParentBalanceForm->amount, $item_balance_amount, 2);
  388. $moneyParentBalanceForm->save();
  389. }
  390. }
  391. }
  392. }
  393. return [
  394. 'code' => 0,
  395. 'msg' => 'success'
  396. ];
  397. } catch (\Exception $e) {
  398. return [
  399. 'code' => 1,
  400. 'msg' => $e->getMessage()
  401. ];
  402. }
  403. }
  404. /**
  405. * 添加拼购区数据
  406. */
  407. //如果是一层(最高层):查询不到上级以及自己位置--应该要先查询自己(查询最新位置)防止下级先入队 上级后入队 造成入队混乱问题
  408. //查询自己或者上级时候需要判断一下当前队列的位置 查看当前行是否满员:points == 2 ** child_level
  409. //如果满员则查询下一层未满员的
  410. //新增订单下级
  411. public static function addOrder($user_id, $order_id, $is_auto_repeat = 0) {
  412. try {
  413. $child_user = User::findOne(['id' => $user_id, 'is_delete' => 0]);
  414. if (empty($child_user)) {
  415. throw new \Exception('用户不存在');
  416. }
  417. $purchaseLog = self::findOne(['order_id' => $order_id]);
  418. if ($purchaseLog) {
  419. throw new \Exception('订单已经添加过 请勿重复添加');
  420. }
  421. $order_detail = OrderDetail::find()->where(['order_id' => $order_id])->select('id, goods_id, total_price, goods_name')
  422. ->asArray()->all();
  423. $share_group_setting = Option::get('share_group_setting', $child_user->store_id, 'share_group')['value'];
  424. $share_group_setting = json_decode($share_group_setting ?? '', true);
  425. if (empty($share_group_setting)) {
  426. throw new \Exception('未配置佣金设置');
  427. }
  428. $goods_ids = explode(',', $share_group_setting['goods_ids'] ?? '');
  429. $open = false;
  430. $order_detail_id = [];
  431. $goods_name = '';
  432. foreach ($order_detail as $goods_item) {
  433. if (in_array($goods_item['goods_id'], $goods_ids)) {
  434. $open = true;
  435. array_push($order_detail_id, $goods_item['id']);
  436. $goods_name .= $goods_item['goods_name'] . ';';
  437. }
  438. }
  439. $order_detail_ids = implode(',', $order_detail_id);
  440. if (!$open) {
  441. throw new \Exception('下单商品未在拼购商品列表内');
  442. }
  443. $order = Order::find()->where(['id' => $order_id, 'is_delete' => 0, 'user_id' => $user_id])->andWhere(['OR', ['is_pay' => 1], [
  444. 'pay_type' => Order::PAY_TYPE_COD
  445. ]])->asArray()->one();
  446. if (empty($order)) {
  447. throw new \Exception('订单不存在或未支付');
  448. }
  449. $purchaseParentLog = self::find()->where(['user_id' => $child_user->id])->orderBy('child_level DESC ,points DESC')->asArray()->one();
  450. if (!$purchaseParentLog) {
  451. $is_user_support = 0;
  452. $old_parent_id = $child_user->old_parent_id;
  453. //查询扶持区 如果扶持区有当前下单人员 就查询扶持区的上级是否在拼购区数据表中然后按照剩余逻辑继续添加成拼购区
  454. $userSupportLog = ShareGroupSupportLog::findOne(['user_id' => $child_user->id]);
  455. if ($userSupportLog) {
  456. $shareGroupPurchaseUser = ShareGroupPurchaseUser::findOne(['user_id' => $child_user->id]);
  457. if ($shareGroupPurchaseUser) {
  458. $old_parent_id = $shareGroupPurchaseUser->parent_user_id;
  459. }
  460. $is_user_support = 1;
  461. }
  462. $parentPurchaseParentLog = self::find()->where(['user_id' => $old_parent_id])->orderBy('child_level DESC ,points DESC')->asArray()->one();
  463. if (!$parentPurchaseParentLog) {
  464. //第0层 数量1
  465. $addPurchaseLog = new self();
  466. $addPurchaseLog->points = 1;
  467. $addPurchaseLog->child_level = 0;
  468. $addPurchaseLog->user_id = $child_user->id;
  469. $addPurchaseLog->parent_user_id = $old_parent_id;
  470. $addPurchaseLog->store_id = $child_user->store_id;
  471. $addPurchaseLog->parent_id = 0;
  472. $addPurchaseLog->order_id = $order_id;
  473. $addPurchaseLog->order_detail_id = $order_detail_ids;
  474. $addPurchaseLog->is_auto_repeat = $is_auto_repeat;
  475. $addPurchaseLog->goods_name = $goods_name;
  476. $addPurchaseLog->save();
  477. } else {
  478. $purchaseParentLog['batch_id'] = $parentPurchaseParentLog['batch_id'] ?: $parentPurchaseParentLog['id'];
  479. //查询上级是否已经有两个下级 也就是当前人员的同级参加拼购活动 已经有两个同级参加活动需要给当前人员判定为扶持区人员
  480. $isSupportUser = 0;
  481. if (!$is_user_support) {
  482. // $isSupportUser = self::find()->where(['parent_user_id' => $old_parent_id, 'batch_id' => $purchaseParentLog])
  483. // ->groupBy('user_id')
  484. // ->orderBy('child_level DESC ,points DESC')->count();
  485. //
  486. // //判断当前上级是否是扶持区的 查询扶持区是不是也有下级 如果有则和拼购区下级加起来 判断是否超过两个人
  487. // $shareGroupSupportParentLog = ShareGroupSupportLog::findOne(['user_id' => $old_parent_id]);
  488. // if ($shareGroupSupportParentLog) {
  489. // $shareGroupSupportChildLog = ShareGroupSupportLog::find()->where(['parent_id' => $shareGroupSupportParentLog->id])
  490. // ->select('id')->count();
  491. //
  492. // $isSupportUser += $shareGroupSupportChildLog;
  493. // }
  494. $isSupportUser = ShareGroupPurchaseUser::find()->where(['parent_user_id' => $old_parent_id])->count();
  495. }
  496. if ($isSupportUser >= 2) {
  497. //寻找上级首次出现的位置 然后获取到上级的batch_id
  498. $parentPurchaseParentFirstLog = self::find()->where(['user_id' => $old_parent_id])
  499. ->orderBy('child_level ASC ,points ASC')->asArray()->one();
  500. $batch_id = $parentPurchaseParentFirstLog['id'];
  501. //进扶持区
  502. $result = ShareGroupSupportLog::addHuman($child_user->id, $order_id, $batch_id);
  503. if (!$result['code']) {
  504. $supportLog = $result['data'];
  505. if (!intval($supportLog->parent_id)) {
  506. //如果扶持区没有上级 也就是自己是最高级 就在拼购区增加一个最高级的树
  507. $addPurchaseLog = new self();
  508. $addPurchaseLog->points = 1;
  509. $addPurchaseLog->child_level = 0;
  510. $addPurchaseLog->user_id = $child_user->id;
  511. $addPurchaseLog->parent_user_id = $old_parent_id;
  512. $addPurchaseLog->store_id = $child_user->store_id;
  513. $addPurchaseLog->parent_id = 0;
  514. $addPurchaseLog->order_id = $order_id;
  515. $addPurchaseLog->order_detail_id = $order_detail_ids;
  516. $addPurchaseLog->goods_name = $goods_name;
  517. $addPurchaseLog->save();
  518. $result = ShareGroupMoney::addMoneyLog(ShareGroupMoney::TYPE_DIRECT, $addPurchaseLog->id, $addPurchaseLog->store_id, $addPurchaseLog->order_id);
  519. if ($result['code']) {
  520. // echo json_encode($result);
  521. debug_log(['进入扶持区添加记录' => $result], 'share_group_purchase_log.log');
  522. }
  523. debug_log(['进入扶持区添加记录-com' => $result], 'share_group_purchase_log.log');
  524. } else {
  525. //获取扶持区最高级的用户 查询后拿到最高用户处于哪条batch_id上的 然后再将该用户添加到对应batch_id的拼购数据表 再修改当前用户的上级用户
  526. $topSupportLog = ShareGroupSupportLog::findOne(['purchase_batch_id' => $batch_id]);
  527. $supportUserParent = self::findOne(['user_id' => $topSupportLog->user_id]);
  528. $purchaseParentLog['batch_id'] = $supportUserParent->batch_id ?: $supportUserParent->id;
  529. goto add_support_user_to_purchase;
  530. }
  531. }
  532. return $result;
  533. }
  534. }
  535. }
  536. add_support_user_to_purchase:
  537. //如果存在上级下单或者当前人员下单
  538. if (empty($addPurchaseLog)) {
  539. //查询到当前下单人是这个树的
  540. $batch_id = $purchaseParentLog['batch_id'] ?: $purchaseParentLog['id'];
  541. //查询未满的层
  542. $child_level_data = self::find()->where([
  543. 'batch_id' => $batch_id
  544. ])->andWhere(['>=', 'points', new \yii\db\Expression('POWER(2, child_level)')])
  545. ->orderBy('child_level DESC')->asArray()->one();
  546. $addPurchaseLog = new self();
  547. if ($child_level_data) {
  548. //如果存在已满的层 就查询正序排列的比当前最大层数大的层 拿到最大的点位
  549. $child_level_data_ = self::find()->where([
  550. 'batch_id' => $batch_id
  551. ])->andWhere(['>', 'child_level', $child_level_data['child_level']])
  552. ->orderBy('child_level ASC, points DESC')->asArray()->one();
  553. $addPurchaseLog->points = ($child_level_data_['points'] + 1);
  554. $addPurchaseLog->child_level = ($child_level_data['child_level'] + 1);
  555. } else {
  556. //如果不存在已满的层 就查询最大的点位 应该不会走到这里
  557. $child_level_data_ = self::find()->where([
  558. 'batch_id' => $batch_id
  559. ])->orderBy('points DESC')->asArray()->one();
  560. $addPurchaseLog->points = (($child_level_data_['points'] ?? 0) + 1);
  561. $addPurchaseLog->child_level = $child_level_data_['child_level'] ?? 1;
  562. }
  563. $parent_data = [];
  564. if ($addPurchaseLog->child_level - 1 >= 0) {
  565. $parent_data = self::find()->where([
  566. 'points' => ceil($addPurchaseLog->points / 2),
  567. 'child_level' => $addPurchaseLog->child_level - 1
  568. ])->andWhere(['OR', ['batch_id' => $batch_id], ['id' => $batch_id]])->orderBy('points DESC')->asArray()->one();
  569. }
  570. if ($purchaseParentLog['id']) {
  571. $addPurchaseLog->is_repeat = 1;
  572. }
  573. $addPurchaseLog->is_auto_repeat = $is_auto_repeat;
  574. $addPurchaseLog->store_id = $child_user->store_id;
  575. $addPurchaseLog->user_id = $child_user->id;
  576. $addPurchaseLog->parent_user_id = $child_user->old_parent_id;
  577. $addPurchaseLog->parent_id = $parent_data['id'] ?? 0;
  578. $addPurchaseLog->batch_id = $batch_id;
  579. $addPurchaseLog->order_id = $order_id;
  580. $addPurchaseLog->order_detail_id = $order_detail_ids;
  581. $addPurchaseLog->goods_name = $goods_name;
  582. if (!$addPurchaseLog->save()) {
  583. throw new \Exception('添加记录失败');
  584. };
  585. //添加完拼购应该会有团长用户记录
  586. $userSupportLog = ShareGroupSupportLog::findOne(['user_id' => $child_user->id]);
  587. if ($userSupportLog && intval($userSupportLog->parent_id) === -1) {
  588. $userSupportLog->parent_id = -2;
  589. $userSupportLog->save();
  590. //修改帮扶区的当前用户的上级用户ID
  591. $newParentLog = ShareGroupPurchaseParentLog::findOne($parent_data['id']);
  592. $shareGroupPurchaseUser = ShareGroupPurchaseUser::findOne(['user_id' => $child_user->id]);
  593. $shareGroupPurchaseUser->parent_user_id = $newParentLog->user_id;
  594. $shareGroupPurchaseUser->save();
  595. }
  596. $result = ShareGroupMoney::addMoneyLog(ShareGroupMoney::TYPE_DIRECT, $addPurchaseLog->id, $addPurchaseLog->store_id, $addPurchaseLog->order_id);
  597. if ($result['code']) {
  598. debug_log(['进入拼购区添加记录' => $result], 'share_group_purchase_log.log');
  599. // echo json_encode($result);
  600. }
  601. debug_log(['开始帮扶区' => $child_user->id], 'share_group_purchase_log.log');
  602. //如果存在帮扶区数据就添加帮扶区记录
  603. if ($userSupportLog) {
  604. debug_log(['id' => $userSupportLog->id, 'store_id' => $userSupportLog->store_id, 'order_id' => $addPurchaseLog->order_id], 'share_group_purchase_log.log');
  605. $result = ShareGroupMoney::addMoneyLog(ShareGroupMoney::TYPE_SUPPORT, $userSupportLog->id, $userSupportLog->store_id, $addPurchaseLog->order_id);
  606. debug_log(['帮扶区结果' => $result], 'share_group_purchase_log.log');
  607. if ($result['code']) {
  608. return $result;
  609. }
  610. }
  611. }
  612. return [
  613. 'code' => 0,
  614. 'msg' => '添加成功'
  615. ];
  616. } catch (\Exception $e) {
  617. return [
  618. 'code' => 1,
  619. 'msg' => $e->getMessage()
  620. ];
  621. }
  622. }
  623. //检测是否满足解冻条件
  624. public static function checkUnfreeze($user_id, $batch_id)
  625. {
  626. $direct_group_purchase_child = self::find()->where(['parent_user_id' => $user_id, 'batch_id' => $batch_id])->groupBy('user_id')
  627. ->select('user_id')->column();
  628. if ($direct_group_purchase_child && count($direct_group_purchase_child) >= 2) {
  629. $group_purchase_child = self::find()->where(['parent_user_id' => $direct_group_purchase_child, 'batch_id' => $batch_id])->groupBy('user_id')
  630. ->select('user_id')->column();
  631. if ($group_purchase_child && count($group_purchase_child) >= 4) {
  632. return true;
  633. }
  634. }
  635. return false;
  636. }
  637. }