| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- <?php
- namespace addons\BossWeight\common\logic;
- use common\traits\ErrorTrait;
- use Yii;
- use Exception;
- use yii\helpers\Json;
- use common\models\mall\Mall;
- use addons\BossWeight\common\enums\BossEnum;
- use addons\BossWeight\common\models\AddonsBoss;
- use common\enums\StatusEnum;
- use addons\BossWeight\common\models\AddonsBossLevelSetting;
- use common\enums\OrderStatusEnum;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\models\user\UserPartitionRelationship;
- use addons\BossWeight\common\models\AddonsBossPrice;
- use common\enums\UpgradeEnum;
- /**
- * 股东权重状态执行
- * @Author lun
- * @DateTime 2021-11-16 19:07:32
- * @copyright: Copyright (c) 2020 广东七件事集团
- */
- class WeightRuleLogic{
- use ErrorTrait;
- public function execute()
- {
- $mall_list = Mall::getEnableMallList(true);
- if(!empty($mall_list)){
- foreach ($mall_list as $mall){
- $mallId = $mall['id'];
- // 获取基础配置
- $config = Yii::$app->services->setting->addons->get($mallId, 'BossWeight', 'basic');
- if ($config['is_enable'] == StatusEnum::ENABLED) {
- // 获取所有股东列表
- $boss_list = AddonsBoss::find()->where(['mall_id' => $mallId, 'status' => StatusEnum::ENABLED])->asArray()->all();
- if (empty($boss_list)) continue;
- foreach ($boss_list as $k => $boss){
- if(!$this->weightRule($boss,1)) continue;
- }
- }
- }
- }
- return true;
- }
- /**
- * 权重限制规则
- */
- public function weightRule($boss,$type = 0){
- $levelSetting = AddonsBossLevelSetting::find()->where([
- 'status' => 1,
- 'mall_id'=> $boss['mall_id'],
- 'level' => $boss['level']
- ])->one();
- if(!$levelSetting || !$levelSetting->weight_turnover_ratio || !$levelSetting->weight_level_ratio || !$levelSetting->weight_status || !$levelSetting->weight_is_limit){
- return false;
- }
- $checked_weight_values = json_decode($levelSetting->checked_weight_values,true);
- if(empty($checked_weight_values)){
- return false;
- }
- //股东权重
- $weight = CommissionLogLogic::getBossWeight($boss);
- foreach($checked_weight_values as $key => $value){
- $boss_update = AddonsBoss::findOne(['id'=>$boss['id']]);
- if($value['key'] == 0 && $value['checked'] ==1){
- //无权重不执行该条件
- if(!$weight && !$boss_update->boss_weight_status){
- continue;
- }
- $bossPrice = AddonsBossPrice::find()->where([
- 'user_id' => $boss['user_id'],
- 'level' => $boss['level']
- ])->one();
- if(!$bossPrice){
- continue;
- }
- //如果boss用户的分红金额达到设置的上限金额 boss权重失效
- if($levelSetting->weight_max_price && $bossPrice->price > $levelSetting->weight_max_price){
- if(!$boss_update->boss_weight_status && $type){
- $boss_update->boss_weight_status = 1;
- if(!$boss_update->save()){
- \Yii::error('股东:'.$boss['id'].'权重失效状态1更新失败');
- }
- }
- //如果规定时间内复购商品 boss权重恢复
- if(isset($value['value']['val']) && isset($value['value']['val1']) && !empty($value['value']['val']) && $value['value']['val1'] && $boss_update->boss_weight_status == 1){
- $end_time = strtotime('+'.$value['value']['val'].' day', $bossPrice->updated_at);
- $count = OrderDetail::find()->where([
- 'status' => 1,
- 'user_id' => $boss['user_id'],
- 'mall_id' => $boss['mall_id'],
- ])->andWhere(['in','goods_id',$value['value']['val1']])
- ->andWhere(['>','order_status',0])
- ->andWhere(['=', 'store_id', StatusEnum::DISABLED]) // 过滤门店订单
- ->andWhere(['>','created_at',$bossPrice->updated_at])
- ->andWhere(['<=','created_at',$end_time])
- ->count();
- if($count > 0){
- $boss_update->boss_weight_status = 0;
- if(!$boss_update->save()){
- \Yii::error('股东:'.$boss['id'].'权重正常使用状态更新失败');
- }
- //恢复权重后恢复分红金额
- $bossPrice->price = 0;
- if(!$bossPrice->save()){
- \Yii::error('股东:'.$boss['id'].'分红额度更新失败');
- }
- }
- }
- }
- }
- if($value['key'] == 1 && $value['checked'] ==1){
- //无权重不执行该条件
- if(!$weight && !$boss_update->boss_weight_status){
- continue;
- }
- if(isset($value['value']['val']) && !empty($value['value']['val'])){
- //根据最近一次获得加权分红时间判断
- $bossPrice = AddonsBossPrice::find()->where([
- 'user_id' => $boss['user_id'],
- 'level' => $boss['level']
- ])->one();
- if(!$bossPrice){
- continue;
- }
- $start_time = time();
- $end_time = strtotime('-'.$value['value']['val'].' day', $start_time);
- //直推会员
- $userChilds = UserPartitionRelationship::getChildIdArr($boss['user_id'],1);
- //直推会员数量
- $userCounts = UserPartitionRelationship::getChildCounts($boss['user_id'],1);
- //直推会员达成条件数量
- $finished = 0;
- foreach ($userChilds as $k=>&$child){
- $count = $this->getOrderCounts($child,$boss['mall_id'],$end_time,$start_time);
- $childrenCount = $this->getChildCounts($child,$end_time,$start_time);
- if(!$count && !$childrenCount){
- //如果已经有了失效状态,则不再执行更新
- if(!$boss_update->boss_weight_status && $type){
- $boss_update->boss_weight_status = 2;
- if(!$boss_update->save()){
- \Yii::error('股东:'.$boss['id'].'权重失效状态2更新失败');
- }
- }
- }
- if(isset($value['value']['val1']) && !empty($value['value']['val1']) && $boss_update->boss_weight_status = 2){
- $boss_child = AddonsBoss::find()->where(['mall_id' => $boss['mall_id'], 'status' => StatusEnum::ENABLED,'user_id'=>$child])->asArray()->one();
- if($boss_child){
- $weight_child = CommissionLogLogic::getBossWeight($boss_child);
- if($weight_child){
- $userCounts--;
- continue;
- }
- }
- $update_count = Order::find()->where([
- 'pay_status' => [OrderStatusEnum::PAY],
- 'mall_id' => $boss['mall_id'],
- 'status' => 1,
- 'user_id' => $child,
- ])->andWhere(['>','created_at',$bossPrice->updated_at])
- ->andWhere(['=', 'store_id', StatusEnum::DISABLED]) // 过滤门店订单
- ->count();
- if($update_count >= $value['value']['val1']){
- $finished++;
- }
- }
- }
- if($finished == $userCounts){
- $boss_update->boss_weight_status = 0;
- if(!$boss_update->save()){
- \Yii::error('股东:'.$boss['id'].'权重正常使用状态更新失败');
- }
- }
- }
- }
- if($value['key'] == 2 && $value['checked'] ==1){
- if(empty($value['value']['val']) || !$type){
- continue;
- }
- $time = $boss['created_at'];
- $weight_new = CommissionLogLogic::getBossWeightOutStatus($boss);
- if($weight_new || strtotime('+'.$value['value']['val'].' day', $time) > time() || $boss_update->upgrade_status != UpgradeEnum::UPGRADE_STATUS_GOODS){
- continue;
- }
- \Yii::error('股东:'.$boss['id'].'股东身份取消');
- $boss_update->status = -1;
- if(!$boss_update->save()){
- \Yii::error('股东:'.$boss['id'].'股东身份取消失败');
- }
- }
- }
- return true;
- }
- /**
- * 查询下级数组
- * @param $user_id
- * @param int $deep
- * @return array
- */
- public function getChildCounts($user_id,$start_at,$end_time)
- {
- $model = UserPartitionRelationship::find()->where(['user_id' => $user_id])->orderBy('part desc')->asArray()->one();
- $tree = $model['tree'];
- $parent_deep = $model['deep'];
- $query = UserPartitionRelationship::find()
- ->where(['<>', 'user_id', $user_id])
- ->andWhere(['like', 'tree', "{$tree},%", false])
- ->andWhere(['>','created_at',$start_at])
- ->andWhere(['<=','created_at',$end_time]);
- return $query->count();
- }
- /**
- * 查询会员订单数
- */
- public function getOrderCounts($user_id,$mall_id,$start_at,$end_time)
- {
- $count = Order::find()->where([
- 'pay_status' => [ OrderStatusEnum::PAY],
- 'mall_id' => $mall_id,
- 'status' => 1,
- 'user_id' => $user_id,
- ])->andWhere(['>','created_at',$start_at])
- ->andWhere(['=', 'store_id', StatusEnum::DISABLED]) // 过滤门店订单
- ->andWhere(['<=','created_at',$end_time])
- ->count();
- return $count;
- }
- }
|