SyncRefundLogic.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace addons\WechatVideoShop\common\logic;
  3. use addons\WechatVideoShop\common\enums\WechatVideoShopEnums;
  4. use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\refund\RefundListModel;
  5. use addons\WechatVideoShop\common\expand\sdk\weixin\src\request\refund\RefundListRequest;
  6. use addons\WechatVideoShop\common\helper\WechatRequestHelper;
  7. use addons\WechatVideoShop\common\models\WechatVideoShop;
  8. use addons\WechatVideoShop\common\models\WechatVideoShopRefund;
  9. use addons\WechatVideoShop\common\service\RefundCallbackService;
  10. use common\enums\RabbitMqEnum;
  11. use common\enums\StatusEnum;
  12. use common\helpers\FormatHelper;
  13. use common\models\mall\Mall;
  14. class SyncRefundLogic
  15. {
  16. private static $lock_key = 'wechat:video:shop:sync:refund';
  17. private static $lock_sec = 300;
  18. public static function refund()
  19. {
  20. $is_sync = \Yii::$app->redis->get(self::$lock_key);
  21. if (!$is_sync) {
  22. \Yii::error(__METHOD__ . ' 执行同步 ');
  23. try {
  24. // 先加锁
  25. \Yii::$app->redis->setex(self::$lock_key, self::$lock_sec, 1);
  26. // 取出所有的商城
  27. $mids = Mall::getEnableMallIds();
  28. foreach ($mids as $mid) {
  29. $config = \Yii::$app->services->setting->addons->get($mid, WechatVideoShopEnums::ADDONS_NAME, 'basic');
  30. if (!empty($config['is_enable'])) { // 开启状态
  31. // 取出所有店铺
  32. $shop_list = WechatVideoShop::lists([
  33. 'where' => [
  34. ['mall_id' => $mid],
  35. ['status' => StatusEnum::ENABLED]
  36. ],
  37. 'order' => 'id asc'
  38. ]);
  39. if (!empty($shop_list)) {
  40. // 遍历所有店铺
  41. foreach ($shop_list as $shop) {
  42. $reqModel = new RefundListModel();
  43. $reqModel->next_key = '';
  44. $reqModel->begin_create_time = time() - 86400; // 相差一天
  45. $reqModel->end_create_time = time();
  46. while (true) {
  47. $resp = WechatRequestHelper::accessTokenAndToArray(new RefundListRequest($shop), $reqModel);
  48. if (!empty($resp['after_sale_order_id_list'])) {
  49. // 这里
  50. foreach ($resp['after_sale_order_id_list'] as $third_refund_id) {
  51. // 这里判断是否需要新建退款
  52. $refund = WechatVideoShopRefund::getOne([
  53. ['third_refund_id' => $third_refund_id]
  54. ]);
  55. if (empty($refund)) {
  56. // 执行创建
  57. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_ORDER,
  58. RabbitMqEnum::ORDER_QUEUE, array_merge($shop, ['after_sale_order_id' => $third_refund_id]),
  59. RefundCallbackService::class, 'syncCreate');
  60. }
  61. }
  62. }
  63. if (!empty($resp['has_more']) || empty($resp['after_sale_order_id_list'])) break; // 没有下一页了
  64. $reqModel->next_key = $resp['next_key'] ?? ''; // 下一页key
  65. }
  66. }
  67. }
  68. }
  69. }
  70. } catch (\Exception $e) {
  71. \Yii::error(FormatHelper::exception($e, __METHOD__));
  72. }
  73. // 解除锁
  74. \Yii::$app->redis->del(self::$lock_key);
  75. } else {
  76. \Yii::error(__METHOD__ . ' 上一次还在同步中... ');
  77. }
  78. }
  79. }