syncVip.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /**
  3. * 同步vip数据
  4. * User: ben
  5. * Date: 2018/10/28
  6. * Time: 下午2:08
  7. */
  8. require_once realpath(dirname(__FILE__)) . '/../common.php';
  9. if (!singleProcess(getCurrentCommand(), ROOT_PATH . "/bin/run/syncVip.pid")) {
  10. exit("Sorry, this script file has already been running ...\n");
  11. }
  12. //$GLOBALS['eosProtocol'] = 'https';
  13. //$GLOBALS['eosHost'] = 'proxy.eosnode.tools';
  14. //$GLOBALS['eosPort'] = 443;
  15. //
  16. //if ($GLOBALS['eosProtocol'] == 'https' && $GLOBALS['eosPort'] == 443) {
  17. // $GLOBALS['eosUrl'] = "{$GLOBALS['eosProtocol']}://{$GLOBALS['eosHost']}/";
  18. //} else {
  19. // $GLOBALS['eosUrl'] = "{$GLOBALS['eosProtocol']}://{$GLOBALS['eosHost']}:{$GLOBALS['eosPort']}/";
  20. //}
  21. $objUserGroup = new TableHelper('user_group', 'dw_chat');
  22. $objGroupEos = new TableHelper('group_eos', 'dw_chat');
  23. $_where = "vip_info IS NOT NULL AND vip_info != ''";
  24. $_field = "group_id, vip_info";
  25. $rows = $objGroupEos->getAll(compact('_where', '_field'));
  26. foreach ($rows as $row) {
  27. // {"code": "eosgetadmin1", "table": "vip", "account_field": "player", "vip_field": "vip_level", "amount_field": "total_bet_amount", "amount_rate": 10000, "index_position": 0}
  28. $vipInfo = json_decode($row['vip_info'], true);
  29. if (!$vipInfo) {
  30. Eos::log("skip error vip info. group_id: {$row['group_id']}, vip_info: {$row['vip_info']}");
  31. continue;
  32. }
  33. Eos::log("sync group_id: {$row['group_id']}.");
  34. syncVip($row['group_id'], $vipInfo);
  35. }
  36. function syncVip($group_id, $vipInfo) {
  37. $code = $vipInfo['code'];
  38. $table = $vipInfo['table'];
  39. $index_pos = $vipInfo['index_position'];
  40. $accountField = $vipInfo['account_field'];
  41. $vipField = $vipInfo['vip_field'];
  42. $amountField = $vipInfo['amount_field'];
  43. $amountRate = $vipInfo['amount_rate'];
  44. $lower = null;
  45. do {
  46. $ret = _getDatas($code, $table, $index_pos, $lower);
  47. $last = end($ret['rows']);
  48. $accounts = array_column($ret['rows'], $accountField);
  49. $map = _getUserMap($accounts);
  50. $user_ids = array_values($map);
  51. $vipMap = _getVipMap($group_id, $user_ids);
  52. $newAccountList = [];
  53. $newJoinList = [];
  54. $skipNum = 0;
  55. foreach ($ret['rows'] as $row) {
  56. $account = $row[$accountField];
  57. $user_id = $map[$account];
  58. $total_bet_amount = $row[$amountField] * $amountRate;
  59. if ($total_bet_amount < 10000) {
  60. $skipNum++;
  61. continue;
  62. }
  63. if (empty($user_id)) {
  64. $newAccountList[] = $account;
  65. } else {
  66. $info = $vipMap[$user_id];
  67. $vip_level = 0;
  68. if (!$vipField && $vipInfo['vip_config']) {
  69. foreach ($vipInfo['vip_config'] as $i => $target) {
  70. if ($total_bet_amount < $target) {
  71. $vip_level = $i + 1;
  72. break;
  73. }
  74. }
  75. } else {
  76. $vip_level = $row[$vipField];
  77. }
  78. if ($info) {
  79. // 已经加入群的老用户
  80. if ($info['vip_level'] != $vip_level) {
  81. _updateVip($user_id, $group_id, $vip_level);
  82. } else if ($amountField && $amountRate) {
  83. $total_bet_amount = $row[$amountField] * $amountRate;
  84. if ($info['total_bet_amount'] != $total_bet_amount) {
  85. _updateVip($user_id, $group_id, $vip_level, $total_bet_amount);
  86. }
  87. }
  88. } else {
  89. // 还没加入群的老用户
  90. $newJoinList[] = $user_id;
  91. }
  92. }
  93. }
  94. if ($newAccountList) {
  95. $newUserIds = _createAccount($newAccountList);
  96. // 加入官方群
  97. _joinGroup(10000, $newUserIds);
  98. $newJoinList = array_merge($newJoinList, $newUserIds);
  99. }
  100. // 这批没加入群的用户要拉入群
  101. if ($newJoinList) {
  102. _joinGroup($group_id, $newJoinList);
  103. }
  104. $rowNum = count($ret['rows']);
  105. $newAccountNum = count($newAccountList);
  106. $newAJoinNum = count($newJoinList);
  107. Eos::log("getData lower:{$lower}, rowNum:{$rowNum}, skipNum:{$skipNum}, newAccountNum:{$newAccountNum}, newAJoinNum:{$newAJoinNum}. ");
  108. $lower = $last[$accountField];
  109. } while($ret['more']);
  110. }
  111. /**
  112. * 批量创建账号,并返回user_id
  113. * @param $accounts
  114. *
  115. * @return array
  116. */
  117. function _createAccount($accounts) {
  118. $datas = [];
  119. $type = 'eos';
  120. $user_names = [];
  121. // 创建用户,并加入到$newJoinList
  122. foreach ($accounts as $account) {
  123. $nick_name = User::getNewName($account);
  124. $user_name = $nick_name . '-' . strtoupper($type);
  125. $datas[] = [
  126. 'first_type' => $type,
  127. 'nick_name' => $nick_name,
  128. 'user_name' => $user_name,
  129. 'last_login_time' => NOW,
  130. 'update_time' => NOW,
  131. ];
  132. $user_names[] = $user_name;
  133. }
  134. $objUserInfo = new TableHelper('user_info', 'dw_chat');
  135. $objUserInfo->addObjectsIfNoExist($datas);
  136. $rows = $objUserInfo->getAll(['user_name' => $user_names], ['_field' => 'user_id, user_name']);
  137. $datas2 = [];
  138. foreach ($rows as $row) {
  139. $parts = explode('-', $row['user_name']);
  140. $datas2[] = [
  141. 'user_id' => $row['user_id'],
  142. 'type' => $type,
  143. 'account' => $parts[0],
  144. 'create_time' => NOW,
  145. ];
  146. }
  147. $objUserBindInfo = new TableHelper('user_bind_info', 'dw_chat');
  148. $objUserBindInfo->addObjectsIfNoExist($datas2);
  149. return array_column($rows, 'user_id');
  150. }
  151. function _joinGroup($group_id, $user_ids) {
  152. global $objUserGroup;
  153. $datas = [];
  154. $sessDatas = [];
  155. foreach ($user_ids as $user_id) {
  156. $datas[] = [
  157. 'user_id' => $user_id,
  158. 'group_id' => $group_id,
  159. 'join_time' => NOW,
  160. 'update_time' => NOW,
  161. 'state' => UserGroup::STATE_IN_GROUP,
  162. ];
  163. $sessDatas[] = [
  164. 'user_id' => $user_id,
  165. 'session_id' => $group_id,
  166. 'is_group' => 1,
  167. 'update_time' => NOW,
  168. 'update_time_int' => TIME,
  169. ];
  170. }
  171. // 加入群
  172. $objUserGroup->addObjectsIfNoExist($datas);
  173. // 加入会话
  174. $objSession = new Session();
  175. $objSession->objTable->addObjectsIfNoExist($sessDatas);
  176. // 更新群的人数
  177. $member_num = $objUserGroup->getCount(['group_id' => $group_id, 'state' => 1]);
  178. $objGroupInfo = new GroupInfo();
  179. $objGroupInfo->objTable->updateObject(compact('member_num'), compact('group_id'));
  180. }
  181. function _updateVip($user_id, $group_id, $vip_level, $total_bet_amount = 0) {
  182. global $objUserGroup;
  183. $newData = compact('vip_level', 'total_bet_amount');
  184. $where = compact('user_id', 'group_id');
  185. $objUserGroup->updateObject($newData, $where);
  186. }
  187. function _getVipMap($group_id, $user_ids) {
  188. global $objUserGroup;
  189. $where = [
  190. 'group_id' => $group_id,
  191. 'user_id' => $user_ids,
  192. '_field' => 'user_id, vip_level, total_bet_amount'
  193. ];
  194. $map = $objUserGroup->getAll($where);
  195. $map = arrayFormatKey($map, 'user_id');
  196. return $map;
  197. }
  198. function _getUserMap($accounts) {
  199. $where = [
  200. 'type' => 'eos',
  201. 'account' => $accounts,
  202. '_field' => 'user_id, account'
  203. ];
  204. $objUserBindInfo = new TableHelper('user_bind_info', 'dw_chat');
  205. $map = $objUserBindInfo->getAll($where);
  206. $map = arrayFormatKey($map, 'account', 'user_id');
  207. return $map;
  208. }
  209. function _getDatas($code, $table, $index_pos = 0, $lower = null, $limit = 1000) {
  210. $cmd = "cleos -u {$GLOBALS['eosUrl']} get table {$code} {$code} {$table} --index {$index_pos} -l {$limit} ";
  211. if ($lower) {
  212. $cmd .= " -L '{$lower}' ";
  213. }
  214. $json = Eos::execCmd($cmd);
  215. $datas = json_decode($json, true);
  216. // Eos::log("get {$code} {$table} lower:{$lower}, count:" . count($datas['rows']));
  217. return $datas;
  218. }