|
|
@@ -13,9 +13,12 @@ namespace App\Services\Api;
|
|
|
|
|
|
use App\Models\GoodsModel;
|
|
|
use App\Models\MemberModel;
|
|
|
+use App\Models\OrderGoodsModel;
|
|
|
use App\Models\OrderModel;
|
|
|
+use App\Models\StoreModel;
|
|
|
use App\Services\BaseService;
|
|
|
use App\Services\ConfigService;
|
|
|
+use App\Services\PaymentService;
|
|
|
use App\Services\RedisService;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
@@ -67,7 +70,7 @@ class OrderService extends BaseService
|
|
|
// 进行中
|
|
|
if ($status > 0 && is_array($status)) {
|
|
|
$query->whereIn('a.status', $status);
|
|
|
- }else if($status>0){
|
|
|
+ } else if ($status > 0) {
|
|
|
$query->where('a.status', $status);
|
|
|
}
|
|
|
})->select(['a.*'])
|
|
|
@@ -87,8 +90,8 @@ class OrderService extends BaseService
|
|
|
}
|
|
|
|
|
|
$item['goods'] = isset($item['goods']) && $item['goods'] ? $item['goods'] : [];
|
|
|
- $item['goods']['shipper_phone_text'] = $item['goods']['shipper_phone']?format_mobile($item['goods']['shipper_phone']) : '';
|
|
|
- $item['goods']['receiver_phone_text'] = $item['goods']['receiver_phone']?format_mobile($item['goods']['receiver_phone']) : '';
|
|
|
+ $item['goods']['shipper_phone_text'] = $item['goods']['shipper_phone'] ? format_mobile($item['goods']['shipper_phone']) : '';
|
|
|
+ $item['goods']['receiver_phone_text'] = $item['goods']['receiver_phone'] ? format_mobile($item['goods']['receiver_phone']) : '';
|
|
|
}
|
|
|
unset($item);
|
|
|
}
|
|
|
@@ -111,9 +114,9 @@ class OrderService extends BaseService
|
|
|
return $this->model->from('orders as a')->with(['goods'])
|
|
|
->leftJoin('member as b', 'b.id', '=', 'a.user_id')
|
|
|
->where($where)
|
|
|
- ->where(function($query) use($params){
|
|
|
+ ->where(function ($query) use ($params) {
|
|
|
$userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
|
|
|
- if($userId>0){
|
|
|
+ if ($userId > 0) {
|
|
|
$query->where('a.user_id', $userId);
|
|
|
}
|
|
|
})
|
|
|
@@ -150,8 +153,8 @@ class OrderService extends BaseService
|
|
|
}
|
|
|
|
|
|
$info['goods'] = isset($info['goods']) && $info['goods'] ? $info['goods'] : [];
|
|
|
- $info['goods']['shipper_phone_text'] = $info['goods']['shipper_phone']?format_mobile($info['goods']['shipper_phone']) : '';
|
|
|
- $info['goods']['receiver_phone_text'] = $info['goods']['receiver_phone']?format_mobile($info['goods']['receiver_phone']) : '';
|
|
|
+ $info['goods']['shipper_phone_text'] = $info['goods']['shipper_phone'] ? format_mobile($info['goods']['shipper_phone']) : '';
|
|
|
+ $info['goods']['receiver_phone_text'] = $info['goods']['receiver_phone'] ? format_mobile($info['goods']['receiver_phone']) : '';
|
|
|
}
|
|
|
|
|
|
return $info;
|
|
|
@@ -165,105 +168,111 @@ class OrderService extends BaseService
|
|
|
*/
|
|
|
public function createOrder($userId, $params)
|
|
|
{
|
|
|
- $goodsId = isset($params['goods_id']) && $params['goods_id'] ? intval($params['goods_id']) : 0;
|
|
|
+ $addressId = isset($params['address_id']) && $params['address_id'] ? $params['address_id'] : 0;
|
|
|
+ $goods = isset($params['goods']) && $params['goods'] ? $params['goods'] : [];
|
|
|
+ $ids = $goods ? array_column($goods,'id') : [];
|
|
|
// 参数验证
|
|
|
- if (empty($goodsId)) {
|
|
|
- $this->error = 2103;
|
|
|
+ if (empty($goods) || empty($ids)) {
|
|
|
+ $this->error = '商品参数不为空';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($addressId <= 0) {
|
|
|
+ $this->error = '请选择收货地址';
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 缓存锁
|
|
|
- $cacheLockKey = "caches:orders:submit_lock:{$userId}_{$goodsId}";
|
|
|
+ $cacheLockKey = "caches:orders:submit_lock:{$userId}";
|
|
|
if (RedisService::get($cacheLockKey)) {
|
|
|
- $this->error = 2107;
|
|
|
+ $this->error = '订单处理中~';
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- // 货物信息
|
|
|
- RedisService::set($cacheLockKey, ['params' => $params, 'user_id' => $userId], rand(2, 3));
|
|
|
- $goodsInfo = GoodsModel::where(['id' => $goodsId,'status'=>1, 'mark' => 1])
|
|
|
- ->select(['id','num', 'price','picker_status', 'status'])
|
|
|
- ->first();
|
|
|
- $pickerStatus = isset($goodsInfo['picker_status']) ? $goodsInfo['picker_status'] : 0;
|
|
|
- $price = isset($goodsInfo['price']) ? floatval($goodsInfo['price']) : 0;
|
|
|
- $num = isset($goodsInfo['num']) ? intval($goodsInfo['num']) : 0;
|
|
|
- if(empty($goodsInfo)){
|
|
|
- $this->error = 2201;
|
|
|
+ // 商品数据
|
|
|
+ $orderNo = get_order_num('GX');
|
|
|
+ RedisService::set($cacheLockKey, ['params' => $params, 'user_id' => $userId], rand(3, 5));
|
|
|
+ $result = GoodsService::make()->getOrderGoods($ids, $goods, $orderNo, $userId);
|
|
|
+ if (empty($result)) {
|
|
|
+ RedisService::clear($cacheLockKey);
|
|
|
+ $this->error = GoodsService::make()->getError();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $orderGoods = isset($result['goods']) ? $result['goods'] : [];
|
|
|
+ $orderTotal = isset($result['total']) ? $result['total'] : 0;
|
|
|
+ $orderCount = isset($result['count']) ? $result['count'] : 0;
|
|
|
+ $storeId = isset($result['store_id']) ? $result['store_id'] : 0;
|
|
|
+ if (empty($orderGoods)) {
|
|
|
RedisService::clear($cacheLockKey);
|
|
|
+ $this->error = '获取订单商品错误~';
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- if(in_array($pickerStatus,[2,3])){
|
|
|
- $this->error = "220{$pickerStatus}";
|
|
|
+ if ($orderTotal <= 0) {
|
|
|
RedisService::clear($cacheLockKey);
|
|
|
+ $this->error = '订单金额错误~';
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- if($price<=0){
|
|
|
- $this->error = 2204;
|
|
|
+ if ($orderCount <= 0) {
|
|
|
RedisService::clear($cacheLockKey);
|
|
|
+ $this->error = '订单商品数量错误~';
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 用户信息
|
|
|
$userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
|
|
|
- ->select(['id', 'mobile', 'nickname','realname','deposit','picker_status','confirm_status', 'status'])
|
|
|
+ ->select(['id', 'openid', 'mobile', 'nickname', 'realname', 'balance', 'status'])
|
|
|
->first();
|
|
|
$status = isset($userInfo['status']) ? $userInfo['status'] : 0;
|
|
|
- $nickname = isset($userInfo['nickname']) ? $userInfo['nickname'] : '';
|
|
|
- $realname = isset($userInfo['realname']) ? $userInfo['realname'] : '';
|
|
|
- $mobile = isset($userInfo['mobile']) ? $userInfo['mobile'] : '';
|
|
|
- $confirmStatus = isset($userInfo['confirm_status']) ? $userInfo['confirm_status'] : 0;
|
|
|
- $userPickerStatus = isset($userInfo['picker_status']) ? $userInfo['picker_status'] : 0;
|
|
|
- $userDeposit = isset($userInfo['deposit']) ? floatval($userInfo['deposit']) : 0;
|
|
|
- if(empty($userInfo) || $status != 1){
|
|
|
- $this->error = 2016;
|
|
|
+ $openid = isset($userInfo['openid']) ? $userInfo['openid'] : '';
|
|
|
+ if (empty($userInfo) || $status != 1) {
|
|
|
+ $this->error = 1045;
|
|
|
RedisService::clear($cacheLockKey);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- // 账号审核情况
|
|
|
- if($confirmStatus != 1){
|
|
|
- $this->error = 2042;
|
|
|
+ if (empty($openid)) {
|
|
|
+ $this->error = '用户微信未授权,请重新授权登录';
|
|
|
RedisService::clear($cacheLockKey);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- if($userPickerStatus != 1){
|
|
|
- $this->error = 2205;
|
|
|
+ // 收货地址信息
|
|
|
+ $addressInfo = MemberAddressService::make()->getBindInfo($userId, $addressId);
|
|
|
+ $realname = isset($addressInfo['realname']) ? $addressInfo['realname'] : '';
|
|
|
+ $mobile = isset($addressInfo['mobile']) ? $addressInfo['mobile'] : '';
|
|
|
+ $area = isset($addressInfo['area']) ? $addressInfo['area'] : '';
|
|
|
+ $address = isset($addressInfo['address']) ? $addressInfo['address'] : '';
|
|
|
+ if (empty($addressInfo) || empty($realname) || empty($mobile) || empty($area) || empty($address)) {
|
|
|
RedisService::clear($cacheLockKey);
|
|
|
+ $this->error = '收货地址信息错误,请核对后重试~';
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- // 缴纳保证金验证
|
|
|
- $depositMoney = ConfigService::make()->getConfigByCode('deposit_money',0);
|
|
|
- if($depositMoney>0 && ($userDeposit < $depositMoney)){
|
|
|
- $this->error = 2206;
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- return false;
|
|
|
- }
|
|
|
+ // 商家佣金
|
|
|
+ $storeInfo = StoreModel::where(['id' => $storeId])->first();
|
|
|
+ $bonusRate = isset($storeInfo['bonus_rate']) ? floatval($storeInfo['bonus_rate']) : 0;
|
|
|
+ $storeBonusRate = ConfigService::make()->getConfigByCode('store_bonus_rate', 0);
|
|
|
+ $storeBonusRate = $storeBonusRate > 0 && $storeBonusRate <= 100 ? $storeBonusRate : 0;
|
|
|
+ $bonusRate = $bonusRate > 0 && $bonusRate <= 100 ? $bonusRate : $storeBonusRate;
|
|
|
+ $bonus = moneyFormat($orderTotal * $bonusRate, 2);
|
|
|
+
|
|
|
+ $orderTotal = 0.1;
|
|
|
|
|
|
// 订单数据
|
|
|
- $pickerCount = $this->model->where(['goods_id'=> $goodsId,'mark'=>1])->whereIn('status',[1,2])->count('id');
|
|
|
- if($this->model->where(['user_id'=>$userId,'goods_id'=>$goodsId,'mark'=>1])->whereIn('status',[1,2,3])->value('id')){
|
|
|
- $this->error = 2209;
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- return false;
|
|
|
- }
|
|
|
- if($this->model->where(['user_id'=>$userId,'mark'=>1])->whereIn('status',[1,2])->value('id')){
|
|
|
- $this->error = 2209;
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- return false;
|
|
|
- }
|
|
|
- $bonusRate = ConfigService::make()->getConfigByCode('picker_bonus_rate',100);
|
|
|
- $bonusRate = $bonusRate>0 && $bonusRate<=100? intval($bonusRate) : 100;
|
|
|
- $bonus = moneyFormat($price * $bonusRate/100,2);
|
|
|
- $orderNo = get_order_num('PD');
|
|
|
$order = [
|
|
|
'order_no' => $orderNo,
|
|
|
'user_id' => $userId,
|
|
|
- 'goods_id' => $goodsId,
|
|
|
- 'total' => $price,
|
|
|
+ 'store_id' => $storeId,
|
|
|
+ 'total' => $orderTotal,
|
|
|
+ 'num' => $orderCount,
|
|
|
+ 'pay_total' => $orderTotal,
|
|
|
+ 'receiver_name' => $realname,
|
|
|
+ 'receiver_mobile' => $mobile,
|
|
|
+ 'receiver_area' => $area,
|
|
|
+ 'receiver_address' => $address,
|
|
|
'bonus' => $bonus,
|
|
|
'create_time' => time(),
|
|
|
'update_time' => time(),
|
|
|
@@ -275,36 +284,48 @@ class OrderService extends BaseService
|
|
|
DB::beginTransaction();
|
|
|
if (!$orderId = $this->model->insertGetId($order)) {
|
|
|
DB::rollBack();
|
|
|
- $this->error = 2207;
|
|
|
+ $this->error = '创建订单失败';
|
|
|
+ RedisService::clear($cacheLockKey);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 订单商品
|
|
|
+ if($orderGoods && !OrderGoodsModel::insert($orderGoods)){
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = '处理订单商品错误';
|
|
|
RedisService::clear($cacheLockKey);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- $title = "用户【{$realname}-{$mobile}】有新的抢单";
|
|
|
- $message = [
|
|
|
- 'from_uid'=> $userId,
|
|
|
- 'to_uid'=> 1,
|
|
|
- 'op'=> 'notice',
|
|
|
- 'scene'=> 'picker',
|
|
|
- 'message'=> '接单消息',
|
|
|
- 'type'=> 5,
|
|
|
- 'order'=>[
|
|
|
- 'title'=> $title.'<span class="ele-text-primary">查看订单</span>',
|
|
|
- 'order_no'=> $orderNo,
|
|
|
- 'money'=> $bonus,
|
|
|
- 'date'=> date('Y-m-d H:i:s'),
|
|
|
- 'user_id'=> $userId,
|
|
|
- 'type'=> 'deposit',
|
|
|
- 'remark'=> '接单消息',
|
|
|
- ]
|
|
|
+ // 获取支付参数
|
|
|
+ /* TODO 支付处理 */
|
|
|
+ $payOrder = [
|
|
|
+ 'type' => 1,
|
|
|
+ 'order_no' => $orderNo,
|
|
|
+ 'pay_money' => $orderTotal,
|
|
|
+ 'body' => '购物消费',
|
|
|
+ 'openid' => $openid
|
|
|
];
|
|
|
|
|
|
+ // 调起支付
|
|
|
+ $payment = PaymentService::make()->minPay($userInfo, $payOrder, 'store');
|
|
|
+ if (empty($payment)) {
|
|
|
+ DB::rollBack();
|
|
|
+ RedisService::clear($cacheLockKey);
|
|
|
+ $this->error = PaymentService::make()->getError();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 用户操作记录
|
|
|
DB::commit();
|
|
|
- $pickerCount = $pickerCount+1;
|
|
|
- $this->error = lang('2210');
|
|
|
+ $this->error = '订单创建成功,请前往支付~';
|
|
|
RedisService::clear($cacheLockKey);
|
|
|
- RedisService::keyDel("caches:goods:picker*");
|
|
|
- return ['order_id' => $orderId,'message'=>$message, 'total' => $price, 'num' => $pickerCount<=3?3:$pickerCount, 'goods' => $goodsId];
|
|
|
+ return [
|
|
|
+ 'order_id' => $orderId,
|
|
|
+ 'payment' => $payment,
|
|
|
+ 'total' => $payOrder['pay_money'],
|
|
|
+ 'pay_type' => 10,
|
|
|
+ ];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -313,19 +334,19 @@ class OrderService extends BaseService
|
|
|
* @param int $type
|
|
|
* @return array|mixed
|
|
|
*/
|
|
|
- public function getCountByDay($userId, $status=3)
|
|
|
+ public function getCountByDay($userId, $status = 3)
|
|
|
{
|
|
|
$cacheKey = "caches:orders:count_day_{$userId}_{$status}";
|
|
|
$data = RedisService::get($cacheKey);
|
|
|
- if($data){
|
|
|
+ if ($data) {
|
|
|
return $data;
|
|
|
}
|
|
|
|
|
|
- $data = $this->model->where(['user_id'=> $userId,'status'=>$status,'mark'=>1])
|
|
|
- ->where('create_time','>=', strtotime(date('Y-m-d')))
|
|
|
+ $data = $this->model->where(['user_id' => $userId, 'status' => $status, 'mark' => 1])
|
|
|
+ ->where('create_time', '>=', strtotime(date('Y-m-d')))
|
|
|
->count('id');
|
|
|
- if($data){
|
|
|
- RedisService::set($cacheKey, $data, rand(5,10));
|
|
|
+ if ($data) {
|
|
|
+ RedisService::set($cacheKey, $data, rand(5, 10));
|
|
|
}
|
|
|
|
|
|
return $data;
|
|
|
@@ -337,18 +358,18 @@ class OrderService extends BaseService
|
|
|
* @param int $status
|
|
|
* @return array|mixed
|
|
|
*/
|
|
|
- public function getCountByStatus($userId, $status=3)
|
|
|
+ public function getCountByStatus($userId, $status = 3)
|
|
|
{
|
|
|
$cacheKey = "caches:orders:count_status_{$userId}_{$status}";
|
|
|
$data = RedisService::get($cacheKey);
|
|
|
- if($data){
|
|
|
+ if ($data) {
|
|
|
return $data;
|
|
|
}
|
|
|
|
|
|
- $data = $this->model->where(['user_id'=> $userId,'status'=>$status,'mark'=>1])
|
|
|
+ $data = $this->model->where(['user_id' => $userId, 'status' => $status, 'mark' => 1])
|
|
|
->count('id');
|
|
|
- if($data){
|
|
|
- RedisService::set($cacheKey, $data, rand(5,10));
|
|
|
+ if ($data) {
|
|
|
+ RedisService::set($cacheKey, $data, rand(5, 10));
|
|
|
}
|
|
|
|
|
|
return $data;
|
|
|
@@ -363,45 +384,45 @@ class OrderService extends BaseService
|
|
|
{
|
|
|
$cacheKey = "caches:orders:checkOrder:{$userId}";
|
|
|
$data = RedisService::get($cacheKey);
|
|
|
- if($data){
|
|
|
+ if ($data) {
|
|
|
return $data;
|
|
|
}
|
|
|
|
|
|
- $data = $this->model->with(['confirm'])->where(function($query){
|
|
|
- $query->where(function($query){
|
|
|
- $query->where('status',2)->where('confirm_at','>=', date('Y-m-d H:i:s', time() - 3600));
|
|
|
- })->orWhere(function($query){
|
|
|
- $query->where('status', 9)->where('confirm_at','>=', date('Y-m-d H:i:s', time() - 600));
|
|
|
- })->orWhere(function($query){
|
|
|
- $query->where('status', 3)->where('confirm_at','>=', date('Y-m-d H:i:s', time() - 600));
|
|
|
- })->orWhere('status', 1);
|
|
|
- })
|
|
|
- ->where(['user_id'=> $userId,'mark'=>1])
|
|
|
- ->select(['id','order_no','user_id','goods_id','status'])
|
|
|
- ->orderBy('id','desc')
|
|
|
+ $data = $this->model->with(['confirm'])->where(function ($query) {
|
|
|
+ $query->where(function ($query) {
|
|
|
+ $query->where('status', 2)->where('confirm_at', '>=', date('Y-m-d H:i:s', time() - 3600));
|
|
|
+ })->orWhere(function ($query) {
|
|
|
+ $query->where('status', 9)->where('confirm_at', '>=', date('Y-m-d H:i:s', time() - 600));
|
|
|
+ })->orWhere(function ($query) {
|
|
|
+ $query->where('status', 3)->where('confirm_at', '>=', date('Y-m-d H:i:s', time() - 600));
|
|
|
+ })->orWhere('status', 1);
|
|
|
+ })
|
|
|
+ ->where(['user_id' => $userId, 'mark' => 1])
|
|
|
+ ->select(['id', 'order_no', 'user_id', 'goods_id', 'status'])
|
|
|
+ ->orderBy('id', 'desc')
|
|
|
->first();
|
|
|
- $data = $data? $data->toArray() : [];
|
|
|
+ $data = $data ? $data->toArray() : [];
|
|
|
$result = [];
|
|
|
- if($data){
|
|
|
- $orderId = isset($data['id'])? $data['id'] : 0;
|
|
|
- $status = isset($data['status'])? $data['status'] : 0;
|
|
|
- $goodsId = isset($data['goods_id'])? $data['goods_id'] : 0;
|
|
|
- $confirmOrder = isset($data['confirm'])? $data['confirm'] : [];
|
|
|
- $confirmOrderId = isset($confirmOrder['id'])? $confirmOrder['id'] : 0;
|
|
|
- $confirmOrderUser = isset($confirmOrder['user'])? $confirmOrder['user'] : [];
|
|
|
- $realname = isset($confirmOrderUser['realname'])? $confirmOrderUser['realname'] : '';
|
|
|
- if($status==9){
|
|
|
+ if ($data) {
|
|
|
+ $orderId = isset($data['id']) ? $data['id'] : 0;
|
|
|
+ $status = isset($data['status']) ? $data['status'] : 0;
|
|
|
+ $goodsId = isset($data['goods_id']) ? $data['goods_id'] : 0;
|
|
|
+ $confirmOrder = isset($data['confirm']) ? $data['confirm'] : [];
|
|
|
+ $confirmOrderId = isset($confirmOrder['id']) ? $confirmOrder['id'] : 0;
|
|
|
+ $confirmOrderUser = isset($confirmOrder['user']) ? $confirmOrder['user'] : [];
|
|
|
+ $realname = isset($confirmOrderUser['realname']) ? $confirmOrderUser['realname'] : '';
|
|
|
+ if ($status == 9) {
|
|
|
$message = "抱歉,订单已被其他师傅接走";
|
|
|
- }else if($status == 2){
|
|
|
+ } else if ($status == 2) {
|
|
|
$message = "恭喜您,抢单成功,请前往完成订单!";
|
|
|
- }else if($status == 3){
|
|
|
+ } else if ($status == 3) {
|
|
|
$message = "恭喜您,订单已经完成!";
|
|
|
- }else {
|
|
|
- $pickerCount = $this->model->where(['goods_id'=> $goodsId,'mark'=>1])->whereIn('status',[1,2])->count('id');
|
|
|
- $pickerCount = $pickerCount<=3? 3 : $pickerCount;
|
|
|
+ } else {
|
|
|
+ $pickerCount = $this->model->where(['goods_id' => $goodsId, 'mark' => 1])->whereIn('status', [1, 2])->count('id');
|
|
|
+ $pickerCount = $pickerCount <= 3 ? 3 : $pickerCount;
|
|
|
$message = "<p style='padding: 5px 0;'>正在抢单</p><p>({$pickerCount}位师傅正在抢单中)</p>";
|
|
|
}
|
|
|
- $result = ['order_id'=>$orderId,'goods_id'=>$goodsId,'confirm_order_id'=>$confirmOrderId,'status'=>$status, 'message'=> $message];
|
|
|
+ $result = ['order_id' => $orderId, 'goods_id' => $goodsId, 'confirm_order_id' => $confirmOrderId, 'status' => $status, 'message' => $message];
|
|
|
RedisService::set($cacheKey, $result, rand(10, 20));
|
|
|
}
|
|
|
|