run_qm_iaa_match_7day.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //同步七猫免费的匹配书籍
  2. const config = require("../etc/config.json");
  3. const mysql = require('mysql2/promise');
  4. const taskdbConfig = config.isDebug?config.debug_task_mysql:config.release_task_mysql
  5. const redis_help = require('../src/use_redis');
  6. const video_product_controllers = require('../src/data_manager/Controllers/video_product_controllers');
  7. const helper = require("../src/helper");
  8. const schedule = require('node-schedule');
  9. const CMD = {}
  10. async function processTask(){
  11. let right_status = true
  12. let connection = null
  13. try{
  14. connection = await mysql.createConnection({
  15. ...taskdbConfig,
  16. multipleStatements: true
  17. });
  18. const [records] = await connection.execute(
  19. `SELECT * FROM video_product WHERE create_at >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND create_at <= CURDATE()
  20. AND book_platform = ${config.platform_qmmf} AND match_book = "NONE" LIMIT 1000 ;`
  21. );
  22. if(records.length<=0){
  23. console.log("完成所有七猫免费的匹配")
  24. throw 0
  25. }
  26. for (let index = 0; index < records.length; index++) {
  27. const element = records[index];
  28. let iap_book_info = await require('../src/api/qm/qm_search_book').根据七猫免费D查询付费书籍(element.product_id,true)
  29. if(iap_book_info!=null){
  30. let book_info = await video_product_controllers.getProductData({book_id:iap_book_info.book_id})
  31. if(!book_info.success){
  32. //创建一个付费书籍
  33. await video_product_controllers.createProductData({
  34. book_platform:config.platform_qimao,
  35. product_name:iap_book_info.book_name,
  36. genre:iap_book_info.genre,
  37. product_id:iap_book_info.book_id,
  38. words:iap_book_info.words,
  39. match_book:element.product_id,
  40. publish_time:iap_book_info.publish_time,
  41. author:iap_book_info.author,
  42. gender:iap_book_info.gender
  43. })
  44. }else{
  45. await video_product_controllers.updateData({id:book_info.data.id},{
  46. match_book:element.product_id
  47. })
  48. }
  49. await connection.execute(
  50. `UPDATE video_product
  51. SET match_book = ?,
  52. product_parent_id = ?
  53. WHERE id = ?`,
  54. [iap_book_info.book_id, iap_book_info.book_id, element.id]
  55. );
  56. }
  57. }
  58. }catch(e){
  59. if(e==0){
  60. right_status = false
  61. }
  62. console.error("processTask error:",e)
  63. } finally{
  64. if(connection!=null){
  65. connection.end()
  66. }
  67. if(right_status){
  68. global.setTimeout(processTask, 2000);
  69. }
  70. }
  71. }
  72. CMD.init = async function(){
  73. redis_help.connect((results)=>{
  74. if(results){
  75. const job = schedule.scheduleJob('0 06,18 * * *', async function() {
  76. await processTask();
  77. });
  78. }
  79. })
  80. }
  81. CMD.init()