ShareGroupSupportLog.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace app\models;
  3. use yii\behaviors\TimestampBehavior;
  4. use yii\db\ActiveRecord;
  5. /**
  6. * This is the model class for table "{{%share}}".
  7. *
  8. * @property integer $id
  9. * @property integer $store_id
  10. * @property integer $user_id
  11. * @property integer $order_id
  12. * @property integer $parent_id
  13. * @property integer $parent_user_id
  14. * @property integer $child_level
  15. * @property integer $batch_id
  16. * @property integer $purchase_batch_id
  17. * @property integer $points
  18. * @property integer $created_at
  19. * @property integer $updated_at
  20. */
  21. class ShareGroupSupportLog extends \yii\db\ActiveRecord
  22. {
  23. //扶持区
  24. /**
  25. * @inheritdoc
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%share_group_support_log}}';
  30. }
  31. public function behaviors()
  32. {
  33. return [
  34. [
  35. 'class' => TimestampBehavior::class
  36. ]
  37. ];
  38. }
  39. /**
  40. * @inheritdoc
  41. */
  42. public function rules()
  43. {
  44. return [
  45. [['id', 'user_id', 'order_id', 'parent_id', 'parent_user_id', 'child_level', 'points', 'created_at', 'updated_at', 'batch_id', 'purchase_batch_id', 'store_id'], 'integer']
  46. ];
  47. }
  48. /**
  49. * @inheritdoc
  50. */
  51. public function attributeLabels()
  52. {
  53. return [
  54. 'id' => '',
  55. 'user_id' => '用户ID',
  56. 'order_id' => '入位订单ID',
  57. 'parent_id' => '上级ID',
  58. 'parent_user_id' => '用户上级ID',
  59. 'child_level' => '当前层级(一个关系树内的层级)',
  60. 'batch_id' => '同一树标识ID',
  61. 'purchase_batch_id' => '',
  62. 'points' => '同一上级下的入位顺序',
  63. 'created_at' => '',
  64. 'updated_at' => '',
  65. ];
  66. }
  67. /**
  68. * 添加扶持区数据
  69. */
  70. //如果是一层(最高层):查询不到上级以及自己位置--应该要先查询自己(查询最新位置)防止下级先入队 上级后入队 造成入队混乱问题
  71. //查询自己或者上级时候需要判断一下当前队列的位置 查看当前行是否满员:points == 2 ** child_level
  72. //如果满员则查询下一层未满员的
  73. //新增订单下级
  74. public static function addHuman($user_id, $order_id, $purchase_batch_id) {
  75. try {
  76. $child_user = User::findOne(['id' => $user_id, 'is_delete' => 0]);
  77. $purchaseLog = self::findOne(['order_id' => $order_id]);
  78. if ($purchaseLog) {
  79. throw new \Exception('订单已经添加过 请勿重复添加1');
  80. }
  81. $purchaseUserLog = self::findOne(['user_id' => $user_id]);
  82. if ($purchaseUserLog) {
  83. throw new \Exception('当前用户已经进入扶持区');
  84. }
  85. $topSupportLog = self::findOne(['purchase_batch_id' => $purchase_batch_id]);
  86. if ($topSupportLog) {//已经存在其他首个帮扶了
  87. $batch_id = $topSupportLog->id;
  88. // $child_level_data = self::find()->where(['OR', [
  89. // 'batch_id' => $batch_id
  90. // ], [
  91. // 'id' => $batch_id
  92. // ]])->andWhere(['>=', 'points', new \yii\db\Expression('POWER(2, child_level)')])
  93. // ->orderBy('child_level DESC')->asArray()->one();
  94. $addPurchaseLog = new self();
  95. // if ($child_level_data) {
  96. // //如果存在已满的层 就查询正序排列的比当前最大层数大的层 拿到最大的点位
  97. // $child_level_data_ = self::find()->where([
  98. // 'batch_id' => $batch_id
  99. // ])->andWhere(['>', 'child_level', $child_level_data['child_level']])
  100. // ->orderBy('child_level ASC, points DESC')->asArray()->one();
  101. //
  102. //
  103. // $addPurchaseLog->points = ($child_level_data_['points'] + 1);
  104. // $addPurchaseLog->child_level = ($child_level_data['child_level'] + 1);
  105. // } else {
  106. // //如果不存在已满的层 就查询最大的点位 应该不会走到这里
  107. // $child_level_data_ = self::find()->where([
  108. // 'batch_id' => $batch_id
  109. // ])->orderBy('points DESC')->asArray()->one();
  110. // $addPurchaseLog->points = (($child_level_data_['points'] ?? 0) + 1);
  111. // $addPurchaseLog->child_level = $child_level_data_['child_level'] ?? 0;
  112. // }
  113. // $parent_data = [];
  114. // if ($addPurchaseLog->child_level - 1 >= 0) {
  115. //
  116. // $parent_data = self::find()->where([
  117. // 'points' => ceil($addPurchaseLog->points / 2),
  118. // 'child_level' => $addPurchaseLog->child_level - 1
  119. // ])->andWhere(['OR', ['batch_id' => $batch_id], ['id' => $batch_id]])->orderBy('points DESC')
  120. // ->asArray()->one();
  121. // }
  122. $parent_id = -1;//已经存在其他首个帮扶的上级了
  123. $addPurchaseLog->points = 0;
  124. $addPurchaseLog->child_level = 0;
  125. } else {
  126. $batch_id = 0;
  127. $addPurchaseLog = new self();
  128. $addPurchaseLog->points = 0;
  129. $addPurchaseLog->child_level = 0;
  130. $addPurchaseLog->purchase_batch_id = $purchase_batch_id;
  131. }
  132. $addPurchaseLog->store_id = $child_user->store_id;
  133. $addPurchaseLog->user_id = $child_user->id;
  134. $addPurchaseLog->parent_user_id = $child_user->old_parent_id;
  135. $addPurchaseLog->parent_id = $parent_id ?? 0;
  136. $addPurchaseLog->batch_id = $batch_id;
  137. $addPurchaseLog->order_id = $order_id;
  138. if (!$addPurchaseLog->save()) {
  139. throw new \Exception('添加扶持失败');
  140. };
  141. $result = ShareGroupMoney::addMoneyLog(ShareGroupMoney::TYPE_SUPPORT, $addPurchaseLog->id, $addPurchaseLog->store_id, $addPurchaseLog->order_id);
  142. if ($result['code']) {
  143. return $result;
  144. }
  145. return [
  146. 'code' => 0,
  147. 'msg' => '添加成功',
  148. 'data' => $addPurchaseLog
  149. ];
  150. } catch (\Exception $e) {
  151. return [
  152. 'code' => 1,
  153. 'msg' => $e->getMessage()
  154. ];
  155. }
  156. }
  157. }