PublicRankingTask.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\publicRanking\PublicRankingForm;
  12. class PublicRankingTask extends \yii\db\ActiveRecord
  13. {
  14. /**
  15. * @inheritdoc
  16. */
  17. public static function tableName()
  18. {
  19. return '{{%public_ranking_task}}';
  20. }
  21. public function behaviors()
  22. {
  23. return [
  24. [
  25. // 自动更新创建和更新时间
  26. 'class' => TimestampBehavior::class,
  27. ]
  28. ];
  29. }
  30. public static function getTask($store_id, $user_id) {
  31. // $user_id = 13902;
  32. $task = self::findOne(['user_id' => $user_id]);
  33. if($task){
  34. $task = self::findAll(['user_id' => $user_id, 'status' => 0]);
  35. return [
  36. 'code' => 0,
  37. 'data' => $task,
  38. ];
  39. }
  40. $pru = PublicRankingUser::findOne(['user_id' => $user_id]);
  41. if(!$pru){
  42. return [
  43. 'code' => 1,
  44. 'msg' => '未加入公排,任务列表未生成',
  45. ];
  46. }
  47. return self::_genTask($store_id, $user_id);
  48. }
  49. public static function genTaskFugou($store_id, $user_id) {
  50. return self::_genTask($store_id, $user_id);
  51. }
  52. public static function _genTask($store_id, $user_id) {
  53. $pru = PublicRankingUser::findOne(['user_id' => $user_id]);
  54. $conf = PublicRankingForm::conf($store_id);
  55. $task_count = $conf['task_count'];
  56. $sorts = PublicRankingUser::getParentSorts($pru['sort'], $conf['PublicRankingRow'], $task_count);
  57. $promoterIds = [];
  58. $i = 0;
  59. while($i++ < $task_count){
  60. $sort = $sorts[$i-1];
  61. $promoterId = 0;
  62. if($sort){
  63. $ipru = PublicRankingUser::findOne(['store_id' => $pru['store_id'], 'sort' => $sort]);
  64. $promoter = Promoter::findOne(['user_id' => $ipru['user_id']]);
  65. if($promoter && $promoter['promoter_id'] && $promoter['is_fugou'] == 0 && PublicRankingForm::isAutoPriceSuccess($store_id, $ipru['user_id'])){
  66. $orderCount = (int)self::find()->where(['user_id' => $promoter['user_id'], 'status' => 1])->count();
  67. if($orderCount >= $i){
  68. $promoterId = $promoter['id'];
  69. }
  70. }
  71. }
  72. $promoterIds[] = $promoterId;
  73. }
  74. $rpromoterIds = $promoterIds;
  75. // var_dump($promoterId, $rpromoterIds);die;
  76. $t = \Yii::$app->db->beginTransaction();
  77. try {
  78. foreach($rpromoterIds as $item){
  79. $PublicRankingTask = new self();
  80. $PublicRankingTask->store_id = $store_id;
  81. $PublicRankingTask->user_id = $user_id;
  82. $PublicRankingTask->promoter_id = $item;
  83. if(!$PublicRankingTask->save()){
  84. debug_log([__LINE__, $PublicRankingTask->getErrors()], __CLASS__);
  85. throw new \Exception(array_shift($PublicRankingTask->getFirstErrors()));
  86. }
  87. }
  88. $t->commit();
  89. } catch (\Exception $e) {
  90. $t->rollBack();
  91. \Yii::error($e);
  92. debug_log([__METHOD__, __LINE__, $store_id, $user_id, $e->getMessage()], __CLASS__ . '.log');
  93. return [
  94. 'code' => 1,
  95. 'msg' => $e->getMessage(),
  96. ];
  97. }
  98. return [
  99. 'code' => 0,
  100. 'data' => self::findAll(['user_id' => $user_id]),
  101. ];
  102. }
  103. public static function doTask($store_id, $user_id, $promoter_id, $order) {
  104. $conf = PublicRankingForm::conf($store_id);
  105. $pru = PublicRankingUser::findOne(['user_id' => $user_id]);
  106. if(!$pru){
  107. if($promoter_id == $conf['promoter_id']){
  108. $initUser = PublicRankingUser::initUser($store_id, $user_id, $order);
  109. if($initUser['code']){
  110. throw new \Exception('加入公排错误:' . $initUser['msg']);
  111. }
  112. debug_log([__LINE__, '首单加入公排成功', $store_id, $user_id, $order['id']], __CLASS__);
  113. return null;
  114. }else{
  115. debug_log([__LINE__, '带货ID非系统值,不能加入公排', $store_id, $user_id, $order['id'], $promoter_id, $conf['promoter_id']], __CLASS__);
  116. throw new \Exception('带货ID非系统值,不能加入公排,此带货订单' . $order['id'] . '不记录。:' . $promoter_id . ' != ' . $conf['promoter_id']);
  117. }
  118. }
  119. $has = self::findOne(['store_id' => $store_id, 'order_id' => (int)$order['id']]);
  120. if($has){
  121. return null;
  122. }
  123. if(($promoter_id == $conf['promoter_id'])){
  124. $prom_id = [0];
  125. }
  126. $promoter = Promoter::findOne(['store_id' => $store_id, 'promoter_id' => $promoter_id]);
  127. $promoter && $prom_id[] = $promoter['id'];
  128. $task = self::findOne(['store_id' => $store_id, 'user_id' => $user_id, 'status' => 0, 'promoter_id' => $prom_id]);
  129. if($task){
  130. $task->order_id = (int)$order['id'];
  131. $task->status = 1;
  132. if(!$task->save()){
  133. debug_log([__LINE__, $task->getErrors()], __CLASS__);
  134. throw new \Exception(array_shift($task->getFirstErrors()));
  135. }
  136. $promoterUser = Promoter::findOne(['store_id' => $store_id, 'user_id' => $user_id]);
  137. if($promoterUser){
  138. //复购完成
  139. if($promoterUser->is_fugou){
  140. if(self::taskIsOver($store_id, $user_id)){
  141. $promoterUser->is_fugou = 0;
  142. if(!$promoterUser->save()){
  143. debug_log([__LINE__, $promoterUser->getErrors()], __CLASS__);
  144. throw new \Exception(array_shift($promoterUser->getFirstErrors()));
  145. }
  146. }
  147. }
  148. }
  149. }
  150. return $task;
  151. }
  152. public static function taskIsOver($store_id, $user_id) {
  153. $task = self::findOne(['store_id' => $store_id, 'user_id' => $user_id]);
  154. if($task){
  155. $task0 = self::findOne(['store_id' => $store_id, 'user_id' => $user_id, 'status' => 0]);
  156. if(!$task0){
  157. return true;
  158. }
  159. }
  160. return false;
  161. }
  162. }