|
|
@@ -100,7 +100,7 @@ class OrderService extends BaseService
|
|
|
|
|
|
$item['goods'] = isset($item['goods']) && $item['goods'] ? $item['goods'] : [];
|
|
|
$item['real_total'] = $item['pay_total'];
|
|
|
- $item['pay_total'] = moneyFormat($item['pay_total'] + $item['delivery_fee'],2);
|
|
|
+ $item['pay_total'] = moneyFormat($item['pay_total'] + $item['delivery_fee'], 2);
|
|
|
}
|
|
|
unset($item);
|
|
|
}
|
|
|
@@ -119,7 +119,7 @@ class OrderService extends BaseService
|
|
|
*/
|
|
|
public function getQuery($params)
|
|
|
{
|
|
|
- $where = ['a.is_hide'=>0,'a.mark' => 1];
|
|
|
+ $where = ['a.is_hide' => 0, 'a.mark' => 1];
|
|
|
return $this->model->from('orders as a')
|
|
|
->with(['orderGoods', 'store'])
|
|
|
->leftJoin('member as b', 'b.id', '=', 'a.user_id')
|
|
|
@@ -143,18 +143,18 @@ class OrderService extends BaseService
|
|
|
* 订单详情
|
|
|
* @param $id
|
|
|
*/
|
|
|
- public function getOrderInfo($id, $no='')
|
|
|
+ public function getOrderInfo($id, $no = '')
|
|
|
{
|
|
|
- $where = ['a.order_no'=>$no,'a.id' => $id, 'a.mark' => 1];
|
|
|
- if($no){
|
|
|
+ $where = ['a.order_no' => $no, 'a.id' => $id, 'a.mark' => 1];
|
|
|
+ if ($no) {
|
|
|
unset($where['a.id']);
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
unset($where['a.order_no']);
|
|
|
}
|
|
|
|
|
|
$statusArr = [1 => '待支付', 2 => '待发货', 3 => '待收货', 4 => '已完成'];
|
|
|
$info = $this->model->from('orders as a')
|
|
|
- ->with(['orderGoods','store'])
|
|
|
+ ->with(['orderGoods', 'store'])
|
|
|
->leftJoin('member as b', 'b.id', '=', 'a.user_id')
|
|
|
->where($where)
|
|
|
->select(['a.*'])
|
|
|
@@ -164,9 +164,9 @@ class OrderService extends BaseService
|
|
|
$info = $info->toArray();
|
|
|
$info['create_time'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
|
|
|
$info['pay_at'] = $info['pay_at'] ? datetime(strtotime($info['pay_at']), 'Y-m-d H:i:s') : '';
|
|
|
- $info['receiver_mobile_text'] = $info['receiver_mobile']? format_mobile($info['receiver_mobile']):'';
|
|
|
+ $info['receiver_mobile_text'] = $info['receiver_mobile'] ? format_mobile($info['receiver_mobile']) : '';
|
|
|
$info['real_total'] = $info['pay_total'];
|
|
|
- $info['pay_total'] = moneyFormat($info['pay_total'] + $info['delivery_fee'],2);
|
|
|
+ $info['pay_total'] = moneyFormat($info['pay_total'] + $info['delivery_fee'], 2);
|
|
|
$status = isset($info['status']) ? $info['status'] : 0;
|
|
|
$info['status_text'] = '待支付';
|
|
|
if ($status) {
|
|
|
@@ -186,198 +186,193 @@ class OrderService extends BaseService
|
|
|
*/
|
|
|
public function createOrder($userId, $params)
|
|
|
{
|
|
|
- try {
|
|
|
- $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($goods) || empty($ids)) {
|
|
|
- $this->error = '商品参数不为空';
|
|
|
- return false;
|
|
|
- }
|
|
|
+ $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($goods) || empty($ids)) {
|
|
|
+ $this->error = '商品参数不为空';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- if ($addressId <= 0) {
|
|
|
- $this->error = '请选择收货地址';
|
|
|
- return false;
|
|
|
- }
|
|
|
+ if ($addressId <= 0) {
|
|
|
+ $this->error = '请选择收货地址';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- // 缓存锁
|
|
|
- $cacheLockKey = "caches:orders:submit_lock:{$userId}";
|
|
|
- if (RedisService::get($cacheLockKey)) {
|
|
|
- $this->error = '订单处理中~';
|
|
|
- return false;
|
|
|
- }
|
|
|
+ // 缓存锁
|
|
|
+ $cacheLockKey = "caches:orders:submit_lock:{$userId}";
|
|
|
+ if (RedisService::get($cacheLockKey)) {
|
|
|
+ $this->error = '订单处理中~';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- // 用户信息
|
|
|
- RedisService::set($cacheLockKey, ['params' => $params, 'user_id' => $userId], rand(3, 5));
|
|
|
- $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
|
|
|
- ->select(['id','openid', 'discount_point', 'status'])
|
|
|
- ->first();
|
|
|
- $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
|
|
|
- $openid = isset($userInfo['openid']) ? $userInfo['openid'] : 0;
|
|
|
- $discountPoint = isset($userInfo['discount_point']) ? $userInfo['discount_point'] : 0; // 折扣
|
|
|
- if (empty($userInfo) || $status != 1) {
|
|
|
- $this->error = 1045;
|
|
|
- return false;
|
|
|
- }
|
|
|
+ // 用户信息
|
|
|
+ RedisService::set($cacheLockKey, ['params' => $params, 'user_id' => $userId], rand(3, 5));
|
|
|
+ $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
|
|
|
+ ->select(['id', 'openid', 'discount_point', 'status'])
|
|
|
+ ->first();
|
|
|
+ $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
|
|
|
+ $openid = isset($userInfo['openid']) ? $userInfo['openid'] : 0;
|
|
|
+ $discountPoint = isset($userInfo['discount_point']) ? $userInfo['discount_point'] : 0; // 折扣
|
|
|
+ if (empty($userInfo) || $status != 1) {
|
|
|
+ $this->error = 1045;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- if (empty($openid)) {
|
|
|
- $this->error = 1042;
|
|
|
- return false;
|
|
|
- }
|
|
|
+ if (empty($openid)) {
|
|
|
+ $this->error = 1042;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- // 商品数据
|
|
|
- $orderNo = get_order_num('JK');
|
|
|
- $result = GoodsService::make()->getOrderGoods($ids, $goods, $userId, $orderNo);
|
|
|
- if (empty($result)) {
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- $this->error = GoodsService::make()->getError();
|
|
|
- return false;
|
|
|
- }
|
|
|
+ // 商品数据
|
|
|
+ $orderNo = get_order_num('JK');
|
|
|
+ $result = GoodsService::make()->getOrderGoods($ids, $goods, $userId, $orderNo);
|
|
|
+ if (empty($result)) {
|
|
|
+ RedisService::clear($cacheLockKey);
|
|
|
+ $this->error = GoodsService::make()->getError();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- $orderGoods = isset($result['goods']) ? $result['goods'] : [];
|
|
|
- $goodsTotal = isset($result['goods_total']) ? $result['goods_total'] : 0; // 商品总价
|
|
|
- $orderTotal = isset($result['order_total']) ? $result['order_total'] : 0; // 订单结算金额(折后)
|
|
|
- $discountTotal = isset($result['discount_total']) ? $result['discount_total'] : 0; // 折扣金额
|
|
|
- $orderCount = isset($result['count']) ? $result['count'] : 0;
|
|
|
- $deliveryFee = isset($result['delivery_fee']) ? $result['delivery_fee'] : 0; // 运费
|
|
|
- $storeId = isset($result['store_id']) ? $result['store_id'] : 0; // 企业/商家
|
|
|
- if (empty($orderGoods)) {
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- $this->error = '获取订单商品错误~';
|
|
|
- return false;
|
|
|
- }
|
|
|
+ $orderGoods = isset($result['goods']) ? $result['goods'] : [];
|
|
|
+ $goodsTotal = isset($result['goods_total']) ? $result['goods_total'] : 0; // 商品总价
|
|
|
+ $orderTotal = isset($result['order_total']) ? $result['order_total'] : 0; // 订单结算金额(折后)
|
|
|
+ $discountTotal = isset($result['discount_total']) ? $result['discount_total'] : 0; // 折扣金额
|
|
|
+ $orderCount = isset($result['count']) ? $result['count'] : 0;
|
|
|
+ $deliveryFee = isset($result['delivery_fee']) ? $result['delivery_fee'] : 0; // 运费
|
|
|
+ $storeId = isset($result['store_id']) ? $result['store_id'] : 0; // 企业/商家
|
|
|
+ if (empty($orderGoods)) {
|
|
|
+ RedisService::clear($cacheLockKey);
|
|
|
+ $this->error = '获取订单商品错误~';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- if ($orderTotal <= 0) {
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- $this->error = '订单金额错误~';
|
|
|
- return false;
|
|
|
- }
|
|
|
+ if ($orderTotal <= 0) {
|
|
|
+ RedisService::clear($cacheLockKey);
|
|
|
+ $this->error = '订单金额错误~';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- if ($orderCount <= 0) {
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- $this->error = '订单商品数量错误~';
|
|
|
- return false;
|
|
|
- }
|
|
|
+ if ($orderCount <= 0) {
|
|
|
+ RedisService::clear($cacheLockKey);
|
|
|
+ $this->error = '订单商品数量错误~';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- // 收货地址信息
|
|
|
- $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;
|
|
|
- }
|
|
|
+ // 收货地址信息
|
|
|
+ $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;
|
|
|
+ }
|
|
|
|
|
|
- // 商家佣金
|
|
|
- $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 / 100, 2);
|
|
|
+ // 商家佣金
|
|
|
+ $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 / 100, 2);
|
|
|
|
|
|
- if (env('PAY_DEBUG')) {
|
|
|
- $orderTotal = 0.1;
|
|
|
- }
|
|
|
+ if (env('PAY_DEBUG')) {
|
|
|
+ $orderTotal = 0.1;
|
|
|
+ }
|
|
|
|
|
|
- // 订单数据
|
|
|
- $order = [
|
|
|
- 'order_no' => $orderNo,
|
|
|
- 'user_id' => $userId,
|
|
|
- 'store_id' => $storeId,
|
|
|
- 'total' => $goodsTotal, // 商品总价
|
|
|
- 'num' => $orderCount,
|
|
|
- 'pay_total' => $orderTotal, // 折扣后商品总价
|
|
|
- 'discount_point' => $discountPoint,
|
|
|
- 'discount_total' => $discountTotal, // 折扣金额
|
|
|
- 'delivery_fee' => $deliveryFee, // 运费
|
|
|
- 'receiver_name' => $realname,
|
|
|
- 'receiver_mobile' => $mobile,
|
|
|
- 'receiver_area' => $area,
|
|
|
- 'receiver_address' => $address,
|
|
|
- 'bonus' => $bonus,
|
|
|
- 'create_time' => time(),
|
|
|
- 'update_time' => time(),
|
|
|
- 'status' => 1,
|
|
|
- 'mark' => 1,
|
|
|
- ];
|
|
|
-
|
|
|
- // 订单处理
|
|
|
- DB::beginTransaction();
|
|
|
- if (!$orderId = $this->model->insertGetId($order)) {
|
|
|
- DB::rollBack();
|
|
|
- $this->error = '创建订单失败';
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- return false;
|
|
|
- }
|
|
|
+ // 订单数据
|
|
|
+ $order = [
|
|
|
+ 'order_no' => $orderNo,
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'store_id' => $storeId,
|
|
|
+ 'total' => $goodsTotal, // 商品总价
|
|
|
+ 'num' => $orderCount,
|
|
|
+ 'pay_total' => $orderTotal, // 折扣后商品总价
|
|
|
+ 'discount_point' => $discountPoint,
|
|
|
+ 'discount_total' => $discountTotal, // 折扣金额
|
|
|
+ 'delivery_fee' => $deliveryFee, // 运费
|
|
|
+ 'receiver_name' => $realname,
|
|
|
+ 'receiver_mobile' => $mobile,
|
|
|
+ 'receiver_area' => $area,
|
|
|
+ 'receiver_address' => $address,
|
|
|
+ 'bonus' => $bonus,
|
|
|
+ 'create_time' => time(),
|
|
|
+ 'update_time' => time(),
|
|
|
+ 'status' => 1,
|
|
|
+ 'mark' => 1,
|
|
|
+ ];
|
|
|
|
|
|
- // 订单商品
|
|
|
- if ($orderGoods && !OrderGoodsModel::insert($orderGoods)) {
|
|
|
- DB::rollBack();
|
|
|
- $this->error = '处理订单商品错误';
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- return false;
|
|
|
- }
|
|
|
+ // 订单处理
|
|
|
+ DB::beginTransaction();
|
|
|
+ if (!$orderId = $this->model->insertGetId($order)) {
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = '创建订单失败';
|
|
|
+ RedisService::clear($cacheLockKey);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- // 获取支付参数
|
|
|
- /* TODO 支付处理 */
|
|
|
- $payOrder = [
|
|
|
- 'type' => 1,
|
|
|
- 'order_no' => $orderNo,
|
|
|
- 'pay_money' => moneyFormat($orderTotal + $deliveryFee,2),
|
|
|
- '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;
|
|
|
- }
|
|
|
+ // 订单商品
|
|
|
+ if ($orderGoods && !OrderGoodsModel::insert($orderGoods)) {
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = '处理订单商品错误';
|
|
|
+ RedisService::clear($cacheLockKey);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- // 商品库存扣除
|
|
|
- if($orderGoods){
|
|
|
- foreach($orderGoods as $goods){
|
|
|
- $id = isset($goods['goods_id'])?$goods['goods_id']:0;
|
|
|
- $num = isset($goods['num'])?$goods['num']:0;
|
|
|
- $skuId = isset($goods['sku_id'])?$goods['sku_id']:0;
|
|
|
- if($id && !GoodsModel::where(['id'=>$id])->update(['stock'=>DB::raw("stock - {$num}"),'update_time'=>time()])){
|
|
|
- DB::rollBack();
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- $this->error = '商品库存处理失败';
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- if($skuId && !GoodsSkuModel::where(['id'=>$skuId])->update(['stock'=>DB::raw("stock - {$num}"),'update_time'=>time()])){
|
|
|
- DB::rollBack();
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- $this->error = '商品库存处理失败';
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ // 获取支付参数
|
|
|
+ /* TODO 支付处理 */
|
|
|
+ $payOrder = [
|
|
|
+ 'type' => 1,
|
|
|
+ 'order_no' => $orderNo,
|
|
|
+ 'pay_money' => moneyFormat($orderTotal + $deliveryFee, 2),
|
|
|
+ 'body' => '购物消费',
|
|
|
+ 'openid' => $openid
|
|
|
+ ];
|
|
|
|
|
|
- // 用户操作记录
|
|
|
- DB::commit();
|
|
|
- $this->error = '订单创建成功,请前往支付~';
|
|
|
+ // 调起支付
|
|
|
+ $payment = PaymentService::make()->minPay($userInfo, $payOrder, 'store');
|
|
|
+ if (empty($payment)) {
|
|
|
+// DB::rollBack();
|
|
|
RedisService::clear($cacheLockKey);
|
|
|
- return [
|
|
|
- 'order_id' => $orderId,
|
|
|
- 'payment' => $payment,
|
|
|
- 'total' => $payOrder['pay_money'],
|
|
|
- 'pay_type' => 10,
|
|
|
- ];
|
|
|
- } catch (\Exception $exception){
|
|
|
- $this->error = '创建订单失败:'.$exception->getMessage();
|
|
|
+ $this->error = PaymentService::make()->getError();
|
|
|
return false;
|
|
|
}
|
|
|
+
|
|
|
+ // 商品库存扣除
|
|
|
+ if ($orderGoods) {
|
|
|
+ foreach ($orderGoods as $goods) {
|
|
|
+ $id = isset($goods['goods_id']) ? $goods['goods_id'] : 0;
|
|
|
+ $num = isset($goods['num']) ? $goods['num'] : 0;
|
|
|
+ $skuId = isset($goods['sku_id']) ? $goods['sku_id'] : 0;
|
|
|
+ if ($id && !GoodsModel::where(['id' => $id])->update(['stock' => DB::raw("stock - {$num}"), 'update_time' => time()])) {
|
|
|
+ DB::rollBack();
|
|
|
+ RedisService::clear($cacheLockKey);
|
|
|
+ $this->error = '商品库存处理失败';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($skuId && !GoodsSkuModel::where(['id' => $skuId])->update(['stock' => DB::raw("stock - {$num}"), 'update_time' => time()])) {
|
|
|
+ DB::rollBack();
|
|
|
+ RedisService::clear($cacheLockKey);
|
|
|
+ $this->error = '商品库存处理失败';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 用户操作记录
|
|
|
+ DB::commit();
|
|
|
+ $this->error = '订单创建成功,请前往支付~';
|
|
|
+ RedisService::clear($cacheLockKey);
|
|
|
+ return [
|
|
|
+ 'order_id' => $orderId,
|
|
|
+ 'payment' => $payment,
|
|
|
+ 'total' => $payOrder['pay_money'],
|
|
|
+ 'pay_type' => 10,
|
|
|
+ ];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -423,7 +418,7 @@ class OrderService extends BaseService
|
|
|
|
|
|
// 订单信息
|
|
|
$info = $this->model->where(['id' => $id, 'mark' => 1])
|
|
|
- ->select(['id', 'order_no', 'pay_total','delivery_fee', 'status'])
|
|
|
+ ->select(['id', 'order_no', 'pay_total', 'delivery_fee', 'status'])
|
|
|
->first();
|
|
|
$orderTotal = isset($info['pay_total']) ? $info['pay_total'] : 0;
|
|
|
$deliveryFee = isset($info['delivery_fee']) ? $info['delivery_fee'] : 0;
|
|
|
@@ -522,23 +517,23 @@ class OrderService extends BaseService
|
|
|
RedisService::clear($cacheLockKey);
|
|
|
return false;
|
|
|
}
|
|
|
- $orderGoods = OrderGoodsModel::where(['order_no'=> $orderNo,'mark'=>1])
|
|
|
- ->select(['goods_id','num','sku_id'])
|
|
|
+ $orderGoods = OrderGoodsModel::where(['order_no' => $orderNo, 'mark' => 1])
|
|
|
+ ->select(['goods_id', 'num', 'sku_id'])
|
|
|
->get();
|
|
|
DB::beginTransaction();
|
|
|
- if($orderGoods){
|
|
|
- foreach($orderGoods as $goods){
|
|
|
- $goodsId = isset($goods['goods_id'])?$goods['goods_id']:0;
|
|
|
- $num = isset($goods['num'])?$goods['num']:0;
|
|
|
- $skuId = isset($goods['sku_id'])?$goods['sku_id']:0;
|
|
|
- if($goodsId && !GoodsModel::where(['id'=>$goodsId])->update(['stock'=>DB::raw("stock + {$num}"),'update_time'=>time()])){
|
|
|
+ if ($orderGoods) {
|
|
|
+ foreach ($orderGoods as $goods) {
|
|
|
+ $goodsId = isset($goods['goods_id']) ? $goods['goods_id'] : 0;
|
|
|
+ $num = isset($goods['num']) ? $goods['num'] : 0;
|
|
|
+ $skuId = isset($goods['sku_id']) ? $goods['sku_id'] : 0;
|
|
|
+ if ($goodsId && !GoodsModel::where(['id' => $goodsId])->update(['stock' => DB::raw("stock + {$num}"), 'update_time' => time()])) {
|
|
|
DB::rollBack();
|
|
|
RedisService::clear($cacheLockKey);
|
|
|
$this->error = '商品库存处理失败';
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- if($skuId && !GoodsSkuModel::where(['id'=>$skuId])->update(['stock'=>DB::raw("stock + {$num}"),'update_time'=>time()])){
|
|
|
+ if ($skuId && !GoodsSkuModel::where(['id' => $skuId])->update(['stock' => DB::raw("stock + {$num}"), 'update_time' => time()])) {
|
|
|
DB::rollBack();
|
|
|
RedisService::clear($cacheLockKey);
|
|
|
$this->error = '商品库存处理失败';
|
|
|
@@ -618,7 +613,7 @@ class OrderService extends BaseService
|
|
|
* @param $id 订单ID
|
|
|
* @return array|false
|
|
|
*/
|
|
|
- public function complete($userId, $id, $check= true)
|
|
|
+ public function complete($userId, $id, $check = true)
|
|
|
{
|
|
|
if ($id <= 0) {
|
|
|
$this->error = '请选择订单';
|
|
|
@@ -674,15 +669,15 @@ class OrderService extends BaseService
|
|
|
|
|
|
if (empty($deliveryNo) || empty($deliverCompany)) {
|
|
|
$this->error = '订单发货信息错误,请联系客服';
|
|
|
- $this->model->where(['id'=>$id])->update(['is_complete'=>1,'complete_remark'=>$this->error]);
|
|
|
+ $this->model->where(['id' => $id])->update(['is_complete' => 1, 'complete_remark' => $this->error]);
|
|
|
RedisService::clear("caches:orders:completeList");
|
|
|
RedisService::clear($cacheLockKey);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
DB::beginTransaction();
|
|
|
- $updateData = ['status' => 4,'complete_at'=>date('Y-m-d H:i:s'), 'update_time' => time()];
|
|
|
- if(!$check){
|
|
|
+ $updateData = ['status' => 4, 'complete_at' => date('Y-m-d H:i:s'), 'update_time' => time()];
|
|
|
+ if (!$check) {
|
|
|
$updateData['is_complete'] = 1;
|
|
|
$updateData['complete_remark'] = '自动收货';
|
|
|
}
|
|
|
@@ -717,12 +712,11 @@ class OrderService extends BaseService
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
DB::commit();
|
|
|
|
|
|
$this->error = '确认收货成功';
|
|
|
RedisService::clear("caches:orders:completeList");
|
|
|
- return ['id' => $id,'msg'=>$this->error];
|
|
|
+ return ['id' => $id, 'msg' => $this->error];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -780,7 +774,7 @@ class OrderService extends BaseService
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- if ($status == 4 && $afterType==2) {
|
|
|
+ if ($status == 4 && $afterType == 2) {
|
|
|
$this->error = '订单已完成';
|
|
|
RedisService::clear($cacheLockKey);
|
|
|
return false;
|
|
|
@@ -830,7 +824,7 @@ class OrderService extends BaseService
|
|
|
$deliveryNo = isset($info['delivery_no']) ? $info['delivery_no'] : '';
|
|
|
$deliveryCode = isset($info['delivery_code']) ? $info['delivery_code'] : '';
|
|
|
$mobile = isset($info['receiver_mobile']) ? $info['receiver_mobile'] : '';
|
|
|
- $receiverArea = isset($info['receiver_area']) && $info['receiver_area']? $info['receiver_area'] : '';
|
|
|
+ $receiverArea = isset($info['receiver_area']) && $info['receiver_area'] ? $info['receiver_area'] : '';
|
|
|
if (empty($info)) {
|
|
|
$this->error = '请选择订单';
|
|
|
return false;
|
|
|
@@ -841,30 +835,30 @@ class OrderService extends BaseService
|
|
|
if ($data) {
|
|
|
return $data;
|
|
|
}
|
|
|
- $result = Kd100Service::make()->query($deliveryNo, $mobile, $deliveryCode,$receiverArea);
|
|
|
- RedisService::set($cacheKey.'_result', $result, 300);
|
|
|
- $status = isset($result['status'])?$result['status']:0;
|
|
|
- $data = isset($result['data'])?$result['data']:[];
|
|
|
- $courierInfo = isset($result['courierInfo'])?$result['courierInfo']:[];
|
|
|
- $arrivalTime = isset($result['arrivalTime'])?$result['arrivalTime']:'';
|
|
|
- $predictedRoute = isset($result['predictedRoute'])?$result['predictedRoute']:[];
|
|
|
- $predictedData = $predictedRoute?end($predictedRoute):[];
|
|
|
+ $result = Kd100Service::make()->query($deliveryNo, $mobile, $deliveryCode, $receiverArea);
|
|
|
+ RedisService::set($cacheKey . '_result', $result, 300);
|
|
|
+ $status = isset($result['status']) ? $result['status'] : 0;
|
|
|
+ $data = isset($result['data']) ? $result['data'] : [];
|
|
|
+ $courierInfo = isset($result['courierInfo']) ? $result['courierInfo'] : [];
|
|
|
+ $arrivalTime = isset($result['arrivalTime']) ? $result['arrivalTime'] : '';
|
|
|
+ $predictedRoute = isset($result['predictedRoute']) ? $result['predictedRoute'] : [];
|
|
|
+ $predictedData = $predictedRoute ? end($predictedRoute) : [];
|
|
|
$arrivalData = [];
|
|
|
- if($arrivalTime){
|
|
|
- $arrivalData['arrivalTime'] = dayFormat(strtotime($arrivalTime.':00:00'));
|
|
|
- $arrivalData['predictedData'] = $predictedData;
|
|
|
+ if ($arrivalTime) {
|
|
|
+ $arrivalData['arrivalTime'] = dayFormat(strtotime($arrivalTime . ':00:00'));
|
|
|
+ $arrivalData['predictedData'] = $predictedData;
|
|
|
}
|
|
|
|
|
|
- if($courierInfo && $courierInfo['deliveryManPhone']){
|
|
|
+ if ($courierInfo && $courierInfo['deliveryManPhone']) {
|
|
|
$courierInfo['deliveryManPhone'] = explode(',', $courierInfo['deliveryManPhone']);
|
|
|
- $courierInfo['deliveryPhone'] = $courierInfo['deliveryManPhone'][1]?$courierInfo['deliveryManPhone'][1]:$courierInfo['deliveryManPhone'][0];
|
|
|
+ $courierInfo['deliveryPhone'] = $courierInfo['deliveryManPhone'][1] ? $courierInfo['deliveryManPhone'][1] : $courierInfo['deliveryManPhone'][0];
|
|
|
}
|
|
|
|
|
|
- if ($data && $status==200) {
|
|
|
- RedisService::set($cacheKey, ['info'=>$courierInfo,'arrivalData'=>$arrivalData,'list'=>$data], 1200);
|
|
|
+ if ($data && $status == 200) {
|
|
|
+ RedisService::set($cacheKey, ['info' => $courierInfo, 'arrivalData' => $arrivalData, 'list' => $data], 1200);
|
|
|
}
|
|
|
|
|
|
- return $data?['info'=>$courierInfo,'arrivalData'=>$arrivalData,'list'=>$data]:[];
|
|
|
+ return $data ? ['info' => $courierInfo, 'arrivalData' => $arrivalData, 'list' => $data] : [];
|
|
|
|
|
|
}
|
|
|
|
|
|
@@ -876,22 +870,22 @@ class OrderService extends BaseService
|
|
|
{
|
|
|
$cacheKey = "caches:orders:completeList";
|
|
|
$datas = RedisService::get($cacheKey);
|
|
|
- if($datas){
|
|
|
+ if ($datas) {
|
|
|
return $datas;
|
|
|
}
|
|
|
|
|
|
- $completeDay = ConfigService::make()->getConfigByCode('order_complete_day',7);
|
|
|
- $limitNum = ConfigService::make()->getConfigByCode('order_complete_batch_num',300);
|
|
|
- $limitNum = $limitNum>10 && $limitNum<2000? $limitNum : 300;
|
|
|
- $completeDay = $completeDay>=1 && $completeDay<30? $completeDay : 7;
|
|
|
- $datas = $this->model->where(['status'=>3,'is_complete'=>2,'mark'=>1])
|
|
|
+ $completeDay = ConfigService::make()->getConfigByCode('order_complete_day', 7);
|
|
|
+ $limitNum = ConfigService::make()->getConfigByCode('order_complete_batch_num', 300);
|
|
|
+ $limitNum = $limitNum > 10 && $limitNum < 2000 ? $limitNum : 300;
|
|
|
+ $completeDay = $completeDay >= 1 && $completeDay < 30 ? $completeDay : 7;
|
|
|
+ $datas = $this->model->where(['status' => 3, 'is_complete' => 2, 'mark' => 1])
|
|
|
->whereNotNull('delivery_no')
|
|
|
- ->select(['id','user_id','order_no','status'])
|
|
|
- ->where('pay_at','<=', date('Y-m-d H:i:s', time() - $completeDay * 86400))
|
|
|
+ ->select(['id', 'user_id', 'order_no', 'status'])
|
|
|
+ ->where('pay_at', '<=', date('Y-m-d H:i:s', time() - $completeDay * 86400))
|
|
|
->limit($limitNum)
|
|
|
->get();
|
|
|
- $datas = $datas?$datas->toArray() : [];
|
|
|
- if($datas){
|
|
|
+ $datas = $datas ? $datas->toArray() : [];
|
|
|
+ if ($datas) {
|
|
|
RedisService::set($cacheKey, $datas, rand(300, 600));
|
|
|
}
|
|
|
|