| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- namespace console\controllers;
- use backend\models\SyncDesposit;
- use common\helpers\MtKit;
- use Yii;
- class SyncDespositJobController extends BaseJobController
- {
- public function actionRun()
- {
- if (isset(Yii::$app->params['isAutoSyncDesposit']) && Yii::$app->params['isAutoSyncDesposit'] == 1) {
- $isAutoSyncDeposit = true;
- } else {
- $isAutoSyncDeposit = false;
- }
- if ($isAutoSyncDeposit == false) {
- return;
- }
- $beginTime = time();
- $this->outLog("Job start " . date('Y-m-d H:i:s'));
- if ($this->redis->set("aike_service:SyncDespositJob", "1", "EX", "60", "NX") == false) {
- $this->outLog("Job start failed, lock exist, exit.");
- return;
- }
- $skipLogins = [];
- $maxPk = null;
- $limit = 100;
- while (true) {
- if (time() - $beginTime > 55) {
- // 退出脚本
- break;
- }
- $query = SyncDesposit::find()->andWhere(['is_sync' => 0])->orderBy('id asc')->limit($limit)->asArray();
- if ($maxPk != null) {
- $query->andWhere(['>', 'id', $maxPk]);
- }
- $syncDesposits = $query->all();
- if (empty($syncDesposits)) {
- break;
- }
- foreach ($syncDesposits as $syncDesposit) {
- $maxPk = $syncDesposit['id'];
- if (time() - $beginTime > 55) {
- // 退出脚本
- break;
- }
- // 每个mt4账户1分钟只入一次金 防止请求频繁
- if (isset($skipLogins[$syncDesposit['login']])) {
- continue;
- }
- $skipLogins[$syncDesposit['login']] = 1;
- $affectRows = SyncDesposit::updateAll(['is_sync' => 2], ['id' => $syncDesposit['id'], 'is_sync' => 0]);
- if ($affectRows == 0) {
- $this->outLog("Desposit(id: {$syncDesposit['id']}) is_sync update fail({$syncDesposit['is_sync']} => 2), skipped.");
- continue;
- }
- //根据同步入金表的订单查询入金表的数据
- $result = Deposit::find()->where(['order_sn' =>$syncDesposit['memo']])->one();
- //调用入金的接口,这里传递进去id
- $isSuccess = MtKit::deposit($syncDesposit['login'], $syncDesposit['amount'], $syncDesposit['comment'],$result['id']); //尾部增加了一个字段
- if ($isSuccess == false) {
- $this->outLog("Desposit(id: {$syncDesposit['id']}) fail.");
- continue;
- }
- $this->outLog("Desposit(id: {$syncDesposit['id']}) success.");
- $affectRows = SyncDesposit::updateAll(['is_sync' => 1], ['id' => $syncDesposit['id']]);
- if ($affectRows == 0) {
- $this->outLog("Desposit(id: {$syncDesposit['id']}) is_sync update fail({$syncDesposit['is_sync']} => 1).");
- }
- usleep(50000); // sleep 50ms
- }
- if (count($syncDesposits) != $limit) {
- break;
- }
- }
- $this->outLog("Job end, " . date('Y-m-d H:i:s'));
- $this->redis->del("aike_service:SyncDespositJob");
- }
- }
|