ExportController.php 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. <?php
  2. namespace console\controllers;
  3. use addons\LuckyGroup\common\models\LuckyGroupOrder;
  4. use addons\Salary\common\models\SalaryMerchant;
  5. use addons\Store\common\models\Store;
  6. use addons\Store\common\models\StoreGoods;
  7. use addons\Store\common\models\StoreOrder;
  8. use backend\models\AdminRole;
  9. use common\models\backend\Admin;
  10. use common\models\common\CapitalLog;
  11. use common\models\goods\Goods;
  12. use common\models\mall\Mall;
  13. use common\models\mall\MallAddons;
  14. use common\models\order\OrderDetail;
  15. use common\models\order\OrderRefund;
  16. use common\models\user\User;
  17. use yii\console\Controller;
  18. use common\models\order\Order;
  19. class ExportController extends Controller
  20. {
  21. private $te_read_table = ['qimall_addons_juhe_shop_goods'];//同一sql,存在换行,且数据比较大,无法导入的表
  22. private $no_data_table_arr = ['qimall_crontab_task_execute', 'qimall_region', 'qimall_region_copy1', 'qimall_addons_star_chain_region',
  23. 'qimall_addons_wechat_video_shop_region', 'qimall_census_mall_goods_copy1', 'qimall_census_mall_payment_single_month_copy1', 'qimall_census_mall_single_day_copy1',
  24. 'qimall_town_copy1',
  25. 'qimall_global_var_map',
  26. 'qimall_migration',
  27. 'qimall_menu',
  28. 'qimall_link_category',
  29. 'qimall_link',
  30. 'qimall_express',
  31. 'qimall_diy_template',
  32. 'qimall_diy_components',
  33. 'qimall_crontab_task',
  34. 'qimall_backend_log',
  35. 'qimall_attachment',
  36. 'qimall_admin_role',
  37. 'qimall_admin',
  38. 'qimall_addons_wechat_video_shop_express_company',
  39. 'qimall_rbac_auth_item',
  40. 'qimall_town',
  41. 'qimall_system_version_log',
  42. 'qimall_platform_order',
  43. 'qimall_payment_trade_order',
  44. 'qimall_addons',
  45. 'qimall_addons_combo',
  46. 'qimall_addons_wechat_video_shop_cate',
  47. 'qimall_addons_store_addons',
  48. 'qimall_addons_event',
  49. 'qimall_addons_invoker',
  50. 'qimall_addons_juhe_shop_category',
  51. 'qimall_addons_juhe_shop_express',
  52. 'qimall_addons_mch_menu',
  53. 'qimall_addons_qijuhe_express',
  54. 'qimall_addons_smart_form_components',
  55. 'qimall_addons_star_chain_category',
  56. 'qimall_addons_store_menu'
  57. ];//不需要导入数据的表
  58. private $all_read_table = ['qimall_addons_smart_form_template_column', 'qimall_member_benefit', 'qimall_addons_ai_spirit_use', 'qimall_addons_boss_level',
  59. 'qimall_addons_business_card', 'qimall_addons_business_card_material', 'qimall_addons_card_material', 'qimall_addons_clockin_apply', 'qimall_addons_course',
  60. 'qimall_addons_material', 'qimall_addons_mch', 'qimall_platform_setting'
  61. ];//字段内容有换行,且数据量少,可以file_get_content
  62. private $input_db = 'input_db';
  63. private $source_db = 'source_db';
  64. public function getTableArr()
  65. {
  66. return [
  67. 'qimall_goods_attr' => [
  68. 'field' => 'goods_id',
  69. 'class' => \common\models\goods\Goods::class,
  70. 'select' => 'id',
  71. ],
  72. 'qimall_payment_trade_order' => [
  73. 'field' => 'order_no',
  74. 'class' => \common\models\order\Order::class,
  75. 'select' => 'order_no',
  76. ],
  77. 'qimall_goods_cate_relate' => [
  78. 'field' => 'goods_id',
  79. 'class' => \common\models\goods\Goods::class,
  80. 'select' => 'id',
  81. ],
  82. 'qimall_addons_store_goods_attr' => [
  83. 'field' => 'goods_id',
  84. 'class' => StoreGoods::class,
  85. 'select' => 'id',
  86. ],
  87. 'qimall_user_partition_relationship' => [
  88. 'field' => 'user_id',
  89. 'class' => User::class,
  90. 'select' => 'id',
  91. ],
  92. 'qimall_access_token' => [
  93. 'field' => 'user_id',
  94. 'class' => User::class,
  95. 'select' => 'id',
  96. ],
  97. 'qimall_user_extra' => [
  98. 'field' => 'user_id',
  99. 'class' => User::class,
  100. 'select' => 'id',
  101. ],
  102. 'qimall_order_marketing' => [
  103. 'field' => 'order_detail_id',
  104. 'class' => OrderDetail::class,
  105. 'select' => 'id',
  106. ],
  107. 'qimall_order_express' => [
  108. 'field' => 'order_id',
  109. 'class' => \common\models\order\Order::class,
  110. 'select' => 'id',
  111. ],
  112. 'qimall_payment_trade_log' => [
  113. 'field' => 'trade_id',
  114. 'class' => \common\models\common\PaymentTrade::class,
  115. 'select' => 'id',
  116. ],
  117. 'qimall_addons_salary_api_log' => [
  118. 'field' => 'merchant_no',
  119. 'class' => SalaryMerchant::class,
  120. 'select' => 'merchant_no',
  121. ],
  122. 'qimall_addons_store_goods_cate_relate' => [
  123. 'field' => 'goods_id',
  124. 'class' => StoreGoods::class,
  125. 'select' => 'id',
  126. ],
  127. 'qimall_user_address' => [
  128. 'field' => 'user_id',
  129. 'class' => User::class,
  130. 'select' => 'id',
  131. ],
  132. 'qimall_user_relationship_log' => [
  133. 'field' => 'user_id',
  134. 'class' => User::class,
  135. 'select' => 'id',
  136. ],
  137. 'qimall_order_refund_step' => [
  138. 'field' => 'order_refund_id',
  139. 'class' => OrderRefund::class,
  140. 'select' => 'id',
  141. ],
  142. 'qimall_addons_boss_bonus_user_log' => [
  143. 'field' => 'user_id',
  144. 'class' => User::class,
  145. 'select' => 'id',
  146. ],
  147. 'qimall_addons_smart_form_template_user_behavior' => [
  148. 'field' => 'user_id',
  149. 'class' => User::class,
  150. 'select' => 'id',
  151. ],
  152. 'qimall_addons_boss_bonus_user_partition_relationship' => [
  153. 'field' => 'user_id',
  154. 'class' => User::class,
  155. 'select' => 'id',
  156. ],
  157. 'qimall_addons_score_mall_goods_attr' => [
  158. 'field' => 'goods_id',
  159. 'class' => Goods::class,
  160. 'select' => 'id',
  161. ],
  162. 'qimall_addons_smart_form_template_user_form_info' => [
  163. 'field' => 'user_id',
  164. 'class' => User::class,
  165. 'select' => 'id',
  166. ],
  167. 'qimall_addons_coupon_goods_relate' => [
  168. 'field' => 'goods_id',
  169. 'class' => Goods::class,
  170. 'select' => 'id',
  171. ],
  172. 'qimall_addons_boss_bonus_user_relationship_log' => [
  173. 'field' => 'user_id',
  174. 'class' => User::class,
  175. 'select' => 'id',
  176. ],
  177. 'qimall_capital_error_log' => [
  178. 'field' => 'user_id',
  179. 'class' => User::class,
  180. 'select' => 'id',
  181. ],
  182. 'qimall_addons_lucky_group_order_marketing' => [
  183. 'field' => 'order_id',
  184. 'class' => LuckyGroupOrder::class,
  185. 'select' => 'id',
  186. ],
  187. 'qimall_addons_store_settlement_log' => [
  188. 'field' => 'store_id',
  189. 'class' => Store::class,
  190. 'select' => 'id',
  191. ],
  192. ];
  193. }
  194. private function getCommonPath()
  195. {
  196. // return dirname(dirname(__DIR__)) . '\sql\/common\/';
  197. return dirname(dirname(dirname(__DIR__))) . '\sql\/common\/';
  198. }
  199. private function getMallPath($mall_id)
  200. {
  201. // return dirname(dirname(__DIR__)) . '\sql\/mall_' . $mall_id . '\/';
  202. return dirname(dirname(dirname(__DIR__))) . '\sql\/mall_' . $mall_id . '\/';
  203. }
  204. private function getStructPath()
  205. {
  206. return dirname(dirname(__DIR__)) . '\sql\/struct\/';
  207. }
  208. //导出mysql的表结构和数据的sql
  209. public function actionIndex()
  210. {
  211. ini_set('memory_limit', 1024);
  212. $malll_list = Mall::lists(['where' => [['>', 'id', 0]]]);
  213. $common_path = $this->getCommonPath();
  214. if (!is_dir($common_path)) {
  215. mkdir($common_path, 777, true);
  216. }
  217. $table_arr = $this->getTableArr();
  218. foreach ($malll_list as $mall) {
  219. $mall_id = $mall['id'];
  220. if ($mall_id == 1) continue;
  221. $path = $this->getMallPath($mall_id);
  222. if (!is_dir($path)) {
  223. mkdir($path, 777, true);
  224. }
  225. $sqls = "show tables";
  226. $table_list = \Yii::$app->get($this->source_db)->createCommand($sqls)->queryAll();//获取表名
  227. foreach ($table_list as $table) {
  228. // $table_name = $table['Tables in qimall'];//mycat
  229. $table_name = $table['Tables_in_qimall'];
  230. $temp_sql = 'show create table ' . $table_name;
  231. $table_field_type_arr = $this->getTableFieldType($table_name);
  232. $table_struct = \Yii::$app->get($this->source_db)->createCommand($temp_sql)->queryOne();//获取表名
  233. $table_field = $table_struct['Create Table'];
  234. $is_common = false;
  235. if (strpos($table_field, 'mall_id') === false) {
  236. $to_file_name = $common_path . $table_name . ".sql"; // 导出文件名
  237. $is_common = true;
  238. } else {
  239. $to_file_name = $path . $table_name . ".sql"; // 导出文件名
  240. }
  241. if (file_exists($to_file_name)) continue;
  242. if (in_array($table_name, $this->no_data_table_arr)) {
  243. continue;
  244. }
  245. $file_source = fopen($to_file_name, 'w+');
  246. $i = 1;
  247. $page_size = 1000;
  248. while (true) {
  249. $sql = "select * from " . $table_name;
  250. if (!in_array($table_name, ['qimall_common_sync_logs', 'qimall_sync_goods_logs'])) {
  251. if (strpos($table_field, 'mall_id') !== false) {
  252. $sql .= ' where mall_id in (' . $mall_id . ' ,0 )';
  253. }
  254. }
  255. $sql .= ' limit ' . ($i - 1) * $page_size . ',' . $page_size;
  256. $data_list = \Yii::$app->get($this->source_db)->createCommand($sql)->queryAll();//获取表数据
  257. if ($data_list) {
  258. if (isset($table_arr[$table_name])) {
  259. $field = $table_arr[$table_name]['field'];
  260. $class = $table_arr[$table_name]['class'];
  261. $select = $table_arr[$table_name]['select'];
  262. $fields = array_column($data_list, $field);
  263. $list = $class::lists(['where' => [[$select => $fields], ['mall_id' => $mall_id]], 'select' => $select]);
  264. $id_arr = array_column($list, $select);
  265. $this->formatData($data_list, $table_name, $file_source, true, $field, $id_arr, null, $table_field_type_arr, $is_common);
  266. } else {
  267. switch ($table_name) {
  268. case 'qimall_rbac_auth_item_child':
  269. $field = 'role_id';
  270. $role_ids = array_column($data_list, $field);
  271. $admin_role_list = AdminRole::lists(['where' => [['role_id' => $role_ids]], 'select' => 'role_id,admin_id']);
  272. $admin_ids = array_column($admin_role_list, 'admin_id');
  273. $admin_list = Admin::lists(['where' => [['id' => $admin_ids], ['mall_id' => $mall_id]], 'select' => 'id']);
  274. $admin_id_arr = array_column($admin_list, 'id');
  275. $role_id_arr = [];
  276. foreach ($admin_role_list as $v) {
  277. if (in_array($v['admin_id'], $admin_id_arr)) {
  278. $role_id_arr[] = $v['role_id'];
  279. }
  280. }
  281. $this->formatData($data_list, $table_name, $file_source, true, $field, $role_id_arr, null, $table_field_type_arr, $is_common);
  282. break;
  283. case 'qimall_commission_log_relation':
  284. case 'qimall_balance_log_relation':
  285. case 'qimall_score_log_relation':
  286. $field = 'log_id';
  287. $source_type_arr = [
  288. 'commission', 'commission_frozen', 'score', 'score_frozen', 'balance', 'balance_frozen'
  289. ];
  290. foreach ($source_type_arr as $source_type) {
  291. $commission_ids = [];
  292. foreach ($data_list as $v) {
  293. if ($v['source_type'] == $source_type) {
  294. $commission_ids[] = $v[$field];
  295. }
  296. }
  297. CapitalLog::$capitalType = $source_type;
  298. if (!empty($commission_ids)) {
  299. $list = CapitalLog::lists(['where' => [['id' => $commission_ids], ['mall_id' => $mall_id]], 'select' => 'id']);
  300. $id_arr = array_column($list, 'id');
  301. $this->formatData($data_list, $table_name, $file_source, true, $field, $id_arr, $source_type, $table_field_type_arr, $is_common);
  302. }
  303. }
  304. break;
  305. case 'qimall_addons_score_expansion_rate_task':
  306. $field = 'order_id';
  307. $source_type_arr = [
  308. '{{%order}}' => Order::class,
  309. '{{%addons_store_order}}' => StoreOrder::class
  310. ];
  311. foreach ($source_type_arr as $k => $class) {
  312. $order_ids = [];
  313. foreach ($data_list as $v) {
  314. if ($v[$field] == $k) {
  315. $order_ids[] = $v[$field];
  316. }
  317. }
  318. if (!empty($order_ids)) {
  319. $list = $class::lists(['where' => [['id' => $order_ids], ['mall_id' => $mall_id]], 'select' => 'id']);
  320. $id_arr = array_column($list, 'id');
  321. $this->formatData($data_list, $table_name, $file_source, true, $field, $id_arr, 'source_table', $table_field_type_arr, $is_common);
  322. }
  323. }
  324. break;
  325. case 'qimall_mall':
  326. foreach ($data_list as $row) {
  327. if ($row['id'] != $mall_id) {
  328. continue;
  329. }
  330. $insert_sql = 'INSERT INTO `' . $table_name . '` VALUES (';
  331. foreach ($row as $v) {
  332. $v = str_replace("\r\n", "", $v);
  333. $insert_sql .= "'" . $v . "', ";
  334. }
  335. $insert_sql = substr($insert_sql, 0, strlen($insert_sql) - 2);
  336. $insert_sql .= ");\r\n";
  337. fwrite($file_source, $insert_sql);
  338. }
  339. break;
  340. default:
  341. $this->formatData($data_list, $table_name, $file_source, false, '', [], null, $table_field_type_arr, $is_common);
  342. }
  343. }
  344. } else {
  345. break;
  346. }
  347. $i++;
  348. }
  349. fclose($file_source);
  350. echo "{$table_name} 表数据生成成功\r\n";
  351. }
  352. dd(11111);
  353. }
  354. }
  355. public function actionIndexByTable($table)
  356. {
  357. ini_set('memory_limit', 1024);
  358. $malll_list = Mall::lists(['where' => [['>', 'id', 0]]]);
  359. $common_path = $this->getCommonPath();
  360. if (!is_dir($common_path)) {
  361. mkdir($common_path, 777, true);
  362. }
  363. $table_arr = $this->getTableArr();
  364. foreach ($malll_list as $mall) {
  365. $mall_id = $mall['id'];
  366. $mall_id = 2;
  367. $path = $this->getMallPath($mall_id);
  368. if (!is_dir($path)) {
  369. mkdir($path, 777, true);
  370. }
  371. // $table_name = $table['Tables in qimall'];//mycat
  372. $table_name = $table;
  373. $temp_sql = 'show create table ' . $table_name;
  374. $table_field_type_arr = $this->getTableFieldType($table_name);
  375. $table_struct = \Yii::$app->get($this->source_db)->createCommand($temp_sql)->queryOne();//获取表名
  376. $table_field = $table_struct['Create Table'];
  377. $is_common = false;
  378. if (strpos($table_field, 'mall_id') === false) {
  379. $is_common = true;
  380. $to_file_name = $common_path . $table_name . ".sql"; // 导出文件名
  381. } else {
  382. $to_file_name = $path . $table_name . ".sql"; // 导出文件名
  383. }
  384. if (in_array($table_name, $this->no_data_table_arr)) {
  385. continue;
  386. }
  387. $file_source = fopen($to_file_name, 'w+');
  388. $i = 1;
  389. $page_size = 1000;
  390. while (true) {
  391. $sql = "select * from " . $table_name;
  392. if (!in_array($table_name, ['qimall_common_sync_logs', 'qimall_sync_goods_logs'])) {
  393. if (strpos($table_field, 'mall_id') !== false) {
  394. $sql .= ' where mall_id in (' . $mall_id . ' ,0 )';
  395. }
  396. }
  397. $sql .= ' limit ' . ($i - 1) * $page_size . ',' . $page_size;
  398. $data_list = \Yii::$app->get($this->source_db)->createCommand($sql)->queryAll();//获取表数据
  399. if ($data_list) {
  400. if (isset($table_arr[$table_name])) {
  401. $field = $table_arr[$table_name]['field'];
  402. $class = $table_arr[$table_name]['class'];
  403. $select = $table_arr[$table_name]['select'];
  404. $fields = array_column($data_list, $field);
  405. $list = $class::lists(['where' => [[$select => $fields], ['mall_id' => $mall_id]], 'select' => $select]);
  406. $id_arr = array_column($list, $select);
  407. $this->formatData($data_list, $table_name, $file_source, true, $field, $id_arr, null, $table_field_type_arr, $is_common);
  408. } else {
  409. switch ($table_name) {
  410. case 'qimall_rbac_auth_item_child':
  411. $field = 'role_id';
  412. $role_ids = array_column($data_list, $field);
  413. $admin_role_list = AdminRole::lists(['where' => [['role_id' => $role_ids]], 'select' => 'role_id,admin_id']);
  414. $admin_ids = array_column($admin_role_list, 'admin_id');
  415. $admin_list = Admin::lists(['where' => [['id' => $admin_ids], ['mall_id' => $mall_id]], 'select' => 'id']);
  416. $admin_id_arr = array_column($admin_list, 'id');
  417. $role_id_arr = [];
  418. foreach ($admin_role_list as $v) {
  419. if (in_array($v['admin_id'], $admin_id_arr)) {
  420. $role_id_arr[] = $v['role_id'];
  421. }
  422. }
  423. $this->formatData($data_list, $table_name, $file_source, true, $field, $role_id_arr, null, $table_field_type_arr, $is_common);
  424. break;
  425. case 'qimall_commission_log_relation':
  426. case 'qimall_balance_log_relation':
  427. case 'qimall_score_log_relation':
  428. $field = 'log_id';
  429. $source_type_arr = [
  430. 'commission', 'commission_frozen', 'score', 'score_frozen', 'balance', 'balance_frozen'
  431. ];
  432. foreach ($source_type_arr as $source_type) {
  433. $commission_ids = [];
  434. foreach ($data_list as $v) {
  435. if ($v['source_type'] == $source_type) {
  436. $commission_ids[] = $v[$field];
  437. }
  438. }
  439. CapitalLog::$capitalType = $source_type;
  440. if (!empty($commission_ids)) {
  441. $list = CapitalLog::lists(['where' => [['id' => $commission_ids], ['mall_id' => $mall_id]], 'select' => 'id']);
  442. $id_arr = array_column($list, 'id');
  443. $this->formatData($data_list, $table_name, $file_source, true, $field, $id_arr, $source_type, $table_field_type_arr, $is_common);
  444. }
  445. }
  446. break;
  447. case 'qimall_addons_score_expansion_rate_task':
  448. $field = 'order_id';
  449. $source_type_arr = [
  450. '{{%order}}' => Order::class,
  451. '{{%addons_store_order}}' => StoreOrder::class
  452. ];
  453. foreach ($source_type_arr as $k => $class) {
  454. $order_ids = [];
  455. foreach ($data_list as $v) {
  456. if ($v[$field] == $k) {
  457. $order_ids[] = $v[$field];
  458. }
  459. }
  460. if (!empty($order_ids)) {
  461. $list = $class::lists(['where' => [['id' => $order_ids], ['mall_id' => $mall_id]], 'select' => 'id']);
  462. $id_arr = array_column($list, 'id');
  463. $this->formatData($data_list, $table_name, $file_source, true, $field, $id_arr, 'source_table', $table_field_type_arr, $is_common);
  464. }
  465. }
  466. break;
  467. case 'qimall_mall':
  468. foreach ($data_list as $row) {
  469. if ($row['id'] != $mall_id) {
  470. continue;
  471. }
  472. $insert_sql = 'INSERT INTO `' . $table_name . '` VALUES (';
  473. foreach ($row as $v) {
  474. $v = str_replace("\r\n", "", $v);
  475. $insert_sql .= "'" . $v . "', ";
  476. }
  477. $insert_sql = substr($insert_sql, 0, strlen($insert_sql) - 2);
  478. $insert_sql .= ");\r\n";
  479. fwrite($file_source, $insert_sql);
  480. }
  481. break;
  482. default:
  483. $this->formatData($data_list, $table_name, $file_source, false, '', [], null, $table_field_type_arr, $is_common);
  484. }
  485. }
  486. } else {
  487. break;
  488. }
  489. $i++;
  490. }
  491. fclose($file_source);
  492. echo "{$table_name} 表数据生成成功\r\n";
  493. dd(222);
  494. }
  495. }
  496. public function actionIn($mall_id)
  497. {
  498. ini_set('memory_limit', 1024);
  499. if (!isset($mall_id)) return '请输入原商城id';
  500. $common_path = $this->getCommonPath();
  501. $this->inData($common_path);
  502. $mall_path = $this->getMallPath($mall_id);
  503. $this->inData($mall_path);
  504. }
  505. public function actionStruct()
  506. {
  507. $struct_path = $this->getStructPath();
  508. if (!is_dir($struct_path)) {
  509. mkdir($struct_path, 777, true);
  510. }
  511. $sqls = "show tables";
  512. $table_list = \Yii::$app->get($this->source_db)->createCommand($sqls)->queryAll();//获取表名
  513. foreach ($table_list as $table) {
  514. $table_name = $table['Tables_in_qimall'];
  515. $temp_sql = 'show create table ' . $table_name;
  516. $table_struct = \Yii::$app->get($this->source_db)->createCommand($temp_sql)->queryOne();//获取表名
  517. $table_field = $table_struct['Create Table'];
  518. $to_file_name = $struct_path . $table_name . ".sql"; // 导出文件名
  519. if (file_exists($to_file_name)) continue;
  520. $file_source = fopen($to_file_name, 'w+');
  521. $info = "-- ----------------------------\r\n";
  522. $info .= "-- Table structure for `" . $table_name . "`\r\n";
  523. $info .= "-- ----------------------------\r\n";
  524. $info .= "DROP TABLE IF EXISTS `" . $table_name . "`;\r\n";
  525. $sqlStr = $info . $table_field . ";\r\n\r\n";
  526. fwrite($file_source, $sqlStr);
  527. echo "{$table_name} 表结构生成成功\r\n";
  528. }
  529. }
  530. public function actionInStruct()
  531. {
  532. $struct_path = dirname(dirname(__DIR__)) . '\sql\/struct\/';
  533. $filename = scandir($struct_path);
  534. foreach ($filename as $k => $v) {
  535. if ($v == '.' || $v == '..') continue;
  536. $f = $struct_path . $v;
  537. $redis_key = 'export_in_struct:' . md5($f);
  538. // if (\Yii::$app->redis->get($redis_key)) continue;
  539. $str = file_get_contents($f);
  540. \Yii::$app->get($this->input_db)->createCommand($str)->execute();
  541. echo $v . '执行成功' . $k . "\r\n";
  542. \Yii::$app->redis->set($redis_key, 1, 'EX', 10 * 3600);
  543. }
  544. }
  545. //单条
  546. public function formatData($data_list, $table_name, $file_source, $is_bool, $field, $ids, $source_type = null, $table_field_type_arr = [])
  547. {
  548. foreach ($data_list as $row) {
  549. if ($is_bool) {
  550. if ((isset($source_type) && isset($row[$source_type]) && $source_type != $row[$source_type]) || !in_array($row[$field], $ids)) {
  551. continue;
  552. }
  553. }
  554. $insert_sql = 'INSERT INTO `' . $table_name . '` VALUES (';
  555. foreach ($row as $field => $v) {
  556. $v = str_replace("\r\n", "", $v);
  557. $v = str_replace("'", "\'", $v);
  558. if (isset($table_field_type_arr[$field])) {
  559. switch ($table_field_type_arr[$field]) {
  560. case "json":
  561. if (empty($v)) {
  562. $v = json_encode($v);
  563. } else {
  564. if (strpos($v, '\"') !== false) {
  565. $v = str_replace('\"', '\\/"', $v);
  566. } else {
  567. $v = str_replace('\\', "\\\\\\\\", $v);
  568. }
  569. }
  570. break;
  571. case "float":
  572. case "decimal":
  573. case "int":
  574. case "tinyint":
  575. if (empty($v)) {
  576. $v = 0;
  577. }
  578. break;
  579. }
  580. }
  581. // if (in_array($table_name, $this->table_json) && in_array($field, $this->table_json_field)) {
  582. // if (empty($v)) {
  583. // $v = json_encode($v);
  584. // } else {
  585. // $v = str_replace('\\', "\\\\\\\\", $v);
  586. // }
  587. // }
  588. // if (in_array($table_name, $this->empty_str_to_0_table) && in_array($field, $this->empty_str_to_0)) {
  589. // if (empty($v)) {
  590. // $v = 0;
  591. // }
  592. // }
  593. $insert_sql .= "'" . $v . "', ";
  594. }
  595. $insert_sql = substr($insert_sql, 0, strlen($insert_sql) - 2);
  596. $insert_sql .= ");\r\n";
  597. fwrite($file_source, $insert_sql);
  598. }
  599. }
  600. public function formatDataX($data_list, $is_bool, $field, $ids, $source_type = null)
  601. {
  602. $list = [];
  603. foreach ($data_list as $row) {
  604. if ($is_bool) {
  605. if ((isset($source_type) && isset($row[$source_type]) && $source_type != $row[$source_type]) || !in_array($row[$field], $ids)) {
  606. continue;
  607. }
  608. }
  609. $list[] = $row;
  610. }
  611. return $list;
  612. }
  613. //批量
  614. // public function formatDataBatch($data_list, $table_name, $file_source, $is_bool, $field, $ids, $source_type = null, $table_field_type_arr = [],$is_common=true)
  615. // {
  616. // if($is_common){
  617. // foreach ($data_list as $row) {
  618. // if ($is_bool) {
  619. // if ((isset($source_type) && isset($row[$source_type]) && $source_type != $row[$source_type]) || !in_array($row[$field], $ids)) {
  620. // continue;
  621. // }
  622. // }
  623. // $insert_sql = 'INSERT INTO `' . $table_name . '` VALUES (';
  624. // foreach ($row as $field => $v) {
  625. // $v = str_replace("\r\n", "", $v);
  626. // $v = str_replace("'", "\'", $v);
  627. // if (isset($table_field_type_arr[$field])) {
  628. // switch ($table_field_type_arr[$field]) {
  629. // case "json":
  630. // if (empty($v)) {
  631. // $v = json_encode($v);
  632. // } else {
  633. // $v = str_replace('\\', "\\\\\\\\", $v);
  634. // }
  635. // break;
  636. // case "float":
  637. // case "decimal":
  638. // case "int":
  639. // case "tinyint":
  640. // if (empty($v)) {
  641. // $v = 0;
  642. // }
  643. // break;
  644. // }
  645. // }
  646. //
  647. //
  648. //// if (in_array($table_name, $this->table_json) && in_array($field, $this->table_json_field)) {
  649. //// if (empty($v)) {
  650. //// $v = json_encode($v);
  651. //// } else {
  652. //// $v = str_replace('\\', "\\\\\\\\", $v);
  653. //// }
  654. //// }
  655. //// if (in_array($table_name, $this->empty_str_to_0_table) && in_array($field, $this->empty_str_to_0)) {
  656. //// if (empty($v)) {
  657. //// $v = 0;
  658. //// }
  659. //// }
  660. // $insert_sql .= "'" . $v . "', ";
  661. // }
  662. // $insert_sql = substr($insert_sql, 0, strlen($insert_sql) - 2);
  663. // $insert_sql .= ");\r\n";
  664. // fwrite($file_source, $insert_sql);
  665. //
  666. // }
  667. //
  668. // }else{
  669. // if ($data_list) {
  670. // $insert_sql = 'INSERT INTO `' . $table_name . '` VALUES (';
  671. // foreach ($data_list as $row) {
  672. // if ($is_bool) {
  673. // if ((isset($source_type) && isset($row[$source_type]) && $source_type != $row[$source_type]) || !in_array($row[$field], $ids)) {
  674. // continue;
  675. // }
  676. // }
  677. // foreach ($row as $field => $v) {
  678. // $v = str_replace("\r\n", "", $v);
  679. // $v = str_replace("'", "\'", $v);
  680. // if (isset($table_field_type_arr[$field])) {
  681. // switch ($table_field_type_arr[$field]) {
  682. // case "json":
  683. // if (empty($v)) {
  684. // $v = json_encode($v);
  685. // } else {
  686. // $v = str_replace('\\', "\\\\\\\\", $v);
  687. // }
  688. // break;
  689. // case "float":
  690. // case "decimal":
  691. // case "int":
  692. // case "tinyint":
  693. // if (empty($v)) {
  694. // $v = 0;
  695. // }
  696. // break;
  697. // }
  698. // }
  699. //
  700. //
  701. // $insert_sql .= "'" . $v . "', ";
  702. // }
  703. // $insert_sql = substr($insert_sql, 0, strlen($insert_sql) - 2);
  704. // $insert_sql .= "),(";
  705. // }
  706. // if ($insert_sql != ('INSERT INTO `' . $table_name . '` VALUES (')) {
  707. // $insert_sql = trim($insert_sql, ',(');
  708. // $insert_sql .= ";\r\n";
  709. // fwrite($file_source, $insert_sql);
  710. // }
  711. // }
  712. //
  713. // }
  714. //
  715. // }
  716. public function getNoMallIdTable()
  717. {
  718. $common_path = $this->getCommonPath();
  719. $filename = scandir($common_path);
  720. $table_arr = [];
  721. foreach ($filename as $k => $v) {
  722. if ($v == '.' || $v == '..') continue;
  723. $table_arr[] = substr($v, 0, strpos($v, '.'));
  724. }
  725. return $table_arr;
  726. }
  727. protected function inData($path)
  728. {
  729. $filename = scandir($path);
  730. foreach ($filename as $k => $v) {
  731. if ($v == '.' || $v == '..') continue;
  732. $f = $path . $v;
  733. $redis_key = 'export_in:' . md5($f);
  734. if (\Yii::$app->redis->get($redis_key)) continue;
  735. $table_arr = explode('.', $v);
  736. $table = $table_arr[0];
  737. $sql = "select * from information_schema.TABLES where TABLE_NAME = '{$table}';";
  738. $res = \Yii::$app->get($this->input_db)->createCommand($sql)->execute();
  739. if (empty($res)) {
  740. echo $table . '表不存在' . "\r\n";
  741. continue;
  742. }
  743. if (in_array($table_arr[0], $this->te_read_table)) continue;
  744. if (in_array($table_arr[0], $this->all_read_table)) {
  745. $str = file_get_contents($f);
  746. \Yii::$app->get($this->input_db)->createCommand($str)->execute();
  747. } else {
  748. $source = fopen($f, 'r+');
  749. while (true) {
  750. $r = fgets($source);
  751. if ($r === false) break;
  752. \Yii::$app->get($this->input_db)->createCommand($r)->execute();
  753. var_dump($r);
  754. }
  755. }
  756. echo $table_arr[0] . '导入成功' . $k . "\r\n";
  757. \Yii::$app->redis->set($redis_key, 1, 'EX', 10 * 3600);
  758. }
  759. }
  760. public function getTableFieldType($table)
  761. {
  762. $sqls = "DESCRIBE {$table}";
  763. $list = \Yii::$app->get($this->source_db)->createCommand($sqls)->queryAll();//获取表名
  764. $arr = [];
  765. foreach ($list as $item) {
  766. if (substr($item['Type'], 0, strpos($item['Type'], '('))) {
  767. $arr[$item['Field']] = substr($item['Type'], 0, strpos($item['Type'], '('));
  768. }
  769. if ($item['Type'] == 'json') {
  770. $arr[$item['Field']] = $item['Type'];
  771. }
  772. if ($item['Type'] == 'float') {
  773. $arr[$item['Field']] = $item['Type'];
  774. }
  775. }
  776. return $arr;
  777. }
  778. public function actionInTableList($mall_id)
  779. {
  780. if (!isset($mall_id)) return '缺失商城id';
  781. // ini_set('memory_limit', 1024);
  782. $sqls = "show tables";
  783. $table_list = \Yii::$app->get($this->source_db)->createCommand($sqls)->queryAll();//获取表名
  784. $start_time = time();
  785. $error_table = [];
  786. foreach ($table_list as $k => $table) {
  787. // $table_name = $table['Tables in qimall'];//mycat
  788. $start_time_temp = time();
  789. $table_name = $table['Tables_in_qimall'];
  790. echo "开始导入表{$table_name},表编号" . ($k + 1) . "\r\n";
  791. $res = $this->InTable($table_name, $mall_id);
  792. $end_time_temp = time();
  793. if ($res == true) {
  794. echo $table_name . '数据导入成功,耗时' . ($end_time_temp - $start_time_temp) . "秒\r\n";
  795. } else {
  796. $error_table[] = $table_name;
  797. }
  798. }
  799. $end_time = time();
  800. echo "耗时" . ($end_time - $start_time) . "秒\r\n";
  801. MallAddons::getInstallAddonsByMysql($mall_id);
  802. }
  803. public function actionInTable($table, $mall_id)
  804. {
  805. ini_set('memory_limit', 1024);
  806. $res = $this->InTable($table, $mall_id);
  807. var_dump($res);
  808. }
  809. public function InTable($table, $mall_id)
  810. {
  811. try {
  812. $redis_key = 'export_in:' . $table . $mall_id;
  813. if (\Yii::$app->redis->get($redis_key)) return true;
  814. $sql = "select * from information_schema.TABLES where TABLE_NAME = '{$table}';";
  815. $res = \Yii::$app->get($this->input_db)->createCommand($sql)->execute();
  816. if (empty($res)) {
  817. echo $table . '表不存在' . "\r\n";
  818. return false;
  819. }
  820. $table_name = $table;
  821. $table_field_type_arr = $this->getTableFieldType($table_name);
  822. if (in_array($table_name, $this->no_data_table_arr)) {
  823. return false;
  824. }
  825. $i = 1;
  826. $page_size = 1000;
  827. while (true) {
  828. if ($table_name == 'qimall_order_action') {
  829. $sql = "select id from qimall_order where mall_id = {$mall_id} ";
  830. $sql .= ' order by id asc limit ' . ($i - 1) * $page_size . ',' . $page_size;
  831. $data_list = \Yii::$app->get($this->source_db)->createCommand($sql)->queryAll();//获取表数据
  832. if ($data_list) {
  833. $order_id_arr = array_column($data_list, 'id');
  834. $order_ids_str = join(',', $order_id_arr);
  835. $sqls = "select * from qimall_order_action where order_id in ({$order_ids_str})";
  836. $order_action_list = \Yii::$app->get($this->source_db)->createCommand($sqls)->queryAll();//获取表数据
  837. $fields = [];
  838. $insert = [];
  839. if (!empty($order_action_list)) {
  840. foreach ($order_action_list as $item) {
  841. $temp_field = [];
  842. $temp_value = [];
  843. foreach ($item as $field => $value) {
  844. $temp_field[] = $field;
  845. if (isset($table_field_type_arr[$field]) && $table_field_type_arr[$field] == 'json') {
  846. $temp_value[] = json_decode($value, true);
  847. } else {
  848. $temp_value[] = $value;
  849. }
  850. }
  851. $fields = $temp_field;
  852. $insert[] = $temp_value;
  853. }
  854. \Yii::$app->get($this->input_db)->createCommand()->batchInsert($table_name, $fields, $insert)->execute();
  855. }
  856. } else {
  857. break;
  858. }
  859. } else {
  860. $sql = "select * from " . $table_name;
  861. if ($table_name == 'qimall_mall') {
  862. $sql .= ' where id in (' . $mall_id . ' ,0 )';
  863. } elseif (!in_array($table_name, ['qimall_common_sync_logs', 'qimall_sync_goods_logs'])) {
  864. if (isset($table_field_type_arr['mall_id'])) {
  865. $sql .= ' where mall_id in (' . $mall_id . ' ,0 )';
  866. }
  867. }
  868. if (in_array($table_name, ['qimall_migration'])) {
  869. $sql .= ' limit ' . ($i - 1) * $page_size . ',' . $page_size;
  870. } else {
  871. $sql .= ' order by id asc limit ' . ($i - 1) * $page_size . ',' . $page_size;
  872. }
  873. $data_list = \Yii::$app->get($this->source_db)->createCommand($sql)->queryAll();//获取表数据
  874. $fields = [];
  875. $insert = [];
  876. if ($data_list) {
  877. $table_arr = $this->getTableArr();
  878. if (isset($table_arr[$table_name])) {
  879. $field = $table_arr[$table_name]['field'];
  880. $class = $table_arr[$table_name]['class'];
  881. $select = $table_arr[$table_name]['select'];
  882. $fields = array_column($data_list, $field);
  883. $str_in = join(',', $fields);
  884. $t = $this->getTableNameByClass($class);
  885. $sql = "select {$select} from {$t} where mall_id={$mall_id} and {$select} in ({$str_in})";
  886. $list = \Yii::$app->get($this->source_db)->createCommand($sql)->queryAll();//获取表数据
  887. $id_arr = array_column($list, $select);
  888. $data_list = $this->formatDataX($data_list, true, $field, $id_arr, null);
  889. } else {
  890. switch ($table_name) {
  891. case 'qimall_rbac_auth_item_child':
  892. $field = 'role_id';
  893. $role_ids = array_column($data_list, $field);
  894. $str_in = join(',', $role_ids);
  895. $t = $this->getTableNameByClass(AdminRole::class);
  896. $sql = "select role_id,admin_id from {$t} where role_id in ({$str_in})";
  897. $admin_role_list = \Yii::$app->get($this->source_db)->createCommand($sql)->queryAll();//获取表数据
  898. $admin_ids = array_column($admin_role_list, 'admin_id');
  899. $str_in = join(',', $admin_ids);
  900. $t = $this->getTableNameByClass(Admin::class);
  901. $sql = "select id from {$t} where mall_id = {$mall_id} and id in ({$str_in})";
  902. $admin_list = \Yii::$app->get($this->source_db)->createCommand($sql)->queryAll();//获取表数据
  903. $admin_id_arr = array_column($admin_list, 'id');
  904. $role_id_arr = [];
  905. foreach ($admin_role_list as $v) {
  906. if (in_array($v['admin_id'], $admin_id_arr)) {
  907. $role_id_arr[] = $v['role_id'];
  908. }
  909. }
  910. $data_list = $this->formatDataX($data_list, true, $field, $role_id_arr, null);
  911. break;
  912. case 'qimall_commission_log_relation':
  913. case 'qimall_balance_log_relation':
  914. case 'qimall_score_log_relation':
  915. $field = 'log_id';
  916. $source_type_arr = [
  917. 'commission', 'commission_frozen', 'score', 'score_frozen', 'balance', 'balance_frozen'
  918. ];
  919. foreach ($source_type_arr as $source_type) {
  920. $commission_ids = [];
  921. foreach ($data_list as $v) {
  922. if ($v['source_type'] == $source_type) {
  923. $commission_ids[] = $v[$field];
  924. }
  925. }
  926. CapitalLog::$capitalType = $source_type;
  927. if (!empty($commission_ids)) {
  928. $str_in = join(',', $commission_ids);
  929. $t = $this->getTableNameByClass(CapitalLog::class);
  930. $sql = "select id from {$t} where mall_id = {$mall_id} and id in ({$str_in})";
  931. $list = \Yii::$app->get($this->source_db)->createCommand($sql)->queryAll();//获取表数据
  932. $id_arr = array_column($list, 'id');
  933. $data_list = $this->formatDataX($data_list, true, $field, $id_arr, $source_type);
  934. }
  935. }
  936. break;
  937. case 'qimall_addons_score_expansion_rate_task':
  938. $field = 'order_id';
  939. $source_type_arr = [
  940. '{{%order}}' => Order::class,
  941. '{{%addons_store_order}}' => StoreOrder::class
  942. ];
  943. foreach ($source_type_arr as $k => $class) {
  944. $order_ids = [];
  945. foreach ($data_list as $v) {
  946. if ($v[$field] == $k) {
  947. $order_ids[] = $v[$field];
  948. }
  949. }
  950. if (!empty($order_ids)) {
  951. $str_in = join(',', $order_ids);
  952. $t = $this->getTableNameByClass($class);
  953. $sql = "select id from {$t} where mall_id = {$mall_id} and id in ({$str_in})";
  954. $list = \Yii::$app->get($this->source_db)->createCommand($sql)->queryAll();//获取表数据
  955. $id_arr = array_column($list, 'id');
  956. $data_list = $this->formatDataX($data_list, true, $field, $id_arr, 'source_table');
  957. }
  958. }
  959. break;
  960. default:
  961. }
  962. }
  963. if (!empty($data_list)) {
  964. foreach ($data_list as $item) {
  965. $temp_field = [];
  966. $temp_value = [];
  967. foreach ($item as $field => $value) {
  968. $temp_field[] = $field;
  969. if (isset($table_field_type_arr[$field]) && $table_field_type_arr[$field] == 'json') {
  970. $temp_value[] = json_decode($value, true);
  971. } else {
  972. $temp_value[] = $value;
  973. }
  974. }
  975. $fields = $temp_field;
  976. $insert[] = $temp_value;
  977. }
  978. \Yii::$app->get($this->input_db)->createCommand()->batchInsert($table_name, $fields, $insert)->execute();
  979. }
  980. } else {
  981. break;
  982. }
  983. }
  984. $i++;
  985. }
  986. \Yii::$app->redis->set($redis_key, 1, 'EX', 10 * 3600);
  987. return true;
  988. } catch (\Exception $e) {
  989. var_dump($e->getMessage());
  990. return false;
  991. }
  992. }
  993. private function getTableNameByClass($class)
  994. {
  995. $t = $class::tableName();
  996. $t = str_replace('{{%', 'qimall_', $t);
  997. return str_replace('}}', '', $t);
  998. }
  999. }