164 lines
6.4 KiB
PHP
Executable File
164 lines
6.4 KiB
PHP
Executable File
<?php
|
|
namespace app\shopapi\logic;
|
|
use app\common\enum\OrderEnum;
|
|
use app\common\enum\OrderLogEnum;
|
|
use app\common\logic\BaseLogic;
|
|
use app\common\logic\CoachLogic;
|
|
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\FileService;
|
|
use 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,total_refund_amount,user_remark,total_order_amount,pay_status,pay_way,goods_price,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','dispatch_btn','pay_way_desc','appoint_time','appoint_date','order_status_desc','order_cancel_time'])
|
|
->with(['order_goods' => function($query){
|
|
$query->field('order_id,goods_id,goods_num,goods_snap,duration,goods_image,goods_name,goods_price')->hidden(['goods_snap']);
|
|
},'order_gap','order_append','coach_info'])
|
|
->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')
|
|
->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();
|
|
}
|
|
$shopCommission = $settleOrder['shop_commission'] ?? 0;
|
|
$shopCarAmount = $settleOrder['shop_car_amount'] ?? 0;
|
|
$coachCommission = $settleOrder['coach_commission'] ?? 0;
|
|
$coachCarAmount = $settleOrder['coach_car_amount'] ?? 0;
|
|
// $totalOrderAmount = $settleOrder['order_amount'] ?? 0;
|
|
// $carAmount = $settleOrder['car_amount'] ?? 0;
|
|
$detail['settle_info'] = [
|
|
'status' => $detail['is_settle'],
|
|
'order_amount' => $settleOrder['order_amount'] ?? 0,
|
|
'refund_amount' => $detail['total_refund_amount'],
|
|
'settle_car' => $settleOrder['car_amount'] ?? 0,
|
|
'settle_amount' => $settleOrder['total_commission'] ?? 0,
|
|
'coach_settle' => $settleOrder['coach_commission'] ?? 0,
|
|
'shop_settle' => $settleOrder['shop_commission'] ?? 0,
|
|
// 'total_settle_amount'=> round($totalOrderAmount + $carAmount ,2),
|
|
];
|
|
$detail['server_log_lists'] = $logLists;
|
|
return $detail;
|
|
}
|
|
|
|
|
|
/**
|
|
* @notes 订单更换技师时获取技师列表
|
|
* @param $params
|
|
* @return array|bool
|
|
* @author cjhao
|
|
* @date 2024/10/9 16:30
|
|
*/
|
|
public function coachLists($params,$shopId)
|
|
{
|
|
try {
|
|
$id = $params['id'] ?? '';
|
|
$keyword = $params['keyword'] ?? '';
|
|
if(empty($id)){
|
|
throw new Exception('请选择订单');
|
|
}
|
|
$order = Order::where(['id'=>$id])->findOrEmpty();
|
|
if(OrderEnum::ORDER_STATUS_WAIT_PAY == $order->order_status || OrderEnum::ORDER_STATUS_DEPART < $order->order_status){
|
|
throw new Exception('当前订单不可更改技师');
|
|
}
|
|
$startTime = $order->getData('appoint_time');
|
|
$serverFinishTime = $order->getData('server_finish_time');
|
|
$coachLists = CoachLogic::getLeisureCoach($order['coach_id'],$startTime,$serverFinishTime,$order,$keyword);
|
|
|
|
return $coachLists;
|
|
}catch (Exception $e){
|
|
self::$error = $e->getMessage();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @notes 指派技师
|
|
* @param array $params
|
|
* @return bool
|
|
* @author cjhao
|
|
* @date 2024/11/6 14:36
|
|
*/
|
|
public function dispatchCoach(array $params)
|
|
{
|
|
try {
|
|
Db::startTrans();
|
|
$id = $params['id'] ?? '';
|
|
$coachId = $params['coach_id'] ?? '';
|
|
if(empty($id)){
|
|
throw new Exception('请选择订单');
|
|
}
|
|
if(empty($coachId)){
|
|
throw new Exception('请选择技师');
|
|
}
|
|
$order = Order::where(['id'=>$id])
|
|
->findOrEmpty();
|
|
|
|
if(OrderEnum::ORDER_STATUS_WAIT_PAY == $order->order_status || OrderEnum::ORDER_STATUS_DEPART < $order->order_status){
|
|
throw new Exception('当前订单不可更改技师');
|
|
}
|
|
$startTime = $order->getData('appoint_time');
|
|
$serverFinishTime = $order->getData('server_finish_time');
|
|
$coachLists = CoachLogic::getLeisureCoach($order['coach_id'],$startTime,$serverFinishTime,$order);
|
|
$coachIds = array_column($coachLists,'id');
|
|
if(!in_array($coachId,$coachIds)){
|
|
throw new Exception('该技师该时间段忙');
|
|
}
|
|
//技师更换
|
|
$order->coach_id = $coachId;
|
|
$order->save();
|
|
$originalCoachId = $order['coach_id'];
|
|
$extra = [
|
|
'original_coach_id' => $originalCoachId,
|
|
'coach_id' => $coachId
|
|
];
|
|
(new OrderLogLogic())
|
|
->record(OrderLogEnum::TYPE_SHOP,OrderLogEnum::SHOP_CHANGE_COACH,$order['id'],$params['shop_id'],'','',$extra);
|
|
Db::commit();
|
|
return true;
|
|
|
|
}catch (Exception $e){
|
|
Db::rollback();
|
|
self::$error = $e->getMessage();
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
} |