| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- namespace addons\WechatVideoShop\common\logic;
- use addons\WechatVideoShop\common\enums\WechatVideoShopEnums;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\refund\RefundListModel;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\request\refund\RefundListRequest;
- use addons\WechatVideoShop\common\helper\WechatRequestHelper;
- use addons\WechatVideoShop\common\models\WechatVideoShop;
- use addons\WechatVideoShop\common\models\WechatVideoShopRefund;
- use addons\WechatVideoShop\common\service\RefundCallbackService;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- use common\models\mall\Mall;
- class SyncRefundLogic
- {
- private static $lock_key = 'wechat:video:shop:sync:refund';
- private static $lock_sec = 300;
- public static function refund()
- {
- $is_sync = \Yii::$app->redis->get(self::$lock_key);
- if (!$is_sync) {
- \Yii::error(__METHOD__ . ' 执行同步 ');
- try {
- // 先加锁
- \Yii::$app->redis->setex(self::$lock_key, self::$lock_sec, 1);
- // 取出所有的商城
- $mids = Mall::getEnableMallIds();
- foreach ($mids as $mid) {
- $config = \Yii::$app->services->setting->addons->get($mid, WechatVideoShopEnums::ADDONS_NAME, 'basic');
- if (!empty($config['is_enable'])) { // 开启状态
- // 取出所有店铺
- $shop_list = WechatVideoShop::lists([
- 'where' => [
- ['mall_id' => $mid],
- ['status' => StatusEnum::ENABLED]
- ],
- 'order' => 'id asc'
- ]);
- if (!empty($shop_list)) {
- // 遍历所有店铺
- foreach ($shop_list as $shop) {
- $reqModel = new RefundListModel();
- $reqModel->next_key = '';
- $reqModel->begin_create_time = time() - 86400; // 相差一天
- $reqModel->end_create_time = time();
- while (true) {
- $resp = WechatRequestHelper::accessTokenAndToArray(new RefundListRequest($shop), $reqModel);
- if (!empty($resp['after_sale_order_id_list'])) {
- // 这里
- foreach ($resp['after_sale_order_id_list'] as $third_refund_id) {
- // 这里判断是否需要新建退款
- $refund = WechatVideoShopRefund::getOne([
- ['third_refund_id' => $third_refund_id]
- ]);
- if (empty($refund)) {
- // 执行创建
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_ORDER,
- RabbitMqEnum::ORDER_QUEUE, array_merge($shop, ['after_sale_order_id' => $third_refund_id]),
- RefundCallbackService::class, 'syncCreate');
- }
- }
- }
- if (!empty($resp['has_more']) || empty($resp['after_sale_order_id_list'])) break; // 没有下一页了
- $reqModel->next_key = $resp['next_key'] ?? ''; // 下一页key
- }
- }
- }
- }
- }
- } catch (\Exception $e) {
- \Yii::error(FormatHelper::exception($e, __METHOD__));
- }
- // 解除锁
- \Yii::$app->redis->del(self::$lock_key);
- } else {
- \Yii::error(__METHOD__ . ' 上一次还在同步中... ');
- }
- }
- }
|