149 lines
4.6 KiB
PHP
149 lines
4.6 KiB
PHP
<?php
|
||
|
||
namespace app\common\Logic;
|
||
|
||
use app\admin\controller\orders\Dispatch;
|
||
use app\admin\model\Order;
|
||
use app\admin\model\OrderDispatch;
|
||
use fast\Auth;
|
||
use think\Db;
|
||
use think\db\exception\DataNotFoundException;
|
||
use think\db\exception\ModelNotFoundException;
|
||
use think\Exception;
|
||
use think\exception\DbException;
|
||
use think\Hook;
|
||
|
||
class OrderLogic
|
||
{
|
||
|
||
private $OrderModel;
|
||
|
||
public function __construct()
|
||
{
|
||
|
||
$this->OrderModel = new Order();
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* 任务完成以后,修改订单
|
||
* $roleInfo = ['role'=>1,'auth'=>$userInfo,'remark'='']
|
||
*
|
||
* @throws DataNotFoundException
|
||
* @throws DbException
|
||
* @throws ModelNotFoundException
|
||
* @throws Exception
|
||
*/
|
||
public function dispachFinishAfter(OrderDispatch $orderDispatch,$roleInfo=[]): void
|
||
{
|
||
|
||
$order = $this->OrderModel->where('id',$orderDispatch->order_id)->find();
|
||
if($order->status != Order::STATUS_DISPATCHED){
|
||
throw new Exception('订单状态不允许当前操作');
|
||
}
|
||
|
||
$orderUpdate = [
|
||
'status' => Order::STATUS_CHECKING
|
||
];
|
||
$orderDispatch->follow = 2; //结束跟进
|
||
$orderDispatch->save();
|
||
|
||
if($orderDispatch->is_receipt == 1){ //要收款,计算确认尾款
|
||
$offline_amount = $orderDispatch->total;
|
||
$total = bcadd($order->online_amount,$offline_amount,2);
|
||
$orderUpdate['offline_amount'] = $offline_amount;
|
||
$orderUpdate['total'] = $total;
|
||
//$orderUpdate['offline_amount_type'] = $orderDispatch->offline_total_type;
|
||
}
|
||
$order->allowField(true)->save($orderUpdate);
|
||
|
||
$params['role'] = $roleInfo['role'];
|
||
$params['auth'] = $roleInfo['auth'];
|
||
$params['order']= $order;
|
||
$params['remark'] = $roleInfo['remark'] ?? $orderDispatch->remark;
|
||
|
||
Hook::listen('order_change', $params);
|
||
|
||
|
||
$hookParams = [
|
||
'dispatch' => $orderDispatch,
|
||
'remark' => $roleInfo['remark'] ?? $orderDispatch->remark,
|
||
];
|
||
Hook::listen('order_dispatch_change', $hookParams);
|
||
}
|
||
|
||
|
||
/**
|
||
*
|
||
* 师傅超时未接单的处理逻辑
|
||
* @return void
|
||
*/
|
||
public function noWorkerCanGetIt(OrderDispatch $dispatch)
|
||
{
|
||
$maxNoticeNum = 3;
|
||
if($dispatch->notice > $maxNoticeNum){ //超过三次,直接取消
|
||
Db::startTrans();
|
||
try {
|
||
$dispatch->notice_num ++;
|
||
$dispatch->notice_time = date('Y-m-d H:i:s');
|
||
$remark = '师傅超时未接单,任务取消';
|
||
$this->cancelOrderDispatch($dispatch,null,$remark);
|
||
Db::commit();
|
||
}catch (Exception $exception){
|
||
Db::rollback();
|
||
$remark = '任务取消异常,请联系技术人员:'.$exception->getMessage();
|
||
$dispatch->notice_num ++;
|
||
$dispatch->remark = $remark;
|
||
$dispatch->notice_time = date('Y-m-d H:i:s');
|
||
$dispatch->save();
|
||
}
|
||
}else{ //未超过最大值,则通知短信通知
|
||
|
||
$dispatch->notice_num ++;
|
||
$dispatch->notice_time = date('Y-m-d H:i:s');
|
||
$dispatch->save();
|
||
|
||
//发送短信通知师傅 todo...
|
||
$smsLogic = new NoticeLogic();
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 取消任务
|
||
* @param Dispatch $dispatch
|
||
* @return void
|
||
*/
|
||
public function cancelOrderDispatch(OrderDispatch $dispatch,$auth=null,$remark='',$cancelOrder=true)
|
||
{
|
||
//取消
|
||
$dispatch->allowField(true)->save(['status' => OrderDispatch::STATUS_CANCEL, 'follow'=>0,'remark' => $remark]);
|
||
|
||
$hookParams = [
|
||
'dispatch' => $dispatch,
|
||
'remark' => '后台取消,操作说明:'.$remark?:'无'.',操作人:'.$auth->nickname
|
||
];
|
||
Hook::listen('order_dispatch_change', $hookParams);
|
||
|
||
|
||
if($cancelOrder){
|
||
$order = Order::where('id',$dispatch->order_id)->where('status',Order::STATUS_DISPATCHED)->find();
|
||
if(empty($order)){
|
||
throw new Exception('未找到关联订单');
|
||
}
|
||
//回退订单状态
|
||
$order->status = Order::STATUS_DISPATCHING;
|
||
|
||
$params['order'] = $order;
|
||
$params['role'] = 1;
|
||
$params['auth'] = $auth;
|
||
$params['remark'] = '任务被取消[ID:' . $dispatch->id . '],订单状态回退';
|
||
if (!empty($remark)) {
|
||
$params['remark'] .= ',备注:' . $remark;
|
||
}
|
||
Hook::listen('order_change', $params);
|
||
}
|
||
}
|
||
|
||
} |