run_fq_iap_match_7day.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 publish_time >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND publish_time <= CURDATE()
  20. AND book_platform = ${config.platform_fanqie} 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 iaa_book_info = await require('../src/api/fq/fq_search_book').根据番茄付费ID查询免费书籍(element.product_id)
  29. if(iaa_book_info!=null){
  30. let book_info = await video_product_controllers.getProductData({book_id:iaa_book_info.book_id})
  31. if(!book_info.success){
  32. //创建一个免费书籍
  33. await video_product_controllers.createProductData({
  34. book_platform:config.platform_fanqiemf,
  35. product_name:iaa_book_info.book_name,
  36. genre:iaa_book_info.genre,
  37. product_id:iaa_book_info.book_id,
  38. words:iaa_book_info.words,
  39. match_book:element.product_id,
  40. product_parent_id:element.product_id
  41. })
  42. }else{
  43. await video_product_controllers.updateData({id:book_info.data.id},{
  44. match_book:element.product_id
  45. })
  46. }
  47. await connection.execute(
  48. `UPDATE video_product
  49. SET match_book = '${iaa_book_info.book_id}'
  50. WHERE id = ${element.id}`
  51. );
  52. }
  53. }
  54. }catch(e){
  55. if(e==0){
  56. right_status = false
  57. }
  58. console.error("processTask error:",e)
  59. } finally{
  60. if(connection!=null){
  61. connection.end()
  62. }
  63. if(right_status){
  64. global.setTimeout(processTask, 2000);
  65. }
  66. }
  67. }
  68. CMD.init = async function(){
  69. redis_help.connect((results)=>{
  70. if(results){
  71. const job = schedule.scheduleJob('0 07,19 * * *', async function() {
  72. await processTask();
  73. });
  74. }
  75. })
  76. }
  77. CMD.init()