CommissionJobController.php 13 KB

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