| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?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\localPublicRanking\LocalPublicRankingForm;
- class LocalPublicRankingTask extends \yii\db\ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%local_public_ranking_task}}';
- }
- public function behaviors()
- {
- return [
- [
- // 自动更新创建和更新时间
- 'class' => TimestampBehavior::class,
- ]
- ];
- }
- public function afterSave($insert, $changedAttributes)
- {
- parent::afterSave($insert, $changedAttributes);
- LocalPublicRankingForm::afterTaskSave($this, $insert, $changedAttributes);
- }
- public static function doTask($store_id, $user_id, $order, $goods_id) {
- $conf = LocalPublicRankingForm::conf($store_id);
- $t = \Yii::$app->db->beginTransaction();
- try {
- $firstPriceParent = $conf['firstPriceParent'];
- $hasPru = LocalPublicRankingUser::findOne(['user_id' => $user_id]);
- $isFirstPrice = $hasPru ? 0 : 1;
- $initUser = LocalPublicRankingUser::initUser($store_id, $user_id, $order);
- if($initUser['code']){
- throw new \Exception('加入公排错误:' . $initUser['msg']);
- }
- $pru = $initUser['data'];
- LocalPublicRankingUser::upStart($user_id);
- $pru->refresh();
-
- $has = self::findOne(['store_id' => $store_id, 'order_id' => (int)$order['id']]);
- if($has){
- debug_log([__LINE__, '订单已经记录过了', $store_id, $order['id']], __CLASS__);
- throw new \Exception('订单已经记录过了:' . $has['id']);
- }
- //找上级
- $findStar = $pru->star;
- if($firstPriceParent == 1){
- $findStar -= 1;
- }
- $sorts = LocalPublicRankingUser::getParentSorts($pru['sort'], $conf['PublicRankingRow'], $findStar);
- $sort = (int)$sorts[$findStar - 1];
- $parent_user_id = 0;
- if($sort){
- $parent_pru = LocalPublicRankingUser::findOne(['store_id' => $pru['store_id'], 'sort' => $sort]);
- if($parent_pru){
- $parent_user_id = $parent_pru['user_id'];
- if($parent_pru->star < $findStar){
- debug_log([__LINE__, '上级星级条件不满足', $parent_pru->star, $findStar, $parent_pru['user_id'], $user_id, $order['id']], __CLASS__);
- $parent_user_id = 0;
- }
- if($parent_pru['is_fugou']){
- debug_log([__LINE__, '上级为强制复购状态', $parent_pru->is_fugou, $parent_pru['user_id'], $user_id, $order['id']], __CLASS__);
- $parent_user_id = 0;
- }
- if(!LocalPublicRankingForm::isAutoPriceSuccess($store_id, $parent_pru['user_id'])){
- debug_log([__LINE__, '上级躺赚条件不满足', $parent_pru['user_id'], $user_id, $order['id']], __CLASS__);
- $parent_user_id = 0;
- }
- }
- }
- if($firstPriceParent == 1 && $isFirstPrice){
- $user = User::findOne($user_id);
- $parent_user_id = $user->old_parent_id;
- $parent_user_type = 1;
- debug_log([__LINE__, '首单奖励直推人', $findStar, $pru->star, $user_id, $order['id']], __CLASS__);
- }
- $actGoods = LocalPublicRankingForm::actGoods($goods_id);
- $commission_amount = $actGoods['commission_amount'] ? $actGoods['commission_amount'] : 0;
- //记录订单
- $task = new self();
- $task->store_id = $store_id;
- $task->user_id = $user_id;
- $parent_user_type && $task->parent_user_type = $parent_user_type;
- $task->parent_user_id = $parent_user_id;
- $task->commission_amount = $commission_amount;
- $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()));
- }
- if($parent_user_id){
- //佣金
- if($commission_amount > 0){
- $parent_user = User::findOne($parent_user_id);
- $sendPrice = $parent_user->updateCounters(['total_price' => $commission_amount, 'price' => $commission_amount]);
- if(!$sendPrice){
- debug_log([__LINE__, '佣金写入失败', $parent_user_id, $commission_amount], __CLASS__);
- throw new \Exception('佣金写入失败');
- }
- UserShareMoney::set($commission_amount, $parent_user_id, (int)$order['id'], 0, UserShareMoney::SOURCE_PUBLIC_RANK, $store_id, 0, '公排下级下单' . $order['order_no']);
- }
- }
- //复购完成
- $pru->refresh();
- if($pru){
- if($pru->is_fugou){
- if(LocalPublicRankingForm::isFugouFinish($store_id, $user_id)){
- $pru->is_fugou = 0;
- if(!$pru->save()){
- debug_log([__LINE__, $pru->getErrors()], __CLASS__);
- throw new \Exception(array_shift($pru->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' => 1,
- 'data' => $task,
- ];
- }
-
- public static function priceTotal($store_id = 0, $parent_user_id = '', $has_fugou = null) {
- $query = self::find()->where(['store_id' => $store_id, 'parent_user_id' => $parent_user_id, 'status' => 1]);
- if($has_fugou !== null){
- $query->andWhere(['has_fugou' => $has_fugou]);
- }
- $r = $query->sum('commission_amount');
- // var_dump($query->createCommand()->getRawSql());
- return (float)$r;
- }
-
- public static function payTotal($store_id = 0, $user_id = '', $has_fugou = null) {
- $query = self::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'status' => 1]);
- if($has_fugou !== null){
- $query->andWhere(['has_fugou' => $has_fugou]);
- }
- $r = Order::find()->where(['id' => $query->select('order_id')])->sum('pay_price');
- // var_dump($query->createCommand()->getRawSql());
- return (float)$r;
- }
- }
|