qm_mf_book_async.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. const config = require("./etc/config.json");
  2. const helper = require("./src/helper");
  3. const redis_help = require('./src/use_redis');
  4. const mysql = require('mysql2/promise');
  5. const tools = require("./tools");
  6. const dbConfig = config.isDebug?config.debug_mysql:config.release_mysql
  7. const CMD = {}
  8. async function processTask(){
  9. let connection = null
  10. let status = 0
  11. try{
  12. const today = helper.getLocalDate();
  13. let date = today.replace(/-/g, '');
  14. let table_name = `qm_iap_lib`;
  15. // 创建数据库连接
  16. connection = await mysql.createConnection({
  17. ...dbConfig,
  18. multipleStatements: true
  19. });
  20. // 检查表是否存在
  21. const [tableCheck] = await connection.execute(
  22. `SELECT TABLE_NAME
  23. FROM information_schema.tables
  24. WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?`,
  25. [dbConfig.database, table_name]
  26. );
  27. if (tableCheck.length === 0) {
  28. // 表不存在
  29. console.error(`表 ${table_name} 不存在`);
  30. throw new Error(`表 ${table_name} 不存在`);
  31. } else {
  32. const [rows] = await connection.execute(
  33. `SELECT * FROM ${table_name} WHERE is_have_iaa = -1 LIMIT 100`
  34. );
  35. if(rows.length<=0){
  36. throw -1
  37. }
  38. for (let index = 0; index < rows.length; index++) {
  39. const element = rows[index];
  40. let iaa_book_info = await require('./src/api/qm_mf/qm_mf_search_book').search_name(element.book_author,element.book_origin_name)
  41. if(iaa_book_info!=null){
  42. await connection.execute(
  43. `UPDATE ${table_name} SET is_have_iaa = 1 , iaa_book_id = ${iaa_book_info.book_id} WHERE book_id = ${element.book_id}`
  44. );
  45. }else{
  46. await connection.execute(
  47. `UPDATE ${table_name} SET is_have_iaa = 0 WHERE book_id = ${element.book_id}`
  48. );
  49. }
  50. }
  51. }
  52. }catch(e){
  53. if(e==-1){
  54. status = -1
  55. }
  56. console.error("processTask error:",e)
  57. } finally{
  58. if(status!=-1){
  59. global.setTimeout(processTask, 1000);
  60. }else{
  61. console.log("完成所有定义!")
  62. }
  63. if(connection!=null){
  64. await connection.end();
  65. }
  66. }
  67. }
  68. CMD.init = async function(){
  69. redis_help.connect((results)=>{
  70. if(results){
  71. processTask();
  72. }
  73. })
  74. }
  75. CMD.init()