123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- namespace App\Http\Logic\Order;
- use App\Http\Bean\Cron\logic\HandleOrderParamBean;
- use App\Http\Enum\OrderStatusEnum;
- use App\Http\Enum\PlatformTypeEnum;
- use App\Http\Enum\PlatformTypeSonEnum;
- use App\Http\Logic\BaseLogic;
- use App\Http\Utils\BaseUtil;
- use App\Models\PlatformSidModel;
- use App\Models\UserModel;
- use App\Models\UserOrderCommissionModel;
- use App\Models\UserOrderModel;
- class OrderLogic extends BaseLogic
- {
- /**
- * 获取用户订单列表逻辑
- */
- public static function getOrderListLogic()
- {
- $params = request()->all();
- $orderQuery = UserOrderModel::query()->where("user_id",BaseUtil::getUserId());
- if(isset($params["order_number"]) && $params["order_number"]){
- $orderQuery->where("order_number","like","%{$params['order_number']}%");
- }
- $res = $orderQuery
- ->select(
- [
- "order_number",
- "product_name",
- "produt_unit_price",
- "product_num",
- "order_price",
- "order_realy_price",
- "order_status",
- "order_pay_at",
- "order_commission",
- "order_refund_at",
- 'order_refund_commission',
- "order_refund_price",
- "order_platform_type",
- "created_at",
- "id"
- ]
- )->orderBy("id","desc")
- ->paginate($params["page_size"]);
- return [
- "total"=>$res->total(),
- "list"=>$res->items()
- ];
- }
- /**
- * 订单类型逻辑
- */
- public static function getOrderTypeListLogic()
- {
- $types = PlatformTypeSonEnum::PLATFORM_SON_MAP[PlatformTypeEnum::PLATFORM_MEITUAN];
- $datas = [];
- foreach ($types as $key=>$type){
- $datas[] = [
- "order_platform_son_type"=>$key,
- "type_name"=>$type
- ];
- }
- return $datas;
- }
- /**
- * 订单详情逻辑
- */
- public static function getOrderDetailLogic()
- {
- $params = request()->all();
- $order = UserOrderModel::query()
- ->select(
- [
- "order_number",
- "order_price",
- "order_status",
- "order_pay_at",
- "order_commission",
- "order_platform_son_type"
- ]
- )->find($params["order_id"]);
- return $order;
- }
- /**
- * 处理订单逻辑
- * @param HandleOrderParamBean $handleOrderParamBean
- */
- public static function handleOrderLogic(HandleOrderParamBean $handleOrderParamBean)
- {
- //根据推广位找到用户
- $userId = PlatformSidModel::findUserBySidAndPlatformType($handleOrderParamBean->getPlatformSpreadId(),$handleOrderParamBean->getSpreadType());
- if($userId){
- //存在用户
- $user = UserModel::findById($userId);
- //处理佣金
- self::handleUserOrderCommissionLogic($user,$handleOrderParamBean);
- //是否存在推荐人
- if($user->invite_user_id){
- $inviteUser = UserModel::findById($user->invite_user_id);
- //处理推荐人佣金
- self::handleUserOrderCommissionLogic($inviteUser,$handleOrderParamBean,1,$userId);
- }
- //处理订单
- self::handleUserOrderLogic($user,$handleOrderParamBean);
- }
- }
- /**
- * 处理用户佣金
- * @param $user 用户
- * @param HandleOrderParamBean $handleOrderParamBean 数据
- * @param int $isInvite 是否是推荐人(0否1是)
- * @param int $inviteUserId 推荐人用户ID
- */
- public static function handleUserOrderCommissionLogic($user, HandleOrderParamBean $handleOrderParamBean, $isInvite = 0,$inviteUserId=0)
- {
- //判断用户佣金是否存在
- $orderCommission = UserOrderCommissionModel::query()
- ->where("user_id",$user->id)
- ->where("order_number",$handleOrderParamBean->getOrderNumber())
- ->first();
- $orderCommissionStatus = 0;
- switch ($handleOrderParamBean->getOrderStatus()){
- case OrderStatusEnum::PRE_PAY:
- $orderCommissionStatus = 0;
- break;
- case OrderStatusEnum::ALREADY_PAY:
- $orderCommissionStatus = 0;
- break;
- case OrderStatusEnum::ALREADY_RECEIVE:
- $orderCommissionStatus = 0;
- break;
- case OrderStatusEnum::ALREADY_FINISH:
- $orderCommissionStatus = 0;
- break;
- case OrderStatusEnum::ALREADY_REFUND:
- $orderCommissionStatus = 2;
- break;
- case OrderStatusEnum::ALREADY_SITE_REFUND:
- $orderCommissionStatus = 2;
- break;
- case OrderStatusEnum::ALREADY_CANCEL:
- $orderCommissionStatus = 2;
- break;
- }
- $orderCommissionData = [
- "user_id"=>$user->id,
- "user_provider_id"=>$inviteUserId,
- "order_commission"=>$handleOrderParamBean->getOrderCommission()*$user->user_commission_rate/100,
- "order_number"=>$handleOrderParamBean->getOrderNumber(),
- "commission_status"=>$orderCommissionStatus,
- "commission_type"=>"",
- "user_nickname"=>$user->user_nickname,
- "user_head_img_url"=>$user->user_head_img_url,
- "order_title"=>$handleOrderParamBean->getOrderTitle(),
- "created_at"=>$handleOrderParamBean->getCreatedAt(),
- "updated_at"=>date("Y-m-d H:i:s")
- ];
- if($inviteUserId){
- //存在推荐人
- $inviteUser = UserModel::findById($inviteUserId);
- $orderCommissionData["user_provider_nickname"] = $inviteUser->user_nickname;
- $orderCommissionData["user_provider_head_img_url"] = $inviteUser->user_head_img_url;
- $orderCommissionData["order_commission"] = $handleOrderParamBean->getOrderCommission()*0.1;
- }
- if ($orderCommission){
- //存在则更新
- if($orderCommission->commission_status!=1){
- UserOrderCommissionModel::query()
- ->where("user_id",$user->id)
- ->where("order_number",$handleOrderParamBean->getOrderNumber())
- ->update($orderCommissionData);
- }
- }else{
- //不存在则写入
- UserOrderCommissionModel::query()->insert($orderCommissionData);
- }
- }
- /**
- * 处理用户订单
- * @param $user UserModel 用户
- * @param HandleOrderParamBean $handleOrderParamBean 订单明细
- */
- public static function handleUserOrderLogic($user, HandleOrderParamBean $handleOrderParamBean)
- {
- $orderData = [
- "user_id"=>$user->id,
- "order_title"=>$handleOrderParamBean->getOrderTitle(),
- "product_name"=>$handleOrderParamBean->getOrderTitle(),
- "produt_unit_price"=>$handleOrderParamBean->getOrderPrice(),
- "product_num"=>1,
- "order_number"=>$handleOrderParamBean->getOrderNumber(),
- "order_price"=>$handleOrderParamBean->getOrderPrice(),
- "order_realy_price"=>$handleOrderParamBean->getOrderPrice(),
- "order_status"=>$handleOrderParamBean->getOrderStatus(),
- "order_pay_at"=>$handleOrderParamBean->getPayTime(),
- "order_user_sid"=>$handleOrderParamBean->getUserSpreadId(),
- "order_platform_sid"=>$handleOrderParamBean->getPlatformSpreadId(),
- "order_commission_rate"=>$user->user_commission_rate,
- "order_commission"=>$handleOrderParamBean->getOrderCommission()*$user->user_commission_rate/100,
- "order_total_commission"=>$handleOrderParamBean->getOrderCommission(),
- "order_platform_type"=>$handleOrderParamBean->getSpreadType(),
- "created_at"=>$handleOrderParamBean->getCreatedAt(),
- "updated_at"=>date("Y-m-d H:i:s")
- ];
- //判断用户订单是否存在
- $order = UserOrderModel::query()
- ->where("order_number",$handleOrderParamBean->getOrderNumber())
- ->where("user_id",$user->id)
- ->first();
- if($order){
- if ($order["order_status"]!=OrderStatusEnum::ALREADY_FINISH){
- //存在订单,则更新
- UserOrderModel::query()
- ->where("order_number",$handleOrderParamBean->getOrderNumber())
- ->update($orderData);
- }
- }else{
- //不存在订单,则写入
- UserOrderModel::query()->insert($orderData);
- }
- }
- }
|