CommissionJobController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace console\controllers;
  3. use backend\models\Commission;
  4. use backend\models\Config;
  5. use backend\models\Member;
  6. use backend\models\Mt4Trades;
  7. use backend\models\UserMember;
  8. use Yii;
  9. use yii\db\Connection;
  10. use yii\db\Query;
  11. class CommissionJobController extends BaseJobController
  12. {
  13. const COMMISSION_JOB_RUN_LOCK_KEY = 'aike_service:CommissionJobRun';
  14. public function actionRun()
  15. {
  16. $this->outLog("Job start " . date('Y-m-d H:i:s'));
  17. if ($this->redis->get(self::COMMISSION_JOB_RUN_LOCK_KEY) != false) {
  18. $this->outLog("Job is already running, exit.");
  19. return;
  20. }
  21. if ($this->redis->set(self::COMMISSION_JOB_RUN_LOCK_KEY, "1", "EX", "600", "NX") == false) {
  22. $this->outLog("Job start failed, lock exist, exit.");
  23. return;
  24. }
  25. $now = time();
  26. $lastRunTime = $this->redis->get("aike_service:CommissionJobRunTime");
  27. if ($lastRunTime == false) {
  28. $lastRunTime = $now;
  29. }
  30. $this->redis->set("aike_service:CommissionJobRunTime", $now);
  31. $config = Config::find()->asArray()->limit(1)->one();
  32. $commissionDealTime = strtotime($config['commission_deal_time']);
  33. // 为了防止数据同步慢,故实际查找返佣的订单往前推24小时
  34. $closeTime = max($lastRunTime - 86400, $commissionDealTime);
  35. // 获取所有存在上级代理的MT4 USER
  36. $userMembers = UserMember::find()->where(['is_commission_run' => 1])->asArray()->all();
  37. $logins = [];
  38. foreach ($userMembers as $userMember) {
  39. $logins[] = $userMember['login'];
  40. }
  41. // forex
  42. $forexSymbols = preg_split('/\s*,\s*/', $config['forex_symbols'], -1, PREG_SPLIT_NO_EMPTY);
  43. // cfd
  44. $cfdSymbols = preg_split('/\s*,\s*/', $config['cfd_symbols'], -1, PREG_SPLIT_NO_EMPTY);
  45. // silver
  46. $xagSymbols = preg_split('/\s*,\s*/', $config['xag_symbols'], -1, PREG_SPLIT_NO_EMPTY);
  47. // gold
  48. $xauSymbols = preg_split('/\s*,\s*/', $config['xau_symbols'], -1, PREG_SPLIT_NO_EMPTY);
  49. // metal
  50. $metalSymbols = preg_split('/\s*,\s*/', $config['metal_symbols'], -1, PREG_SPLIT_NO_EMPTY);
  51. // stock
  52. $stockSymbols = preg_split('/\s*,\s*/', $config['stock_symbols'], -1, PREG_SPLIT_NO_EMPTY);
  53. // btc
  54. $btcSymbols = preg_split('/\s*,\s*/', trim($config['btc_symbols']), -1, PREG_SPLIT_NO_EMPTY);
  55. $this->outLog("Auto commission, CLOSE_TIME >= " . date('Y-m-d H:i:s', $closeTime));
  56. $maxPk = null;
  57. $limit = 1000;
  58. $memberCache = [];
  59. $parentMemberCache = [];
  60. $commissionCache = [];
  61. while (true) {
  62. $this->redis->expire(self::COMMISSION_JOB_RUN_LOCK_KEY, "600");
  63. // 获取未返佣的订单
  64. $query = Mt4Trades::find();
  65. if ($maxPk != null) {
  66. $query->andWhere(['>', 'TICKET', $maxPk]);
  67. $this->outLog("Auto commission, CLOSE_TIME >= " . date('Y-m-d H:i:s', $closeTime) . ", TICKET > {$maxPk}");
  68. }
  69. $allTrades = $query->andWhere('CMD=0 or CMD=1')->andWhere(['LOGIN' => $logins])
  70. ->andWhere(['>=', 'CLOSE_TIME', date('Y-m-d H:i:s', $closeTime)])
  71. ->orderBy('TICKET asc')
  72. ->limit($limit)
  73. ->asArray()->all();
  74. if (empty($allTrades)) {
  75. break;
  76. }
  77. foreach ($allTrades as $trade) {
  78. $maxPk = $trade['TICKET'];
  79. $isExist = (new Query())->from('crm_commission_ticket')->where(['ticket' => $trade['TICKET']])->exists($this->getDb());
  80. if ($isExist) {
  81. $this->outLog("Commission ticket({$trade['TICKET']}) exist, skipped.");
  82. continue;
  83. }
  84. $volume = $trade['VOLUME'] / 100;
  85. // 获取上级代理
  86. $ibMember = null;
  87. foreach ($userMembers as $userMember) {
  88. if ($userMember['login'] == $trade['LOGIN']) {
  89. if (!array_key_exists($userMember['member_id'], $memberCache)) {
  90. $memberCache[$userMember['member_id']] = Member::find()->where(['id' => $userMember['member_id']])->asArray()->limit(1)->one();
  91. }
  92. $ibMember = $memberCache[$userMember['member_id']];
  93. if ($ibMember == null) {
  94. $this->outLog("IB member(id: {$userMember['member_id']}) not found, skipped.");
  95. }
  96. break;
  97. }
  98. }
  99. if ($ibMember == null || $ibMember['is_enable'] != 1) {
  100. $this->outLog("IB member(id: {$ibMember['id']}) is disabled, skipped.");
  101. continue;
  102. }
  103. if (!array_key_exists($ibMember['id'], $parentMemberCache)) {
  104. $parentMemberCache[$ibMember['id']] = Member::findParents($ibMember['id']);
  105. }
  106. $parentMembers = $parentMemberCache[$ibMember['id']];
  107. // 返佣记录 crm_commission_record
  108. $records = [];
  109. foreach ((array)$parentMembers as $key => $member) {
  110. $loginArr = explode(',', $member['logins']);
  111. $ibLogin = isset($loginArr[0]) ? intval($loginArr[0]) : null;
  112. if ($ibLogin == null) {
  113. $this->outLog("IB member login({$ibLogin}) is null, skipped.");
  114. continue;
  115. }
  116. // 获取返佣规则
  117. if (!array_key_exists($member['id'] . '_' . $trade['LOGIN'], $commissionCache)) {
  118. $commissionCache[$member['id'] . '_' . $trade['LOGIN']] = Commission::find()->where(['login' => $trade['LOGIN'], 'member_id' => $member['id']])->asArray()->limit(1)->one();
  119. }
  120. $commission = $commissionCache[$member['id'] . '_' . $trade['LOGIN']];
  121. if ($commission == null) {
  122. $this->outLog("Commission rule(login: {$trade['LOGIN']}, member_id: {$member['id']}) not found, skipped.");
  123. continue;
  124. }
  125. $comm = 0; // 返佣
  126. $commRule = ''; // 返佣规则
  127. $commWy = 0; // 外佣
  128. $commWyRule = ''; // 外佣规则
  129. if (in_array($trade['SYMBOL'], $forexSymbols)) {
  130. // FOREX
  131. $tradeType = 0;
  132. if ($commission['forex'] > 0) {
  133. $comm = $volume * $commission['forex'];
  134. $commRule = "N" . sprintf('%.1f', $commission['forex']);
  135. }
  136. } elseif (in_array($trade['SYMBOL'], $cfdSymbols)) {
  137. // CFD
  138. $tradeType = 1;
  139. if ($commission['cfd'] > 0) {
  140. $comm = $volume * $commission['cfd'];
  141. $commRule = "N" . sprintf('%.1f', $commission['cfd']);
  142. }
  143. } elseif (in_array($trade['SYMBOL'], $xauSymbols)) {
  144. // 黄金
  145. $tradeType = 2;
  146. if ($commission['gold'] > 0) {
  147. $comm = $volume * $commission['gold'];
  148. $commRule = "N" . sprintf('%.1f', $commission['gold']);
  149. } elseif ($commission['metal'] > 0) {
  150. $comm = $volume * $commission['metal'];
  151. $commRule = "N" . sprintf('%.1f', $commission['metal']);
  152. }
  153. } elseif (in_array($trade['SYMBOL'], $xagSymbols)) {
  154. // 白银
  155. $tradeType = 3;
  156. if ($commission['silver'] > 0) {
  157. $comm = $volume * $commission['silver'];
  158. $commRule = "N" . sprintf('%.1f', $commission['silver']);
  159. } elseif ($commission['metal'] > 0) {
  160. $comm = $volume * $commission['metal'];
  161. $commRule = "N" . sprintf('%.1f', $commission['metal']);
  162. }
  163. } elseif (in_array($trade['SYMBOL'], $stockSymbols)) {
  164. // 股指
  165. $tradeType = 4;
  166. if ($commission['stock'] > 0) {
  167. $comm = $volume * $commission['stock'];
  168. $commRule = "N" . sprintf('%.1f', $commission['stock']);
  169. }
  170. } elseif (in_array($trade['SYMBOL'], $btcSymbols)) {
  171. // BTC
  172. $tradeType = 5;
  173. if ($commission['btc'] > 0) {
  174. $comm = $volume * $commission['btc'];
  175. $commRule = "N" . sprintf('%.1f', $commission['btc']);
  176. }
  177. } else {
  178. // 未知
  179. $tradeType = 9;
  180. }
  181. // 外汇,黄金,白银有外佣,CFD,股指,BTC没有外佣
  182. if ($tradeType == 0 || $tradeType == 2 || $tradeType == 3) {
  183. // 外佣
  184. if ($commission['wy'] > 0) {
  185. $commWy = $volume * $commission['wy'];
  186. $commWyRule = "W" . sprintf('%.1f', $commission['wy']);
  187. }
  188. }
  189. if ($comm > 0) {
  190. $record = [
  191. 'ib_id' => $member['id'],
  192. 'ib_login' => $ibLogin,
  193. 'user_login' => $trade['LOGIN'],
  194. 'trade_ticket' => $trade['TICKET'],
  195. 'trade_type' => $tradeType,
  196. 'trade_volume' => $volume,
  197. 'commission_rule' => $commRule,
  198. 'commission' => $comm,
  199. 'in_time' => intval(microtime(true) * 1000),
  200. ];
  201. $records[] = $record;
  202. }
  203. if ($commWy > 0) {
  204. $record = [
  205. 'ib_id' => $member['id'],
  206. 'ib_login' => $ibLogin,
  207. 'user_login' => $trade['LOGIN'],
  208. 'trade_ticket' => $trade['TICKET'],
  209. 'trade_type' => $tradeType,
  210. 'trade_volume' => $volume,
  211. 'commission_rule' => $commWyRule,
  212. 'commission' => $commWy,
  213. 'in_time' => intval(microtime(true) * 1000),
  214. ];
  215. $records[] = $record;
  216. }
  217. }
  218. if ($records) {
  219. $transaction = $this->getDb()->beginTransaction();
  220. try {
  221. $command1 = $this->getDb()->createCommand();
  222. $ticket = [
  223. 'ticket' => $trade['TICKET'],
  224. ];
  225. $affectRows = $command1->insert('crm_commission_ticket', $ticket)->execute();
  226. if ($affectRows == 0) {
  227. throw new \Exception("Ticket insert failed. {$command1->getRawSql()}");
  228. }
  229. foreach ($records as $record) {
  230. $command2 = $this->getDb()->createCommand();
  231. $affectRows = $command2->insert('crm_commission_record', $record)->execute();
  232. if ($affectRows == 0) {
  233. throw new \Exception("Record insert failed. {$command2->getRawSql()}");
  234. }
  235. $command3 = $this->getDb()->createCommand();
  236. $deposit = [
  237. 'login' => $record['ib_login'],
  238. 'amount' => round($record['commission'], 2),
  239. 'comment' => "Auto Commission",
  240. 'is_sync' => 0,
  241. 'record_id' => $this->getDb()->getLastInsertID(),
  242. 'in_time' => intval(microtime(true) * 1000),
  243. ];
  244. $affectRows = $command3->insert('crm_sync_desposit', $deposit)->execute();
  245. if ($affectRows == 0) {
  246. throw new \Exception("SyncDeposit insert failed. {$command3->getRawSql()}");
  247. }
  248. }
  249. $transaction->commit();
  250. } catch (\Exception $e) {
  251. $this->outLog($e->getMessage());
  252. $transaction->rollBack();
  253. }
  254. }
  255. }
  256. if (count($allTrades) != $limit) {
  257. break;
  258. }
  259. }
  260. $this->outLog("Job end, " . date('Y-m-d H:i:s'));
  261. $this->redis->del(self::COMMISSION_JOB_RUN_LOCK_KEY);
  262. }
  263. /**
  264. * @return null|Connection
  265. */
  266. protected function getDb()
  267. {
  268. return Yii::$app->get('dbXcrm');
  269. }
  270. }