366 lines
14 KiB
PHP
Executable File
366 lines
14 KiB
PHP
Executable File
<?php
|
|
namespace app\coachapi\logic;
|
|
use app\common\enum\notice\NoticeEnum;
|
|
use app\common\enum\OrderEnum;
|
|
use app\common\enum\OrderLogEnum;
|
|
use app\common\logic\BaseLogic;
|
|
use app\common\logic\OrderLogLogic;
|
|
use app\common\model\coach\Coach;
|
|
use app\common\model\order\Order;
|
|
use app\common\model\order\OrderLog;
|
|
use app\common\model\settle\SettleOrder;
|
|
use app\common\service\ConfigService;
|
|
use app\common\service\FileService;
|
|
use app\common\service\TencentMapKeyService;
|
|
use think\Exception;
|
|
use think\facade\Db;
|
|
|
|
class OrderLogic extends BaseLogic
|
|
{
|
|
|
|
/**
|
|
* @notes 订单详情
|
|
* @param int $id
|
|
* @return array
|
|
* @author cjhao
|
|
* @date 2024/9/20 02:19
|
|
*/
|
|
public function detail(int $id)
|
|
{
|
|
$detail = Order::field('id,sn,order_status,total_order_amount,user_remark,pay_status,pay_way,total_refund_amount,goods_price,order_amount,total_order_amount,total_amount,total_gap_amount,total_append_amount,appoint_time,order_amount,user_remark,address_snap,is_settle,server_finish_time,create_time,refund_amount,coach_id,car_amount,order_distance,update_time,true_server_finish_time')
|
|
->order('id','desc')
|
|
->append(['order_distance_desc','pay_way_desc','appoint_time','appoint_date','order_status_desc','take_order_btn','depart_btn','arrive_btn','server_start_btn','server_finish_btn','order_cancel_time'])
|
|
->with(['order_goods' => function($query){
|
|
$query->field('order_id,goods_id,goods_snap,goods_num,duration,goods_image,goods_name,goods_price')->hidden(['goods_snap']);
|
|
},'order_gap','order_append'])
|
|
->where(['id'=>$id])
|
|
->findOrEmpty()
|
|
->toArray();
|
|
|
|
if(!isset($detail['address_snap']['house_number'])){
|
|
$detail['address_snap']['house_number'] = '';
|
|
}
|
|
$orderLog = OrderLog::where(['order_id'=>$id,'type'=>OrderLogEnum::TYPE_COACH])
|
|
->field('content,location,extra,create_time')
|
|
->order('id asc')
|
|
->select()->toArray();
|
|
$logLists = [];
|
|
foreach ($orderLog as $key => $log){
|
|
$date = date('Y-m-d',strtotime($log['create_time']));
|
|
if($log['extra']){
|
|
foreach ($log['extra'] as $key => $extra){
|
|
$log['extra'][$key] = FileService::getFileUrl($extra);
|
|
}
|
|
}
|
|
$logLists[$date][] = $log;
|
|
}
|
|
// $logLists = $orderLog;
|
|
$settleOrder = [];
|
|
if($detail['is_settle']){
|
|
$settleOrder = SettleOrder::where(['order_id'=>$detail['id']])->findOrEmpty();
|
|
}
|
|
$coachCommission = $settleOrder['coach_commission'] ?? 0;
|
|
$coachCarCommission = $settleOrder['coach_car_amount'] ?? 0;
|
|
// $totalOrderAmount = $settleOrder['order_amount'] ?? 0;
|
|
// $carAmount = $settleOrder['car_amount'] ?? 0;
|
|
$detail['settle_info'] = [
|
|
'status' => $detail['is_settle'],
|
|
'refund_amount' => $detail['total_refund_amount'],
|
|
'order_amount' => $settleOrder['order_amount'] ?? 0,
|
|
'settle_car' => $settleOrder['car_amount'] ?? 0 ,
|
|
'settle_amount' => $settleOrder['total_commission'] ?? 0 ,
|
|
'coach_settle' => $settleOrder['coach_commission'] ?? 0,
|
|
'shop_settle' => 0,
|
|
// 'total_settle_amount'=> round($totalOrderAmount + $carAmount ,2),
|
|
];
|
|
$detail['server_log_lists'] = $logLists;
|
|
|
|
return $detail;
|
|
}
|
|
|
|
|
|
/**
|
|
* @notes 接单操作
|
|
* @param $params
|
|
* @return bool
|
|
* @author cjhao
|
|
* @date 2024/9/13 17:21
|
|
*/
|
|
public function take($params)
|
|
{
|
|
try {
|
|
Db::startTrans();
|
|
$order = Order::where(['id'=>$params['id'],'coach_id'=>$params['coach_id']])
|
|
->findOrEmpty();
|
|
|
|
if($order->isEmpty()){
|
|
throw new Exception('订单不存在');
|
|
}
|
|
if(OrderEnum::ORDER_STATUS_WAIT_RECEIVING != $order->order_status){
|
|
throw new Exception('订单状态已改变,请刷新页面');
|
|
}
|
|
$coach = Coach::where(['id'=>$params['coach_id']])->findOrEmpty();
|
|
//验证接单数量
|
|
\app\common\logic\CoachLogic::ckechCoachTakeOrderNum($coach);
|
|
$order->order_status = OrderEnum::ORDER_STATUS_WAIT_DEPART;
|
|
$order->save();
|
|
|
|
//订单日志
|
|
(new OrderLogLogic())->record(OrderLogEnum::TYPE_COACH,OrderLogEnum::COACH_TAKE_ORDER,$order['id'],$params['coach_id']);
|
|
event('Notice', [
|
|
'scene_id' => NoticeEnum::ACCEPT_ORDER_NOTICE,
|
|
'params' => [
|
|
'user_id' => $order['user_id'],
|
|
'order_id' => $order['id']
|
|
]
|
|
]);
|
|
//提交事务
|
|
Db::commit();
|
|
return true;
|
|
}catch (Exception $e) {
|
|
Db::rollback();
|
|
self::$error = $e->getMessage();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @notes 出发操作
|
|
* @param $params
|
|
* @return bool
|
|
* @author cjhao
|
|
* @date 2024/9/13 17:21
|
|
*/
|
|
public function depart($params)
|
|
{
|
|
try {
|
|
Db::startTrans();
|
|
|
|
$order = Order::where(['id'=>$params['id'],'coach_id'=>$params['coach_id']])
|
|
->findOrEmpty();
|
|
if($order->isEmpty()){
|
|
throw new Exception('订单不存在');
|
|
}
|
|
if(OrderEnum::ORDER_STATUS_WAIT_DEPART != $order->order_status){
|
|
throw new Exception('订单状态已改变,请刷新页面');
|
|
}
|
|
$order->order_status = OrderEnum::ORDER_STATUS_DEPART;
|
|
$order->save();
|
|
//订单日志
|
|
(new OrderLogLogic())->record(OrderLogEnum::TYPE_COACH,OrderLogEnum::COACH_DEPART,$order['id'],$params['coach_id']);
|
|
|
|
//提交事务
|
|
Db::commit();
|
|
return true;
|
|
}catch (Exception $e) {
|
|
Db::rollback();
|
|
self::$error = $e->getMessage();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @notes 达到操作
|
|
* @param $params
|
|
* @return bool
|
|
* @author cjhao
|
|
* @date 2024/9/13 17:21
|
|
*/
|
|
public function arrive($params)
|
|
{
|
|
try {
|
|
$imageLists = $params['image_lists'] ?? [];
|
|
if(empty($imageLists)){
|
|
throw new Exception('请上传图片');
|
|
}
|
|
Db::startTrans();
|
|
if (!isset($params['latitude']) || $params['longitude'] == '') {
|
|
throw new Exception('请上传位置');
|
|
}
|
|
|
|
$order = Order::where(['id'=>$params['id'],'coach_id'=>$params['coach_id']])
|
|
->findOrEmpty();
|
|
if($order->isEmpty()){
|
|
throw new Exception('订单不存在');
|
|
}
|
|
if(OrderEnum::ORDER_STATUS_DEPART != $order->order_status){
|
|
throw new Exception('订单状态已改变,请刷新页面');
|
|
}
|
|
$order->order_status = OrderEnum::ORDER_STATUS_ARRIVE;
|
|
$order->save();
|
|
$key = (new TencentMapKeyService())->getTencentMapKey();
|
|
if (empty($key)) {
|
|
throw new Exception('请联系管理员检查腾讯地图配置');
|
|
}
|
|
$data['location'] = $params['latitude'].','.$params['longitude'];
|
|
$data['key'] = $key;
|
|
$url = 'https://apis.map.qq.com/ws/geocoder/v1/';
|
|
$query = http_build_query($data);
|
|
$result = json_decode(file_get_contents($url . '?' . $query), true);
|
|
if ($result['status'] !== 0) {
|
|
$check = (new TencentMapKeyService())->checkResult($result);
|
|
while (!$check) {
|
|
$data['key'] = (new TencentMapKeyService())->getTencentMapKey(true);
|
|
if (empty($data['key'])) {
|
|
break;
|
|
}
|
|
$query = http_build_query($data);
|
|
$result = json_decode(file_get_contents($url . '?' . $query), true);
|
|
$check = (new TencentMapKeyService())->checkResult($result);
|
|
}
|
|
}
|
|
$data = [];
|
|
$data = $result['result']['address_component'];
|
|
$data['longitude'] = $params['longitude'];
|
|
$data['latitude'] = $params['latitude'];
|
|
foreach($imageLists as $key => $image){
|
|
$imageLists[$key] = FileService::setFileUrl($image);
|
|
}
|
|
//订单日志
|
|
(new OrderLogLogic())->record(OrderLogEnum::TYPE_COACH,OrderLogEnum::COACH_ARRIVE,$order['id'],$params['coach_id'],'',$data,$imageLists);
|
|
//提交事务
|
|
Db::commit();
|
|
return true;
|
|
}catch (Exception $e) {
|
|
Db::rollback();
|
|
self::$error = $e->getMessage();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @notes 服务开始操作
|
|
* @param $params
|
|
* @return bool
|
|
* @author cjhao
|
|
* @date 2024/9/13 17:21
|
|
*/
|
|
public function startServer($params)
|
|
{
|
|
try {
|
|
Db::startTrans();
|
|
$order = Order::where(['id'=>$params['id'],'coach_id'=>$params['coach_id']])
|
|
->findOrEmpty();
|
|
if($order->isEmpty()){
|
|
throw new Exception('订单不存在');
|
|
}
|
|
if(OrderEnum::ORDER_STATUS_ARRIVE != $order->order_status){
|
|
throw new Exception('订单状态已改变,请刷新页面');
|
|
}
|
|
$order->order_status = OrderEnum::ORDER_STATUS_START_SERVER;
|
|
$order->save();
|
|
//订单日志
|
|
(new OrderLogLogic())->record(OrderLogEnum::TYPE_COACH,OrderLogEnum::COACH_START_SERVER,$order['id'],$params['coach_id']);
|
|
//开始服务通知用户
|
|
event('Notice', [
|
|
'scene_id' => NoticeEnum::START_SERVICE_NOTICE,
|
|
'params' => [
|
|
'user_id' => $order['user_id'],
|
|
'order_id' => $order['id']
|
|
]
|
|
]);
|
|
//开始服务通知师傅
|
|
event('Notice', [
|
|
'scene_id' => NoticeEnum::START_SERVICE_NOTICE_STAFF,
|
|
'params' => [
|
|
'coach_id' => $order['coach_id'],
|
|
'order_id' => $order['id']
|
|
]
|
|
]);
|
|
//提交事务
|
|
Db::commit();
|
|
return true;
|
|
}catch (Exception $e) {
|
|
Db::rollback();
|
|
self::$error = $e->getMessage();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @notes 服务完成
|
|
* @param $params
|
|
* @return bool
|
|
* @author cjhao
|
|
* @date 2024/9/14 14:52
|
|
*/
|
|
public function finishServer($params)
|
|
{
|
|
try {
|
|
$order = Order::where(['id'=>$params['id'],'coach_id'=>$params['coach_id']])
|
|
->findOrEmpty();
|
|
Coach::update(['order_num'=>['inc',1]],['id'=>$order['coach_id']]);
|
|
Db::startTrans();
|
|
$imageLists = $params['image_lists'] ?? [];
|
|
if(empty($imageLists)){
|
|
throw new Exception('请上传图片');
|
|
}
|
|
if($order->isEmpty()){
|
|
throw new Exception('订单不存在');
|
|
}
|
|
if(OrderEnum::ORDER_STATUS_START_SERVER != $order->order_status){
|
|
throw new Exception('订单状态已改变,请刷新页面');
|
|
}
|
|
$order->order_status = OrderEnum::ORDER_STATUS_SERVER_FINISH;
|
|
$order->true_server_finish_time = time();
|
|
$order->save();
|
|
$key = (new TencentMapKeyService())->getTencentMapKey();
|
|
if (empty($key)) {
|
|
throw new Exception('请联系管理员检查腾讯地图配置');
|
|
}
|
|
$data['location'] = $params['latitude'].','.$params['longitude'];
|
|
$data['key'] = $key;
|
|
$url = 'https://apis.map.qq.com/ws/geocoder/v1/';
|
|
$query = http_build_query($data);
|
|
$result = json_decode(file_get_contents($url . '?' . $query), true);
|
|
if ($result['status'] !== 0) {
|
|
$check = (new TencentMapKeyService())->checkResult($result);
|
|
while (!$check) {
|
|
$data['key'] = (new TencentMapKeyService())->getTencentMapKey(true);
|
|
if (empty($data['key'])) {
|
|
break;
|
|
}
|
|
$query = http_build_query($data);
|
|
$result = json_decode(file_get_contents($url . '?' . $query), true);
|
|
$check = (new TencentMapKeyService())->checkResult($result);
|
|
}
|
|
}
|
|
$data = [];
|
|
$data = $result['result']['address_component'];
|
|
$data['longitude'] = $params['longitude'];
|
|
$data['latitude'] = $params['latitude'];
|
|
foreach($imageLists as $key => $image){
|
|
$imageLists[$key] = FileService::setFileUrl($image);
|
|
}
|
|
(new OrderLogLogic())->record(OrderLogEnum::TYPE_COACH,OrderLogEnum::COACH_SERVER_FINISH,$order['id'],$params['coach_id'],'',$data,$imageLists);
|
|
//完成服务-用户
|
|
event('Notice', [
|
|
'scene_id' => NoticeEnum::FINISH_SERVICE_NOTICE,
|
|
'params' => [
|
|
'user_id' => $order['user_id'],
|
|
'order_id' => $order['id']
|
|
]
|
|
]);
|
|
//完成服务-师傅
|
|
event('Notice', [
|
|
'scene_id' => NoticeEnum::END_SERVICE_NOTICE_STAFF,
|
|
'params' => [
|
|
'coach_id' => $order['coach_id'],
|
|
'order_id' => $order['id']
|
|
]
|
|
]);
|
|
Coach::update(['order_num'=>['inc',1]],['id'=>$order['coach_id']]);
|
|
//提交事务
|
|
Db::commit();
|
|
return true;
|
|
}catch (Exception $e) {
|
|
Db::rollback();
|
|
self::$error = $e->getMessage();
|
|
return false;
|
|
}
|
|
|
|
}
|
|
} |