修改配送操作逻辑

This commit is contained in:
luofei 2023-08-10 10:53:03 +08:00
parent aea96bec8b
commit 4a3ea8e9ae
2 changed files with 99 additions and 129 deletions

View File

@ -15,7 +15,6 @@
namespace app\api\controller;
use app\api\logic\LogisticsLogic;
use think\facade\Db;
/**
* 文章管理
@ -91,9 +90,9 @@ class LogisticsController extends BaseApiController
public function courierTakeGoods(): \think\response\Json
{
//获取参数
$params = $this->request->post(['logistics_id','user_id']);
$params = $this->request->post(['logistics_id','order_id','order_sn']);
//验证参数
if(empty($params['logistics_id']) || empty($params['user_id'])) return $this->fail('参数错误');
if(empty($params['logistics_id']) || empty($params['order_id']) || empty($params['order_sn'])) return $this->fail('参数错误');
//提取商品
$result = LogisticsLogic::takeGoods($params);
//返回数据
@ -109,33 +108,15 @@ class LogisticsController extends BaseApiController
public function courierCompleteDelivery(): \think\response\Json
{
//获取参数
$params = $this->request->post(['logistics_id','user_id']);
$params = $this->request->post(['logistics_id','take_code']);
//验证参数
if(empty($params['logistics_id']) || empty($params['user_id'])) return $this->fail('参数错误');
if(empty($params['logistics_id']) || empty($params['take_code'])) return $this->fail('参数错误');
//完成配送
$result = LogisticsLogic::doneDelivery($params);
//返回数据
return $result['code'] ==1 ? $this->success('配送完成') : $this->fail($result['msg']);
}
/*
* 用户确认收货
* @method post
* @param int $logistics_id 物流id
* @return \think\response\Json
*/
public function userConfirmReceipt(): \think\response\Json
{
//获取参数
$params = $this->request->post(['order_id','order_sn']);
//验证参数
if(empty($params['order_id']) || empty($params['order_sn'])) return $this->fail('参数错误');
//完成配送
$result = LogisticsLogic::receipt($params);
//返回数据
return $result['code'] ==1 ? $this->success('确认收货成功') : $this->fail($result['msg']);
}
/*
* 用户取消订单
* @method post
@ -154,6 +135,12 @@ class LogisticsController extends BaseApiController
return $result['code'] ==1 ? $this->success('取消成功') : $this->fail($result['msg']);
}
/*
* 商超端物流详情
* @method post
* @param int $logistics_id 物流id
* @return \think\response\Json
*/
public function userLogisticsDetail(): \think\response\Json
{
//获取参数

View File

@ -52,7 +52,7 @@ class LogisticsLogic extends BaseLogic
$lst_item['status_name'] = $lst_item->status_name;
$product_count = 0;
//获取产品信息
$lst_item['products'] = Product::field('product_num,cart_info')->where('order_id', $lst_item['order_id'])->select()->each(function($pro_item) use(&$product_count){
$lst_item['products'] = Product::field('product_num,cart_info')->where('order_id', $lst_item['order_id'])->select()->each( function($pro_item) use(&$product_count){
$pro_item['cart_info'] = json_decode($pro_item['cart_info'], true);
$pro_item['goods_name'] = $pro_item['cart_info']['product']['store_name'];
$pro_item['goods_unit'] = $pro_item['cart_info']['product']['unit_name'];
@ -217,30 +217,48 @@ class LogisticsLogic extends BaseLogic
*/
public static function takeGoods($params):array {
//获取物流信息
$logistics = Logistics::where('id', $params['logistics_id'])->where('courier_id',$params['user_id'])->where('status',0)->find();
$logistics = Logistics::where('id', $params['logistics_id'])->find();
if (!$logistics) return ['code'=>0, 'msg'=>'物流信息不存在'];
//判断订单是否一致
if($logistics['order_id'] !== $params['order_id'] || $logistics['order_sn'] !== $params['order_sn']) return ['code'=>0, 'msg'=>'订单信息不一致'];
//判断物流信息状态
switch ($logistics['status']) {
case 1:
return ['code'=>0, 'msg'=>'该商品正在配送中'];
case 2:
return ['code'=>0, 'msg'=>'该商品已完成配送'];
case 3:
return ['code'=>0, 'msg'=>'该订单已经被取消'];
}
//获取配送员信息
$courier = Courier::field('nickname,mobile')->where('id', $params['user_id'])->find();
if (!$courier) return ['code'=>0, 'msg'=>'配送员信息不存在'];
//设置记录信息
$record = [
$courier = Courier::field('nickname,mobile')->where('id', $logistics['courier_id'])->find();
//更改物流信息状态
Logistics::startTrans();
try {
//查询订单用户取件码
$order = Order::field('logistics_code')->where('order_id', $params['order_id'])->where('order_sn', $params['order_sn'])->find();
Logistics::where('id', $params['logistics_id'])->update([
'status' => 1,
'update_time' => time(),
'qh_time' => time(),
'user_take_code' => $order['logistics_code']
]);
LogisticsRecord::create([
'lst_id' => $logistics['id'],
'type' => 2,
'user_name' => $courier['nickname'],
'user_phone' => $courier['mobile'],
'content' => '已提取商品',
'create_time' => time(),
];
//更改物流信息状态
$res = self::status($params['logistics_id'], 1,$record);
if($res){
]);
Logistics::commit();
return ['code'=>1, 'msg'=>'操作成功'];
}else{
return ['code'=>0, 'msg'=>'操作失败'];
} catch (\Exception $e) {
Logistics::rollback();
return ['code'=>0, 'msg'=>$e->getMessage()];
}
}
/*
* 配送员配送
* @param $params
@ -248,54 +266,42 @@ class LogisticsLogic extends BaseLogic
*/
public static function doneDelivery($params):array {
//获取物流信息
$logistics = Logistics::where('id', $params['logistics_id'])->where('courier_id',$params['user_id'])->where('status',1)->find();
$logistics = Logistics::where('id', $params['logistics_id'])->find();
if (!$logistics) return ['code'=>0, 'msg'=>'物流信息不存在'];
//判断物流信息状态
switch ($logistics['status']) {
case 0:
return ['code'=>0, 'msg'=>'该商品未揽件'];
case 2:
return ['code'=>0, 'msg'=>'该商品已配送'];
case 3:
return ['code'=>0, 'msg'=>'订单已被取消'];
}
//验证取件码
if($logistics['user_take_code'] !== $params['take-code']) return ['code'=>0, 'msg'=>'取件码错误'];
//获取配送员信息
$courier = Courier::field('nickname,mobile')->where('id', $params['user_id'])->find();
if (!$courier) return ['code'=>0, 'msg'=>'配送员信息不存在'];
//设置记录信息
$record = [
$courier = Courier::field('nickname,mobile')->where('id', $logistics['courier_id'])->find();
//更改物流信息状态
Logistics::startTrans();
try {
Logistics::where('id', $params['logistics_id'])->update([
'status' => 2,
'update_time' => time(),
'ps_time' => time(),
]);
LogisticsRecord::create([
'lst_id' => $logistics['id'],
'type' => 2,
'user_name' => $courier['nickname'],
'user_phone' => $courier['mobile'],
'content' => '已完成配送',
'create_time' => time(),
];
//更改物流信息状态
$res = self::status($params['logistics_id'], 2,$record);
if($res){
]);
Logistics::commit();
return ['code'=>1, 'msg'=>'操作成功'];
}else{
return ['code'=>0, 'msg'=>'操作失败'];
}
}
/*
* 用户确认收货
* @param $params
* @return array
*/
public static function receipt($params):array {
//获取物流信息
$logistics = Logistics::where('order_id', $params['order_id'])->where('order_sn',$params['order_sn'])->find();
if (!$logistics) return ['code'=>0, 'msg'=>'物流信息不存在'];
if ($logistics['status'] == 4) return ['code'=>0, 'msg'=>'状态不可更改'];
//设置记录信息
$record = [
'lst_id' => $logistics['id'],
'type' => 1,
'user_name' => $logistics['user_name'],
'user_phone' => $logistics['user_phone'],
'content' => '已确认收货',
'create_time' => time(),
];
//更改物流信息状态
$res = self::status($logistics['id'], 3,$record);
if($res){
return ['code'=>1, 'msg'=>'操作成功'];
}else{
return ['code'=>0, 'msg'=>'操作失败'];
} catch (\Exception $e) {
Logistics::rollback();
return ['code'=>0, 'msg'=>$e->getMessage()];
}
}
@ -308,57 +314,34 @@ class LogisticsLogic extends BaseLogic
//获取物流信息
$logistics = Logistics::where('order_id', $params['order_id'])->where('order_sn',$params['order_sn'])->find();
if (!$logistics) return ['code'=>0, 'msg'=>'物流信息不存在'];
if ($logistics['status'] == 2 || $logistics['status'] == 3 || $logistics['status'] == 4) return ['code'=>0, 'msg'=>'状态不可更改'];
//设置记录信息
$record = [
//判断物流信息状态
switch ($logistics['status']) {
case 1:
return ['code'=>0, 'msg'=>'该商品正在配送中'];
case 2:
return ['code'=>0, 'msg'=>'该商品已完成配送'];
}
//更改物流信息状态
Logistics::startTrans();
try {
Logistics::where('id', $params['logistics_id'])->update([
'status' => 3,
'update_time' => time(),
'qx_time' => time(),
]);
LogisticsRecord::create([
'lst_id' => $logistics['id'],
'type' => 1,
'user_name' => $logistics['user_name'],
'user_phone' => $logistics['user_phone'],
'content' => '已取消订单',
'create_time' => time(),
];
//更改物流信息状态
$res = self::status($logistics['id'], 4,$record);
if($res){
return ['code'=>1, 'msg'=>'操作成功'];
}else{
return ['code'=>0, 'msg'=>'操作失败'];
}
}
private static function status($id,$status,$data):bool
{
if(empty($id) || empty($status) || empty($data)) return false;
Logistics::startTrans();
try {
$update = [
'status' => $status,
'update_time' => time(),
];
switch($status){
case 1:
$update['qh_time'] = time();
break;
case 2:
$update['pc_time'] = time();
break;
case 3:
$update['sh_time'] = time();
break;
case 4:
$update['qx_time'] = time();
break;
default:
break;
}
Logistics::where('id', $id)->update($update);
LogisticsRecord::create($data);
]);
Logistics::commit();
return true;
return ['code'=>1, 'msg'=>'操作成功'];
} catch (\Exception $e) {
Logistics::rollback();
return false;
return ['code'=>0, 'msg'=>$e->getMessage()];
}
}
}