156 lines
6.5 KiB
PHP
156 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace app\controller\api\dataview;
|
|
|
|
use app\common\repositories\article\ArticleRepository as repository;
|
|
use crmeb\basic\BaseController;
|
|
use GuzzleHttp\Exception\ClientException;
|
|
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, repository $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);
|
|
}
|
|
|
|
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) {
|
|
$getUrl = env('LOGISTICS_HOST_URL') . '/api/courierData?order_sn=' . $order['order_sn'];
|
|
$response = $client->request('GET', $getUrl);
|
|
$courierData = json_decode($response->getBody(), true);
|
|
$currOrderList[$k]['courier'] = $courierData['data']['courier_name'];
|
|
}
|
|
|
|
// 待取货订单数
|
|
$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;
|
|
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;
|
|
}
|
|
unset($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($townList, 'order_count');
|
|
array_multisort($orderCountArr, SORT_DESC, $townList);
|
|
return app('json')->success(compact('orderCount', 'townList'));
|
|
}
|
|
} |