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; } }