123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- namespace app\common\command;
- use app\common\model\FlowLogs;
- use app\common\model\User;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\Db;
- use think\Exception;
- class SettlementTraffic extends Command
- {
- protected function configure()
- {
- // parent::configure(); // TODO: Change the autogenerated stub
- $this->setName('settlement')->setDescription('后台统计用户流量使用, 并扣除相关流量');
- }
- protected function execute(Input $input, Output $output)
- {
- // parent::execute($input, $output); // TODO: Change the autogenerated stub
- // $output->writeln("TestCommand");
- $dateTime = time() - 5; // 设置处理数据的时间为当前时间的前5秒,避免数据竞争
- // 分组统计出指定时间内用户使用了的流量
- $data = FlowLogs::where('created_at', '<=', $dateTime)
- ->field('username, sum(bytes) as bytes')
- ->group('username')
- ->select()->toArray();
- // 当前数据所有用户名的数组
- $userNames = array_column($data, 'username');
- $users = collection(User::whereIn('username', $userNames)->field('id, username')->select())->toArray();
- $userNameMapId = array_column($users, 'id', 'username');
- Db::startTrans();
- try {
- foreach ($data as $datum) {
- if (!$this->usedTraffic($userNameMapId[$datum['username']], $datum['bytes'])) {
- throw new Exception('统计用户使用流量失败');
- }
- }
- // 指定时间段的流量已统计,删除相关记录
- FlowLogs::where('created_at', '<=', $dateTime)->delete();
- // 统计数据成功,提交事物
- Db::commit();
- } catch (Exception $e) { // 操作失败,数据库信息回滚
- Db::rollback();
- }
- }
- protected function usedTraffic($uid, $bytes)
- {
- }
- }
|