diff --git a/app/Constants/v3/OrderState.php b/app/Constants/v3/OrderState.php index 718dffc..af1ff7c 100644 --- a/app/Constants/v3/OrderState.php +++ b/app/Constants/v3/OrderState.php @@ -75,6 +75,14 @@ class OrderState extends AbstractConstants */ const REFUND = [self::REFUNDING, self::REFUNDED, self::REFUND_REFUSE]; + /** + * @Message("可删除") + */ const CAN_DEL = [self::COMPLETED, self::EVALUATED, self::CANCELED, self::REFUNDED, self::REFUND_REFUSE]; + /** + * @Message("可强行退款") + */ + const CAN_REFUND_DIRECT = [self::COMPLETED, self::EVALUATED, self::REFUND_REFUSE]; + } \ No newline at end of file diff --git a/app/JsonRpc/LocationService.php b/app/JsonRpc/LocationService.php new file mode 100644 index 0000000..bb9358f --- /dev/null +++ b/app/JsonRpc/LocationService.php @@ -0,0 +1,32 @@ +locationService->getDistanceByTencent($lng1, $lat1, $lng2, $lat2); + } +} \ No newline at end of file diff --git a/app/JsonRpc/LocationServiceInterface.php b/app/JsonRpc/LocationServiceInterface.php new file mode 100644 index 0000000..cf076bb --- /dev/null +++ b/app/JsonRpc/LocationServiceInterface.php @@ -0,0 +1,10 @@ +orderOnlineService->doRefund($global_order_id, $user_id); + $result = $this->orderOnlineService->doRefund($global_order_id, $user_id); Db::commit(); - return [ - "status" => 200, - "code" => 0, - "result" => [], - "message" => '处理成功' - ]; + if ($result['return_code'] == 'SUCCESS' && isset($result['result_code']) && $result['result_code'] == "SUCCESS") { + return [ + "status" => 200, + "code" => 0, + "result" => [], + "message" => '处理成功' + ]; + } else { + return [ + "status" => 200, + "code" => -1, + "result" => [], + "message" => $result['err_code_des'] + ]; + } } catch (\Exception $e) { Db::rollBack(); @@ -98,15 +113,149 @@ class OrderService implements OrderServiceInterface } /** - * 线上订单单笔退款 + * 线上订单单笔退款,主要用于后台强行操作退单退款 * 支持单商品、单店、整单 - * @param $global_order_id 全局总订单ID - * @param $child_order_id 主订单ID, - * @param $order_goods_id 订单商品ID + * 按比例计算红包进行退款 + * 比如:两个子订单和子订单商品,分别是2元,98元,使用了10元优惠券 + * 退2元商品时,退款金额为 + * 红包 :(2/(98+2))*10 = 0.2 + * 退款:2-0.2=1.8元 + * @param $user_id *用户ID + * @param $global_order_id *全局总订单ID + * @param $order_child_id *主订单ID, + * @param $order_goods_id *订单商品ID * @param $note + * @throws InvalidConfigException */ - public function onlineSingleRefund($global_order_id, $child_order_id, $order_goods_id, $note) + public function onlineSingleRefund($user_id, $note, $global_order_id, $order_child_id=null, $order_goods_id=null) { + $params = [ + 'user_id' => $user_id, + 'note' => $note, + 'global_order_id' => $global_order_id, + 'order_child_id' => $order_child_id, + 'order_goods_id' => $order_goods_id, + ]; + + if (!$user_id || !$global_order_id || !$note) { + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => '参数不对', + 'params' => json_encode($params) + ]); + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } + + // 主订单 + $orderMain = OrderMain::query() + ->where(['global_order_id' => $global_order_id, 'user_id' => $user_id]) + ->whereIn('state', OrderState::CAN_REFUND_DIRECT) + ->first(); + + if (is_null($orderMain)) { + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => '订单不存在', + 'params' => json_encode($params) + ]); + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } + + // 子订单 + $orderChild = null; + if ($order_child_id) { + $orderChild = Order::query()->where(['order_main_id' => $orderMain->global_order_id, 'id' => $order_child_id])->first(); + } + + // 订单商品 + $orderGoods = null; + if ($order_goods_id) { + if (!$order_child_id || is_null($orderChild)) { + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => '子订单参数异常[单品]', + 'params' => json_encode($params) + ]); + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } + + $orderGoods = OrderGoods::query()->where(['order_id' => $orderChild->id, 'id' => $order_goods_id])->first(); + + if (is_null($orderGoods)) { + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => '订单商品参数异常[单品]', + 'params' => json_encode($params) + ]); + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } + + } + + $totalAmount = $orderMain->total_money; // 订单可退款金额,总订单金额不含配送费和服务费 + $preRefundAmount = 0; // 预退款金额 + $refundAmount = 0; // 实际退款金额 + $refundType = 'Main'; // 退款类型, Main整单 Sub子单 Goods单品 + if ($orderGoods) { // 1. 如果订单商品存在则说明要退单品 + $preRefundAmount = bcmul($orderGoods->price, $orderGoods->number, 2); + $refundType = 'Goods'; + } elseif ($orderChild) { // 2. 否则如果存在子订单说明退子订单 + $preRefundAmount = $orderChild->money; + $refundType = 'Sub'; + } else { // 3. 再则如果存在主订单说明退主订单 + $preRefundAmount = $orderMain->total_money; + $refundType = 'Main'; + } + + // 占订单金额的比例 + $rate = bcdiv($preRefundAmount, $totalAmount, 6); + // 计算优惠券所占金额 + $couponMoney = bcmul($orderMain->coupon_money, $rate, 6); + // 计算本次退款实际退款金额 + $refundAmount = bcsub($preRefundAmount, $couponMoney, 2); + + if ($refundAmount <= 0) { + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => '没有可退款金额[实际退款金额]', + 'params' => json_encode($params) + ]); + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } + + // 开始退款 + $config = config('wxpay'); + $app = Factory::payment($config); + $app['guzzle_handler'] = CoroutineHandler::class; + $result = $app->refund->byOutTradeNumber( + $orderMain->global_order_id, + $orderMain->global_order_id, + bcmul($orderMain->money, 100, 0), + bcmul($refundAmount, 100, 0), + [ + 'refund_desc' => '订单协商退款['.$refundType.']', + 'notify_url' => config('wechat.notify_url.refund_single'), + ] + ); + + if ($result['return_code'] == 'FAIL') { + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => $result['return_msg'].'[微信退款失败]', + 'params' => json_encode($result) + ]); + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } + + if ($result['result_code'] == 'FAIL') { + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => $result['err_code_des'].'[微信退款失败]'.$result['err_code'], + 'params' => json_encode($result) + ]); + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } + + // 退款申请成功,查询退款状态 + $refundResult = $app->refund->queryByRefundId($result['refund_id']); + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => '[微信退款查询]', + 'params' => json_encode($refundResult) + ]); + } } \ No newline at end of file diff --git a/app/Model/v3/Category.php b/app/Model/v3/Category.php index 7c3eaa6..1dda811 100644 --- a/app/Model/v3/Category.php +++ b/app/Model/v3/Category.php @@ -21,9 +21,7 @@ class Category extends Model protected $table = 'lanzu_category'; protected $appends = [ - 'goods_types', - 'cover_url', - 'goods_category_ids', + 'cover_url' ]; protected function boot(): void @@ -34,18 +32,18 @@ class Category extends Model }); } - public function getGoodsTypesAttribute() + public function getCoverUrlAttribute() { - return self::query()->where(['parent_id' => $this->attributes['id']])->orderBy('sort', 'desc')->get()->toArray(); + return $this->attachmentService->switchImgToAliOss($this->attributes['cover_img']); } - public function getGoodsCategoryIdsAttribute() + public function goodsTypes() { - return GoodsCategory::query()->where(['category_id' => $this->attributes['id']])->orderBy('sort', 'desc')->pluck('id'); + return $this->hasMany(self::class, 'parent_id', 'id')->with('goodsCategory'); } - - public function getCoverUrlAttribute() + + public function goodsCategory() { - return $this->attachmentService->switchImgToAliOss($this->attributes['cover_img']); + return $this->hasMany(GoodsCategory::class, 'category_id', 'id'); } } \ No newline at end of file diff --git a/app/Model/v3/OrderMain.php b/app/Model/v3/OrderMain.php index 9d4f06b..c3abfc8 100644 --- a/app/Model/v3/OrderMain.php +++ b/app/Model/v3/OrderMain.php @@ -56,6 +56,10 @@ class OrderMain extends Model 'shipping_type_text', ]; + protected $casts = [ + 'global_order_id' => 'string' + ]; + public function getCreatedAtTextAttribute() { return date('Y-m-d H:i:s', $this->attributes['created_at']); @@ -68,6 +72,12 @@ class OrderMain extends Model public function getStateTextAttribute() { + if ($this->attributes['state'] == 3) { + if (!$this->attributes['horseman_id']) { + return '已接单'; + } + } + return OrderState::getMessage($this->attributes['state']); } @@ -99,7 +109,7 @@ class OrderMain extends Model Order::class, 'order_main_id', 'order_id', - 'id', + 'global_order_id', 'id' ); } diff --git a/app/Model/v3/ShoppingCart.php b/app/Model/v3/ShoppingCart.php index b07b814..e1d246c 100644 --- a/app/Model/v3/ShoppingCart.php +++ b/app/Model/v3/ShoppingCart.php @@ -3,10 +3,11 @@ namespace App\Model\v3; use App\Model\Model; +use Hyperf\Database\Model\SoftDeletes; class ShoppingCart extends Model { - + use SoftDeletes; protected $table = 'lanzu_shopping_cart'; protected $fillable = [ diff --git a/app/Request/v3/SearchGoodsRequest.php b/app/Request/v3/SearchGoodsRequest.php index 7b85add..46ec354 100644 --- a/app/Request/v3/SearchGoodsRequest.php +++ b/app/Request/v3/SearchGoodsRequest.php @@ -16,7 +16,8 @@ class SearchGoodsRequest extends BaseFormRequest { return [ 'market_id' => 'required|nonempty|integer', - 'type_id' => 'nonempty|integer', + 'type_id' => 'nonempty', + 'goods_category_ids' => 'nonempty', 'store_id' => 'nonempty|integer', 'keyword' => 'nonempty', 'order_by' => 'nonempty|in:default,sales,price', diff --git a/app/Service/v3/Implementations/CategoryService.php b/app/Service/v3/Implementations/CategoryService.php index 6be32bd..039e532 100644 --- a/app/Service/v3/Implementations/CategoryService.php +++ b/app/Service/v3/Implementations/CategoryService.php @@ -32,7 +32,20 @@ class CategoryService implements CategoryServiceInterface public function all() { - return Category::query()->where(['parent_id' => 0])->get()->toArray(); + $categories = Category::query() + ->with(['goodsTypes']) + ->where(['parent_id' => 0])->get()->toArray(); + + foreach ($categories as $key => &$category) { + foreach ($category['goods_types'] as $key2 => &$item) { + $item['goods_category_ids'] = ''; + if (isset($item['goods_category']) && $item['goods_category']) { + $item['goods_category_ids'] = implode(',', array_values(array_column($item['goods_category'], 'id'))); + } + } + } + + return $categories; } public function allForStore($storeId) diff --git a/app/Service/v3/Implementations/OrderOnlineService.php b/app/Service/v3/Implementations/OrderOnlineService.php index dffbe17..7ee6c7a 100644 --- a/app/Service/v3/Implementations/OrderOnlineService.php +++ b/app/Service/v3/Implementations/OrderOnlineService.php @@ -244,13 +244,7 @@ class OrderOnlineService implements OrderOnlineServiceInterface // 校验订单总金额 if ($totalAmount != $totalMoney) { - throw new ErrorCodeException(ErrorCode::ORDER_TOTAL_AMOUNT_ERROR, json_encode([ - '计算的总订单金额:' => $totalAmount, - '前端的订单总金额:' => $totalMoney, - 'Delivery:' => $deliveryAmount, - 'Service:' => $serviceMoney, - 'Coupon:' => $couponMoney, - ])); + throw new ErrorCodeException(ErrorCode::ORDER_TOTAL_AMOUNT_ERROR); } $dataMain = [ @@ -563,7 +557,6 @@ class OrderOnlineService implements OrderOnlineServiceInterface { $orderMain = $this->check($globalOrderId, $userId, OrderState::REFUNDING); - // 微信退款 if ($orderMain->pay_type == Payment::WECHAT) { return $this->paymentService->undo($orderMain->global_order_id, $userId); diff --git a/app/Service/v3/Implementations/PaymentService.php b/app/Service/v3/Implementations/PaymentService.php index 0f92373..e979635 100644 --- a/app/Service/v3/Implementations/PaymentService.php +++ b/app/Service/v3/Implementations/PaymentService.php @@ -45,7 +45,6 @@ class PaymentService implements PaymentServiceInterface ->where(['state' => OrderState::UNPAID, 'global_order_id' => $globalOrderId, 'user_id' => $userId]) ->where('created_at', '>=', (time()-900)) ->first(); - if (empty($orderMain)) { throw new ErrorCodeException(ErrorCode::ORDER_NOT_AVAILABLE, '[支付订单号]'.$globalOrderId); } @@ -112,10 +111,8 @@ class PaymentService implements PaymentServiceInterface // 已支付的,未退款的,使用微信支付的订单 $orderMain = OrderMain::query() ->whereIn('state', [OrderState::PAID, OrderState::DELIVERY, OrderState::COMPLETED, OrderState::EVALUATED, OrderState::REFUNDING]) - ->where(['global_order_id' => $globalOrderId, 'user_id' => $userId, 'pay_type' => Payment::WECHAT]) - ->whereRaw('refund_time is null') + ->where(['global_order_id' => $globalOrderId, 'user_id' => $userId, 'pay_type' => Payment::WECHAT,'refund_time'=>0]) ->first(); - if (empty($orderMain)) { throw new ErrorCodeException(ErrorCode::ORDER_NOT_AVAILABLE, '[支付订单号]'.$globalOrderId); } @@ -130,7 +127,7 @@ class PaymentService implements PaymentServiceInterface 'notify_url' => config('wechat.notify_url.refund'), ] ); - + return $result; } catch (\Exception $e) { $this->log->event(LogLabel::ORDER_PAYMENT_LOG, ['payment_do_exception_msg' => $e->getMessage()]); throw new ErrorCodeException(ErrorCode::PAYMENT_FAIL, '[退款失败]'.$e->getMessage()); diff --git a/app/Service/v3/Implementations/SearchService.php b/app/Service/v3/Implementations/SearchService.php index e1f5c84..6b6a0b1 100644 --- a/app/Service/v3/Implementations/SearchService.php +++ b/app/Service/v3/Implementations/SearchService.php @@ -23,6 +23,11 @@ class SearchService implements \App\Service\v3\Interfaces\SearchServiceInterface if (isset($params['type_id']) && $params['type_id']) { $typeIds = explode(',', $params['type_id']); + $builder->whereIn('category_id', $typeIds); + } + + if (isset($params['goods_category_ids']) && $params['goods_category_ids']) { + $typeIds = explode(',', $params['goods_category_ids']); $builder->whereIn('goods_category_id', $typeIds); } diff --git a/config/autoload/dependencies.php b/config/autoload/dependencies.php index 2962fb9..e3d4f8e 100644 --- a/config/autoload/dependencies.php +++ b/config/autoload/dependencies.php @@ -80,5 +80,6 @@ return [ \App\Service\v3\Interfaces\SmsServiceInterface::class => \App\Service\v3\Implementations\SmsAliService::class, \App\Service\v3\Interfaces\AttachmentServiceInterface::class => \App\Service\v3\Implementations\AttachmentService::class, \App\JsonRpc\PrintServiceInterface::class => \App\JsonRpc\FeieService::class, + \App\JsonRpc\LocationServiceInterface::class => \App\JsonRpc\LocationService::class, \App\Service\v3\Interfaces\OrderStatisticsServiceInterface::class => \App\Service\v3\Implementations\OrderStatisticsService::class, ];