run_qm_iap_match_7day.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_qimao} 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/qm_mf/qm_mf_search_book').根据七猫付费ID查询免费书籍(element.product_id,true)
  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_qmmf,
  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. publish_time:iaa_book_info.publish_time,
  42. author:iaa_book_info.author,
  43. gender:iaa_book_info.gender
  44. })
  45. }else{
  46. await video_product_controllers.updateData({id:book_info.data.id},{
  47. match_book:element.product_id
  48. })
  49. }
  50. await connection.execute(
  51. `UPDATE video_product
  52. SET match_book = ? WHERE id = ?`,
  53. [iaa_book_info.book_id, element.id]
  54. );
  55. }
  56. }
  57. }catch(e){
  58. if(e==0){
  59. right_status = false
  60. }
  61. console.error("processTask error:",e)
  62. } finally{
  63. if(connection!=null){
  64. connection.end()
  65. }
  66. if(right_status){
  67. global.setTimeout(processTask, 2000);
  68. }
  69. }
  70. }
  71. CMD.init = async function(){
  72. redis_help.connect((results)=>{
  73. if(results){
  74. const job = schedule.scheduleJob('0 04,16 * * *', async function() {
  75. await processTask();
  76. });
  77. }
  78. })
  79. }
  80. CMD.init()