find_have_350_book.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //找到带有300的数据,并且创建新书籍
  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_applet_product_controllers = require('../src/data_manager/Controllers/video_applet_product_controllers');
  7. const video_product_controllers = require('../src/data_manager/Controllers/video_product_controllers');
  8. const helper = require("../src/helper");
  9. const schedule = require('node-schedule');
  10. const tools = require("../tools");
  11. const CMD = {}
  12. async function processTask(){
  13. let right_status = true
  14. let connection = null
  15. try{
  16. connection = await mysql.createConnection({
  17. ...taskdbConfig,
  18. multipleStatements: true
  19. });
  20. let sql = `SELECT * FROM video_product WHERE book_platform = ${config.platform_qmmf} AND status != 2 AND is_check = 0 LIMIT 500`
  21. console.log("sql:",sql)
  22. const [rows] = await connection.execute(
  23. sql
  24. );
  25. if(rows.length<=0){
  26. throw 0
  27. }
  28. for (let index = 0; index < rows.length; index++) {
  29. let video_product_info = rows[index]
  30. let data = await require('../src/api/qm_mf/qm_mf_search_book').search_350_name(video_product_info.product_name+"(350)")
  31. if(data!=null&&data!=""){
  32. await connection.execute(
  33. ` UPDATE video_product SET status = 2 , is_check = 1 WHERE id = ${video_product_info.id} `
  34. );
  35. let book_data = {
  36. book_name:data.book_name,
  37. book_id:data.book_id,
  38. genre:data.genre,
  39. words:data.words,
  40. product_parent_id:video_product_info.product_parent_id
  41. }
  42. let result = await video_product_controllers.getProductData({book_id:book_data.book_id})
  43. if(result.success){
  44. await connection.execute(
  45. ` UPDATE video_product SET is_check = 3 WHERE id = ${result.data.id} `
  46. );
  47. }else{
  48. //1创建这本书
  49. await CMD.insert_product(book_data)
  50. //2在创建推广链接
  51. await CMD.update_filter_data(book_data)
  52. }
  53. }else{
  54. await connection.execute(
  55. ` UPDATE video_product SET is_check = 1 WHERE id = ${video_product_info.id} `
  56. );
  57. }
  58. }
  59. }catch(e){
  60. if(e==0){
  61. right_status = false
  62. }
  63. console.error("processTask error:",e)
  64. } finally{
  65. if(connection!=null){
  66. connection.end()
  67. }
  68. if(right_status==true){
  69. global.setTimeout(processTask, 1000);
  70. }
  71. }
  72. }
  73. CMD.insert_product = async function(data){
  74. console.log("创建这本书",data)
  75. return await video_product_controllers.createProductData({
  76. book_platform:config.platform_qmmf,
  77. product_name:data.book_name,
  78. genre:data.genre,
  79. product_id:data.book_id,
  80. product_parent_id:data.product_parent_id,
  81. words:data.words
  82. })
  83. }
  84. CMD.getAppInfoByPlatformId = function(AppConfig,tg_platform_id){
  85. let temp = []
  86. for (let index = 0; index < AppConfig.length; index++) {
  87. const element = AppConfig[index];
  88. if(element.tg_platform_id==tg_platform_id){
  89. temp.push(element)
  90. }
  91. }
  92. return temp
  93. }
  94. CMD.isBlackBook = function(all_black_books,book_id){
  95. if(all_black_books==null){
  96. return false
  97. }
  98. for (let index = 0; index < all_black_books.length; index++) {
  99. const black_book = all_black_books[index];
  100. if(black_book.product_id==book_id){
  101. return true
  102. }
  103. }
  104. return false
  105. }
  106. CMD.getMainInfoByAppid = function(MainConfig,app_id){
  107. let list = []
  108. for (let index = 0; index < MainConfig.length; index++) {
  109. const element = MainConfig[index];
  110. if(element.app_id==app_id){
  111. list.push(element)
  112. }
  113. }
  114. return list
  115. }
  116. CMD.getSharedPlatform = function(PlatformShared,platform_shared_id){
  117. for (let index = 0; index < PlatformShared.length; index++) {
  118. const element = PlatformShared[index];
  119. if(element.id==platform_shared_id){
  120. return JSON.parse(element.info)
  121. }
  122. }
  123. }
  124. CMD.getPlatformInfo = function(PlatformConfig,tg_platform_id){
  125. for (let index = 0; index < PlatformConfig.length; index++) {
  126. const element = PlatformConfig[index];
  127. if(element.tg_platform_id==tg_platform_id){
  128. return element
  129. }
  130. }
  131. }
  132. CMD.update_filter_data = async function(data) {
  133. let MainConfig = await redis_help.getKeyValue("MainConfig")
  134. MainConfig = JSON.parse(MainConfig)
  135. let AppConfig = await redis_help.getKeyValue("AppConfig")
  136. AppConfig = JSON.parse(AppConfig)
  137. let PlatformShared = await redis_help.getKeyValue("PlatformShared")
  138. PlatformShared = JSON.parse(PlatformShared)
  139. let PlatformConfig = await redis_help.getKeyValue("PlatformConfig")
  140. PlatformConfig = JSON.parse(PlatformConfig)
  141. let PlatformInfo = CMD.getPlatformInfo(PlatformConfig,config.platform_qmmf)
  142. let all_platform_list = CMD.getSharedPlatform(PlatformShared,PlatformInfo.platform_shared_id)
  143. for (let i = 0; i < all_platform_list.length; i++) {
  144. const tg_platform_id = all_platform_list[i];
  145. let AppInfoList = CMD.getAppInfoByPlatformId(AppConfig,tg_platform_id)
  146. for (let j = 0; j < AppInfoList.length; j++) {
  147. const AppInfo = AppInfoList[j];
  148. data.app_id = AppInfo.app_id
  149. let list = CMD.getMainInfoByAppid(MainConfig,data.app_id)
  150. if(list.length<0){
  151. return console.error("获取主体错误:",data)
  152. }
  153. let black_books = await redis_help.getKeyValue("all_black_books")
  154. if(black_books!=null){
  155. black_books = JSON.parse(black_books)
  156. }
  157. for (let index = 0; index < list.length; index++) {
  158. const main_info = list[index];
  159. data.main_id = main_info.id
  160. data.main_name = main_info.main_name
  161. let butler_id = main_info.butler_id
  162. if(main_info.tg_link_config==undefined||main_info.tg_link_config==null){
  163. console.error("主体配置推广链接错误:",main_info)
  164. }else{
  165. let min_book_word = main_info.min_book_word
  166. let max_book_word = main_info.max_book_word
  167. let word = data.words
  168. let material_platform_id = main_info.material_platform_id
  169. if(!CMD.isBlackBook(black_books,data.book_id)&&word>=min_book_word&&word<=max_book_word){
  170. //符合标准进行创建表
  171. let butler_video_applet_info = await tools.getAppletProductDataByButlerId(butler_id,data.book_id,data.main_id)
  172. if(butler_video_applet_info==null){
  173. const result = await video_applet_product_controllers.getAppletProductData({book_id:data.book_id,main_id:data.main_id})
  174. if(!result.success){
  175. let product_parent_id = data.product_parent_id
  176. //没有找到 可以创建
  177. await video_applet_product_controllers.createAppletProductData({
  178. book_platform:config.platform_qmmf,
  179. product_name:data.book_name,
  180. product_id:data.book_id,
  181. dy_small_program_start:"",
  182. dy_small_program_start_data:"",
  183. dy_small_applet_app_id:data.app_id,
  184. status:0,
  185. main_id:data.main_id,
  186. promotion_id:"",
  187. wait_status:0,
  188. material_platform_id:material_platform_id,
  189. butler_id:butler_id,
  190. product_parent_id: product_parent_id
  191. })
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. CMD.updateAllFqMfBookCheckStatus = async function(){
  201. let connection = await mysql.createConnection({
  202. ...taskdbConfig,
  203. multipleStatements: true
  204. });
  205. await connection.execute(
  206. ` UPDATE video_product SET is_check = 0 WHERE is_check = 1 AND status !=2 AND book_platform = ${config.platform_qmmf} `
  207. );
  208. if(connection!=null){
  209. connection.end()
  210. }
  211. }
  212. CMD.init = async function(){
  213. redis_help.connect(async (results)=>{
  214. if(results){
  215. const job = schedule.scheduleJob('0 17 * * *', async function() {
  216. await CMD.updateAllFqMfBookCheckStatus()
  217. console.log('整点执行的任务:', new Date().toLocaleString());
  218. processTask();
  219. });
  220. }
  221. })
  222. }
  223. CMD.init()