LocalPublicRankingTask.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2025 赤店商城 All rights reserved.
  6. */
  7. namespace app\models;
  8. use Yii;
  9. use yii\behaviors\TimestampBehavior;
  10. use yii\db\ActiveRecord;
  11. use app\modules\admin\models\localPublicRanking\LocalPublicRankingForm;
  12. class LocalPublicRankingTask extends \yii\db\ActiveRecord
  13. {
  14. /**
  15. * @inheritdoc
  16. */
  17. public static function tableName()
  18. {
  19. return '{{%local_public_ranking_task}}';
  20. }
  21. public function behaviors()
  22. {
  23. return [
  24. [
  25. // 自动更新创建和更新时间
  26. 'class' => TimestampBehavior::class,
  27. ]
  28. ];
  29. }
  30. public function afterSave($insert, $changedAttributes)
  31. {
  32. parent::afterSave($insert, $changedAttributes);
  33. LocalPublicRankingForm::afterTaskSave($this, $insert, $changedAttributes);
  34. }
  35. public static function doTask($store_id, $user_id, $order, $goods_id) {
  36. $conf = LocalPublicRankingForm::conf($store_id);
  37. $t = \Yii::$app->db->beginTransaction();
  38. try {
  39. $firstPriceParent = $conf['firstPriceParent'];
  40. $hasPru = LocalPublicRankingUser::findOne(['user_id' => $user_id]);
  41. $isFirstPrice = $hasPru ? 0 : 1;
  42. $initUser = LocalPublicRankingUser::initUser($store_id, $user_id, $order);
  43. if($initUser['code']){
  44. throw new \Exception('加入公排错误:' . $initUser['msg']);
  45. }
  46. $pru = $initUser['data'];
  47. LocalPublicRankingUser::upStart($user_id);
  48. $pru->refresh();
  49. $has = self::findOne(['store_id' => $store_id, 'order_id' => (int)$order['id']]);
  50. if($has){
  51. debug_log([__LINE__, '订单已经记录过了', $store_id, $order['id']], __CLASS__);
  52. throw new \Exception('订单已经记录过了:' . $has['id']);
  53. }
  54. //找上级
  55. $findStar = $pru->star;
  56. if($firstPriceParent == 1){
  57. $findStar -= 1;
  58. }
  59. $sorts = LocalPublicRankingUser::getParentSorts($pru['sort'], $conf['PublicRankingRow'], $findStar);
  60. $sort = (int)$sorts[$findStar - 1];
  61. $parent_user_id = 0;
  62. if($sort){
  63. $parent_pru = LocalPublicRankingUser::findOne(['store_id' => $pru['store_id'], 'sort' => $sort]);
  64. if($parent_pru){
  65. $parent_user_id = $parent_pru['user_id'];
  66. if($parent_pru->star < $findStar){
  67. debug_log([__LINE__, '上级星级条件不满足', $parent_pru->star, $findStar, $parent_pru['user_id'], $user_id, $order['id']], __CLASS__);
  68. $parent_user_id = 0;
  69. }
  70. if($parent_pru['is_fugou']){
  71. debug_log([__LINE__, '上级为强制复购状态', $parent_pru->is_fugou, $parent_pru['user_id'], $user_id, $order['id']], __CLASS__);
  72. $parent_user_id = 0;
  73. }
  74. if(!LocalPublicRankingForm::isAutoPriceSuccess($store_id, $parent_pru['user_id'])){
  75. debug_log([__LINE__, '上级躺赚条件不满足', $parent_pru['user_id'], $user_id, $order['id']], __CLASS__);
  76. $parent_user_id = 0;
  77. }
  78. }
  79. }
  80. if($firstPriceParent == 1 && $isFirstPrice){
  81. $user = User::findOne($user_id);
  82. $parent_user_id = $user->old_parent_id;
  83. $parent_user_type = 1;
  84. debug_log([__LINE__, '首单奖励直推人', $findStar, $pru->star, $user_id, $order['id']], __CLASS__);
  85. }
  86. $actGoods = LocalPublicRankingForm::actGoods($goods_id);
  87. $commission_amount = $actGoods['commission_amount'] ? $actGoods['commission_amount'] : 0;
  88. //记录订单
  89. $task = new self();
  90. $task->store_id = $store_id;
  91. $task->user_id = $user_id;
  92. $parent_user_type && $task->parent_user_type = $parent_user_type;
  93. $task->parent_user_id = $parent_user_id;
  94. $task->commission_amount = $commission_amount;
  95. $task->order_id = (int)$order['id'];
  96. $task->status = 1;
  97. if(!$task->save()){
  98. debug_log([__LINE__, $task->getErrors()], __CLASS__);
  99. throw new \Exception(array_shift($task->getFirstErrors()));
  100. }
  101. if($parent_user_id){
  102. //佣金
  103. if($commission_amount > 0){
  104. $parent_user = User::findOne($parent_user_id);
  105. $sendPrice = $parent_user->updateCounters(['total_price' => $commission_amount, 'price' => $commission_amount]);
  106. if(!$sendPrice){
  107. debug_log([__LINE__, '佣金写入失败', $parent_user_id, $commission_amount], __CLASS__);
  108. throw new \Exception('佣金写入失败');
  109. }
  110. UserShareMoney::set($commission_amount, $parent_user_id, (int)$order['id'], 0, UserShareMoney::SOURCE_PUBLIC_RANK, $store_id, 0, '公排下级下单' . $order['order_no']);
  111. }
  112. }
  113. //复购完成
  114. $pru->refresh();
  115. if($pru){
  116. if($pru->is_fugou){
  117. if(LocalPublicRankingForm::isFugouFinish($store_id, $user_id)){
  118. $pru->is_fugou = 0;
  119. if(!$pru->save()){
  120. debug_log([__LINE__, $pru->getErrors()], __CLASS__);
  121. throw new \Exception(array_shift($pru->getFirstErrors()));
  122. }
  123. }
  124. }
  125. }
  126. $t->commit();
  127. } catch (\Exception $e) {
  128. $t->rollBack();
  129. \Yii::error($e);
  130. debug_log([__METHOD__, __LINE__, $store_id, $user_id, $e->getMessage()], __CLASS__ . '.log');
  131. return [
  132. 'code' => 1,
  133. 'msg' => $e->getMessage(),
  134. ];
  135. }
  136. return [
  137. 'code' => 1,
  138. 'data' => $task,
  139. ];
  140. }
  141. public static function priceTotal($store_id = 0, $parent_user_id = '', $has_fugou = null) {
  142. $query = self::find()->where(['store_id' => $store_id, 'parent_user_id' => $parent_user_id, 'status' => 1]);
  143. if($has_fugou !== null){
  144. $query->andWhere(['has_fugou' => $has_fugou]);
  145. }
  146. $r = $query->sum('commission_amount');
  147. // var_dump($query->createCommand()->getRawSql());
  148. return (float)$r;
  149. }
  150. public static function payTotal($store_id = 0, $user_id = '', $has_fugou = null) {
  151. $query = self::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'status' => 1]);
  152. if($has_fugou !== null){
  153. $query->andWhere(['has_fugou' => $has_fugou]);
  154. }
  155. $r = Order::find()->where(['id' => $query->select('order_id')])->sum('pay_price');
  156. // var_dump($query->createCommand()->getRawSql());
  157. return (float)$r;
  158. }
  159. }