You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
9.1 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
declare(strict_types=1);
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use app\common\model\game\Heroes;
use app\common\model\game\UserRoles;
use app\common\model\game\UserTroops;
use app\common\model\game\UserAccount;
use app\common\model\log\UserPayOrder;
use app\common\model\config\CidConfig;
use think\facade\Db;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
class StatDailyData extends Command
{
protected function configure()
{
// 指令配置
$this->setName('stat_daily_data')
->setDescription('the stat_daily_data command');
}
protected function execute(Input $input, Output $output)
{
$output->writeln("SDD开始统计 === ");
$appid = env('app.appid', 'daodao');
$date = date('Y-m-d', strtotime('-1 day'));
$cids = CidConfig::where('Appid', $appid)
->where('Inner', 0)
->where('Status', 1)
->order('Cid', 'asc')
->column('Cid');
$this->_createGameDaily($cids, $date, $output);
$output->writeln("SDD结束统计 === ");
}
private function _createGameDaily($cids, $date, $output)
{
$output->writeln("生成游戏日报开始 --- ");
$spreadsheet = new Spreadsheet();
foreach ($cids as $k => $cid) {
$output->writeln($cid . " 开始 --- ");
$sheet = new Worksheet($spreadsheet, $cid . '区');
$spreadsheet->addSheet($sheet, $k);
$roles = UserRoles::where('zone', $cid)
->field('uid,zone,barrierId,missionId,arenaAllWinCount,online,storey')
->order('uid', 'asc')
->select()
->toArray();
if (!$roles) {
$output->writeln($cid . "区,暂无数据");
}
$firstUid = $cid * 100000;
$endUid = $firstUid + 99999;
$account = UserAccount::where('AppId', 'daodao')
->whereBetween('Uid', [$firstUid, $endUid])
->column('Nickname,Lv,LastLoginDate,RegisterDate', 'Uid');
$powers = $this->_getHeroPowers($firstUid, $endUid);
// SELECT uid,limitKey,limitNum FROM userLimit_2 WHERE uid >= 200000
// AND limitKey IN ('ExpHardLv', 'FragmentHardLv', 'GlyphsHardLv');
$tmpLimits = Db::connect('game')
->table('userLimit_' . $cid)
->whereBetween('uid', [$firstUid, $endUid])
->where('limitKey', 'in', ['ExpHardLv', 'FragmentHardLv', 'GlyphsHardLv'])
->select();
$limits = [];
if ($tmpLimits) {
foreach ($tmpLimits as $t) {
$limits[$t['uid']][$t['limitKey']] = $t['limitNum'];
}
}
$money = UserPayOrder::where('status', 4)
->whereBetween('uid', [$firstUid, $endUid])
->group('uid')
->column('SUM(commodityPrice) as s', 'uid');
$header = [
'uid' => '用户ID',
'zone' => '区服',
'barrierId' => '主战场进度',
'missionId' => '任务进度',
'arenaAllWinCount' => '竞技场胜场',
'online' => '在线时长',
'money' => '充值金额',
'storey' => '王座之塔',
'Nickname' => '昵称',
'Lv' => '等级',
'LastLoginDate' => '最后登录时间',
'RegisterDate' => '注册时间',
'power' => '主战场英雄战力',
'ExpHardLv' => '无灯矿坑',
'FragmentHardLv' => '纷争之地',
'GlyphsHardLv' => '符文森林',
];
//设置单元格内容
$columns = [];
$k = 0;
foreach ($header as $key => $value) {
$c = int_to_chr($k);
$k++;
// 单元格内容写入
$sheet->setCellValue($c . '1', $value);
$columns[$key] = $c;
}
$getHighestRowAndColumn = $sheet->getHighestRowAndColumn();
$HighestRow = $getHighestRowAndColumn['row'];
$column = $getHighestRowAndColumn['column'];
$titleScope = 'A1:' . $column . '1';
$row = 2; //从第二行开始
foreach ($roles as $role) {
if (isset($account[$role['uid']])) {
$data = array_merge($role, $account[$role['uid']]);
} else {
$data = $role;
}
unset($data['Uid']);
if (isset($powers[$role['uid']])) {
$data['power'] = $powers[$role['uid']];
} else {
$data['power'] = 0;
}
if (isset($money[$role['uid']])) {
$data['money'] = round($money[$role['uid']], 2);
} else {
$data['money'] = 0;
}
if (isset($limits[$role['uid']])) {
$data = array_merge($data, $limits[$role['uid']]);
} else {
$data['ExpHardLv'] = 0;
$data['FragmentHardLv'] = 0;
$data['GlyphsHardLv'] = 0;
}
foreach ($data as $k => $v) {
//单元格内容写入
$sheet->setCellValue($columns[$k] . $row, $v);
}
$row++;
}
$sheet->getStyle($titleScope)
->getFill()
->setFillType(Fill::FILL_SOLID) // 设置填充样式
->getStartColor()
->setARGB('00B0F0');
// 设置文字颜色为白色
$sheet->getStyle($titleScope)->getFont()->getColor()
->setARGB('FFFFFF');
$spreadsheet->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
$allCope = 'A1:' . $column . $HighestRow; //整个表格范围A1:D5
$sheet->getStyle($allCope)->getBorders()->getAllBorders()->setBorderStyle(Border::BORDER_THIN);
$output->writeln($cid . " 结束 --- ");
}
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$src = app()->getRootPath() . '/public/download/' . date('Y-m') . '/';
if (!file_exists($src)) {
mkdir($src, 0775, true);
}
$fileName = '游戏日报-' . $date . '.xlsx';
$writer->save($src . $fileName);
$data = [
'type' => 1,
'statkey' => $date,
'filename' => '/download/' . date('Y-m') . '/' . $fileName,
'createtime' => date('Y-m-d H:i:s'),
'updatetime' => date('Y-m-d H:i:s'),
];
Db::connect('log')->table('log_download')->insert($data);
$output->writeln("生成游戏日报结束 --- ");
}
private function _getHeroPowers($firstUid, $endUid)
{
// 英雄战力
$troops = UserTroops::whereBetween('uid', [$firstUid, $endUid])
->where('type', 1)
->where('sort', 1)
->column('uuids', 'uid');
$heroes = Heroes::whereBetween('uid', [$firstUid, $endUid])
->field('uid,uuid,power')
->select()
->toArray();
$powers = [];
foreach ($heroes as $h) {
$powers[$h['uid']][$h['uuid']] = $h['power'];
}
$powerData = [];
foreach ($troops as $uid => $t) {
if (!isset($powers[$uid])) {
continue;
}
$heroPower = 0;
$uuids = json_decode($t, true);
if (!$uuids || !is_array($uuids)) {
continue;
}
foreach ($uuids as $uuid) {
if ($uuid <= 0) {
continue;
}
$heroPower += $powers[$uid][$uuid];
}
$powerData[$uid] = $heroPower;
}
return $powerData;
}
private function _sendEmail($src, $fileName)
{
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.163.com';
$mail->SMTPAuth = true;
$mail->Username = 'zhoulianbolove@163.com';
$mail->Password = 'IWOPTTSZZDGDIUFI';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
$mail->setFrom('zhoulianbolove@163.com', 'Miracle');
$mail->addAddress('zhuolu@17pkmj.com', '卓露');
// $mail->addAddress('375556714@qq.com', '我自己');
$mail->isHTML(true);
$mail->addAttachment($src . $fileName, $fileName);
$mail->Subject = '刀刀游戏日报-' . date('Y-m-d');
$mail->Body = '每日零点发送';
return $mail->send();
}
}