chore(src): 所有代码上传
This commit is contained in:
@@ -1,221 +1,213 @@
|
||||
<?php
|
||||
/**
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
namespace app\model\stat;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use Carbon\Carbon;
|
||||
|
||||
use extend\Stat;
|
||||
use think\facade\Db;
|
||||
use think\facade\Log;
|
||||
|
||||
/**
|
||||
* 统计
|
||||
* @author Administrator
|
||||
*
|
||||
*/
|
||||
class StatShop extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 添加店铺统计(按照天统计)
|
||||
* @param array $data
|
||||
*/
|
||||
public function addShopStat($data)
|
||||
{
|
||||
$site_id = $data['site_id'] ?? 1;
|
||||
log_write('店铺按天新统计数据开始添加,site_id:'.$site_id, 'debug');
|
||||
try{
|
||||
$carbon = Carbon::now();
|
||||
$dir = __UPLOAD__.'/stat/stat_shop/';
|
||||
if (!is_dir($dir) && !mkdir($dir, 0777, true) && !is_dir($dir)) {
|
||||
return $this->error(sprintf('Directory "%s" was not created', $dir));
|
||||
}
|
||||
$filename = $dir.$carbon->year.'_'.$carbon->month.'_'.$carbon->day.'_'.$carbon->second.'_s'.$site_id.'_'.unique_random().'.json';
|
||||
$stat_extend = new Stat($filename, 'stat_shop', $site_id);
|
||||
$stat_extend->handleData($data);//写入文件
|
||||
|
||||
//增加当天时统计
|
||||
$this->addShopHourStat($data, $carbon);
|
||||
}catch (\Exception $e){
|
||||
log_write('店铺按天新统计数据添加失败,site_id:'.$site_id . ',错误信息:' . $e->getMessage(), 'error');
|
||||
return $this->error('店铺按天新统计数据添加失败');
|
||||
}
|
||||
|
||||
log_write('店铺按天新统计数据已添加,site_id:'.$site_id, 'debug');
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 从stat_shop目录下读取所有文件,将数据处理后,写入数据表中
|
||||
* 处理完每个文件后,删除文件
|
||||
*/
|
||||
public function cronShopStat()
|
||||
{
|
||||
log_write('店铺按天统计数据开始处理', 'debug');
|
||||
$path = __UPLOAD__.'/stat/stat_shop';
|
||||
if(!is_dir($path)) {
|
||||
log_write('店铺按天统计数据处理失败:目录不存在', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
$result = $this->scanFile($path);
|
||||
if(empty($result)) {
|
||||
log_write('店铺按天统计数据处理失败:目录下无文件', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$json_array = [];
|
||||
foreach ($result as $key => $val){
|
||||
$stat_extend = new Stat($path.'/'.$val, 'stat_shop');
|
||||
$json_array[] = $stat_extend->load();
|
||||
unlink($path.'/'.$val); // 处理完文件后,删除文件
|
||||
}
|
||||
|
||||
|
||||
$data_array = [];
|
||||
foreach ($json_array as $json_k => $json_v){
|
||||
$k = $json_v['year'].'_'.$json_v['month'].'_'.$json_v['day'];
|
||||
if (isset($data_array[$k])){
|
||||
foreach ($data_array[$k] as $data_k => $data_v){
|
||||
if($data_k != 'site_id' && $data_k != 'year' && $data_k != 'month' && $data_k != 'day' && $data_k != 'day_time'){
|
||||
if ($json_v[$data_k] > 0) {
|
||||
$data_array[$k][$data_k] += $json_v[$data_k];
|
||||
} else if ($json_v[$data_k] < 0) {
|
||||
$data_array[$k][$data_k] -= abs($json_v[$data_k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
$data_array[$k] = $json_v;
|
||||
}
|
||||
}
|
||||
Log::write(time().'stat_shop_'.json_encode($data_array));
|
||||
$system_stat = new \app\model\system\Stat();
|
||||
foreach ($data_array as $json_k => $json_v){
|
||||
$json_v = $this->removeExtraFields($json_v);
|
||||
$system_stat->addStatShopModel($json_v);
|
||||
}
|
||||
log_write('店铺按天统计数据处理成功', 'debug');
|
||||
} catch (\Exception $e) {
|
||||
log_write('店铺按天统计数据处理失败:' . $e->getMessage(), 'error');
|
||||
return $this->error('店铺按天统计数据处理失败');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加当日的时统计记录
|
||||
* @param $data
|
||||
* @param $carbon
|
||||
* @return array
|
||||
*/
|
||||
public function addShopHourStat($data, $carbon)
|
||||
{
|
||||
$site_id = $data['site_id'] ?? 1;
|
||||
log_write('店铺按小时新统计数据开始添加,site_id:'.$site_id, 'debug');
|
||||
try{
|
||||
$dir = __UPLOAD__.'/stat/stat_shop_hour/';
|
||||
if (!is_dir($dir) && !mkdir($dir, 0777, true) && !is_dir($dir)) {
|
||||
return $this->error(sprintf('Directory "%s" was not created', $dir));
|
||||
}
|
||||
$filename = $dir.$carbon->year.'_'.$carbon->month.'_'.$carbon->day.'_'.$carbon->hour.'_'.$carbon->second.'_s'.$site_id.'_'.unique_random().'.json';
|
||||
$stat_extend = new Stat($filename, 'stat_shop_hour', $site_id);
|
||||
$stat_extend->handleData($data);//写入文件
|
||||
log_write('店铺按小时新统计数据已添加,site_id:'.$site_id, 'debug');
|
||||
}catch (\Exception $e){
|
||||
log_write('店铺按小时新统计数据添加失败,site_id:'.$site_id . ',错误信息:' . $e->getMessage(), 'error');
|
||||
return $this->error('店铺按小时新统计数据添加失败');
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
public function cronShopStatHour()
|
||||
{
|
||||
log_write('系统计划任务开始执行店铺按小时统计');
|
||||
$path = __UPLOAD__.'/stat/stat_shop_hour';
|
||||
if(!is_dir($path)) {
|
||||
log_write('系统计划任务执行店铺按小时统计异常:目录不存在' . $path . ',请检查目录是否存在');
|
||||
return;
|
||||
}
|
||||
|
||||
$result = $this->scanFile($path);
|
||||
if(empty($result)) {
|
||||
log_write('系统计划任务执行店铺按小时统计异常:目录下无文件' . $path . ',请检查是否有文件存在');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$json_array = [];
|
||||
foreach ($result as $key => $val){
|
||||
$stat_extend = new Stat($path.'/'.$val, 'stat_shop_hour');
|
||||
$json_array[] = $stat_extend->load();
|
||||
unlink($path.'/'.$val);
|
||||
}
|
||||
|
||||
$data_array = [];
|
||||
foreach ($json_array as $json_k => $json_v){
|
||||
$k = $json_v['year'].'_'.$json_v['month'].'_'.$json_v['day'];
|
||||
if (isset($data_array[$k])){
|
||||
foreach ($data_array[$k] as $data_k => $data_v){
|
||||
if($data_k != 'site_id' && $data_k != 'year' && $data_k != 'month' && $data_k != 'day' && $data_k != 'day_time'){
|
||||
if ($json_v[$data_k] > 0) {
|
||||
$data_array[$k][$data_k] += $json_v[$data_k];
|
||||
} else if ($json_v[$data_k] < 0) {
|
||||
$data_array[$k][$data_k] -= abs($json_v[$data_k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
$data_array[$k] = $json_v;
|
||||
}
|
||||
}
|
||||
Log::write(time().'stat_shop_hour_'.json_encode($data_array));
|
||||
$system_stat = new \app\model\system\Stat();
|
||||
foreach ($json_array as $json_k => $json_v){
|
||||
$json_v = $this->removeExtraFields($json_v);
|
||||
$system_stat->addStatShopHourModel($json_v);
|
||||
}
|
||||
log_write('系统计划任务执行店铺按小时统计完成');
|
||||
} catch (\Exception $e) {
|
||||
log_write('系统计划任务执行店铺按小时统计异常:'.$e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将不需要的字段从json_v中移除,并返回处理后的数组
|
||||
* @param array $json_v 原始json数组
|
||||
* @return array 处理后的json数组
|
||||
*/
|
||||
public function removeExtraFields($json_v)
|
||||
{
|
||||
try {
|
||||
// 从json_v中移除多余字段
|
||||
unset($json_v['store_id']); // 移除store_id字段
|
||||
} catch (\Exception $e) {}
|
||||
return $json_v;
|
||||
}
|
||||
|
||||
public function scanFile($path) {
|
||||
$result = [];
|
||||
$files = scandir($path);
|
||||
foreach ($files as $file) {
|
||||
if ($file != '.' && $file != '..') {
|
||||
if (is_dir($path . '/' . $file)) {
|
||||
$this->scanFile($path . '/' . $file);
|
||||
} else {
|
||||
$result[] = basename($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\model\stat;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use Carbon\Carbon;
|
||||
|
||||
use extend\Stat;
|
||||
use think\facade\Db;
|
||||
use think\facade\Log;
|
||||
|
||||
/**
|
||||
* 统计
|
||||
* @author Administrator
|
||||
*
|
||||
*/
|
||||
class StatShop extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 添加店铺统计(按照天统计)
|
||||
* @param array $data
|
||||
*/
|
||||
public function addShopStat($data)
|
||||
{
|
||||
$site_id = $data['site_id'] ?? 1;
|
||||
log_write('店铺按天新统计数据开始添加,site_id:'.$site_id, 'debug');
|
||||
try{
|
||||
$carbon = Carbon::now();
|
||||
$dir = __UPLOAD__.'/stat/stat_shop/';
|
||||
if (!is_dir($dir) && !mkdir($dir, 0777, true) && !is_dir($dir)) {
|
||||
return $this->error(sprintf('Directory "%s" was not created', $dir));
|
||||
}
|
||||
$filename = $dir.$carbon->year.'_'.$carbon->month.'_'.$carbon->day.'_'.$carbon->second.'_s'.$site_id.'_'.unique_random().'.json';
|
||||
$stat_extend = new Stat($filename, 'stat_shop', $site_id);
|
||||
$stat_extend->handleData($data);//写入文件
|
||||
|
||||
//增加当天时统计
|
||||
$this->addShopHourStat($data, $carbon);
|
||||
}catch (\Exception $e){
|
||||
log_write('店铺按天新统计数据添加失败,site_id:'.$site_id . ',错误信息:' . $e->getMessage(), 'error');
|
||||
return $this->error('店铺按天新统计数据添加失败');
|
||||
}
|
||||
|
||||
log_write('店铺按天新统计数据已添加,site_id:'.$site_id, 'debug');
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 从stat_shop目录下读取所有文件,将数据处理后,写入数据表中
|
||||
* 处理完每个文件后,删除文件
|
||||
*/
|
||||
public function cronShopStat()
|
||||
{
|
||||
log_write('店铺按天统计数据开始处理', 'debug');
|
||||
$path = __UPLOAD__.'/stat/stat_shop';
|
||||
if(!is_dir($path)) {
|
||||
log_write('店铺按天统计数据处理失败:目录不存在', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
$result = $this->scanFile($path);
|
||||
if(empty($result)) {
|
||||
log_write('店铺按天统计数据处理失败:目录下无文件', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$json_array = [];
|
||||
foreach ($result as $key => $val){
|
||||
$stat_extend = new Stat($path.'/'.$val, 'stat_shop');
|
||||
$json_array[] = $stat_extend->load();
|
||||
unlink($path.'/'.$val); // 处理完文件后,删除文件
|
||||
}
|
||||
|
||||
|
||||
$data_array = [];
|
||||
foreach ($json_array as $json_k => $json_v){
|
||||
$k = $json_v['year'].'_'.$json_v['month'].'_'.$json_v['day'];
|
||||
if (isset($data_array[$k])){
|
||||
foreach ($data_array[$k] as $data_k => $data_v){
|
||||
if($data_k != 'site_id' && $data_k != 'year' && $data_k != 'month' && $data_k != 'day' && $data_k != 'day_time'){
|
||||
if ($json_v[$data_k] > 0) {
|
||||
$data_array[$k][$data_k] += $json_v[$data_k];
|
||||
} else if ($json_v[$data_k] < 0) {
|
||||
$data_array[$k][$data_k] -= abs($json_v[$data_k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
$data_array[$k] = $json_v;
|
||||
}
|
||||
}
|
||||
Log::write(time().'stat_shop_'.json_encode($data_array));
|
||||
$system_stat = new \app\model\system\Stat();
|
||||
foreach ($data_array as $json_k => $json_v){
|
||||
$json_v = $this->removeExtraFields($json_v);
|
||||
$system_stat->addStatShopModel($json_v);
|
||||
}
|
||||
log_write('店铺按天统计数据处理成功', 'debug');
|
||||
} catch (\Exception $e) {
|
||||
log_write('店铺按天统计数据处理失败:' . $e->getMessage(), 'error');
|
||||
return $this->error('店铺按天统计数据处理失败');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加当日的时统计记录
|
||||
* @param $data
|
||||
* @param $carbon
|
||||
* @return array
|
||||
*/
|
||||
public function addShopHourStat($data, $carbon)
|
||||
{
|
||||
$site_id = $data['site_id'] ?? 1;
|
||||
log_write('店铺按小时新统计数据开始添加,site_id:'.$site_id, 'debug');
|
||||
try{
|
||||
$dir = __UPLOAD__.'/stat/stat_shop_hour/';
|
||||
if (!is_dir($dir) && !mkdir($dir, 0777, true) && !is_dir($dir)) {
|
||||
return $this->error(sprintf('Directory "%s" was not created', $dir));
|
||||
}
|
||||
$filename = $dir.$carbon->year.'_'.$carbon->month.'_'.$carbon->day.'_'.$carbon->hour.'_'.$carbon->second.'_s'.$site_id.'_'.unique_random().'.json';
|
||||
$stat_extend = new Stat($filename, 'stat_shop_hour', $site_id);
|
||||
$stat_extend->handleData($data);//写入文件
|
||||
log_write('店铺按小时新统计数据已添加,site_id:'.$site_id, 'debug');
|
||||
}catch (\Exception $e){
|
||||
log_write('店铺按小时新统计数据添加失败,site_id:'.$site_id . ',错误信息:' . $e->getMessage(), 'error');
|
||||
return $this->error('店铺按小时新统计数据添加失败');
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
public function cronShopStatHour()
|
||||
{
|
||||
log_write('系统计划任务开始执行店铺按小时统计');
|
||||
$path = __UPLOAD__.'/stat/stat_shop_hour';
|
||||
if(!is_dir($path)) {
|
||||
log_write('系统计划任务执行店铺按小时统计异常:目录不存在' . $path . ',请检查目录是否存在');
|
||||
return;
|
||||
}
|
||||
|
||||
$result = $this->scanFile($path);
|
||||
if(empty($result)) {
|
||||
log_write('系统计划任务执行店铺按小时统计异常:目录下无文件' . $path . ',请检查是否有文件存在');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$json_array = [];
|
||||
foreach ($result as $key => $val){
|
||||
$stat_extend = new Stat($path.'/'.$val, 'stat_shop_hour');
|
||||
$json_array[] = $stat_extend->load();
|
||||
unlink($path.'/'.$val);
|
||||
}
|
||||
|
||||
$data_array = [];
|
||||
foreach ($json_array as $json_k => $json_v){
|
||||
$k = $json_v['year'].'_'.$json_v['month'].'_'.$json_v['day'];
|
||||
if (isset($data_array[$k])){
|
||||
foreach ($data_array[$k] as $data_k => $data_v){
|
||||
if($data_k != 'site_id' && $data_k != 'year' && $data_k != 'month' && $data_k != 'day' && $data_k != 'day_time'){
|
||||
if ($json_v[$data_k] > 0) {
|
||||
$data_array[$k][$data_k] += $json_v[$data_k];
|
||||
} else if ($json_v[$data_k] < 0) {
|
||||
$data_array[$k][$data_k] -= abs($json_v[$data_k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
$data_array[$k] = $json_v;
|
||||
}
|
||||
}
|
||||
Log::write(time().'stat_shop_hour_'.json_encode($data_array));
|
||||
$system_stat = new \app\model\system\Stat();
|
||||
foreach ($json_array as $json_k => $json_v){
|
||||
$json_v = $this->removeExtraFields($json_v);
|
||||
$system_stat->addStatShopHourModel($json_v);
|
||||
}
|
||||
log_write('系统计划任务执行店铺按小时统计完成');
|
||||
} catch (\Exception $e) {
|
||||
log_write('系统计划任务执行店铺按小时统计异常:'.$e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将不需要的字段从json_v中移除,并返回处理后的数组
|
||||
* @param array $json_v 原始json数组
|
||||
* @return array 处理后的json数组
|
||||
*/
|
||||
public function removeExtraFields($json_v)
|
||||
{
|
||||
try {
|
||||
// 从json_v中移除多余字段
|
||||
unset($json_v['store_id']); // 移除store_id字段
|
||||
} catch (\Exception $e) {}
|
||||
return $json_v;
|
||||
}
|
||||
|
||||
public function scanFile($path) {
|
||||
$result = [];
|
||||
$files = scandir($path);
|
||||
foreach ($files as $file) {
|
||||
if ($file != '.' && $file != '..') {
|
||||
if (is_dir($path . '/' . $file)) {
|
||||
$this->scanFile($path . '/' . $file);
|
||||
} else {
|
||||
$result[] = basename($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user