2023-12-02 19:52:39 +08:00
< ? php
namespace app\controller\api\dataview ;
2023-12-04 16:00:35 +08:00
use app\common\model\Company ;
2024-01-02 15:54:43 +08:00
use app\common\model\store\order\StoreOrderProduct ;
2023-12-02 19:52:39 +08:00
use app\common\repositories\BaseRepository ;
use crmeb\basic\BaseController ;
use think\App ;
use think\exception\ValidateException ;
2023-12-04 10:40:20 +08:00
use think\facade\Db ;
2023-12-02 19:52:39 +08:00
class Logistics extends BaseController
{
2023-12-06 14:05:22 +08:00
/**
* @ var repository
*/
protected $repository ;
public $areaCode ; // 区县地区码
public $streetCode ; // 镇街道地区码
2023-12-02 19:52:39 +08:00
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 ( '请选择地区' );
}
}
2023-12-05 16:18:39 +08:00
// 三轮车列表 每个乡镇订单最多的配送员的三轮车
2023-12-02 19:52:39 +08:00
public function vehicleList ()
{
2023-12-05 16:18:39 +08:00
// 查区县的镇农科公司
$list = [];
2023-12-22 14:28:51 +08:00
$companyList = Db :: connect ( 'work_task' ) -> name ( 'company' ) -> where ( 'id' , 69 ) -> where ([ 'area' => $this -> areaCode , 'company_type' => 41 ]) -> select () -> toArray ();
2023-12-05 16:18:39 +08:00
foreach ( $companyList as $company ) {
// 先从供销系统 查出镇下边的所有配送员-小组服务公司的负责人
$serviceGroupCompanyIds = Db :: connect ( 'work_task' ) -> name ( 'company' )
-> where ([ 'street' => $company [ 'street' ], 'company_type' => 18 ])
-> column ( 'id' );
2023-12-22 14:28:51 +08:00
if ( empty ( $serviceGroupCompanyIds )) {
continue ;
}
2023-12-05 16:18:39 +08:00
$userIdList = Db :: connect ( 'work_task' ) -> name ( 'user' )
-> whereIn ( 'company_id' , $serviceGroupCompanyIds )
-> where ([ 'group_id' => 5 ])
2023-12-22 14:28:51 +08:00
-> field ( 'id, nickname' ) -> select () -> toArray ();
if ( empty ( $userIdList )) {
continue ;
}
2023-12-05 16:18:39 +08:00
// 从物流系统 查物流订单排序 确定谁是 镇辖区内配送订单最多的配送员
$topCourier = Db :: connect ( 'logistics' ) -> name ( 'logistics' )
-> field ([ 'courier_id' , 'courier_name' , 'COUNT(order_id) AS order_count' ])
2023-12-22 14:28:51 +08:00
-> whereIn ( 'courier_id' , array_column ( $userIdList , 'id' ))
2023-12-05 16:18:39 +08:00
-> group ( 'courier_id' )
-> order ( 'order_count DESC' )
-> find ();
2023-12-22 14:28:51 +08:00
if ( ! empty ( $userIdList ) && empty ( $topCourier )) {
$topCourier [ 'courier_id' ] = $userIdList [ 0 ][ 'id' ];
$topCourier [ 'courier_name' ] = $userIdList [ 0 ][ 'nickname' ];
$topCourier [ 'order_count' ] = 0 ;
}
2023-12-05 16:18:39 +08:00
// 小组公司没有配送员或是没有三轮车
if ( empty ( $topCourier )) {
continue ;
}
// 三轮车车牌号 根据配送员id反查公司id, 公司id反查车牌号
$courier = Db :: connect ( 'work_task' ) -> name ( 'user' ) -> where ([ 'id' => $topCourier [ 'courier_id' ]]) -> find ();
2023-12-22 14:28:51 +08:00
if ( empty ( $courier )) {
continue ;
}
2023-12-05 16:18:39 +08:00
$vehicleRent = Db :: connect ( 'work_task' ) -> name ( 'vehicle_rent' ) -> where ([ 'rent_company_id' => $courier [ 'company_id' ]]) -> find ();
2023-12-22 14:28:51 +08:00
if ( empty ( $vehicleRent )) {
continue ;
}
2023-12-05 16:18:39 +08:00
$topCourier [ 'id' ] = $vehicleRent [ 'car_id' ];
$topCourier [ 'license' ] = $vehicleRent [ 'car_license' ];
$topCourier [ 'area_code' ] = $courier [ 'area' ];
$topCourier [ 'street_code' ] = $courier [ 'street' ];
$list [] = $topCourier ;
2023-12-04 16:00:35 +08:00
}
2023-12-05 16:18:39 +08:00
2023-12-04 16:00:35 +08:00
// 查镇车辆列表
2023-12-05 16:18:39 +08:00
// $list = Db::connect('work_task')->name('vehicle_rent')
// ->field('company_id, car_id as id, car_license as license')
// ->append(['area_code','street_code'])
// ->whereIn('company_id', $companyIds)
// ->where('status','in','0,1,2')
// ->withAttr('area_code', function ($value, $data){
// $company = Db::connect('work_task')->name('company')->where('id', $data['company_id'])->find();
// return $company['area'];
// })
// ->withAttr('street_code', function ($value, $data){
// $company = Db::connect('work_task')->name('company')->where('id', $data['company_id'])->find();
// return $company['street'];
// })
// ->group('company_id')
// ->select()->toArray();
2023-12-04 16:00:35 +08:00
2023-12-02 19:58:12 +08:00
$count = count ( $list );
return app ( 'json' ) -> success ( compact ( 'count' , 'list' ));
2023-12-02 19:52:39 +08:00
}
2023-12-04 10:40:20 +08:00
2024-01-02 13:58:45 +08:00
// 三轮车订单排行榜
public function vehicleOrderRanking ()
{
// 查区县的镇农科公司
$list = [];
// 先从供销系统 查出镇下边的所有配送员-小组服务公司的负责人
$serviceGroupCompanyIds = Db :: connect ( 'work_task' ) -> name ( 'company' )
-> where ([ 'area' => $this -> areaCode , 'company_type' => 18 ])
-> column ( 'id' );
2024-01-02 15:54:43 +08:00
if ( empty ( $serviceGroupCompanyIds )) {
return app ( 'json' ) -> success ( $list );
}
2024-01-02 13:58:45 +08:00
$userIdList = Db :: connect ( 'work_task' ) -> name ( 'user' )
-> whereIn ( 'company_id' , $serviceGroupCompanyIds )
-> where ([ 'group_id' => 5 ])
-> field ( 'id, nickname' ) -> select () -> toArray ();
// 从物流系统 查物流订单排序
$topCourierList = Db :: connect ( 'logistics' ) -> name ( 'logistics' )
-> field ([ 'courier_id' , 'courier_name' , 'COUNT(order_id) AS order_count' ])
-> whereIn ( 'courier_id' , array_column ( $userIdList , 'id' ))
-> group ( 'courier_id' )
-> order ( 'order_count DESC' )
-> select ();
2024-01-02 15:54:43 +08:00
2024-01-02 13:58:45 +08:00
foreach ( $topCourierList as $topCourier ) {
// 三轮车车牌号 根据配送员id反查公司id, 公司id反查车牌号
$courier = Db :: connect ( 'work_task' ) -> name ( 'user' ) -> where ([ 'id' => $topCourier [ 'courier_id' ]]) -> find ();
2024-01-02 15:54:43 +08:00
2024-01-02 13:58:45 +08:00
$vehicleRent = Db :: connect ( 'work_task' ) -> name ( 'vehicle_rent' ) -> where ([ 'rent_company_id' => $courier [ 'company_id' ]]) -> find ();
2024-01-02 15:54:43 +08:00
if ( empty ( $vehicleRent )) {
continue ;
}
2024-01-02 13:58:45 +08:00
$topCourier [ 'id' ] = $vehicleRent [ 'car_id' ];
$topCourier [ 'license' ] = $vehicleRent [ 'car_license' ];
$topCourier [ 'area_code' ] = $courier [ 'area' ];
$topCourier [ 'street_code' ] = $courier [ 'street' ];
$list [] = $topCourier ;
}
return app ( 'json' ) -> success ( $list );
}
2023-12-04 10:40:20 +08:00
// 镇级最新物流配送详情
public function latestLogistics ()
{
if ( $this -> streetCode == '' ) {
return app ( 'json' ) -> fail ( '未获取到位置信息' );
}
$detail = Db :: name ( 'store_order' ) -> alias ( 'o' )
2023-12-04 13:59:48 +08:00
-> 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' , 'm.area_id' , 'm.street_id' , 'm.village_id' , 'm.mer_address' , 'm.long as mer_long' , 'm.lat as mer_lat' ])
2023-12-04 10:40:20 +08:00
-> 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' )
-> where ( 'og.street_code' , $this -> streetCode )
-> order ( 'o.order_id' , 'desc' )
-> find ();
2023-12-04 16:00:35 +08:00
if ( empty ( $detail )) {
return app ( 'json' ) -> success ([]);
}
2023-12-04 13:59:48 +08:00
// 拼接商户的详细地址 area_id street_id village_id
$area = Db :: name ( 'geo_area' ) -> where ( 'area_code' , $detail [ 'area_id' ]) -> find ();
$city = Db :: name ( 'geo_city' ) -> where ( 'city_code' , $area [ 'city_code' ]) -> find ();
$province = Db :: name ( 'geo_province' ) -> where ( 'province_code' , $city [ 'province_code' ]) -> find ();
$street = Db :: name ( 'geo_street' ) -> where ( 'street_code' , $detail [ 'street_id' ]) -> find ();
$village = Db :: name ( 'geo_village' ) -> where ( 'village_id' , $detail [ 'village_id' ]) -> find ();
$merAddress = $province [ 'province_name' ] . $city [ 'city_name' ] . $area [ 'area_name' ] . $street [ 'street_name' ] . $village [ 'village_name' ] . $detail [ 'mer_address' ];
$detail [ 'mer_address' ] = $merAddress ;
2023-12-04 10:40:20 +08:00
$detail [ 'status' ] = app () -> make ( Order :: class ) -> getStatusDesc ( $detail [ 'status' ]);
return app ( 'json' ) -> success ( $detail );
}
2023-12-04 18:42:46 +08:00
// 第二页 物流信息统计
public function logisticsCount ()
{
2023-12-22 14:35:56 +08:00
$topCourier = [];
2023-12-05 16:21:29 +08:00
// 查询镇辖区内配送订单最多的配送员
// 先从供销系统 查出镇下边的所有配送员-小组服务公司的负责人
$serviceGroupCompanyIds = Db :: connect ( 'work_task' ) -> name ( 'company' )
-> where ([ 'street' => $this -> streetCode , 'company_type' => 18 ])
-> column ( 'id' );
2023-12-22 14:35:56 +08:00
if ( empty ( $serviceGroupCompanyIds )) {
return app ( 'json' ) -> success ( $topCourier );
}
2023-12-05 16:21:29 +08:00
$userIdList = Db :: connect ( 'work_task' ) -> name ( 'user' )
-> whereIn ( 'company_id' , $serviceGroupCompanyIds )
-> where ([ 'group_id' => 5 ])
-> column ( 'id' );
2023-12-22 14:35:56 +08:00
if ( empty ( $userIdList )) {
return app ( 'json' ) -> success ( $topCourier );
}
2023-12-05 16:21:29 +08:00
// 从物流系统 查物流订单排序 确定谁是 镇辖区内配送订单最多的配送员
$topCourier = Db :: connect ( 'logistics' ) -> name ( 'logistics' )
-> field ([ 'courier_id' , 'courier_name' , 'COUNT(order_id) AS order_count' ])
-> whereIn ( 'courier_id' , $userIdList )
-> group ( 'courier_id' )
-> order ( 'order_count DESC' )
-> find ();
2023-12-22 14:35:56 +08:00
if ( ! empty ( $userIdList ) && empty ( $topCourier )) {
$user = Db :: connect ( 'work_task' ) -> name ( 'user' ) -> where ( 'id' , $userIdList [ 0 ]) -> find ();
$topCourier [ 'courier_id' ] = $user [ 'id' ];
$topCourier [ 'courier_name' ] = $user [ 'nickname' ];
$topCourier [ 'order_count' ] = 0 ;
}
if ( empty ( $topCourier )) {
return app ( 'json' ) -> success ( $topCourier );
}
2023-12-05 16:21:29 +08:00
// 返查配送员的物流配送订单统计信息
2023-12-04 18:42:46 +08:00
// 待取货数
2023-12-05 16:21:29 +08:00
$topCourier [ 'pending_order_count' ] = Db :: connect ( 'logistics' ) -> name ( 'logistics' ) -> where ([ 'status' => 0 , 'courier_id' => $topCourier [ 'courier_id' ]]) -> count ();
2023-12-04 18:42:46 +08:00
// 配送中数
2023-12-05 16:21:29 +08:00
$topCourier [ 'delivering_order_count' ] = Db :: connect ( 'logistics' ) -> name ( 'logistics' ) -> where ([ 'status' => 1 , 'courier_id' => $topCourier [ 'courier_id' ]]) -> count ();
2023-12-04 18:42:46 +08:00
// 已完成数
2023-12-05 16:21:29 +08:00
$topCourier [ 'finished_order_count' ] = Db :: connect ( 'logistics' ) -> name ( 'logistics' ) -> where ([[ 'status' , 'in' , [ 2 , 3 ]], 'courier_id' => $topCourier [ 'courier_id' ]]) -> count ();
2023-12-04 18:42:46 +08:00
2023-12-05 16:21:29 +08:00
// 三轮车车牌号 根据配送员id反查公司id, 公司id反查车牌号
$courierCompanyId = Db :: connect ( 'work_task' ) -> name ( 'user' ) -> where ([ 'id' => $topCourier [ 'courier_id' ]]) -> value ( 'company_id' );
$vehicleRent = Db :: connect ( 'work_task' ) -> name ( 'vehicle_rent' ) -> where ([ 'rent_company_id' => $courierCompanyId ]) -> find ();
$topCourier [ 'car_id' ] = $vehicleRent [ 'car_id' ];
$topCourier [ 'car_license' ] = $vehicleRent [ 'car_license' ];
2023-12-04 18:42:46 +08:00
2023-12-05 16:21:29 +08:00
return app ( 'json' ) -> success ( $topCourier );
2023-12-04 18:42:46 +08:00
}
2023-12-05 16:18:39 +08:00
// 第二页 地图 最新的10笔配送订单地址 以及最近一次取货地址发散
public function logisticsMapCount ()
{
$courierId = $this -> request -> param ( 'courier_id' );
2023-12-22 14:39:16 +08:00
$latestOrder = [];
$latestTenOrder = [];
2023-12-05 16:18:39 +08:00
// 最近一次取货地址 最新一笔的配送中订单的取货地址
2023-12-05 16:46:13 +08:00
$latestLogistics = Db :: connect ( 'logistics' ) -> name ( 'logistics' ) -> where ([ 'status' => 1 , 'courier_id' => $courierId ]) -> order ( 'id' , 'desc' ) -> find ();
2023-12-22 14:39:16 +08:00
if ( empty ( $latestLogistics )) {
return app ( 'json' ) -> success ( compact ( 'latestOrder' , 'latestTenOrder' ));
}
2023-12-05 16:46:13 +08:00
$latestOrderInfo = Db :: name ( 'store_order' ) -> where ([ 'order_id' => $latestLogistics [ 'order_id' ]]) -> find ();
$merchant = Db :: name ( 'merchant' ) -> where ([ 'mer_id' => $latestOrderInfo [ 'mer_id' ]]) -> find ();
// 拼接商户的详细地址 area_id street_id village_id
$area = Db :: name ( 'geo_area' ) -> where ( 'area_code' , $merchant [ 'area_id' ]) -> find ();
$city = Db :: name ( 'geo_city' ) -> where ( 'city_code' , $area [ 'city_code' ]) -> find ();
$province = Db :: name ( 'geo_province' ) -> where ( 'province_code' , $city [ 'province_code' ]) -> find ();
$street = Db :: name ( 'geo_street' ) -> where ( 'street_code' , $merchant [ 'street_id' ]) -> find ();
$village = Db :: name ( 'geo_village' ) -> where ( 'village_id' , $merchant [ 'village_id' ]) -> find ();
$merAddress = $province [ 'province_name' ] . $city [ 'city_name' ] . $area [ 'area_name' ] . $street [ 'street_name' ] . $village [ 'village_name' ] . $merchant [ 'mer_address' ];
$latestOrder [ 'mer_address' ] = $merAddress ;
2023-12-05 16:18:39 +08:00
// 最新的10笔订单
2023-12-05 16:46:13 +08:00
$latestTenOrder = Db :: connect ( 'logistics' ) -> name ( 'logistics' ) -> field ( 'receiver_address' ) -> where ([ 'status' => 1 , 'courier_id' => $courierId ]) -> order ( 'id' , 'desc' ) -> limit ( 10 ) -> select () -> toArray ();
return app ( 'json' ) -> success ( compact ( 'latestOrder' , 'latestTenOrder' ));
2023-12-05 16:18:39 +08:00
}
2023-12-09 17:57:57 +08:00
public function logisticsList ()
{
$type = $this -> request -> param ( 'type' );
$courierId = $this -> request -> param ( 'courier_id' );
2023-12-09 18:14:39 +08:00
$startTime = $this -> request -> param ( 'start_time' );
$endTime = $this -> request -> param ( 'end_time' );
2023-12-09 17:57:57 +08:00
$list = [];
$count = 0 ;
if ( $type == 1 ) {
// 待取货
2023-12-09 18:14:39 +08:00
$list = Db :: connect ( 'logistics' ) -> name ( 'logistics' ) -> where ([ 'status' => 0 , 'courier_id' => $courierId ]) -> when ( $startTime && $endTime , function ( $query ) use ( $startTime , $endTime ){
$query -> whereBetween ( 'qh_time' , [ $startTime , $endTime ]);
}) -> select ();
$count = Db :: connect ( 'logistics' ) -> name ( 'logistics' ) -> where ([ 'status' => 0 , 'courier_id' => $courierId ]) -> when ( $startTime && $endTime , function ( $query ) use ( $startTime , $endTime ){
$query -> whereBetween ( 'qh_time' , [ $startTime , $endTime ]);
}) -> count ();
2023-12-09 17:57:57 +08:00
}
if ( $type == 2 ) {
// 配送中
2023-12-09 18:14:39 +08:00
$list = Db :: connect ( 'logistics' ) -> name ( 'logistics' ) -> where ([ 'status' => 1 , 'courier_id' => $courierId ]) -> when ( $startTime && $endTime , function ( $query ) use ( $startTime , $endTime ){
$query -> whereBetween ( 'create_time' , [ $startTime , $endTime ]);
}) -> select ();
$count = Db :: connect ( 'logistics' ) -> name ( 'logistics' ) -> where ([ 'status' => 1 , 'courier_id' => $courierId ]) -> when ( $startTime && $endTime , function ( $query ) use ( $startTime , $endTime ){
$query -> whereBetween ( 'create_time' , [ $startTime , $endTime ]);
}) -> count ();
2023-12-09 17:57:57 +08:00
}
if ( $type == 3 ) {
// 已完成
2023-12-09 18:14:39 +08:00
$list = Db :: connect ( 'logistics' ) -> name ( 'logistics' ) -> where ([[ 'status' , 'in' , [ 2 , 3 ]], 'courier_id' => $courierId ]) -> when ( $startTime && $endTime , function ( $query ) use ( $startTime , $endTime ){
$query -> whereBetween ( 'ps_time' , [ $startTime , $endTime ]);
}) -> select ();
2023-12-09 18:17:35 +08:00
2023-12-09 18:14:39 +08:00
$count = Db :: connect ( 'logistics' ) -> name ( 'logistics' ) -> where ([[ 'status' , 'in' , [ 2 , 3 ]], 'courier_id' => $courierId ]) -> when ( $startTime && $endTime , function ( $query ) use ( $startTime , $endTime ){
$query -> whereBetween ( 'ps_time' , [ $startTime , $endTime ]);
2023-12-09 18:17:35 +08:00
}) -> count ();
2023-12-09 17:57:57 +08:00
}
2024-01-02 16:08:40 +08:00
$list = $list -> toArray ();
2024-01-02 15:54:43 +08:00
foreach ( $list as & $item ) {
$item [ 'product_name' ] = StoreOrderProduct :: alias ( 'o' ) -> join ( 'store_product p' , 'o.product_id=p.product_id' ) -> where ( 'o.order_id' , $item [ 'order_id' ]) -> value ( 'p.store_name' );
}
2024-01-02 16:08:40 +08:00
2023-12-09 17:57:57 +08:00
return app ( 'json' ) -> success ( compact ( 'count' , 'list' ));
}
2023-12-02 19:52:39 +08:00
}