| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace console\controllers;
- use backend\models\Deposit;
- 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("crm_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', 1])->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).");
- }
- Deposit::updateAll(['mt4_status' => 1], ['order_sn' => $syncDesposit['memo'], 'type' => 1]);
- usleep(50000); // sleep 50ms
- }
- if (count($syncDesposits) != $limit) {
- break;
- }
- }
- $this->outLog("Job end, " . date('Y-m-d H:i:s'));
- $this->redis->del("crm_service:SyncDespositJob");
- }
- }
|