run_fq_iap_match_7day.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 7 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 = ?,
  50. WHERE id = ?`,
  51. [iaa_book_info.book_id, element.id]
  52. );
  53. }
  54. }
  55. }catch(e){
  56. if(e==0){
  57. right_status = false
  58. }
  59. console.error("processTask error:",e)
  60. } finally{
  61. if(connection!=null){
  62. connection.end()
  63. }
  64. if(right_status){
  65. global.setTimeout(processTask, 2000);
  66. }
  67. }
  68. }
  69. CMD.init = async function(){
  70. redis_help.connect((results)=>{
  71. if(results){
  72. const job = schedule.scheduleJob('0 07,19 * * *', async function() {
  73. await processTask();
  74. });
  75. }
  76. })
  77. }
  78. CMD.init()