201 lines
8.5 KiB
PHP
201 lines
8.5 KiB
PHP
<?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);
|
||
}
|
||
} |