StakeholderIncomeSyncCommand.php 3.87 KB
<?php

namespace App\Console\Commands;

use App\Helper\CacheKeyTools;
use App\Helper\RedisClient;
use App\Models\Legal\Bills;
use App\Models\Legal\Company;
use App\Models\Legal\StakeholderIncomeByPayer;
use App\Services\ApiService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

/**
 * 合作伙同数据同步
 * Class StakeholderIncomeSyncCommand
 * @package App\Console\Commands
 */
class StakeholderIncomeSyncCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'stakeholder:income:sync';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '权益人收益同步';

    //常量定义
    const COUNT = 1;
    const BLOCK_TIME = 1000;

    const TYPE_BILLS = 1001601; //账单分成
    const TYPE_ROYALTY = 1001602; //合作伙伴打款 type = 1 版税分成

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $id = '0';

        while (true) {

            $redis = RedisClient::instance('bills');
            $key   = CacheKeyTools::billsSync();

            //处理消息体
            if($msg = $redis->xread([$key=>$id], self::COUNT, self::BLOCK_TIME)) {

                $id = key($msg[$key]);
                $income_item = $msg[$key][$id];

                Log::channel('api')->warning(__METHOD__."streamid:{$id}-即将处理任务", ['income_item'=>$income_item]);

                $company = Company::query()->find($income_item['company_id']);
                $bills   = Bills::query()->find($income_item['related_id']);
                $channel = 'TME';

                $http_data = [
                    'cardNo'=>$income_item['identifier'],
                    'faxMoney'=>$income_item['fax_money'],
                    'money'=>$income_item['money'],
                    'totalMoney'=>$income_item['total_money'],
                    'paymentCompany'=>$company->receipt_name,
                    'type'=>self::TYPE_BILLS,
                    'title'=>"{$bills->bill_section_start}-{$bills->bill_section_end}账单/{$channel}",
                ];

                if ($res = StakeholderIncomeByPayer::query()->where(['serial_no'=>$income_item['serial_no']])->first()) {

                    if ($res['sync_status'] == 0) {

                        //直接未同步的数据才请求api
                        $http_res = ApiService::walletAddIncome($http_data);

                        if (empty($http_res)) {
                            //重试 401/403/403
                            Log::channel('api')->warning(__METHOD__."streamid:{$id}-api请求失败", ['income_item'=>$income_item, 'http_data'=>$http_data]);
                        } else {

                            $update = [
                                'http_log'=>json_encode($http_res, JSON_UNESCAPED_UNICODE),
                                'busi_id'=>$http_res['data']['busiId'],
                            ];

                            if ($http_res['code'] == 0) {
                                $update['sync_status'] = 1;
                            } else {
                                $update['sync_status'] = 2;
                            }

                            //记录请求api返回体并修改状态
                            if (StakeholderIncomeByPayer::query()->where(['serial_no'=>$income_item['serial_no']])->update($update)) {
                                $redis->xdel($key, [$id]);
                            }
                        }

                    } else {
                        $redis->xdel($key, [$id]);
                    }
                }
            }

            sleep(1);
        }
    }
}