feat: 修改DemoController和IndexController类,优化了查询逻辑和数据统计方式,增加了对门店统计和商品数量统计的查询功能。

This commit is contained in:
mkm 2024-07-18 15:49:55 +08:00
parent e7cf36928f
commit 82a2bf0fa5
3 changed files with 107 additions and 88 deletions

View File

@ -4,6 +4,8 @@ namespace app\statistics\controller;
use app\admin\logic\statistic\TradeStatisticLogic;
use app\common\controller\BaseLikeController;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_product\StoreProduct;
use app\common\model\system_store\SystemStore;
use think\facade\Db;
@ -15,36 +17,52 @@ class DemoController extends BaseLikeController
{
$time = $this->request->get('date', date('Y-m-d'));
$store_id = $this->request->get('store_id', 0);
$res = Db::name('demo_order_money')->whereDay('dates', $time)->sum('money');
$res = $res ? $res : 0;
switch ($store_id) {
case 1:
$title = '喻寺镇农(特)产品交易大数据';
break;
case 2:
$title = '立石镇农(特)产品交易大数据';
break;
case 3:
$title = '百和镇农(特)产品交易大数据';
break;
case 4:
$title = '得胜镇农(特)产品交易大数据';
break;
case 5:
$title = '玄滩镇农(特)产品交易大数据';
break;
case 6:
$title = '牛滩镇农(特)产品交易大数据';
break;
case 7:
$title = '云锦镇农(特)产品交易大数据';
break;
default:
$title = '泸县农(特)产品交易大数据';
break;
$where = [];
if ($store_id) {
$where['store_id'] = $store_id;
$name = SystemStore::where('id', $store_id)->value('name');
$title = $name . '农(特)产品交易大数据';
} else {
$title = '泸县农(特)产品交易大数据';
}
$res = Db::name('demo_order_money')->where($where)->whereDay('dates', $time)->sum('money');
$res = $res ? $res : 0;
return $this->success('ok', ['dayPayPrice' => $res, 'title' => $title]);
}
/**
* 门店统计
*/
public function sotre_count()
{
$data=SystemStore::where('is_show', 1)->field('id,introduction street_name')->select()->each(function ($item) use (&$data) {
$item['merchant_count'] = 1;
return $item;
});
return $this->success('ok', ['merchatCountList' => $data, 'merchantTotalCount' => count($data)]);
}
/**
* 地方商品数量统计
*/
public function product_count_sotre_count()
{
$time = $this->request->get('date', date('Y-m-d'));
$data=SystemStore::where('is_show', 1)->field('id,introduction street_name')->select()->each(function ($item) {
$item['product_count'] = StoreBranchProduct::where('store_id', $item['id'])->count();
return $item;
});
//总商品
$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'];
}
return $this->success('ok', ['townProductCountList' => $data, 'productRankingList' => $product_count, 'townProductCount' => $townProductCount, 'productRankingTotal' => $productRankingTotal]);
}
public function bottomLeft()
{
$time = $this->request->get('date', date('Y-m'));
@ -53,7 +71,7 @@ class DemoController extends BaseLikeController
if ($store_id) {
$where['store_id'] = $store_id;
}
$arr = SystemStore::where('is_show', 1)->field('id,name street_name')->select()
$arr = SystemStore::where('is_show', 1)->field('id,abbreviation street_name')->select()
->each(function ($item) use ($time) {
$day = Db::name('demo_order_money')->where('store_id', $item['id'])->whereMonth('dates', $time)->sum('money');
$yester = Db::name('demo_order_money')->where('store_id', $item['id'])->whereMonth('dates', date('Y-m', strtotime('-1 month')))->sum('money');
@ -67,10 +85,10 @@ class DemoController extends BaseLikeController
public function store_order_day_two()
{
$time = $this->request->get('date', date('Y-m-d'));
$arr = SystemStore::where('is_show', 1)->field('id,name street_name')->select()
$arr = SystemStore::where('is_show', 1)->field('id,abbreviation street_name')->select()
->each(function ($item) use ($time) {
$today_order_amount = Db::name('demo_order_money')->where('store_id', $item['id'])->whereDay('dates', $time)->sum('money');
$yesterday_order_amount = Db::name('demo_order_money')->where('store_id', $item['id'])->whereDay('dates', date('Y-m-d', strtotime($time.'-1 day')))->sum('money');
$yesterday_order_amount = Db::name('demo_order_money')->where('store_id', $item['id'])->whereDay('dates', date('Y-m-d', strtotime($time . '-1 day')))->sum('money');
$item['today_order_amount'] = $today_order_amount;
$item['yesterday_order_amount'] = $yesterday_order_amount;
return $item;
@ -85,32 +103,26 @@ class DemoController extends BaseLikeController
{
$store_id = $this->request->get('store_id', 0);
$date = $this->request->get('date', '');
$a = new TradeStatisticLogic();
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']];
$time['start_time'] = $date;
$time['end_time'] = date('Y-m-d', strtotime($date . '-1 month'));
$aa = $a->TimeConvert($time);
$time['start_time'] = $aa['end_time'];
$time['end_time'] = $aa['start_time'];
$time['days'] = 30;
} else {
$where = ['create_time' => 'today'];
$where2 = ['create_time' => 'yestoday'];
$time['start_time'] = date('Y-01-01 00:00:00', time());
$time['end_time'] = date('Y-12-t 23:59:59', time());
$time['days'] = 365;
}
$where = [];
if ($store_id) {
$where['store_id'] = $store_id;
$where2['store_id'] = $store_id;
}
$left = [];
$left = $this->getCurveData($where, $time, 'sum(money)', 'dates');
// foreach ($totalleft as $k => $v) {
// $left['name'] = "当日订单金额";
// $left['x'] = $v['curve']['x'];
// $left['series'][$k]['money'] = round($v['total_money'], 2);
// $left['series'][$k]['value'] = array_values($v['curve']['y']);
// }
$time['start_time'] = date('Y-01-01 00:00:00', time());
$time['end_time'] = date('Y-12-t 23:59:59', time());
$time['days'] = 365;
$left = $this->getCurveData([], $time, 'sum(money)', 'dates');
$a = new TradeStatisticLogic();
$totalMoney = $a->trendYdata($left, $time);
$datas['name'] = "门店订单金额曲线";
@ -143,4 +155,42 @@ class DemoController extends BaseLikeController
})
->order("$group ASC")->select()->toArray();
}
/**
* 当日订单金额
*/
public function street_currday_order_count()
{
$time = $this->request->get('date', date('Y-m-d'));
$store_id = $this->request->get('store_id', 0);
$where = [];
if ($store_id) {
$where['store_id'] = $store_id;
}
$startTime = strtotime($time . ' 00:00:00'); // 当天的开始时间戳
$endTime = strtotime($time . ' 23:59:59'); // 当天的结束时间戳
$interval = 4 * 60 * 60; // 4小时的秒数
$data = [];
for ($time = $startTime; $time < $endTime; $time += $interval) {
$endTimeSegment = $time + $interval;
$startTimeSegment = date('Y-m-d H:i:s', $time);
$yesterendTimeSegment = date('Y-m-d H:i:s', $endTimeSegment - 86400);
$endTimeSegment = date('Y-m-d H:i:s', $endTimeSegment);
$yesterstartTimeSegment = date('Y-m-d H:i:s', $time - 86400);
// 统计当前时间段的订单
$todayAmount = Db::name('demo_order_money')->where($where)
->whereBetween('dates', [strtotime($startTimeSegment), strtotime($endTimeSegment)])
->sum('money');
$yesterdayAmount = Db::name('demo_order_money')->where($where)
->whereBetween('dates', [strtotime($yesterstartTimeSegment), strtotime($yesterendTimeSegment)])
->sum('money');
$data[] = [
'todayAmount' => $todayAmount,
'yesterdayAmount' => $yesterdayAmount,
];
}
return $this->success('ok', $data);
}
}

View File

@ -15,6 +15,7 @@ use app\statistics\logic\OrderLogic;
use app\statistics\logic\ProductLogic;
use app\statistics\logic\UserLogic;
use DateTime;
use think\facade\Db;
class IndexController extends BaseLikeController
{
@ -33,50 +34,15 @@ class IndexController extends BaseLikeController
if (OrderLogic::hasError()) {
return $this->fail(OrderLogic::getError()); //获取错误信息并返回错误信息
}
switch ($store_id) {
case 1:
$title = '喻寺镇农(特)产品交易大数据';
break;
case 2:
$title = '立石镇农(特)产品交易大数据';
break;
case 3:
$title = '百和镇农(特)产品交易大数据';
break;
case 4:
$title = '得胜镇农(特)产品交易大数据';
break;
case 5:
$title = '玄滩镇农(特)产品交易大数据';
break;
case 6:
$title = '牛滩镇农(特)产品交易大数据';
break;
case 7:
$title = '云锦镇农(特)产品交易大数据';
break;
default:
$title = '泸县农(特)产品交易大数据';
break;
if ($store_id) {
$name = SystemStore::where('id', $store_id)->value('name');
$title = $name . '农(特)产品交易大数据';
} else {
$title = '泸县农(特)产品交易大数据';
}
return $this->success('ok', ['dayPayPrice' => $res, 'title' => $title]);
}
/**
* 门店统计
*/
public function sotre_count()
{
$data = [
['street_name' => '喻寺镇', 'merchant_count' => 1],
['street_name' => '立石镇', 'merchant_count' => 1],
['street_name' => '百和镇', 'merchant_count' => 1],
['street_name' => '得胜镇', 'merchant_count' => 1],
['street_name' => '玄滩镇', 'merchant_count' => 1],
['street_name' => '云锦镇', 'merchant_count' => 1],
];
return $this->success('ok', ['merchatCountList' => $data, 'merchantTotalCount' => count($data)]);
}
/**
* 地方商品数量统计
@ -126,6 +92,7 @@ class IndexController extends BaseLikeController
return $this->fail(UserLogic::getError()); //获取错误信息并返回错误信息
}
$res['date']=$dates_two;
$res['dayPayPrice']=Db::name('demo_order_money')->where($where)->sum('money');
return $this->success('ok', $res);
}

View File

@ -16,6 +16,8 @@ class ProductLogic extends BaseLogic
$yestertodayProductCount = StoreBranchProduct::where($where)->where('create_time', '<', strtotime($time) - 1)->count();
$todayNewProductCount = StoreBranchProduct::where($where)->whereDay('create_time', $time)->count();
$yestertodayNewProductCount = StoreBranchProduct::where($where)->whereDay('create_time', date('Y-m-d', strtotime($time) - 1))->count();
$where['id']=$where['store_id'];
unset($where['store_id']);
} else {
$todayProductCount = StoreProduct::whereDay('create_time', $time)->count();
$yestertodayProductCount = StoreProduct::count();