CronLogic.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php
  2. namespace App\Http\Logic\Cron;
  3. use App\Http\Bean\Cron\logic\HandleOrderParamBean;
  4. use App\Http\Bean\Util\Jutuike\GetOrderListParamBean;
  5. use App\Http\Bean\Util\Meituan\OrderListParamBean;
  6. use App\Http\Bean\Util\Pdd\Ddk\OrderListIncrementGetParamBean;
  7. use App\Http\Enum\OrderStatusEnum;
  8. use App\Http\Enum\PlatformTypeEnum;
  9. use App\Http\Logic\BaseLogic;
  10. use App\Http\Logic\Order\OrderLogic;
  11. use App\Http\Utils\BaseUtil;
  12. use App\Http\Utils\Jutuike\JutuikeUtil;
  13. use App\Http\Utils\LoggerFactoryUtil;
  14. use App\Http\Utils\Meituan\MeituanLianmengUtil;
  15. use App\Http\Utils\Pdd\DuoDuoKeUtil;
  16. use App\Http\Utils\WechatAccountUtil;
  17. use App\Models\CategoryModel;
  18. use App\Models\UserModel;
  19. use App\Models\UserOrderCommissionModel;
  20. use App\Models\UserOrderModel;
  21. class CronLogic extends BaseLogic
  22. {
  23. /**
  24. * 获取美团订单
  25. */
  26. public static function meituanOrderslogic()
  27. {
  28. $params = request()->all();
  29. $arr = [
  30. "appkey"=>env("MEITUAN_LIANMENG_KEY"),
  31. "type"=>$params["order_type"],
  32. "startTime"=>time()-$params["minute"]*60,
  33. "endTime"=>time(),
  34. // "startTime"=>strtotime("2021-09-22 11:25:00"),
  35. // "endTime"=>time(),
  36. "page"=>1,
  37. "limit"=>30,
  38. "queryTimeType"=>2,
  39. ];
  40. $instance = new LoggerFactoryUtil(CronLogic::class);
  41. $instance->info("获取订单数据开始");
  42. $bean = new OrderListParamBean($arr);
  43. $res = MeituanLianmengUtil::orderList($bean);
  44. // dd($res);
  45. $instance->info("美团返回数据:".json_encode($res));
  46. $instance->info("订单数量:".$res["total"]);
  47. if($res){
  48. //存在数据,写入订单数据表
  49. if($res["total"]){
  50. $instance->info("订单数据:".json_encode($res));
  51. BaseUtil::sendBaoJing("","获取到订单数量:".$res["total"],"定时获取美团订单");
  52. //处理订单
  53. self::checkOrder($res["dataList"],$params["order_type"]);
  54. //判断订单数量是否大于分页
  55. $pageNum = ceil($res["total"]/100)-1;
  56. if($pageNum){
  57. for ($i=1;$i<=$pageNum;$i++){
  58. $arr["page"]++;
  59. $bean = new OrderListParamBean($arr);
  60. $res = MeituanLianmengUtil::orderList($bean);
  61. if ($res) {
  62. //存在数据,写入订单数据表
  63. if ($res["total"]) {
  64. self::checkOrder($res["dataList"],$params["order_type"]);
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }
  72. /**
  73. * 处理订单
  74. */
  75. protected static function checkOrder($orders,$orderType)
  76. {
  77. foreach ($orders as $data) {
  78. switch ($data["status"]) {
  79. case 1://已付款
  80. $orderStatus = OrderStatusEnum::ALREADY_PAY;
  81. break;
  82. case 8://已收货
  83. $orderStatus = OrderStatusEnum::ALREADY_RECEIVE;
  84. break;
  85. case 9://已退款或风控
  86. $orderStatus = OrderStatusEnum::ALREADY_REFUND;
  87. break;
  88. }
  89. $params = [
  90. "spreadSonType" => 0,//订单子类型
  91. "orderNumber" => $data["orderid"],//订单编号
  92. "payTime" => date("Y-m-d H:i:s", $data["paytime"]),//支付时间
  93. "orderPrice" => $data["payprice"] * 100,//订单金额
  94. "orderCommission" => $data["profit"] * 100,//订单佣金
  95. "orderTitle" => $data["smstitle"],//订单标题
  96. "orderRefundPrice" => isset($data["refundprice"]) ? $data["refundprice"] * 100 : 0,//退款金额
  97. "orderRefundTime" => isset($data["refundtime"]) ? date("Y-m-d H:i:s", $data["refundtime"]) : null,//退款时间
  98. "orderRefundCommission" => isset($data["refundprofit"]) ? $data["refundprofit"] : 0,//退款佣金
  99. "orderStatus" => $orderStatus,//订单状态
  100. "orderCouponPrice" => 0,//订单优惠
  101. "productImgUrl" => "https://pic.rmb.bdstatic.com/bjh/f049242a789a22fb1a412bb6418c52e2.jpeg",//商品图片
  102. "spreadType"=>PlatformTypeEnum::PLATFORM_MEITUAN,//订单类型
  103. "userSpreadId"=>$data["sid"],//用户推广位
  104. "platformSpreadId"=>$data["sid"],//平台推广位
  105. "createdAt"=>date("Y-m-d H:i:s", $data["paytime"])
  106. ];
  107. $bean = new HandleOrderParamBean($params);
  108. OrderLogic::handleOrderLogic($bean);
  109. }
  110. }
  111. /**
  112. * 聚推客订单逻辑
  113. */
  114. public static function jutuikeOrdersLogic()
  115. {
  116. $params = request()->all();
  117. $startTime = date("Y-m-d H:i:s",time()-600);
  118. $endTime = date("Y-m-d H:i:s");
  119. if(isset($params["start_time"])){
  120. $startTime = $params["start_time"];
  121. }
  122. if (isset($params["end_time"])){
  123. $endTime= $params["end_time"];
  124. }
  125. $page = 1;
  126. $params = [
  127. "start_time"=>$startTime,
  128. "end_time"=>$endTime,
  129. "query_type"=>2,
  130. "page"=>$page,
  131. "pageSize"=>100
  132. ];
  133. $bean = new GetOrderListParamBean($params);
  134. $data = JutuikeUtil::getOrderList($bean);
  135. $instance = new LoggerFactoryUtil(CronLogic::class);
  136. $instance->info("上游返回数据:".json_encode($data));
  137. if($data["code"]!=1){
  138. //上游错误
  139. return;
  140. }
  141. //判断是否存在订单
  142. $orderLists = $data["data"]["data"];
  143. if(count($orderLists)){
  144. BaseUtil::sendBaoJing("","获取到订单数量:".count($orderLists),"定时获取聚推客订单");
  145. }
  146. foreach ($orderLists as $orderList){
  147. self::handleOrderLogic($orderList);
  148. }
  149. }
  150. public static function handleOrderLogic($orderList)
  151. {
  152. //判断订单状态
  153. switch ($orderList["status"]){
  154. case 0://未付款
  155. $orderStatus = OrderStatusEnum::PRE_PAY;
  156. break;
  157. case 1://已付款
  158. $orderStatus = OrderStatusEnum::ALREADY_PAY;
  159. break;
  160. case 2://待结算
  161. $orderStatus = OrderStatusEnum::ALREADY_RECEIVE;
  162. break;
  163. case 3://已结算
  164. $orderStatus = OrderStatusEnum::ALREADY_FINISH;
  165. break;
  166. case 4://无效订单
  167. $orderStatus = OrderStatusEnum::ALREADY_CANCEL;
  168. break;
  169. }
  170. //获取活动图片
  171. $actImgUrl = CategoryModel::query()
  172. ->where("platform_type",PlatformTypeEnum::PLATFORM_JUTUIKE)
  173. ->where("activity_id",$orderList["act_id"])
  174. ->value("category_img_url");
  175. $params = [
  176. "spreadSonType" => 0,//订单子类型
  177. "orderNumber" => $orderList["order_sn"],//订单编号
  178. "payTime" => $orderList["pay_time"],//支付时间
  179. "orderPrice" => $orderList["order_price"] * 100,//订单金额
  180. "orderCommission" => $orderList["jtk_share_fee"] * 100,//订单佣金
  181. "orderTitle" => $orderList["order_title"],//订单标题
  182. "orderRefundPrice" => 0,//退款金额
  183. "orderRefundTime" => null,//退款时间
  184. "orderRefundCommission" => 0,//退款佣金
  185. "orderStatus" => $orderStatus,//订单状态
  186. "orderCouponPrice" => 0,//订单优惠
  187. "productImgUrl" => $actImgUrl,//商品图片
  188. "spreadType"=>PlatformTypeEnum::PLATFORM_JUTUIKE,//订单类型
  189. "userSpreadId"=>$orderList["sid"],//用户推广位
  190. "platformSpreadId"=>$orderList["sid"],//平台推广位
  191. "createdAt"=>$orderList["create_time"],//订单创建时间
  192. ];
  193. $bean = new HandleOrderParamBean($params);
  194. OrderLogic::handleOrderLogic($bean);
  195. }
  196. /**
  197. * 获取拼多多订单逻辑
  198. */
  199. public static function pddOrdersLogic()
  200. {
  201. $params = [
  202. "startUpdateTime"=>time()-600,
  203. "endUpdateTime"=>time()
  204. ];
  205. $bean = new OrderListIncrementGetParamBean($params);
  206. $res = DuoDuoKeUtil::orderListIncrementGet($bean);
  207. $instance = new LoggerFactoryUtil(CronLogic::class);
  208. $instance->info("上游返回信息:".json_encode($res));
  209. if (isset($res["error_response"])){
  210. return;
  211. }
  212. if($res["order_list_get_response"]["total_count"]){
  213. BaseUtil::sendBaoJing("","获取到订单数量:".$res["order_list_get_response"]["total_count"],"定时获取拼多多订单");
  214. }
  215. //获取数据
  216. $datas = $res["order_list_get_response"]["order_list"];
  217. foreach ($datas as $orderList){
  218. switch ($orderList["order_status"]){
  219. case 0:
  220. //已支付
  221. $orderStatus = OrderStatusEnum::ALREADY_PAY;
  222. break;
  223. case 1:
  224. //已成团
  225. $orderStatus = OrderStatusEnum::ALREADY_PAY;
  226. break;
  227. case 2:
  228. //确认收货
  229. $orderStatus = OrderStatusEnum::ALREADY_RECEIVE;
  230. break;
  231. case 3:
  232. //审核成功
  233. $orderStatus = OrderStatusEnum::ALREADY_RECEIVE;
  234. break;
  235. case 4:
  236. //审核失败(不可提现)
  237. $orderStatus = OrderStatusEnum::ALREADY_CANCEL;
  238. break;
  239. case 5:
  240. //已经结算
  241. $orderStatus = OrderStatusEnum::ALREADY_RECEIVE;
  242. break;
  243. case 10:
  244. //已处罚
  245. $orderStatus = OrderStatusEnum::ALREADY_CANCEL;
  246. break;
  247. }
  248. $params = [
  249. "spreadSonType" => 0,//订单子类型
  250. "orderNumber" => $orderList["order_sn"],//订单编号
  251. "payTime" => date("Y-m-d H:i:s",$orderList["order_pay_time"]),//支付时间
  252. "orderPrice" => $orderList["order_amount"],//订单金额
  253. "orderCommission" => $orderList["promotion_amount"],//订单佣金
  254. "orderTitle" => $orderList["goods_name"],//订单标题
  255. "orderRefundPrice" => 0,//退款金额
  256. "orderRefundTime" => null,//退款时间
  257. "orderRefundCommission" => 0,//退款佣金
  258. "orderStatus" => $orderStatus,//订单状态
  259. "orderCouponPrice" => 0,//订单优惠
  260. "productImgUrl" => "https://pic.rmb.bdstatic.com/bjh/6d9fb4a81eb5fc933da8949854c4fab2.jpeg",//商品图片
  261. "spreadType"=>PlatformTypeEnum::PLATFORM_PINGDUODUO,//订单类型
  262. "userSpreadId"=>$orderList["p_id"],//用户推广位
  263. "platformSpreadId"=>$orderList["p_id"],//平台推广位
  264. "createdAt"=>date("Y-m-d H:i:s",$orderList["order_create_time"]),//订单创建时间
  265. ];
  266. $bean = new HandleOrderParamBean($params);
  267. OrderLogic::handleOrderLogic($bean);
  268. }
  269. }
  270. /**
  271. * 发送用户下单模版
  272. */
  273. public static function sendOrderTemplateLogic()
  274. {
  275. $instance = new LoggerFactoryUtil(CronLogic::class);
  276. //1.新增粉丝通知
  277. $userFans = UserModel::query()
  278. ->where("invite_user_id","<>",0)
  279. ->where("user_notify_status",0)
  280. ->get();
  281. foreach ($userFans as $userFan){
  282. //获取推荐人
  283. $inviteUser = UserModel::findById($userFan->invite_user_id);
  284. if($inviteUser){
  285. try{
  286. //获取公众号实例
  287. $app = WechatAccountUtil::getAppByID($userFan->wechat_account_id);
  288. $data = [
  289. 'touser' => $inviteUser->user_open_id,
  290. 'template_id' => env("WECHAT_FANS_TEMPLATE_URL"),
  291. 'url' => env("WECHAT_FANS_URL"),
  292. 'data' => [
  293. 'first' => "亲,您有新的粉丝",
  294. 'keyword1' => $userFan->user_nickname,
  295. 'keyword2' => $userFan->id+100000,
  296. "remark"=>"粉丝将提供分享佣金,粉丝越多比例越高哦,点击查看详情"
  297. ],
  298. ];
  299. $res = $app->template_message->send($data);
  300. $instance->info("粉丝通知微信返回结果:".json_encode($res));
  301. }catch (\Throwable $exception){
  302. $instance->info("粉丝通知结果异常:".$exception->getMessage());
  303. }
  304. UserModel::query()->where("id",$userFan->id)->update(
  305. [
  306. "user_notify_status"=>1
  307. ]
  308. );
  309. }
  310. }
  311. //2.获取自购已支付且未通知进行模版推送
  312. $orders = UserOrderCommissionModel::query()
  313. ->where("commission_status",0)
  314. ->where("user_id","<>",0)
  315. ->where("order_notify_status",0)
  316. ->limit(10)
  317. ->get();
  318. foreach ($orders as $order){
  319. //判断是否自购订单
  320. if($order->user_provider_id){
  321. //反佣订单
  322. $user = UserModel::findById($order->user_id);
  323. if($user){
  324. try{
  325. //获取公众号实例
  326. $app = WechatAccountUtil::getAppByID($user->wechat_account_id);
  327. $tmpOrder = UserOrderModel::query()->where("order_number",$order->order_number)->first();
  328. $privideUser = UserModel::findById($order->user_provider_id);
  329. $data = [
  330. 'touser' => $user->user_open_id,
  331. 'template_id' => env("WECHAT_FANS_ORDER_TEMPLATE_URL"),
  332. 'url' => env("WECHAT_FANS_ORDER_URL"),
  333. 'data' => [
  334. 'first' => "粉丝下单成功通知",
  335. 'keyword1' => $order->order_number,
  336. 'keyword2' => $tmpOrder->product_name,
  337. 'keyword3' => $order->created_at,
  338. 'keyword4' => round($tmpOrder->order_price/100,2)."元",
  339. 'keyword5' => $privideUser->user_nickname,
  340. "remark"=>"您的粉丝有新的订单,分享佣金已产生,3天后结算到您账户中,点击详情跳转粉丝订单"
  341. ],
  342. ];
  343. $res = $app->template_message->send($data);
  344. $instance->info("粉丝下单微信返回结果:".json_encode($res));
  345. }catch (\Throwable $exception){
  346. $instance->info("粉丝下单通知结果异常:".$exception->getMessage());
  347. }
  348. }
  349. }else{
  350. $user = UserModel::findById($order->user_id);
  351. if($user){
  352. try{
  353. //获取公众号实例
  354. $app = WechatAccountUtil::getAppByID($user->wechat_account_id);
  355. $tmpOrder = UserOrderModel::query()->where("order_number",$order->order_number)->first();
  356. $data = [
  357. 'touser' => $user->user_open_id,
  358. 'template_id' => env("WECHAT_ORDER_TEMPLATE_URL"),
  359. 'url' => env("WECHAT_ORDER_URL"),
  360. 'data' => [
  361. 'first' => "下单成功通知",
  362. 'keyword1' => $order->created_at,
  363. 'keyword2' => $tmpOrder->product_name,
  364. 'keyword3' => $order->order_number,
  365. "remark"=>"您的订单已完成,感谢您的使用,3天后佣金将会结算到您的账户中点击详情跳转我的订单"
  366. ],
  367. ];
  368. $res = $app->template_message->send($data);
  369. $instance->info("自购下单微信返回结果:".json_encode($res));
  370. }catch (\Throwable $exception){
  371. $instance->info("自购下单通知结果异常:".$exception->getMessage());
  372. }
  373. }
  374. }
  375. UserOrderCommissionModel::query()->where("id",$order->id)->update(
  376. [
  377. "order_notify_status"=>1
  378. ]
  379. );
  380. }
  381. //3.获取已结算订单,并通知
  382. $completeOrders = UserOrderCommissionModel::query()
  383. ->where("commission_status",2)
  384. ->where("user_id","<>",0)
  385. ->where("order_complete_notify_status",0)
  386. ->limit(10)
  387. ->get();
  388. foreach ($completeOrders as $order){
  389. $user = UserModel::findById($order->user_id);
  390. if($user){
  391. try{
  392. //获取公众号实例
  393. $app = WechatAccountUtil::getAppByID($user->wechat_account_id);
  394. $tmpOrder = UserOrderModel::query()->where("order_number",$order->order_number)->first();
  395. $data = [
  396. 'touser' => $user->user_open_id,
  397. 'template_id' => env("WECHAT_COMPLETE_ORDER_TEMPLATE_URL"),
  398. 'url' => env("WECHAT_BALANCE_URL"),
  399. 'data' => [
  400. 'first' => "您的佣金已结算完成",
  401. 'keyword1' => round($order->order_commission/100,2)."元",
  402. 'keyword2' => round($tmpOrder->order_price/100,2)."元",
  403. 'keyword3' => $order->updated_at,
  404. "remark"=>"三日前订单佣金已结算完成,感谢您的使用,点击详情跳转我的账户"
  405. ],
  406. ];
  407. $res = $app->template_message->send($data);
  408. $instance->info("订单结算微信返回结果:".json_encode($res));
  409. }catch (\Throwable $exception){
  410. $instance->info("订单结算通知结果异常:".$exception->getMessage());
  411. }
  412. }
  413. UserOrderCommissionModel::query()->where("id",$order->id)->update(
  414. [
  415. "order_complete_notify_status"=>1
  416. ]
  417. );
  418. }
  419. }
  420. /**
  421. * 结算佣金逻辑(每月的20号18点结算上个月的佣金)
  422. */
  423. public static function settlementOrderCommissionLogic()
  424. {
  425. }
  426. }