732 lines
32 KiB
PHP
Executable File
732 lines
32 KiB
PHP
Executable File
<?php
|
||
|
||
namespace app\controller\api\dataview;
|
||
|
||
use app\common\dao\store\order\StoreOrderDao;
|
||
use app\common\dao\store\order\StoreRefundOrderDao;
|
||
use app\common\model\store\order\StoreOrder;
|
||
use app\common\repositories\BaseRepository;
|
||
use app\common\repositories\store\order\StoreOrderRepository;
|
||
use app\common\repositories\store\order\StoreRefundOrderRepository;
|
||
use crmeb\basic\BaseController;
|
||
use Exception;
|
||
use think\App;
|
||
use think\exception\ValidateException;
|
||
use think\facade\Db;
|
||
|
||
class Order extends BaseController
|
||
{
|
||
/**
|
||
* @var repository
|
||
*/
|
||
protected $repository;
|
||
|
||
public $areaCode; // 区县地区码
|
||
|
||
public $streetCode; // 镇街道地区码
|
||
|
||
public $token;
|
||
public function __construct(App $app, BaseRepository $repository)
|
||
{
|
||
parent::__construct($app);
|
||
$this->repository = $repository;
|
||
$this->token = trim($this->request->header('X-Token'));
|
||
$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 = 'today'; // 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('store_product p', 'op.product_id = p.product_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(op.`product_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(op.`product_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 01:59:59'));
|
||
$list[] = $this->getTimeRangeOrderCount([$startTime0, $endTime2]);
|
||
|
||
// 02:00-04:00
|
||
$startTime2 = strtotime(date('Y-m-d 02:00:00'));
|
||
$endTime4 = strtotime(date('Y-m-d 03:59:59'));
|
||
$list[] = $this->getTimeRangeOrderCount([$startTime2, $endTime4]);
|
||
|
||
// 04:00-06:00
|
||
$startTime4 = strtotime(date('Y-m-d 04:00:00'));
|
||
$endTime6 = strtotime(date('Y-m-d 05:59:59'));
|
||
$list[] = $this->getTimeRangeOrderCount([$startTime4, $endTime6]);
|
||
|
||
// 06:00-08:00
|
||
$startTime6 = strtotime(date('Y-m-d 06:00:00'));
|
||
$endTime8 = strtotime(date('Y-m-d 07:59:59'));
|
||
$list[] = $this->getTimeRangeOrderCount([$startTime6, $endTime8]);
|
||
|
||
// 08:00-10:00
|
||
$startTime8 = strtotime(date('Y-m-d 08:00:00'));
|
||
$endTime10 = strtotime(date('Y-m-d 09:59:59'));
|
||
$list[] = $this->getTimeRangeOrderCount([$startTime8, $endTime10]);
|
||
|
||
// 10:00-12:00
|
||
$startTime10 = strtotime(date('Y-m-d 10:00:00'));
|
||
$endTime12 = strtotime(date('Y-m-d 11:59:59'));
|
||
$list[] = $this->getTimeRangeOrderCount([$startTime10, $endTime12]);
|
||
|
||
// 12:00-14:00
|
||
$startTime12 = strtotime(date('Y-m-d 12:00:00'));
|
||
$endTime14 = strtotime(date('Y-m-d 13:59:59'));
|
||
$list[] = $this->getTimeRangeOrderCount([$startTime12, $endTime14]);
|
||
|
||
// 14:00-16:00
|
||
$startTime14 = strtotime(date('Y-m-d 14:00:00'));
|
||
$endTime16 = strtotime(date('Y-m-d 15:59:59'));
|
||
$list[] = $this->getTimeRangeOrderCount([$startTime14, $endTime16]);
|
||
|
||
// 16:00-18:00
|
||
$startTime16 = strtotime(date('Y-m-d 16:00:00'));
|
||
$endTime18 = strtotime(date('Y-m-d 17:59:59'));
|
||
$list[] = $this->getTimeRangeOrderCount([$startTime16, $endTime18]);
|
||
|
||
// 18:00-20:00
|
||
$startTime18 = strtotime(date('Y-m-d 18:00:00'));
|
||
$endTime20 = strtotime(date('Y-m-d 19:59:59'));
|
||
$list[] = $this->getTimeRangeOrderCount([$startTime18, $endTime20]);
|
||
|
||
// 20:00-22:00
|
||
$startTime20 = strtotime(date('Y-m-d 20:00:00'));
|
||
$endTime22 = strtotime(date('Y-m-d 21:59:59'));
|
||
$list[] = $this->getTimeRangeOrderCount([$startTime20, $endTime22]);
|
||
|
||
// 22:00-24:00
|
||
$startTime22 = strtotime(date('Y-m-d 22:00:00'));
|
||
$endTime24 = strtotime(date('Y-m-d 23:59:59'));
|
||
$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('store_product p', 'op.product_id = p.product_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');
|
||
}
|
||
|
||
// 区县订单数据统计
|
||
public function orderStatistics()
|
||
{
|
||
$list = [];
|
||
// 该地区下所有乡镇
|
||
$geoStreetList = Db::name('geo_street')->field('street_name, street_code')->where('area_code',$this->areaCode)->select()->toArray();
|
||
foreach ($geoStreetList as $k=>$street) {
|
||
|
||
$dayOrderquery = 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')
|
||
->where('o.paid', 1)
|
||
->whereNotNull('o.pay_time')
|
||
->whereDay('o.create_time', 'today')
|
||
->where('og.street_code', $street['street_code']);
|
||
|
||
$monthOrderquery = 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')
|
||
->where('o.paid', 1)
|
||
->whereNotNull('o.pay_time')
|
||
->whereMonth('o.create_time', 'this month')
|
||
->where('og.street_code', $street['street_code']);
|
||
// 日订单数
|
||
$street['dayOrderCount'] = $dayOrderquery->count();
|
||
// 日订单金额
|
||
$street['dayOrderAmount'] = $dayOrderquery->sum('o.total_price');
|
||
// 月订单数
|
||
$street['monthOrderCount'] = $monthOrderquery->count();
|
||
// 月订单金额
|
||
$street['monthOrderAmount'] = $monthOrderquery->sum('o.total_price');
|
||
$list[$k] = $street;
|
||
}
|
||
unset($street);
|
||
return \app('json')->success(compact('list'));
|
||
}
|
||
|
||
// 镇/街道 当日订单金额
|
||
public function streetCurrDayOrderCount()
|
||
{
|
||
if ($this->streetCode == '') {
|
||
return app('json')->fail('请选择镇/街道');
|
||
}
|
||
$list = [];
|
||
// 00:00-04:00
|
||
$startTime = strtotime(date('Y-m-d 00:00:00'));
|
||
$endTime = strtotime(date('Y-m-d 03:59:59'));
|
||
$list[] = $this->getTimeRangeOrderAmount([$startTime, $endTime]);
|
||
|
||
// 04:00-08:00
|
||
$startTime = strtotime(date('Y-m-d 04:00:00'));
|
||
$endTime = strtotime(date('Y-m-d 07:59:59'));
|
||
$list[] = $this->getTimeRangeOrderAmount([$startTime, $endTime]);
|
||
|
||
// 08:00-12:00
|
||
$startTime = strtotime(date('Y-m-d 08:00:00'));
|
||
$endTime = strtotime(date('Y-m-d 11:59:59'));
|
||
$list[] = $this->getTimeRangeOrderAmount([$startTime, $endTime]);
|
||
|
||
// 12:00-16:00
|
||
$startTime = strtotime(date('Y-m-d 12:00:00'));
|
||
$endTime = strtotime(date('Y-m-d 15:59:59'));
|
||
$list[] = $this->getTimeRangeOrderAmount([$startTime, $endTime]);
|
||
|
||
// 16:00-20:00
|
||
$startTime = strtotime(date('Y-m-d 16:00:00'));
|
||
$endTime = strtotime(date('Y-m-d 19:59:59'));
|
||
$list[] = $this->getTimeRangeOrderAmount([$startTime, $endTime]);
|
||
|
||
// 20:00-24:00
|
||
$startTime = strtotime(date('Y-m-d 20:00:00'));
|
||
$endTime = strtotime(date('Y-m-d 23:59:59'));
|
||
$list[] = $this->getTimeRangeOrderAmount([$startTime, $endTime]);
|
||
|
||
return app('json')->success($list);
|
||
}
|
||
|
||
public function getTimeRangeOrderAmount($timeRange=[])
|
||
{
|
||
$todayAmount = 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')
|
||
->where('o.paid', 1)
|
||
->whereNotNull('o.pay_time')
|
||
->whereTime('o.create_time', 'between', $timeRange)
|
||
->where('og.street_code', $this->streetCode)
|
||
->sum('o.total_price');
|
||
|
||
$yesterdayStartTime = strtotime('-1 days', $timeRange[0]);
|
||
$yesterdayEndTime = strtotime('-1 days', $timeRange[1]);
|
||
|
||
$yesterdayAmount = 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')
|
||
->where('o.paid', 1)
|
||
->whereNotNull('o.pay_time')
|
||
->whereTime('o.create_time', 'between', [$yesterdayStartTime, $yesterdayEndTime])
|
||
->where('og.street_code', $this->streetCode)
|
||
->sum('o.total_price');
|
||
|
||
return compact('todayAmount', 'yesterdayAmount');
|
||
}
|
||
|
||
// 平台商品/店铺销量排行
|
||
public function salesRanking()
|
||
{
|
||
$list = [];
|
||
|
||
// 商品销量排行榜
|
||
$productRankingList = Db::name('store_product')->alias('p')
|
||
->field('p.product_id, p.store_name, p.image, COUNT(o.`order_id`) AS total_sales')
|
||
->join('store_order_product op', 'p.product_id = op.product_id')
|
||
->join('store_order o', 'op.order_id = o.order_id')
|
||
->join('product_order_log og', 'o.order_id = og.order_id')
|
||
->where(function($query) {
|
||
if ($this->streetCode != '') {
|
||
$query->where('og.street_code', $this->streetCode);
|
||
} else {
|
||
$query->where('og.district_code', $this->areaCode);
|
||
}
|
||
})
|
||
->group('p.product_id')
|
||
->order('total_sales DESC')
|
||
->limit(10)
|
||
->select()->toArray();
|
||
|
||
$productRankingTotal = 0;
|
||
foreach ($productRankingList as $k => $v) {
|
||
$productRankingTotal += $v['total_sales'];
|
||
}
|
||
|
||
// 店铺销量排行榜
|
||
$merchantRankingList = Db::name('store_order')->alias('o')
|
||
->field('m.`mer_id`, m.`mer_name`, m.mini_banner, COUNT(o.`order_id`) AS total_sales')
|
||
->join('merchant m', 'o.`mer_id` = m.`mer_id`')
|
||
->where(function($query) {
|
||
if ($this->streetCode != '') {
|
||
$query->where('m.street_id', $this->streetCode);
|
||
} else {
|
||
$query->where('m.area_id', $this->areaCode);
|
||
}
|
||
})
|
||
->group('m.mer_id')
|
||
->order('total_sales DESC')
|
||
->limit(10)
|
||
->select()->toArray();
|
||
|
||
$merchantRankingTotal = 0;
|
||
foreach ($merchantRankingList as $k => $v) {
|
||
$merchantRankingTotal += $v['total_sales'];
|
||
}
|
||
|
||
// 统计每个镇的商品数.
|
||
$townProductCount = 0;
|
||
$townProductCountList = [];
|
||
$geoStreetList = Db::name('geo_street')->field('street_name, street_code')->where('area_code',$this->areaCode)->select()->toArray();
|
||
foreach ($geoStreetList as $k => $street) {
|
||
$street['product_count'] = Db::name('merchant')->alias('m')->field('p.`product_id`')
|
||
->join('store_product p', 'm.mer_id = p.mer_id')
|
||
->where('m.street_id', $street['street_code'])
|
||
->count();
|
||
$townProductCountList[] = $street;
|
||
$townProductCount += $street['product_count'];
|
||
}
|
||
return \app('json')->success(compact('productRankingTotal','productRankingList', 'merchantRankingTotal','merchantRankingList', 'townProductCount','townProductCountList'));
|
||
}
|
||
|
||
// 当日订单金额
|
||
public function currDayOrderAmount()
|
||
{
|
||
$geoStreetList = Db::name('geo_street')->field('street_name, street_code')->where('area_code',$this->areaCode)->select()->toArray();
|
||
foreach ($geoStreetList as &$street) {
|
||
|
||
$street['today_order_amount'] = 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')
|
||
->where('o.paid', 1)
|
||
->whereNotNull('o.pay_time')
|
||
->whereDay('o.create_time', 'today')
|
||
->where('og.street_code', $street['street_code'])
|
||
->sum('o.total_price');
|
||
|
||
$street['yesterday_order_amount'] = 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')
|
||
->where('o.paid', 1)
|
||
->whereNotNull('o.pay_time')
|
||
->whereDay('o.create_time', 'yesterday')
|
||
->where('og.street_code', $street['street_code'])
|
||
->sum('o.total_price');
|
||
}
|
||
unset($street);
|
||
|
||
return \app('json')->success(compact('geoStreetList'));
|
||
}
|
||
|
||
|
||
// 订单列表
|
||
public function orderList(StoreOrderRepository $repository, StoreOrderDao $dao)
|
||
{
|
||
[$page, $limit] = $this->getPage();
|
||
$where = $this->request->params(['type', 'date', 'mer_id','keywords','status','username','order_sn','is_trader','activity_type','group_order_sn','store_name']);
|
||
$status = $where['status'];
|
||
unset($where['status']);
|
||
$query = $dao->search($where, null)->where($repository->getOrderType($status))
|
||
->with([
|
||
'orderProduct',
|
||
'merchant' => function ($query) {
|
||
return $query->field('mer_id,mer_name,is_trader');
|
||
},
|
||
'groupOrder' => function ($query) {
|
||
$query->field('group_order_id,group_order_sn');
|
||
},
|
||
'user' => function ($query) {
|
||
$query->field('uid,nickname,avatar');
|
||
},
|
||
])->join('product_order_log og', 'StoreOrder.order_id = og.order_id')
|
||
->where(function($query) {
|
||
if ($this->streetCode != '') {
|
||
$query->where('og.street_code', $this->streetCode);
|
||
} else {
|
||
$query->where('og.district_code', $this->areaCode);
|
||
}
|
||
});
|
||
$count = $query->count();
|
||
$list = $query->page($page, $limit)->select();
|
||
return app('json')->success(compact('count', 'list'));
|
||
}
|
||
|
||
// 订单列表统计标题
|
||
public function orderCountTitle(StoreOrderRepository $repository)
|
||
{
|
||
$where = $this->request->params(['type', 'date', 'mer_id','keywords','status','username','order_sn','is_trader','activity_type']);
|
||
$data[0] = $repository->getStat($where, $where['status'])[0];
|
||
$data[1] = $repository->getStat($where, $where['status'])[1];
|
||
$data[2] = $repository->getStat($where, $where['status'])[2];
|
||
return app('json')->success($data);
|
||
}
|
||
|
||
// 退款订单列表
|
||
public function refundOrderList(StoreRefundOrderRepository $repository)
|
||
{
|
||
try{
|
||
[$page,$limit] = $this->getPage();
|
||
$where = $this->request->params(['refund_order_sn','status','refund_type','date','mer_id','order_sn','is_trader']);
|
||
$list = $repository->getAllList($where, $page, $limit);
|
||
$list['list'] = $list['list']->toArray();
|
||
foreach($list['list'] as &$item) {
|
||
if(!empty($item['order'])) {
|
||
$userAddressCode = explode(',', $item['order']['user_address_code']??'')??[];
|
||
if(!empty($userAddressCode)) {
|
||
$area = Db::name('geo_area')->where('area_code', $userAddressCode[2])->value('area_name');
|
||
$street = Db::name('geo_street')->where('street_code', $userAddressCode[3])->value('street_name');
|
||
$item['order']['order_from'] = $area.$street;
|
||
} else {
|
||
$item['order']['order_from'] = '';
|
||
}
|
||
}
|
||
}
|
||
unset($item);
|
||
return app('json')->success($list);
|
||
} catch(Exception $e) {
|
||
throw new ValidateException($e->getFile().$e->getLine().$e->getMessage());
|
||
}
|
||
|
||
}
|
||
|
||
// 核销订单
|
||
public function takeOrderList(StoreOrderRepository $repository, StoreOrderDao $dao)
|
||
{
|
||
[$page, $limit] = $this->getPage();
|
||
$where = $this->request->params(['date','order_sn','keywords','username','is_trader']);
|
||
$where['take_order'] = 1;
|
||
$where['status'] = '';
|
||
$where['verify_date'] = $where['date'];
|
||
unset($where['date']);
|
||
$status = $where['status'];
|
||
unset($where['status']);
|
||
$query = $dao->search($where, null)->where($repository->getOrderType($status))->with([
|
||
'orderProduct',
|
||
'merchant' => function ($query) {
|
||
return $query->field('mer_id,mer_name,is_trader');
|
||
},
|
||
'groupOrder' => function ($query) {
|
||
$query->field('group_order_id,group_order_sn');
|
||
},
|
||
'user' => function ($query) {
|
||
$query->field('uid,nickname,avatar');
|
||
},
|
||
]);
|
||
$count = $query->count();
|
||
$list = $query->page($page, $limit)->select();
|
||
foreach($list as &$item) {
|
||
if(!empty($item['order'])) {
|
||
$userAddressCode = explode(',', $item['user_address_code']??'')??[];
|
||
if(!empty($userAddressCode)){
|
||
$area = Db::name('geo_area')->where('area_code', $userAddressCode[2])->value('area_name');
|
||
$street = Db::name('geo_street')->where('street_code', $userAddressCode[3])->value('street_name');
|
||
$item['order_from'] = $area.$street;
|
||
} else{
|
||
$item['order_from'] = '';
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
return app('json')->success(compact('count', 'list'));
|
||
}
|
||
|
||
public function takeOrderCountTitle(StoreOrderRepository $repository)
|
||
{
|
||
$where = $this->request->params(['date','order_sn','keywords','username','is_trader']);
|
||
$where['take_order'] = 1;
|
||
$where['status'] = '';
|
||
$where['verify_date'] = $where['date'];
|
||
unset($where['date']);
|
||
$data[0] = $repository->getStat($where, '')[0];
|
||
$data[1] = $repository->getStat($where, '')[1];
|
||
$data[2] = $repository->getStat($where, '')[2];
|
||
return app('json')->success($data);
|
||
}
|
||
|
||
public function orderUserNumCount()
|
||
{
|
||
// 订单数
|
||
$orderNum = $this->dayOrderNum('today');
|
||
|
||
$yesterdayNum = $this->dayOrderNum('yesterday');
|
||
$monthOrderNum = $this->dayOrderNum(date('Y/m/d', strtotime('first day of')) . ' 00:00:00' . '-' . date('Y/m/d H:i:s'));
|
||
|
||
$date = date('Y/m/01 00:00:00', strtotime('last Month')) . '-' . date('Y/m/d 00:00:00', strtotime('-1 day', strtotime('first day of')));
|
||
$beforeOrderNum = $this->dayOrderNum($date);
|
||
|
||
$monthOrderNumRate = $this->getRate($beforeOrderNum, $monthOrderNum);
|
||
$orderNumRate = $this->getRate($yesterdayNum, $orderNum);
|
||
|
||
|
||
// 支付数
|
||
$orderPayNum = $this->dayOrderUserNum('today');
|
||
$yesterdayNum = $this->dayOrderUserNum('yesterday');
|
||
$monthOrderPayNum = $this->dayOrderUserNum(date('Y/m/d', strtotime('first day of')) . ' 00:00:00' . '-' . date('Y/m/d H:i:s'));
|
||
|
||
$date = gmdate('Y/m/01 00:00:00', strtotime('last Month')) . '-' . date('Y/m/d 00:00:00', strtotime('-1 day', strtotime('first day of')));
|
||
$beforeOrderNum = $this->dayOrderUserNum($date);
|
||
|
||
$monthOrderPayRate = $this->getRate($beforeOrderNum, $monthOrderNum);
|
||
$orderOrderPayRate = $this->getRate($yesterdayNum, $orderNum);
|
||
|
||
return app('json')->success(compact('orderNum', 'monthOrderNum', 'monthOrderNumRate', 'orderNumRate', 'orderPayNum', 'monthOrderPayNum', 'monthOrderPayRate', 'orderOrderPayRate'));
|
||
}
|
||
|
||
public function dayOrderNum($day)
|
||
{
|
||
return StoreOrder::getDB()->alias('o')
|
||
->join('product_order_log pog', 'o.order_id=pog.order_id')
|
||
->where('o.paid', 1)
|
||
->when($day, function ($query, $day) {
|
||
getModelTime($query, $day, 'o.pay_time');
|
||
})
|
||
->where('pog.street_code', $this->streetCode)
|
||
->count();
|
||
}
|
||
|
||
public function dayOrderUserNum($day, $merId = null)
|
||
{
|
||
return StoreOrder::getDB()->alias('o')
|
||
->join('product_order_log pog', 'o.order_id=pog.order_id')
|
||
->where('o.paid', 1)
|
||
->when($day, function ($query, $day) {
|
||
getModelTime($query, $day, 'o.pay_time');
|
||
})
|
||
->where('pog.street_code', $this->streetCode)->group('o.uid')->count();
|
||
}
|
||
|
||
protected function getRate($last, $today, $scale = 2)
|
||
{
|
||
if ($last == $today)
|
||
return 0;
|
||
else if ($last == 0)
|
||
return $today;
|
||
else if ($today == 0)
|
||
return -$last;
|
||
else
|
||
return (float)bcdiv(bcsub($today, $last, $scale), $last, $scale);
|
||
}
|
||
|
||
} |