db->beginTransaction(); try { if (!LockHelper::lock($lock_key, $identification, 30, 60)) throw new \Exception('业绩进入待结算,当前访问过多'); $commission_type = 'commission'; $text = $commission_type == 'commission' ? '佣金' : '积分'; //加锁 $time = time(); $day = date('d', $time); if ($day != '01') throw new Exception('现在不是每月1号'); $last_month = date('Y-m', strtotime("-1 month")); // $last_month ='2022-07';//调测使用 $mall_list = Mall::getEnableMallList(true); foreach ($mall_list as $mall) { $mall_id = $mall['id']; while (true){ $agent_team_achievement_list = AgentTeamAchievement::find()->where(['mall_id' =>$mall_id, 'month' => $last_month,'is_settlement'=>0])->orderBy('created_at asc')->all(); if(empty($agent_team_achievement_list)) break; $user_ids = array_column($agent_team_achievement_list, 'user_id'); $agent_list = Agent::lists(['where' => [['user_id' => $user_ids], ['status' => StatusEnum::ENABLED]], 'index' => 'user_id'],false); $level_arr = array_column($agent_list, 'level'); $where = []; $where[] = ['mall_id' => $mall_id]; $where[] = ['level' => $level_arr]; $where[] = ['status' => StatusEnum::ENABLED]; $where[] = ['commission_type' => $commission_type]; $agent_commission_level_setting = AgentCommissionLevelSetting::lists(['where' => $where, 'index' => 'level']); foreach ($agent_team_achievement_list as $agent_team_achievement) { $insert_wallet = []; $total_performance = $agent_team_achievement['personal_performance'] + $agent_team_achievement['team_performance']; if (isset($agent_list[$agent_team_achievement['user_id']])) { $agent = $agent_list[$agent_team_achievement['user_id']]; if (isset($agent_commission_level_setting[$agent['level']])) { $team_achievement_rate = $agent_commission_level_setting[$agent['level']]['team_achievement']; $commission_nums = bcdiv($total_performance * $team_achievement_rate, 100, 2); $agent_team_achievement->rate = $team_achievement_rate; $agent_team_achievement->level = $agent['level']; if ($commission_nums >0){ $capital_type = UserWalletEnum::TEAM_ACHIEVEMENT_EXTRA; $prize_name = $type_map[$capital_type] ?? '渠道业绩额外奖励'; $desc = '用户(' . $agent['user_id'] . '),获得奖励,奖励类型:' . $prize_name . ',' . $text . ':' . $commission_nums; // echo $desc.PHP_EOL; $insert_wallet [] = ['mall_id' => $mall_id, 'user_id' => $agent['user_id'], 'is_frozen' => false, 'wallet_type' => $commission_type, 'money' => $commission_nums, 'desc' => $desc, 'source_table' => AgentTeamAchievement::tableName(), 'source_table_id' => $agent_team_achievement['id'], 'from_type' => AddonsEnum::ADDONS_NAME_AGENT, 'capital_type' => $capital_type, 'order_no' => $agent_team_achievement['id'], 'contributor_name' => '', 'contributor_id' => 0, 'contributor_money' => $total_performance, ]; $res = UserWalletService::batchHandleByQueue($insert_wallet); if (empty($res)) throw new \Exception(UserWalletService::getStaticError()); $field = 'total_' . $commission_type; $agent->$field += $commission_nums; $res = $agent->save(); if ($res == false) throw new \Exception(Agent::getStaticError()); } } } $agent_team_achievement->is_settlement = 1; $res = $agent_team_achievement->save(); var_dump($res); if (empty($res)) throw new \Exception($agent_team_achievement->getErrorMessage()); } } } LockHelper::uLock($lock_key,$identification); $t->commit(); return true; } catch (Exception $e) { var_dump($e->getMessage()); $t->rollBack(); LockHelper::uLock($lock_key,$identification); return false; } } }