OrderLogic.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace App\Http\Logic\Order;
  3. use App\Http\Bean\Cron\logic\HandleOrderParamBean;
  4. use App\Http\Enum\OrderStatusEnum;
  5. use App\Http\Enum\PlatformTypeEnum;
  6. use App\Http\Enum\PlatformTypeSonEnum;
  7. use App\Http\Logic\BaseLogic;
  8. use App\Http\Utils\BaseUtil;
  9. use App\Models\PlatformSidModel;
  10. use App\Models\UserModel;
  11. use App\Models\UserOrderCommissionModel;
  12. use App\Models\UserOrderModel;
  13. class OrderLogic extends BaseLogic
  14. {
  15. /**
  16. * 获取用户订单列表逻辑
  17. */
  18. public static function getOrderListLogic()
  19. {
  20. $params = request()->all();
  21. $orderQuery = UserOrderModel::query()->where("user_id",BaseUtil::getUserId());
  22. if(isset($params["order_number"]) && $params["order_number"]){
  23. $orderQuery->where("order_number","like","%{$params['order_number']}%");
  24. }
  25. $res = $orderQuery
  26. ->select(
  27. [
  28. "order_number",
  29. "product_name",
  30. "produt_unit_price",
  31. "product_num",
  32. "order_price",
  33. "order_realy_price",
  34. "order_status",
  35. "order_pay_at",
  36. "order_commission",
  37. "order_refund_at",
  38. 'order_refund_commission',
  39. "order_refund_price",
  40. "order_platform_type",
  41. "created_at",
  42. "id"
  43. ]
  44. )->orderBy("id","desc")
  45. ->paginate($params["page_size"]);
  46. return [
  47. "total"=>$res->total(),
  48. "list"=>$res->items()
  49. ];
  50. }
  51. /**
  52. * 订单类型逻辑
  53. */
  54. public static function getOrderTypeListLogic()
  55. {
  56. $types = PlatformTypeSonEnum::PLATFORM_SON_MAP[PlatformTypeEnum::PLATFORM_MEITUAN];
  57. $datas = [];
  58. foreach ($types as $key=>$type){
  59. $datas[] = [
  60. "order_platform_son_type"=>$key,
  61. "type_name"=>$type
  62. ];
  63. }
  64. return $datas;
  65. }
  66. /**
  67. * 订单详情逻辑
  68. */
  69. public static function getOrderDetailLogic()
  70. {
  71. $params = request()->all();
  72. $order = UserOrderModel::query()
  73. ->select(
  74. [
  75. "order_number",
  76. "order_price",
  77. "order_status",
  78. "order_pay_at",
  79. "order_commission",
  80. "order_platform_son_type"
  81. ]
  82. )->find($params["order_id"]);
  83. return $order;
  84. }
  85. /**
  86. * 处理订单逻辑
  87. * @param HandleOrderParamBean $handleOrderParamBean
  88. */
  89. public static function handleOrderLogic(HandleOrderParamBean $handleOrderParamBean)
  90. {
  91. //根据推广位找到用户
  92. $userId = PlatformSidModel::findUserBySidAndPlatformType($handleOrderParamBean->getPlatformSpreadId(),$handleOrderParamBean->getSpreadType());
  93. if($userId){
  94. //存在用户
  95. $user = UserModel::findById($userId);
  96. //处理佣金
  97. self::handleUserOrderCommissionLogic($user,$handleOrderParamBean);
  98. //是否存在推荐人
  99. if($user->invite_user_id){
  100. $inviteUser = UserModel::findById($user->invite_user_id);
  101. //处理推荐人佣金
  102. self::handleUserOrderCommissionLogic($inviteUser,$handleOrderParamBean,1,$userId);
  103. }
  104. //处理订单
  105. self::handleUserOrderLogic($user,$handleOrderParamBean);
  106. }
  107. }
  108. /**
  109. * 处理用户佣金
  110. * @param $user 用户
  111. * @param HandleOrderParamBean $handleOrderParamBean 数据
  112. * @param int $isInvite 是否是推荐人(0否1是)
  113. * @param int $inviteUserId 推荐人用户ID
  114. */
  115. public static function handleUserOrderCommissionLogic($user, HandleOrderParamBean $handleOrderParamBean, $isInvite = 0,$inviteUserId=0)
  116. {
  117. //判断用户佣金是否存在
  118. $orderCommission = UserOrderCommissionModel::query()
  119. ->where("user_id",$user->id)
  120. ->where("order_number",$handleOrderParamBean->getOrderNumber())
  121. ->first();
  122. $orderCommissionStatus = 0;
  123. switch ($handleOrderParamBean->getOrderStatus()){
  124. case OrderStatusEnum::PRE_PAY:
  125. $orderCommissionStatus = 0;
  126. break;
  127. case OrderStatusEnum::ALREADY_PAY:
  128. $orderCommissionStatus = 0;
  129. break;
  130. case OrderStatusEnum::ALREADY_RECEIVE:
  131. $orderCommissionStatus = 0;
  132. break;
  133. case OrderStatusEnum::ALREADY_FINISH:
  134. $orderCommissionStatus = 0;
  135. break;
  136. case OrderStatusEnum::ALREADY_REFUND:
  137. $orderCommissionStatus = 2;
  138. break;
  139. case OrderStatusEnum::ALREADY_SITE_REFUND:
  140. $orderCommissionStatus = 2;
  141. break;
  142. case OrderStatusEnum::ALREADY_CANCEL:
  143. $orderCommissionStatus = 2;
  144. break;
  145. }
  146. $orderCommissionData = [
  147. "user_id"=>$user->id,
  148. "user_provider_id"=>$inviteUserId,
  149. "order_commission"=>$handleOrderParamBean->getOrderCommission()*$user->user_commission_rate/100,
  150. "order_number"=>$handleOrderParamBean->getOrderNumber(),
  151. "commission_status"=>$orderCommissionStatus,
  152. "commission_type"=>"",
  153. "user_nickname"=>$user->user_nickname,
  154. "user_head_img_url"=>$user->user_head_img_url,
  155. "order_title"=>$handleOrderParamBean->getOrderTitle(),
  156. "created_at"=>$handleOrderParamBean->getCreatedAt(),
  157. "updated_at"=>date("Y-m-d H:i:s")
  158. ];
  159. if($inviteUserId){
  160. //存在推荐人
  161. $inviteUser = UserModel::findById($inviteUserId);
  162. $orderCommissionData["user_provider_nickname"] = $inviteUser->user_nickname;
  163. $orderCommissionData["user_provider_head_img_url"] = $inviteUser->user_head_img_url;
  164. $orderCommissionData["order_commission"] = $handleOrderParamBean->getOrderCommission()*0.1;
  165. }
  166. if ($orderCommission){
  167. //存在则更新
  168. if($orderCommission->commission_status!=1){
  169. UserOrderCommissionModel::query()
  170. ->where("user_id",$user->id)
  171. ->where("order_number",$handleOrderParamBean->getOrderNumber())
  172. ->update($orderCommissionData);
  173. }
  174. }else{
  175. //不存在则写入
  176. UserOrderCommissionModel::query()->insert($orderCommissionData);
  177. }
  178. }
  179. /**
  180. * 处理用户订单
  181. * @param $user UserModel 用户
  182. * @param HandleOrderParamBean $handleOrderParamBean 订单明细
  183. */
  184. public static function handleUserOrderLogic($user, HandleOrderParamBean $handleOrderParamBean)
  185. {
  186. $orderData = [
  187. "user_id"=>$user->id,
  188. "order_title"=>$handleOrderParamBean->getOrderTitle(),
  189. "product_name"=>$handleOrderParamBean->getOrderTitle(),
  190. "produt_unit_price"=>$handleOrderParamBean->getOrderPrice(),
  191. "product_num"=>1,
  192. "order_number"=>$handleOrderParamBean->getOrderNumber(),
  193. "order_price"=>$handleOrderParamBean->getOrderPrice(),
  194. "order_realy_price"=>$handleOrderParamBean->getOrderPrice(),
  195. "order_status"=>$handleOrderParamBean->getOrderStatus(),
  196. "order_pay_at"=>$handleOrderParamBean->getPayTime(),
  197. "order_user_sid"=>$handleOrderParamBean->getUserSpreadId(),
  198. "order_platform_sid"=>$handleOrderParamBean->getPlatformSpreadId(),
  199. "order_commission_rate"=>$user->user_commission_rate,
  200. "order_commission"=>$handleOrderParamBean->getOrderCommission()*$user->user_commission_rate/100,
  201. "order_total_commission"=>$handleOrderParamBean->getOrderCommission(),
  202. "order_platform_type"=>$handleOrderParamBean->getSpreadType(),
  203. "created_at"=>$handleOrderParamBean->getCreatedAt(),
  204. "updated_at"=>date("Y-m-d H:i:s")
  205. ];
  206. //判断用户订单是否存在
  207. $order = UserOrderModel::query()
  208. ->where("order_number",$handleOrderParamBean->getOrderNumber())
  209. ->where("user_id",$user->id)
  210. ->first();
  211. if($order){
  212. if ($order["order_status"]!=OrderStatusEnum::ALREADY_FINISH){
  213. //存在订单,则更新
  214. UserOrderModel::query()
  215. ->where("order_number",$handleOrderParamBean->getOrderNumber())
  216. ->update($orderData);
  217. }
  218. }else{
  219. //不存在订单,则写入
  220. UserOrderModel::query()->insert($orderData);
  221. }
  222. }
  223. }