| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2025 赤店商城 All rights reserved.
- */
- namespace app\models;
- use Yii;
- use yii\behaviors\TimestampBehavior;
- use yii\db\ActiveRecord;
- use app\modules\admin\models\publicRanking\PublicRankingForm;
- class PublicRankingTask extends \yii\db\ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%public_ranking_task}}';
- }
- public function behaviors()
- {
- return [
- [
- // 自动更新创建和更新时间
- 'class' => TimestampBehavior::class,
- ]
- ];
- }
- public static function getTask($store_id, $user_id) {
- // $user_id = 13902;
- $task = self::findOne(['user_id' => $user_id]);
- if($task){
- $task = self::findAll(['user_id' => $user_id, 'status' => 0]);
- return [
- 'code' => 0,
- 'data' => $task,
- ];
- }
- $pru = PublicRankingUser::findOne(['user_id' => $user_id]);
- if(!$pru){
- return [
- 'code' => 1,
- 'msg' => '未加入公排,任务列表未生成',
- ];
- }
- return self::_genTask($store_id, $user_id);
- }
- public static function genTaskFugou($store_id, $user_id) {
- return self::_genTask($store_id, $user_id);
- }
- public static function _genTask($store_id, $user_id) {
- $pru = PublicRankingUser::findOne(['user_id' => $user_id]);
- $conf = PublicRankingForm::conf($store_id);
- $task_count = $conf['task_count'];
- $sorts = PublicRankingUser::getParentSorts($pru['sort'], $conf['PublicRankingRow'], $task_count);
- $promoterIds = [];
- $i = 0;
- while($i++ < $task_count){
- $sort = $sorts[$i-1];
- $promoterId = 0;
- if($sort){
- $ipru = PublicRankingUser::findOne(['store_id' => $pru['store_id'], 'sort' => $sort]);
- $promoter = Promoter::findOne(['user_id' => $ipru['user_id']]);
- if($promoter && $promoter['promoter_id'] && $promoter['is_fugou'] == 0 && PublicRankingForm::isAutoPriceSuccess($store_id, $ipru['user_id'])){
- $orderCount = (int)self::find()->where(['user_id' => $promoter['user_id'], 'status' => 1])->count();
- if($orderCount >= $i){
- $promoterId = $promoter['id'];
- }
- }
- }
- $promoterIds[] = $promoterId;
- }
- $rpromoterIds = $promoterIds;
- // var_dump($promoterId, $rpromoterIds);die;
- $t = \Yii::$app->db->beginTransaction();
- try {
- foreach($rpromoterIds as $item){
- $PublicRankingTask = new self();
- $PublicRankingTask->store_id = $store_id;
- $PublicRankingTask->user_id = $user_id;
- $PublicRankingTask->promoter_id = $item;
- if(!$PublicRankingTask->save()){
- debug_log([__LINE__, $PublicRankingTask->getErrors()], __CLASS__);
- throw new \Exception(array_shift($PublicRankingTask->getFirstErrors()));
- }
- }
- $t->commit();
- } catch (\Exception $e) {
- $t->rollBack();
- \Yii::error($e);
- debug_log([__METHOD__, __LINE__, $store_id, $user_id, $e->getMessage()], __CLASS__ . '.log');
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- return [
- 'code' => 0,
- 'data' => self::findAll(['user_id' => $user_id]),
- ];
- }
- public static function doTask($store_id, $user_id, $promoter_id, $order) {
- $conf = PublicRankingForm::conf($store_id);
- $pru = PublicRankingUser::findOne(['user_id' => $user_id]);
- if(!$pru){
- if($promoter_id == $conf['promoter_id']){
- $initUser = PublicRankingUser::initUser($store_id, $user_id, $order);
- if($initUser['code']){
- throw new \Exception('加入公排错误:' . $initUser['msg']);
- }
- debug_log([__LINE__, '首单加入公排成功', $store_id, $user_id, $order['id']], __CLASS__);
- return null;
- }else{
- debug_log([__LINE__, '带货ID非系统值,不能加入公排', $store_id, $user_id, $order['id'], $promoter_id, $conf['promoter_id']], __CLASS__);
- throw new \Exception('带货ID非系统值,不能加入公排,此带货订单' . $order['id'] . '不记录。:' . $promoter_id . ' != ' . $conf['promoter_id']);
- }
- }
- $has = self::findOne(['store_id' => $store_id, 'order_id' => (int)$order['id']]);
- if($has){
- return null;
- }
- if(($promoter_id == $conf['promoter_id'])){
- $prom_id = [0];
- }
- $promoter = Promoter::findOne(['store_id' => $store_id, 'promoter_id' => $promoter_id]);
- $promoter && $prom_id[] = $promoter['id'];
- $task = self::findOne(['store_id' => $store_id, 'user_id' => $user_id, 'status' => 0, 'promoter_id' => $prom_id]);
- if($task){
- $task->order_id = (int)$order['id'];
- $task->status = 1;
- if(!$task->save()){
- debug_log([__LINE__, $task->getErrors()], __CLASS__);
- throw new \Exception(array_shift($task->getFirstErrors()));
- }
-
- $promoterUser = Promoter::findOne(['store_id' => $store_id, 'user_id' => $user_id]);
- if($promoterUser){
- //复购完成
- if($promoterUser->is_fugou){
- if(self::taskIsOver($store_id, $user_id)){
- $promoterUser->is_fugou = 0;
- if(!$promoterUser->save()){
- debug_log([__LINE__, $promoterUser->getErrors()], __CLASS__);
- throw new \Exception(array_shift($promoterUser->getFirstErrors()));
- }
- }
- }
- }
- }
- return $task;
- }
- public static function taskIsOver($store_id, $user_id) {
- $task = self::findOne(['store_id' => $store_id, 'user_id' => $user_id]);
- if($task){
- $task0 = self::findOne(['store_id' => $store_id, 'user_id' => $user_id, 'status' => 0]);
- if(!$task0){
- return true;
- }
- }
- return false;
- }
- }
|