multi-store/app/statistics/controller/IndexController.php

226 lines
7.9 KiB
PHP

<?php
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_order_cart_info\StoreOrderCartInfo;
use app\common\model\store_product\StoreProduct;
use app\common\model\user\User;
use app\common\model\user_recharge\UserRecharge;
use app\statistics\logic\OrderLogic;
use app\statistics\logic\ProductLogic;
use app\statistics\logic\UserLogic;
use DateTime;
class IndexController extends BaseLikeController
{
// public $store_id = 0;
public function index()
{
$time = $this->request->get('date', date('Y-m-d'));
$store_id = $this->request->get('store_id', 0);
if ($store_id) {
$where['store_id'] = $store_id;
}
$where['paid'] = 1;
$res = OrderLogic::dayPayPrice($where, $time);
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;
}
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)]);
}
public function product_count_sotre_count(){
$data=[
['street_name'=>'喻寺镇','product_count'=>1],
['street_name'=>'立石镇','product_count'=>1],
['street_name'=>'百和镇','product_count'=>1],
['street_name'=>'得胜镇','product_count'=>1],
['street_name'=>'玄滩镇','product_count'=>1],
['street_name'=>'云锦镇','product_count'=>1],
];
$townProductCount=StoreProduct::count();
$product_count=StoreBranchProduct::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 user()
{
$time = $this->request->get('date', date('Y-m-d'));
$store_id = $this->request->get('store_id', 0);
$today = strtotime($time);
$dates = [];
// 循环输出前5天的日期
for ($i = 0; $i <= 4; $i++) {
// 计算前第$i天的日期时间戳
$timestamp = $today - ($i * 86400); // 86400秒等于1天
// 将时间戳格式化为日期
$date = date('Y-m-d', $timestamp);
$dates[] = $date;
}
$where = [];
if ($store_id) {
$where['store_id'] = $store_id;
}
$res = UserLogic::userCount($where, $dates);
if (UserLogic::hasError()) {
return $this->fail(UserLogic::getError()); //获取错误信息并返回错误信息
}
return $this->success('ok', $res);
}
/**
* 中间商品统计
*/
public function product_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;
}
$res = ProductLogic::Count($where, $time);
if (ProductLogic::hasError()) {
return $this->fail(ProductLogic::getError()); //获取错误信息并返回错误信息
}
return $this->success('ok', $res);
}
/**
* 订单统计
*/
public function order_user_num_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;
}
$where['recharge_type'] = 'INDUSTRYMEMBERS';
$where['status'] = 1;
$where['paid'] = 1;
// $res = OrderLogic::Count($where,$time);
// if (ProductLogic::hasError()) {
// return $this->fail(ProductLogic::getError()); //获取错误信息并返回错误信息
// }
$res = UserRecharge::where($where)->whereTime('create_time', $time)->select()->each(function ($item) {
if ($item['uid']) {
$item['nickname'] = User::where('id', $item['uid'])->value('nickname');
} else {
$item['nickname'] = '';
}
});
return $this->success('ok', $res?->toArray());
}
/**
* 商品销量排行榜统计
*/
public function sales_ranking()
{
$time = $this->request->get('date');
$store_id = $this->request->get('store_id', 0);
$where = [];
if ($store_id) {
$where['store_id'] = $store_id;
}
$res = OrderLogic::sales($where, $time);
if (ProductLogic::hasError()) {
return $this->fail(ProductLogic::getError()); //获取错误信息并返回错误信息
}
return $this->success('ok', $res);
}
/**
* 成交用户数据
*/
public function user_trade_count()
{
$store_id = $this->request->get('store_id', 0);
$where = ['create_time' => 'today'];
$where2 = ['create_time' => 'yestoday'];
if ($store_id) {
$where['store_id'] = $store_id;
$where2['store_id'] = $store_id;
}
$logic = (new TradeStatisticLogic());
$leftToday = $logic->getTopLeftTrade($where);
$leftyestoday = $logic->getTopLeftTrade($where2);
$totalleft = [$leftToday, $leftyestoday];
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']);
}
return $this->data($left);
}
/**
* 当日订单金额
*/
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;
}
$res = OrderLogic::Currday($where, $time);
if (ProductLogic::hasError()) {
return $this->fail(ProductLogic::getError()); //获取错误信息并返回错误信息
}
return $this->success('ok', $res);
}
}