multi-store/app/statistics/controller/DemoController.php
mkm e7cf36928f feat: 添加或修改API以支持新的订单创建功能;
fix: 修复在处理生鲜产品订单时的异常;
refactor: 重构代码以优化性能和可读性;
style: 代码风格调整,增加空格、格式化、修复缺失的分号等;
test: 添加测试以验证新订单创建功能的正确性;
docs: 更新相关文档,如README;
build: 更新项目依赖或版本;
ops: 更新基础设施、部署、备份和恢复等操作性组件;
chore: 修改.gitignore等;
2024-07-17 11:19:56 +08:00

147 lines
5.8 KiB
PHP

<?php
namespace app\statistics\controller;
use app\admin\logic\statistic\TradeStatisticLogic;
use app\common\controller\BaseLikeController;
use app\common\model\system_store\SystemStore;
use think\facade\Db;
class DemoController extends BaseLikeController
{
public function index()
{
$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;
}
return $this->success('ok', ['dayPayPrice' => $res, 'title' => $title]);
}
public function bottomLeft()
{
$time = $this->request->get('date', date('Y-m'));
$store_id = $this->request->get('store_id', 0);
$where = [];
if ($store_id) {
$where['store_id'] = $store_id;
}
$arr = SystemStore::where('is_show', 1)->field('id,name 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');
$item['monthDayOrder'] = $day ?? 0;
$item['monthYesterOrder'] = $yester ?? 0;
return $item;
})->toArray();
return $this->data($arr);
}
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()
->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');
$item['today_order_amount'] = $today_order_amount;
$item['yesterday_order_amount'] = $yesterday_order_amount;
return $item;
});
return $this->success('ok', $arr?->toArray());
}
/**
* 成交用户数据
*/
public function user_trade_count()
{
$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 {
$where = ['create_time' => 'today'];
$where2 = ['create_time' => 'yestoday'];
}
if ($store_id) {
$where['store_id'] = $store_id;
$where2['store_id'] = $store_id;
}
$left = [];
// 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'] = "门店订单金额曲线";
$datas['x'] = $totalMoney['x'];
$datas['series'][0]['money'] = 0;
$datas['series'][0]['value'] = array_values($totalMoney['y']);
return $this->data($datas);
}
public function getCurveData($where, $time, $str, string $group = 'dates')
{
return Db::name('demo_order_money')->where($where)
->when(isset($time), function ($query) use ($time, $str, $group) {
$query->whereBetweenTime($group, $time['start_time'], $time['end_time']);
if ($time['days'] == 1) {
$timeUinx = "%H";
} elseif ($time['days'] == 30) {
$timeUinx = "%Y-%m-%d";
} elseif ($time['days'] == 365) {
$timeUinx = "%Y-%m";
} elseif ($time['days'] > 1 && $time['days'] < 30) {
$timeUinx = "%Y-%m-%d";
} elseif ($time['days'] > 30 && $time['days'] < 365) {
$timeUinx = "%Y-%m";
} else {
$timeUinx = "%Y-%m";
}
$query->field("$str as number,DATE_FORMAT($group, '$timeUinx') as time");
$query->group("DATE_FORMAT($group, '$timeUinx')");
})
->order("$group ASC")->select()->toArray();
}
}