2023-12-02 14:45:58 +08:00

177 lines
7.2 KiB
PHP
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\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'];
$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'));
}
private 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];
}
}