DataMigrationServices.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system;
  12. use think\facade\Log;
  13. use think\facade\Cache;
  14. use app\services\BaseServices;
  15. use app\services\user\UserBillServices;
  16. use app\services\user\UserMoneyServices;
  17. use app\services\user\UserBrokerageServices;
  18. use app\services\user\UserBrokerageFrozenServices;
  19. use app\services\order\StoreOrderServices;
  20. use app\services\order\StoreOrderRefundServices;
  21. use app\services\order\StoreOrderCreateServices;
  22. use app\services\order\StoreOrderCartInfoServices;
  23. use app\services\activity\coupon\StoreCouponIssueServices;
  24. use app\services\activity\coupon\StoreCouponProductServices;
  25. /**
  26. * 数据迁移服务
  27. * 用于跨版本升级时处理历史数据迁移
  28. * Class DataMigrationServices
  29. * @package app\services\system
  30. */
  31. class DataMigrationServices extends BaseServices
  32. {
  33. /**
  34. * 迁移状态缓存前缀
  35. */
  36. const MIGRATION_STATUS_PREFIX = 'data_migration_';
  37. /**
  38. * 默认分页大小
  39. */
  40. const DEFAULT_LIMIT = 100;
  41. /**
  42. * 检查迁移是否已完成
  43. * @param string $name 迁移名称
  44. * @return bool
  45. */
  46. public function isMigrationCompleted(string $name): bool
  47. {
  48. return Cache::get(self::MIGRATION_STATUS_PREFIX . $name) === 'completed';
  49. }
  50. /**
  51. * 标记迁移已完成
  52. * @param string $name 迁移名称
  53. * @return void
  54. */
  55. public function markMigrationCompleted(string $name): void
  56. {
  57. Cache::set(self::MIGRATION_STATUS_PREFIX . $name, 'completed', 86400 * 30);
  58. }
  59. /**
  60. * 获取迁移进度
  61. * @param string $name 迁移名称
  62. * @return array
  63. */
  64. public function getMigrationProgress(string $name): array
  65. {
  66. $page = Cache::get(self::MIGRATION_STATUS_PREFIX . $name . '_page', 1);
  67. $total = Cache::get(self::MIGRATION_STATUS_PREFIX . $name . '_total', 0);
  68. $processed = Cache::get(self::MIGRATION_STATUS_PREFIX . $name . '_processed', 0);
  69. return [
  70. 'page' => $page,
  71. 'total' => $total,
  72. 'processed' => $processed
  73. ];
  74. }
  75. /**
  76. * 更新迁移进度
  77. * @param string $name 迁移名称
  78. * @param int $page 当前页
  79. * @param int $processed 已处理数量
  80. * @return void
  81. */
  82. protected function updateMigrationProgress(string $name, int $page, int $processed): void
  83. {
  84. Cache::set(self::MIGRATION_STATUS_PREFIX . $name . '_page', $page, 86400);
  85. Cache::set(self::MIGRATION_STATUS_PREFIX . $name . '_processed', $processed, 86400);
  86. }
  87. /**
  88. * 执行数据迁移处理器
  89. * @param array $handler 处理器配置
  90. * @return array ['success' => bool, 'message' => string, 'completed' => bool]
  91. */
  92. public function executeHandler(array $handler): array
  93. {
  94. $name = $handler['name'] ?? '';
  95. $method = $handler['handler'] ?? '';
  96. $limit = $handler['limit'] ?? self::DEFAULT_LIMIT;
  97. $title = $handler['title'] ?? $name;
  98. if (!$name || !$method) {
  99. return ['success' => false, 'message' => '迁移配置错误', 'completed' => false];
  100. }
  101. // 检查是否已完成
  102. if ($this->isMigrationCompleted($name)) {
  103. return ['success' => true, 'message' => $title . ' 已完成', 'completed' => true, 'skipped' => true];
  104. }
  105. // 检查方法是否存在
  106. if (!method_exists($this, $method)) {
  107. return ['success' => false, 'message' => '迁移方法不存在: ' . $method, 'completed' => false];
  108. }
  109. try {
  110. // 获取当前进度
  111. $progress = $this->getMigrationProgress($name);
  112. $page = $progress['page'];
  113. // 执行迁移方法
  114. $result = $this->$method($page, $limit);
  115. if ($result['completed']) {
  116. $this->markMigrationCompleted($name);
  117. return [
  118. 'success' => true,
  119. 'message' => $title . ' 迁移完成',
  120. 'completed' => true,
  121. 'processed' => $result['processed'] ?? 0
  122. ];
  123. } else {
  124. // 更新进度
  125. $this->updateMigrationProgress($name, $page + 1, ($progress['processed'] ?? 0) + ($result['count'] ?? 0));
  126. return [
  127. 'success' => true,
  128. 'message' => $title . ' 处理中 (第' . $page . '页)',
  129. 'completed' => false,
  130. 'processed' => $result['count'] ?? 0
  131. ];
  132. }
  133. } catch (\Exception $e) {
  134. Log::error('数据迁移失败: ' . $e->getMessage(), ['handler' => $handler]);
  135. return ['success' => false, 'message' => $title . ' 迁移失败: ' . $e->getMessage(), 'completed' => false];
  136. }
  137. }
  138. /**
  139. * 执行所有数据迁移处理器(循环直到全部完成)
  140. * @param array $handlers 处理器列表
  141. * @return array
  142. */
  143. public function executeAllHandlers(array $handlers): array
  144. {
  145. $results = [];
  146. $allCompleted = true;
  147. foreach ($handlers as $handler) {
  148. $name = $handler['name'] ?? '';
  149. // 循环执行直到完成
  150. while (!$this->isMigrationCompleted($name)) {
  151. $result = $this->executeHandler($handler);
  152. if (!$result['success']) {
  153. $results[$name] = $result;
  154. $allCompleted = false;
  155. break; // 失败则跳过此处理器
  156. }
  157. if ($result['completed']) {
  158. $results[$name] = $result;
  159. break;
  160. }
  161. }
  162. if (!isset($results[$name])) {
  163. $results[$name] = ['success' => true, 'message' => ($handler['title'] ?? $name) . ' 已跳过', 'completed' => true, 'skipped' => true];
  164. }
  165. }
  166. return [
  167. 'success' => $allCompleted,
  168. 'results' => $results
  169. ];
  170. }
  171. // ==================== 数据迁移方法 ====================
  172. /**
  173. * 处理历史余额数据
  174. * @param int $page
  175. * @param int $limit
  176. * @return array
  177. */
  178. public function handleMoney(int $page = 1, int $limit = 100): array
  179. {
  180. /** @var UserBillServices $userBillServices */
  181. $userBillServices = app()->make(UserBillServices::class);
  182. $where = ['category' => 'now_money', 'type' => ['pay_product', 'pay_product_refund', 'system_add', 'system_sub', 'recharge', 'lottery_use', 'lottery_add']];
  183. $list = $userBillServices->getList($where, '*', $page, $limit, [], 'id asc');
  184. if (empty($list)) {
  185. return ['completed' => true, 'processed' => 0];
  186. }
  187. $allData = [];
  188. foreach ($list as $item) {
  189. $allData[] = [
  190. 'uid' => $item['uid'],
  191. 'link_id' => $item['link_id'],
  192. 'pm' => $item['pm'],
  193. 'title' => $item['title'],
  194. 'type' => $item['type'],
  195. 'number' => $item['number'],
  196. 'balance' => $item['balance'],
  197. 'mark' => $item['mark'],
  198. 'add_time' => strtotime($item['add_time']),
  199. ];
  200. }
  201. if ($allData) {
  202. /** @var UserMoneyServices $userMoneyServices */
  203. $userMoneyServices = app()->make(UserMoneyServices::class);
  204. $userMoneyServices->saveAll($allData);
  205. }
  206. Log::notice(['type' => 'data_migration', 'handler' => 'handleMoney', 'page' => $page, 'count' => count($list)]);
  207. return ['completed' => false, 'count' => count($list)];
  208. }
  209. /**
  210. * 处理历史佣金数据
  211. * @param int $page
  212. * @param int $limit
  213. * @return array
  214. */
  215. public function handleBrokerage(int $page = 1, int $limit = 100): array
  216. {
  217. /** @var UserBillServices $userBillServices */
  218. $userBillServices = app()->make(UserBillServices::class);
  219. $where = ['category' => ['', 'now_money'], 'type' => ['brokerage', 'brokerage_user', 'extract', 'refund', 'extract_fail']];
  220. $list = $userBillServices->getList($where, '*', $page, $limit, [], 'id asc');
  221. if (empty($list)) {
  222. return ['completed' => true, 'processed' => 0];
  223. }
  224. $allData = [];
  225. /** @var UserBrokerageFrozenServices $brokerageFrozenServices */
  226. $brokerageFrozenServices = app()->make(UserBrokerageFrozenServices::class);
  227. $frozenList = $brokerageFrozenServices->getColumn([['uill_id', 'in', array_column($list, 'id')], ['frozen_time', '>', time()]], 'uill_id,frozen_time', 'uill_id');
  228. foreach ($list as $item) {
  229. if (in_array($item['type'], ['brokerage_user', 'extract', 'refund', 'extract_fail'])) {
  230. $type = $item['type'];
  231. } else {
  232. $type = strpos($item['mark'], '二级') !== false ? 'two_brokerage' : 'one_brokerage';
  233. }
  234. $allData[] = [
  235. 'uid' => $item['uid'],
  236. 'link_id' => $item['link_id'],
  237. 'pm' => $item['pm'],
  238. 'title' => $item['title'],
  239. 'type' => $type,
  240. 'number' => $item['number'],
  241. 'balance' => $item['balance'],
  242. 'mark' => $item['mark'],
  243. 'frozen_time' => $frozenList[$item['id']]['frozen_time'] ?? 0,
  244. 'add_time' => strtotime($item['add_time']),
  245. ];
  246. }
  247. if ($allData) {
  248. /** @var UserBrokerageServices $userBrokerageServices */
  249. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  250. $userBrokerageServices->saveAll($allData);
  251. }
  252. Log::notice(['type' => 'data_migration', 'handler' => 'handleBrokerage', 'page' => $page, 'count' => count($list)]);
  253. return ['completed' => false, 'count' => count($list)];
  254. }
  255. /**
  256. * 处理历史退款数据
  257. * @param int $page
  258. * @param int $limit
  259. * @return array
  260. */
  261. public function handleOrderRefund(int $page = 1, int $limit = 100): array
  262. {
  263. /** @var StoreOrderServices $storeOrderServices */
  264. $storeOrderServices = app()->make(StoreOrderServices::class);
  265. $list = $storeOrderServices->getSplitOrderList(['refund_status' => [1, 2], ['refund_type' => [1, 2, 4, 5, 6]]], ['*'], [], $page, $limit, 'id asc');
  266. if (empty($list)) {
  267. return ['completed' => true, 'processed' => 0];
  268. }
  269. $allData = [];
  270. /** @var StoreOrderCreateServices $storeOrderCreateServices */
  271. $storeOrderCreateServices = app()->make(StoreOrderCreateServices::class);
  272. /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
  273. $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  274. foreach ($list as $order) {
  275. $cartInfos = $storeOrderCartInfoServices->getCartColunm(['oid' => $order['id']], 'id,cart_id,cart_num,cart_info');
  276. foreach ($cartInfos as &$cartInfo) {
  277. $cartInfo['cart_info'] = is_string($cartInfo['cart_info']) ? json_decode($cartInfo['cart_info'], true) : $cartInfo['cart_info'];
  278. }
  279. $allData[] = [
  280. 'uid' => $order['uid'],
  281. 'store_id' => $order['store_id'],
  282. 'store_order_id' => $order['id'],
  283. 'order_id' => $storeOrderCreateServices->getNewOrderId(''),
  284. 'refund_num' => $order['total_num'],
  285. 'refund_type' => $order['refund_type'],
  286. 'refund_price' => $order['pay_price'],
  287. 'refunded_price' => 0,
  288. 'refund_explain' => $order['refund_reason_wap_explain'],
  289. 'refund_img' => $order['refund_reason_wap_img'],
  290. 'refund_reason' => $order['refund_reason_wap'],
  291. 'refund_express' => $order['refund_express'],
  292. 'refunded_time' => $order['refund_type'] == 6 ? $order['refund_reason_time'] : 0,
  293. 'add_time' => $order['refund_reason_time'],
  294. 'cart_info' => json_encode(array_column($cartInfos, 'cart_info'))
  295. ];
  296. }
  297. if ($allData) {
  298. /** @var StoreOrderRefundServices $storeOrderRefundServices */
  299. $storeOrderRefundServices = app()->make(StoreOrderRefundServices::class);
  300. $storeOrderRefundServices->saveAll($allData);
  301. }
  302. Log::notice(['type' => 'data_migration', 'handler' => 'handleOrderRefund', 'page' => $page, 'count' => count($list)]);
  303. return ['completed' => false, 'count' => count($list)];
  304. }
  305. /**
  306. * 更新订单商品表UID
  307. * @param int $page
  308. * @param int $limit
  309. * @return array
  310. */
  311. public function handleCartInfo(int $page = 1, int $limit = 100): array
  312. {
  313. /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
  314. $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  315. $list = $storeOrderCartInfoServices->selectList(['uid' => 0], 'id,oid', $page, $limit)->toArray();
  316. if (empty($list)) {
  317. return ['completed' => true, 'processed' => 0];
  318. }
  319. /** @var StoreOrderServices $storeOrderServices */
  320. $storeOrderServices = app()->make(StoreOrderServices::class);
  321. $uids = $storeOrderServices->getColumn([['id', 'in', array_column($list, 'oid')]], 'uid', 'id');
  322. $allData = [];
  323. foreach ($list as $cart) {
  324. $allData[] = [
  325. 'id' => $cart['id'],
  326. 'uid' => $uids[$cart['oid']] ?? 0
  327. ];
  328. }
  329. if ($allData) {
  330. $storeOrderCartInfoServices->saveAll($allData);
  331. }
  332. Log::notice(['type' => 'data_migration', 'handler' => 'handleCartInfo', 'page' => $page, 'count' => count($list)]);
  333. return ['completed' => false, 'count' => count($list)];
  334. }
  335. /**
  336. * 更新分类券数据
  337. * @param int $page
  338. * @param int $limit
  339. * @return array
  340. */
  341. public function handleCoupon(int $page = 1, int $limit = 100): array
  342. {
  343. /** @var StoreCouponIssueServices $couponIssueServices */
  344. $couponIssueServices = app()->make(StoreCouponIssueServices::class);
  345. $list = $couponIssueServices->selectList([['category_id', '>', 0]], 'id,category_id', $page, $limit)->toArray();
  346. if (empty($list)) {
  347. return ['completed' => true, 'processed' => 0];
  348. }
  349. $allData = [];
  350. foreach ($list as $item) {
  351. $allData[] = [
  352. 'coupon_id' => $item['id'],
  353. 'product_id' => 0,
  354. 'category_id' => $item['category_id']
  355. ];
  356. }
  357. if ($allData) {
  358. /** @var StoreCouponProductServices $couponProductServices */
  359. $couponProductServices = app()->make(StoreCouponProductServices::class);
  360. $couponProductServices->saveAll($allData);
  361. }
  362. Log::notice(['type' => 'data_migration', 'handler' => 'handleCoupon', 'page' => $page, 'count' => count($list)]);
  363. return ['completed' => false, 'count' => count($list)];
  364. }
  365. }