342 lines
15 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\controller\api\dataview;
use app\common\repositories\BaseRepository;
use crmeb\basic\BaseController;
use think\App;
use think\exception\ValidateException;
use think\facade\Db;
class Order extends BaseController
{
/**
* @var repository
*/
protected $repository;
public $areaCode; // 区县地区码
public $streetCode; // 镇街道地区码
/**
* StoreBrand constructor.
* @param App $app
* @param repository $repository
*/
public function __construct(App $app, BaseRepository $repository)
{
parent::__construct($app);
$this->repository = $repository;
$this->areaCode = $this->request->param('areaCode', '');
$this->streetCode = $this->request->param('streetCode', '');
if ($this->areaCode == '' && $this->streetCode == '') {
throw new ValidateException('请选择地区');
}
}
// 今日订单
public function currOrderInfo()
{
try {
$day = '2023-11-29'; // today
$currOrderCountQuery = Db::name('store_order')->alias('o')
->field(['o.order_sn', 'o.real_name', 'o.user_phone', 'o.user_address', 'o.user_address_code', 'p.store_name', 'm.mer_name', 'o.create_time', 'o.status'])
->leftJoin('product_order_log og', 'o.order_id = og.order_id')
->leftJoin('merchant m', 'o.mer_id = m.mer_id')
->leftJoin('store_order_product op', 'o.order_id = op.order_id')
->leftJoin('product_library p', 'op.product_id = p.id')
->whereDay('og.create_time', $day)
->where('o.paid', 1)
->whereNotNull('o.pay_time');
// 待取货订单数统计query 订单待发货
$pendingPickupOrderCountQuery = Db::name('store_order')->alias('o')
->leftJoin('product_order_log og', 'o.order_id = og.order_id')
->whereDay('og.create_time', $day)
->where('o.status', 0)
->where('o.paid', 1)
->whereNotNull('o.pay_time');;
// 未配送订单数统计query 订单待收货
$undeliveredOrderCountQuery = Db::name('store_order')->alias('o')
->leftJoin('product_order_log og', 'o.order_id = og.order_id')
->whereDay('og.create_time', $day)
->where('o.status', 1)
->where('o.paid', 1)
->whereNotNull('o.pay_time');;
// 已完成订单数统计query 订单已完成
$doneOrderCountQuery = Db::name('store_order')->alias('o')
->leftJoin('product_order_log og', 'o.order_id = og.order_id')
->whereDay('og.create_time', $day)
->whereIn('o.status', [2,3])
->where('o.paid', 1)
->whereNotNull('o.pay_time');
if ($this->areaCode != '') {
$currOrderCountQuery->where('og.district_code', $this->areaCode);
$pendingPickupOrderCountQuery->where('og.district_code', $this->areaCode);
$undeliveredOrderCountQuery->where('og.district_code', $this->areaCode);
$doneOrderCountQuery->where('og.district_code', $this->areaCode);
} else if ($this->streetCode != '') {
$currOrderCountQuery->where('og.street_code', $this->streetCode);
$pendingPickupOrderCountQuery->where('og.street_code', $this->streetCode);
$undeliveredOrderCountQuery->where('og.street_code', $this->streetCode);
$doneOrderCountQuery->where('og.street_code', $this->streetCode);
}
// 今日订单数
$currOrderCount = $currOrderCountQuery->count();
[$page, $limit] = $this->getPage();
$client = new \GuzzleHttp\Client();
// 今日订单列表
$currOrderList = $currOrderCountQuery->page($page, $limit)->select()->toArray();
foreach ($currOrderList as $k => $order) {
$courierName = Db::connect('logistics')->name('logistics')->where(['order_sn'=>$order['order_sn']])->value('courier_name');
$currOrderList[$k]['courier'] = $courierName;
$currOrderList[$k]['status'] = $this->getStatusDesc($order['status']);
}
// 待取货订单数
$pendingOrderCount = $pendingPickupOrderCountQuery->count();
// 未配送订单数
$undeliveredOrderCount = $undeliveredOrderCountQuery->count();
// 已完成订单数
$doneOrderCountQuery = $doneOrderCountQuery->count();
return app('json')->success(compact('currOrderCount', 'pendingOrderCount', 'undeliveredOrderCount', 'doneOrderCountQuery', 'currOrderList'));
} catch (ValidateException $e) {
throw new ValidateException($e->getMessage());
}
}
// 镇级订单数排行榜
public function orderRanking()
{
$type = $this->request->get('type',2); // 1今日 2总计
$townList = Db::name('geo_street')->field('street_code,street_name')->where('area_code', $this->areaCode)->select()->toArray(); // 镇/街道列表
$orderCount = 0;
$townOrderList = [];
foreach ($townList as $town) {
// 查询订单数
$orderCountQuery = Db::name('product_order_log')->where('street_code', $town['street_code'])->where('status', 1);
if ($type == 1) {
$orderCountQuery->whereDay('create_time', 'today');
}
$tempOrderCount = $orderCountQuery->count();
$town['order_count'] = $tempOrderCount;
$orderCount += $tempOrderCount;
$townOrderList[] = $town;
}
// $orderRankingQuery = Db::name('product_order_log')->alias('op')
// ->leftJoin('geo_street s','op.street_code = s.street_code')
// ->field('op.street_code,COUNT(op.order_id) AS order_count,s.street_name')
// ->where('op.district_code',$this->areaCode)
// ->where('op.status',1);
//
// if ($type == 1) {
// $orderCountQuery->whereDay('create_time', 'today');
// }
// $orderRankingList = $orderRankingQuery->group('op.street_code')->order('order_count desc')->select();
$orderCountArr = array_column($townOrderList, 'order_count');
array_multisort($orderCountArr, SORT_DESC, $townOrderList);
return app('json')->success(compact('orderCount', 'townOrderList'));
}
public function getStatusDesc($status)
{
// 订单状态0待发货1待收货2待评价3已完成 9: 拼团中 10: 待付尾款 11:尾款超时未付 -1已退款
$desc = [
0 => '待取货',
1 => '待配送',
2 => '待评价',
3 => '已完成',
9 => '拼团中',
10 => '待付尾款',
11 => '尾款超时未付',
-1 => '已退款',
];
if (!isset($desc[$status])) {
return '未知';
}
return $desc[$status];
}
// 配送商品排行榜
public function deliveredProductRanking()
{
// 查到镇级
if ($this->areaCode != '' && $this->streetCode != '') {
$list = Db::query("SELECT p.store_name, SUM(o.`total_num`) AS total_quantity
FROM `eb_store_product` p
JOIN `eb_store_order_product` op ON p.product_id = op.product_id
JOIN `eb_store_order` o ON o.`order_id` = op.`order_id`
JOIN `eb_product_order_log` opg ON o.`order_id`= opg.`order_id`
WHERE opg.`street_code`= '{$this->streetCode}'
GROUP BY p.store_name
ORDER BY total_quantity DESC
LIMIT 50");
} else {
// 查到区县级
$list = Db::query("SELECT p.store_name, SUM(o.`total_num`) AS total_quantity
FROM `eb_store_product` p
JOIN `eb_store_order_product` op ON p.product_id = op.product_id
JOIN `eb_store_order` o ON o.`order_id` = op.`order_id`
JOIN `eb_product_order_log` opg ON o.`order_id`= opg.`order_id`
WHERE opg.`district_code`= '{$this->areaCode}'
GROUP BY p.store_name
ORDER BY total_quantity DESC
LIMIT 50");
}
return app('json')->success($list);
}
// 首页 镇地图统计信息
public function townMapCount()
{
$townList = Db::connect('work_task')->name('geo_street')->field('street_name, street_code, lng, lat')->where('area_code', $this->areaCode)->select()->toArray();
foreach ($townList as &$town) {
// 店铺数
$town['mer_count'] = Db::name('merchant')->where('street_id', $town['street_code'])->count();
// 小组服务团队数
$town['service_group_count'] = Db::connect('work_task')->name('company')->where(['street'=> $town['street_code'], 'company_type'=>18])->count();
}
unset($town);
return app('json')->success(compact('townList'));
}
// 第二页 时间段订单统计
public function dateRangeOrderCount()
{
$list = [];
// 00:00-02:00
$startTime0 = strtotime(date('Y-m-d', time()));
$endTime2 = strtotime(date('Y-m-d 02:00:00'));
$list[] = $this->getTimeRangeOrderCount([$startTime0, $endTime2]);
// 02:00-04:00
$startTime2 = strtotime(date('Y-m-d 02:00:00'));
$endTime4 = strtotime(date('Y-m-d 04:00:00'));
$list[] = $this->getTimeRangeOrderCount([$startTime2, $endTime4]);
// 04:00-06:00
$startTime4 = strtotime(date('Y-m-d 04:00:00'));
$endTime6 = strtotime(date('Y-m-d 06:00:00'));
$list[] = $this->getTimeRangeOrderCount([$startTime4, $endTime6]);
// 06:00-08:00
$startTime6 = strtotime(date('Y-m-d 06:00:00'));
$endTime8 = strtotime(date('Y-m-d 08:00:00'));
$list[] = $this->getTimeRangeOrderCount([$startTime6, $endTime8]);
// 08:00-10:00
$startTime8 = strtotime(date('Y-m-d 08:00:00'));
$endTime10 = strtotime(date('Y-m-d 10:00:00'));
$list[] = $this->getTimeRangeOrderCount([$startTime8, $endTime10]);
// 10:00-12:00
$startTime10 = strtotime(date('Y-m-d 10:00:00'));
$endTime12 = strtotime(date('Y-m-d 12:00:00'));
$list[] = $this->getTimeRangeOrderCount([$startTime10, $endTime12]);
// 12:00-14:00
$startTime12 = strtotime(date('Y-m-d 12:00:00'));
$endTime14 = strtotime(date('Y-m-d 14:00:00'));
$list[] = $this->getTimeRangeOrderCount([$startTime12, $endTime14]);
// 14:00-16:00
$startTime14 = strtotime(date('Y-m-d 14:00:00'));
$endTime16 = strtotime(date('Y-m-d 16:00:00'));
$list[] = $this->getTimeRangeOrderCount([$startTime14, $endTime16]);
// 16:00-18:00
$startTime16 = strtotime(date('Y-m-d 16:00:00'));
$endTime18 = strtotime(date('Y-m-d 18:00:00'));
$list[] = $this->getTimeRangeOrderCount([$startTime16, $endTime18]);
// 18:00-20:00
$startTime18 = strtotime(date('Y-m-d 18:00:00'));
$endTime20 = strtotime(date('Y-m-d 20:00:00'));
$list[] = $this->getTimeRangeOrderCount([$startTime18, $endTime20]);
// 20:00-22:00
$startTime20 = strtotime(date('Y-m-d 20:00:00'));
$endTime22 = strtotime(date('Y-m-d 22:00:00'));
$list[] = $this->getTimeRangeOrderCount([$startTime20, $endTime22]);
// 22:00-24:00
$startTime22 = strtotime(date('Y-m-d 22:00:00'));
$endTime24 = strtotime(date('Y-m-d 24:00:00'));
$list[] = $this->getTimeRangeOrderCount([$startTime22, $endTime24]);
return app('json')->success($list);
}
private function getTimeRangeOrderCount($timeRange)
{
$hourOrderCountQuery = Db::name('store_order')->alias('o')
->field(['o.order_sn', 'o.real_name', 'o.user_phone', 'o.user_address', 'o.user_address_code', 'p.store_name', 'm.mer_name', 'o.create_time', 'o.status'])
->leftJoin('product_order_log og', 'o.order_id = og.order_id')
->leftJoin('merchant m', 'o.mer_id = m.mer_id')
->leftJoin('store_order_product op', 'o.order_id = op.order_id')
->leftJoin('product_library p', 'op.product_id = p.id')
->whereTime('og.create_time', 'between', $timeRange) // whereTime('create_time', 'between', [$a[0],$a[1]]);
->where('o.paid', 1)
->whereNotNull('o.pay_time');
// 待取货订单数统计query 订单待发货
$pendingPickupOrderCountQuery = Db::name('store_order')->alias('o')
->leftJoin('product_order_log og', 'o.order_id = og.order_id')
->whereTime('og.create_time', 'between', $timeRange)
->where('o.status', 0)
->where('o.paid', 1)
->whereNotNull('o.pay_time');;
// 未配送订单数统计query 订单待收货
$undeliveredOrderCountQuery = Db::name('store_order')->alias('o')
->leftJoin('product_order_log og', 'o.order_id = og.order_id')
->whereTime('og.create_time', 'between', $timeRange)
->where('o.status', 1)
->where('o.paid', 1)
->whereNotNull('o.pay_time');;
// 已完成订单数统计query 订单已完成
$doneOrderCountQuery = Db::name('store_order')->alias('o')
->leftJoin('product_order_log og', 'o.order_id = og.order_id')
->whereTime('og.create_time', 'between', $timeRange)
->whereIn('o.status', [2,3])
->where('o.paid', 1)
->whereNotNull('o.pay_time');
if ($this->areaCode != '') {
$hourOrderCountQuery->where('og.district_code', $this->areaCode);
$pendingPickupOrderCountQuery->where('og.district_code', $this->areaCode);
$undeliveredOrderCountQuery->where('og.district_code', $this->areaCode);
$doneOrderCountQuery->where('og.district_code', $this->areaCode);
} else if ($this->streetCode != '') {
$hourOrderCountQuery->where('og.street_code', $this->streetCode);
$pendingPickupOrderCountQuery->where('og.street_code', $this->streetCode);
$undeliveredOrderCountQuery->where('og.street_code', $this->streetCode);
$doneOrderCountQuery->where('og.street_code', $this->streetCode);
}
// 今日订单数
$hourOrderCount = $hourOrderCountQuery->count();
// 待取货订单数
$pendingOrderCount = $pendingPickupOrderCountQuery->count();
// 未配送订单数
$undeliveredOrderCount = $undeliveredOrderCountQuery->count();
// 已完成订单数
$doneOrderCount = $doneOrderCountQuery->count();
return compact('hourOrderCount', 'pendingOrderCount', 'undeliveredOrderCount', 'doneOrderCount');
}
}