WechatOrderCron.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\Lakala\Cron;
  4. use SixShop\Core\Attribute\Cron;
  5. use SixShop\Lakala\Service\PaymentRecordService;
  6. use SixShop\Payment\Model\ExtensionPaymentModel;
  7. use SixShop\Wechat\Service\ExpressService;
  8. use think\db\Query;
  9. use think\facade\Db;
  10. class WechatOrderCron
  11. {
  12. public function __construct(
  13. private PaymentRecordService $paymentRecordService,
  14. private ExpressService $expressService,
  15. )
  16. {
  17. $this->expressService->getAllDelivery();
  18. }
  19. #[Cron('0 */10 * * * *', 'lakala.shipping')]
  20. public function orderShipping(): void
  21. {
  22. ExtensionPaymentModel::alias('p')
  23. ->leftJoin('order o', 'o.id = p.order_id')
  24. ->leftJoin('lakala_wechat_payment w', 'p.id = w.payment_id')
  25. ->where([
  26. 'p.pay_type' => 'lakala',
  27. 'p.biz_type' => 1,
  28. 'p.status' => 2,
  29. 'o.shipping_status' => 1
  30. ])
  31. ->where(function (Query $query) {
  32. $query->whereIn('w.order_state', [1, 2])
  33. ->whereOr('w.order_state', null);
  34. })
  35. ->field('p.id,w.order_state')
  36. ->select()->each(function ($payment) {
  37. if ($payment->order_state === null) {
  38. $this->paymentRecordService->createWechatOrder($payment->id);
  39. }
  40. $this->paymentRecordService->updateWechatOrder($payment->id);
  41. });
  42. }
  43. #[Cron('0 */20 * * * *', 'lakala.expire')]
  44. public function orderExpire(): void
  45. {
  46. // 订单支付超时关闭订单
  47. ExtensionPaymentModel::alias('p')
  48. ->leftJoin('order o', 'o.id = p.order_id')
  49. ->where([
  50. 'p.pay_type' => 'lakala',
  51. 'p.biz_type' => 1,
  52. ['p.status', 'in', [0, 4]], // 待付款、已关闭
  53. ['p.expire_time', '<', time()],
  54. 'o.order_status' => 10,
  55. 'o.delete_time' => null,
  56. ])
  57. ->field('p.id,p.order_id,o.delete_time')
  58. ->select()->each(function ($payment) {
  59. $this->paymentRecordService->closeOrder($payment->id);
  60. });
  61. }
  62. }