refactor(statistics): 优化代码格式及逻辑错误

This commit is contained in:
mkm 2024-07-18 18:25:15 +08:00
parent 9829931e4f
commit 655252803b

View File

@ -51,15 +51,15 @@ class IndexController extends BaseLikeController
{
$time = $this->request->get('date', date('Y-m-d'));
$data = [
['street_name' => '喻寺镇', 'product_count' => StoreBranchProduct::where('store_id',1)->where('create_time','<=',strtotime($time))->count()],
['street_name' => '立石镇', 'product_count' => StoreBranchProduct::where('store_id',2)->where('create_time','<=',strtotime($time))->count()],
['street_name' => '百和镇', 'product_count' => StoreBranchProduct::where('store_id',3)->where('create_time','<=',strtotime($time))->count()],
['street_name' => '得胜镇', 'product_count' => StoreBranchProduct::where('store_id',5)->where('create_time','<=',strtotime($time))->count()],
['street_name' => '玄滩镇', 'product_count' => StoreBranchProduct::where('store_id',6)->where('create_time','<=',strtotime($time))->count()],
['street_name' => '云锦镇', 'product_count' => StoreBranchProduct::where('store_id',7)->where('create_time','<=',strtotime($time))->count()],
['street_name' => '喻寺镇', 'product_count' => StoreBranchProduct::where('store_id', 1)->where('create_time', '<=', strtotime($time))->count()],
['street_name' => '立石镇', 'product_count' => StoreBranchProduct::where('store_id', 2)->where('create_time', '<=', strtotime($time))->count()],
['street_name' => '百和镇', 'product_count' => StoreBranchProduct::where('store_id', 3)->where('create_time', '<=', strtotime($time))->count()],
['street_name' => '得胜镇', 'product_count' => StoreBranchProduct::where('store_id', 5)->where('create_time', '<=', strtotime($time))->count()],
['street_name' => '玄滩镇', 'product_count' => StoreBranchProduct::where('store_id', 6)->where('create_time', '<=', strtotime($time))->count()],
['street_name' => '云锦镇', 'product_count' => StoreBranchProduct::where('store_id', 7)->where('create_time', '<=', strtotime($time))->count()],
];
$townProductCount = StoreProduct::where('create_time','<=',strtotime($time))->count();
$product_count = StoreBranchProduct::where('create_time','<=',strtotime($time))->group('product_id')->order('total_sales desc')->limit(20)->field('image,product_id,store_name,sum(sales) as total_sales')->select();
$townProductCount = StoreProduct::where('create_time', '<=', strtotime($time))->count();
$product_count = StoreBranchProduct::where('create_time', '<=', strtotime($time))->group('product_id')->order('total_sales desc')->limit(20)->field('image,product_id,store_name,sum(sales) as total_sales')->select();
$productRankingTotal = 0;
foreach ($product_count as $item) {
$productRankingTotal += $item['total_sales'];
@ -72,7 +72,7 @@ class IndexController extends BaseLikeController
$store_id = $this->request->get('store_id', 0);
$today = strtotime($time);
$dates = [];
$dates_two=[];
$dates_two = [];
// 循环输出前5天的日期
for ($i = 0; $i <= 4; $i++) {
// 计算前第$i天的日期时间戳
@ -91,12 +91,12 @@ class IndexController extends BaseLikeController
if (UserLogic::hasError()) {
return $this->fail(UserLogic::getError()); //获取错误信息并返回错误信息
}
$res['date']=$dates_two;
$money=Db::name('demo_order_money')->where($where)->sum('money');
$where['paid']=1;
$res['date'] = $dates_two;
$money = Db::name('demo_order_money')->where($where)->sum('money');
$where['paid'] = 1;
// $pay_price=Db::connect('demo')->name('store_order')->where($where)->whereDay('create_time')->sum('pay_price');
$price=Db::connect('demo')->name('user_recharge')->where($where)->sum('price');
$res['dayPayPrice']=bcadd($money,$price,2);
$price = Db::connect('demo')->name('user_recharge')->where($where)->sum('price');
$res['dayPayPrice'] = bcadd($money, $price, 2);
return $this->success('ok', $res);
}
@ -131,7 +131,7 @@ class IndexController extends BaseLikeController
$where['recharge_type'] = 'INDUSTRYMEMBERS';
$where['status'] = 1;
$where['paid'] = 1;
$where[] = ['price','>',1];
$where[] = ['price', '>', 1];
// $res = OrderLogic::Count($where,$time);
// if (ProductLogic::hasError()) {
// return $this->fail(ProductLogic::getError()); //获取错误信息并返回错误信息
@ -140,9 +140,11 @@ class IndexController extends BaseLikeController
$res = Db::connect('demo')->name('user_recharge')->where($where)->limit(20)->order('id desc')->select()->each(function ($item) {
if ($item['uid']) {
$find = Db::connect('demo')->name('user')->where('id', $item['uid'])->field('real_name,nickname')->find();
$item['nickname']=$find['real_name']??'';
if($item['nickname']==''){
$item['nickname'] = $find['nickname']??'';
$item['nickname'] = $find['real_name'] ?? '';
if ($item['nickname'] == '') {
$item['nickname'] = $find['nickname'] ?? '';
} else {
$item['nickname']=substr($item['nickname'], 0, 1) . str_repeat('*', strlen($item['nickname']) - (1 + 2)) . substr($item['nickname'], -2);
}
} else {
$item['nickname'] = '';
@ -168,10 +170,10 @@ class IndexController extends BaseLikeController
// return $this->fail(ProductLogic::getError()); //获取错误信息并返回错误信息
// }
$select = Db::connect('demo')->name('store_order')->where($where)->limit(20)->order('id desc')
->field('id,order_id,pay_price,create_time')->select()->each(function ($item) {
$item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
return $item;
})->toArray();
->field('id,order_id,pay_price,create_time')->select()->each(function ($item) {
$item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
return $item;
})->toArray();
return $this->success('ok', $select);
}
/**
@ -182,10 +184,10 @@ class IndexController extends BaseLikeController
$store_id = $this->request->get('store_id', 0);
$date = $this->request->get('date', '');
if($date!=''){
$where = ['create_time' =>['start_time'=>$date,'end_time'=>$date.' 23:59:59']];
$where2 = ['create_time' =>['start_time'=>$date,'end_time'=>$date. ' 23:59:59']];
}else{
if ($date != '') {
$where = ['create_time' => ['start_time' => $date, 'end_time' => $date . ' 23:59:59']];
$where2 = ['create_time' => ['start_time' => $date, 'end_time' => $date . ' 23:59:59']];
} else {
$where = ['create_time' => 'today'];
$where2 = ['create_time' => 'yestoday'];
}
@ -230,31 +232,31 @@ class IndexController extends BaseLikeController
{
$time = $this->request->get('date', date('Y-m'));
$arr = SystemStore::where('is_show', 1)->field('id,name street_name')->select()
->each(function($item) use($time){
$res = StoreOrder::where('paid', 1)->where('refund_status', 0)->where('store_id', $item['id'])->whereDay('create_time',$time)->field('count(id) count,sum(pay_price) price')->find();
$month = StoreOrder::where('paid', 1)->where('refund_status', 0)->where('store_id', $item['id'])->whereMonth('create_time',$time)->field('count(id) count,sum(pay_price) price')->find();
$item['dayOrderAmount']=$res['price']??0;
$item['dayOrderCount']=$res['count']??0;
$item['monthOrderAmount']=$month['price']??0;
$item['monthOrderCount']=$month['count']??0;
return $item;
});
->each(function ($item) use ($time) {
$res = StoreOrder::where('paid', 1)->where('refund_status', 0)->where('store_id', $item['id'])->whereDay('create_time', $time)->field('count(id) count,sum(pay_price) price')->find();
$month = StoreOrder::where('paid', 1)->where('refund_status', 0)->where('store_id', $item['id'])->whereMonth('create_time', $time)->field('count(id) count,sum(pay_price) price')->find();
$item['dayOrderAmount'] = $res['price'] ?? 0;
$item['dayOrderCount'] = $res['count'] ?? 0;
$item['monthOrderAmount'] = $month['price'] ?? 0;
$item['monthOrderCount'] = $month['count'] ?? 0;
return $item;
});
return $this->success('ok', $arr?->toArray());
}
public function store_order_day_two()
{
$arr = SystemStore::where('is_show', 1)->field('id,name street_name')->select()
->each(function($item){
$today_order_amount = StoreOrder::where('paid', 1)->where('refund_status', 0)->where('store_id', $item['id'])->whereDay('create_time')->sum('pay_price');
$yesterday_order_amount = StoreOrder::where('paid', 1)->where('refund_status', 0)->where('store_id', $item['id'])->whereMonth('create_time','yesterday')->sum('pay_price');
$item['today_order_amount']=$today_order_amount;
$item['yesterday_order_amount']=$yesterday_order_amount;
return $item;
});
->each(function ($item) {
$today_order_amount = StoreOrder::where('paid', 1)->where('refund_status', 0)->where('store_id', $item['id'])->whereDay('create_time')->sum('pay_price');
$yesterday_order_amount = StoreOrder::where('paid', 1)->where('refund_status', 0)->where('store_id', $item['id'])->whereMonth('create_time', 'yesterday')->sum('pay_price');
$item['today_order_amount'] = $today_order_amount;
$item['yesterday_order_amount'] = $yesterday_order_amount;
return $item;
});
return $this->success('ok', $arr?->toArray());
}
/**
/**
* 格式化时间
* @param $time
* @return string
@ -266,9 +268,9 @@ class IndexController extends BaseLikeController
if (!$startTime && !$endTime) {
return date("Y/m/d", strtotime("-30 days", time())) . '-' . date("Y/m/d", time());
} else {
if($startTime==$endTime){
return $startTime . '-' . $endTime.' 23:59:59';
}else{
if ($startTime == $endTime) {
return $startTime . '-' . $endTime . ' 23:59:59';
} else {
return $startTime . '-' . $endTime;
}
}