UpdateRechargeOrdersId.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * 刷充值记录的orders_id字段
  4. * Created by: PhpStorm
  5. * User: lytian
  6. * Date: 2019/11/29
  7. * Time: 19:50
  8. */
  9. namespace app\admin\command;
  10. use app\main\helper\ArrayHelper;
  11. use app\main\service\GoodsService;
  12. use think\Config;
  13. use think\console\Command;
  14. use think\console\Input;
  15. use think\console\input\Argument;
  16. use think\console\Output;
  17. use think\Db;
  18. use think\Env;
  19. use think\Exception;
  20. use think\Log;
  21. use think\Request;
  22. /**
  23. * Class UpdateRechargeOrdersId
  24. *
  25. * @package app\admin\command
  26. */
  27. class UpdateRechargeOrdersId extends Command
  28. {
  29. /**
  30. * @var array
  31. */
  32. protected $db = [];
  33. protected function configure()
  34. {
  35. $this->setName('UpdateRechargeOrdersId')
  36. ->addArgument('startUserId', Argument::REQUIRED, "开始用户ID")
  37. ->addArgument('endUserId', Argument::REQUIRED, "结束用户ID")
  38. ->setDescription('刷充值记录的orders_id字段');
  39. }
  40. protected function execute(Input $input, Output $output)
  41. {
  42. Request::instance()->module('admin');
  43. $startUserId = $input->getArgument('startUserId');
  44. $endUserId = $input->getArgument('endUserId');
  45. $output->writeln('开始用户ID:' . $startUserId);
  46. $output->writeln('结束用户ID:' . $endUserId);
  47. if ($startUserId > $endUserId) {
  48. $output->writeln('参数错误,起始id必须小于结束id');
  49. }
  50. $pageSize = 1000;
  51. $do = true;
  52. $mainSlaveDb = Db::connect($this->getMainSlaveDbConfig());
  53. while ($do) {
  54. $sql = "SELECT DISTINCT user_id FROM orders FORCE INDEX(orders_user_id) WHERE user_id > {$startUserId} AND user_id <= {$endUserId} AND state = '1' LIMIT {$pageSize};";
  55. $userIds = $mainSlaveDb->query($sql);
  56. if ($userIds) {
  57. $userIds = array_column($userIds, 'user_id');
  58. $userIdsStr = implode(',', $userIds);
  59. $sql = "SELECT user_id, id, kandian, free_kandian, day, type, finishtime, deduct, money FROM orders WHERE user_id in ({$userIdsStr}) and state = '1'";
  60. $orderRows = $mainSlaveDb->query($sql);
  61. //订单按照user_id分组
  62. $orderRows = ArrayHelper::index($orderRows, null, 'user_id');
  63. foreach ($userIds as $userId) {
  64. try {
  65. $startUserId = $userId;
  66. $userOrderRows = $orderRows[$userId];
  67. $userDbConfig = $this->getDbDeploy($userId, 'user');
  68. $userTable = $userDbConfig['table'].'.recharge';
  69. unset($userDbConfig['table']);
  70. //拉取所有的recharge记录
  71. $sql = "SELECT id, kandian, free_kandian, day, createtime FROM {$userTable} WHERE user_id = {$userId} AND type in('1', '2') ORDER BY createtime ASC";
  72. $rechargeRows = Db::connect($userDbConfig)->query($sql);
  73. foreach ($userOrderRows as $userOrderRow) {
  74. //开始进行匹配
  75. $result = $this->checkRow($userOrderRow, $rechargeRows);
  76. if ($result) {
  77. Log::info("匹配结果: user_id:{$userId} order_id: {$userOrderRow['id']} recharge: ".json_encode($result, JSON_UNESCAPED_UNICODE));
  78. $rechargeIds = array_values($result);
  79. $condition = [
  80. 'id' => ['in', implode(',', $rechargeIds)],
  81. ];
  82. $res = model("Recharge")
  83. ->setConnect($userId)
  84. ->update([
  85. 'orders_id' => $userOrderRow['id'],
  86. 'dd' => $userOrderRow['deduct']
  87. ], $condition);
  88. if ($res) {
  89. Log::info("更新结果成功: user_id:{$userId} order_id: {$userOrderRow['id']} recharge: ".json_encode($result, JSON_UNESCAPED_UNICODE));
  90. } else {
  91. Log::error("匹配结果失败: user_id:{$userId} order_id: {$userOrderRow['id']} recharge: ".json_encode($result, JSON_UNESCAPED_UNICODE));
  92. }
  93. } else {
  94. //没匹配到 需要记录一下 看情况人工处理
  95. Log::error("未匹配到结果: user_id:{$userId} order_id: {$userOrderRow['id']}");
  96. }
  97. }
  98. unset($userOrderRows);
  99. unset($rechargeRows);
  100. } catch (Exception $e) {
  101. Log::error("脚本异常 user_id: {$userId} ". $e);
  102. }
  103. }
  104. unset($orderRows);
  105. } else {
  106. $do = false;
  107. }
  108. unset($userIds);
  109. }
  110. }
  111. /**
  112. * 匹配记录
  113. * @param $orderRow
  114. * @param $rechargeRows
  115. * @param array $notIn
  116. * @return array
  117. */
  118. private function checkRow($orderRow, &$rechargeRows)
  119. {
  120. //先精准匹配时间和看点数
  121. $orderTime = $orderRow['finishtime'];
  122. $hitData = [];
  123. foreach ($rechargeRows as $key => $rechargeRow) {
  124. if ($rechargeRow['createtime'] != $orderTime) {
  125. continue;
  126. }
  127. //看点充值
  128. if ($orderRow['kandian'] > 0
  129. && $orderRow['kandian'] == $rechargeRow['kandian']
  130. ) {
  131. //永久看点
  132. $hitData[] = $rechargeRow['id'];
  133. unset($rechargeRows[$key]);
  134. }
  135. if ($orderRow['free_kandian'] > 0
  136. && $orderRow['free_kandian'] == $rechargeRow['free_kandian']
  137. ) {
  138. //免费看点
  139. $hitData[] = $rechargeRow['id'];
  140. unset($rechargeRows[$key]);
  141. }
  142. //vip充值
  143. if ($orderRow['day'] > 0
  144. && $orderRow['day'] == $rechargeRow['day']
  145. ) {
  146. $hitData[] = $rechargeRow['id'];
  147. unset($rechargeRows[$key]);
  148. }
  149. }
  150. //模糊匹配一下时间 往后推3s
  151. $extraData = [];
  152. reset($rechargeRows);
  153. $endTime = $orderTime + 3;
  154. foreach ($rechargeRows as $k => $rechargeRow) {
  155. //往后推3s
  156. if ($rechargeRow['createtime'] >= $orderTime && $rechargeRow['createtime'] <= $endTime) {
  157. $extraData[] = $rechargeRow['id'];
  158. unset($rechargeRows[$k]);
  159. }
  160. }
  161. if (!empty($extraData)) {
  162. $hitData = array_merge($hitData, $extraData);
  163. unset($extraData);
  164. Log::error("加购结果: user_id:{$orderRow['user_id']} order_id: {$orderRow['id']}");
  165. } else {
  166. if ($orderRow['type'] == '1' && (((int)($orderRow['money'] * 100)) > ((int)$orderRow['kandian']))) {
  167. //加购
  168. Log::error("加购异常结果失败: user_id:{$orderRow['user_id']} order_id: {$orderRow['id']}");
  169. }
  170. }
  171. return $hitData;
  172. }
  173. /**
  174. * 从库配置
  175. *
  176. * @param $param
  177. * @param string $deploy
  178. * @return array|mixed
  179. */
  180. private function getDbDeploy($param, $deploy = 'shard')
  181. {
  182. $db = Config::get('db');
  183. $mod = $param % $db[$deploy . '_num'];
  184. $mod = abs($mod);
  185. if (isset($this->db[$deploy.'_'.$mod])) {
  186. return $this->db[$deploy.'_'.$mod];
  187. }
  188. $list = explode(';', $db[$deploy . '_list']);
  189. foreach ($list as $item) {
  190. $con = explode(':', $item); // 0=0-191库编号 1=192.168.1.149主IP 2=3306主端口 3=192.168.1.150从IP 4=3306从端口
  191. if (count($con) >= 3) {
  192. $c = explode('-', $con[0]); //库编号 0开始 1结束
  193. if (count($c) >= 2) {
  194. if ($c[0] <= $mod && $mod <= $c[1]) {
  195. $database = Config::get('database');
  196. if (count($con) >= 5) { //开启主从 & 带主从配置
  197. $database['deploy'] = 1;
  198. $database['rw_separate'] = true;
  199. $database['hostname'] = $con[1] . ',' . $con[3]; //192.168.1.149,192.168.1.150
  200. $database['hostport'] = $con[2] . ',' . $con[4]; //3306,3306
  201. } else { //只有主库
  202. $database['hostname'] = $con[1];
  203. $database['hostport'] = $con[2];
  204. }
  205. $database['database'] = 'mysql';
  206. $database['table'] = str_replace('$mod', $mod, $db[$deploy . '_database']);
  207. $this->db[$deploy.'_'.$mod] = $database;
  208. return $database;
  209. }
  210. }
  211. }
  212. }
  213. return [];
  214. }
  215. /**
  216. * 获取主库的从库配置 从库不存在返回主库
  217. *
  218. * @return array
  219. */
  220. private function getMainSlaveDbConfig()
  221. {
  222. $hostArr = explode(',', Env::get('database.admin_hostname'));
  223. $portArr = explode(',', Env::get('database.admin_hostport', '3306,3306'));
  224. //默认主库
  225. $mainDbConfig = array_merge(Config::get("database"), ['hostname' =>$hostArr[0], 'hostport' => $portArr[0]]);
  226. if (count($hostArr) >= 2) {
  227. //从库
  228. if (isset($hostArr[2])) {
  229. //阳光从库2
  230. $mainDbConfig = array_merge(
  231. Config::get("database"),
  232. ['hostname' =>$hostArr[2], 'hostport' => $portArr[2]]
  233. );
  234. } else {
  235. //其他平台从库1
  236. $mainDbConfig = array_merge(
  237. Config::get("database"),
  238. ['hostname' =>$hostArr[1], 'hostport' => $portArr[1]]
  239. );
  240. }
  241. }
  242. return $mainDbConfig;
  243. }
  244. }