run_zy_iap_match_7day.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_zy} 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/zy_mf/zy_mf_search_book').new_search_name(element.product_name)
  29. if(iaa_book_info!=null){
  30. iaa_book_info = await require('../src/api/zy_mf/zy_mf_search_book').new_search_id(iaa_book_info.book_id)
  31. if(iaa_book_info!=null){
  32. let book_info = await video_product_controllers.getProductData({book_id:iaa_book_info.book_id})
  33. if(!book_info.success){
  34. //创建一个免费书籍
  35. await video_product_controllers.createProductData({
  36. book_platform:config.platform_zymf,
  37. product_name:iaa_book_info.book_name,
  38. genre:iaa_book_info.genre,
  39. product_id:iaa_book_info.book_id,
  40. words:iaa_book_info.words,
  41. totalChapterNum:iaa_book_info.totalChapterNum,
  42. publish_time:iaa_book_info.publish_time,
  43. default_pay_section:iaa_book_info.default_pay_section,
  44. default_price:iaa_book_info.default_price,
  45. fee_unit:iaa_book_info.fee_unit,
  46. product_parent_id:element.product_id,
  47. match_book:element.product_id,
  48. })
  49. }else{
  50. await video_product_controllers.updateData({id:book_info.data.id},{
  51. match_book:element.product_id
  52. })
  53. }
  54. await connection.execute(
  55. `UPDATE video_product
  56. SET match_book = ? WHERE id = ?`,
  57. [iaa_book_info.book_id, element.id]
  58. );
  59. }
  60. }
  61. }
  62. }catch(e){
  63. if(e==0){
  64. right_status = false
  65. }
  66. console.error("processTask error:",e)
  67. } finally{
  68. if(connection!=null){
  69. connection.end()
  70. }
  71. if(right_status){
  72. global.setTimeout(processTask, 2000);
  73. }
  74. }
  75. }
  76. CMD.init = async function(){
  77. redis_help.connect((results)=>{
  78. if(results){
  79. const job = schedule.scheduleJob('0 09,21 * * *', async function() {
  80. await processTask();
  81. });
  82. }
  83. })
  84. }
  85. CMD.init()