d_news.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. const mysql = require('./mysql');
  2. const {searchSql,limitSql} = require("../tools/searchSql");
  3. const {getUnixTimeStamp} = require("../tools/time_cjs");
  4. const time = require("../tools/time_cjs");
  5. const log = require("../logger").logger("d_news","info");
  6. function addReadNum(id){
  7. let sql = `UPDATE hfy_news SET hits = hits + 1 WHERE id = ? limit 1`;
  8. let values = [id];
  9. return mysql.pq(sql,values);
  10. }
  11. function loadTypes() {
  12. let sql = `SELECT * FROM hfy_news_type ORDER BY type_sort DESC`;
  13. return mysql.pq(sql, []);
  14. }
  15. function getTypeByKey(key){
  16. let sql = `SELECT * FROM hfy_news_type WHERE type_key = ? limit 1`;
  17. return mysql.pq(sql,[key]);
  18. }
  19. // 轻量搜索接口
  20. function searchAllNewsMini(type='array',searchParam,sort,page,limit){
  21. let sql;
  22. let values = [];
  23. if(type === 'count'){
  24. sql = `select count(*) as total `;
  25. }else{
  26. sql = `select
  27. news.id as id ,
  28. news.title as name,
  29. n_type.type_key
  30. `;
  31. }
  32. sql += `
  33. from
  34. hfy_news as news ,
  35. hfy_news_type as n_type
  36. `
  37. sql += ` where news.type_id = n_type.type_id`
  38. if(searchParam.key){
  39. sql += ` and news.title like '%${searchParam.key}%'`
  40. }
  41. if(searchParam.type){
  42. sql += ` and n_type.type_key = ?`
  43. values.push(searchParam.type)
  44. }
  45. return searchSql(mysql.pq,type,sql,values,limit,page);
  46. }
  47. function getNewsById(id){
  48. let sql = `SELECT news.*, n_type.type_key as type_key FROM hfy_news as news, hfy_news_type as n_type WHERE news.type_id = n_type.type_id and id = ? limit 1`;
  49. return mysql.pq(sql,[id]);
  50. }
  51. function addArticle(article, typeId){
  52. let sql = `INSERT INTO hfy_news (title, remark, author, content, type_id, image, date_time) VALUES (?, ?, ?, ?, ?, ?, ?)`;
  53. let values = [];
  54. values.push(article.title);
  55. values.push(article.remark);
  56. values.push(article.author);
  57. values.push(article.content);
  58. values.push(typeId);
  59. values.push(article.cover);
  60. values.push(getUnixTimeStamp());
  61. log.info(`[新增文章] sql=${sql}, values=${values}`);
  62. return mysql.pq(sql,values);
  63. }
  64. function editArticle(article, typeId){
  65. let sql = `UPDATE hfy_news SET
  66. title=?, remark=?, author=?, content=?,
  67. image=?, type_id=?
  68. WHERE id = ?`;
  69. let values = [];
  70. values.push(article.title);
  71. values.push(article.remark);
  72. values.push(article.author);
  73. values.push(article.content);
  74. values.push(article.cover);
  75. values.push(typeId);
  76. values.push(article.id);
  77. return mysql.pq(sql,values);
  78. }
  79. // 编辑产品类型
  80. function editArticleType(id, typeChange) {
  81. let sql = ``
  82. let values = [];
  83. sql += `UPDATE hfy_news_type SET date_time = ?`;
  84. values.push(time.getUnixTimeStamp());
  85. if(typeChange.type_name){
  86. sql += `,type_name = ?`;
  87. values.push(typeChange.type_name);
  88. }
  89. if(typeChange.type_key){
  90. sql += `,type_key = ?`;
  91. values.push(typeChange.type_key);
  92. }
  93. if(typeChange.type_sort){
  94. sql += `,type_sort = ?`;
  95. values.push(typeChange.type_sort);
  96. }
  97. if(typeChange.type_logo){
  98. sql += `,type_logo = ?`;
  99. values.push(typeChange.type_logo);
  100. }
  101. if(typeChange.seo_key){
  102. sql += `,seo_key = ?`;
  103. values.push(typeChange.seo_key);
  104. }
  105. if(typeChange.sub_text){
  106. sql += `,sub_text = ?`;
  107. values.push(typeChange.sub_text);
  108. }
  109. sql += ` WHERE type_id = ?`;
  110. values.push(id);
  111. return mysql.pq(sql, values);
  112. }
  113. // 删除产品类型
  114. function deleteArticleType(id) {
  115. let sql = `DELETE FROM hfy_news_type WHERE type_id = ?`;
  116. return mysql.pq(sql, [id]);
  117. }
  118. // 新增产品类型
  119. function addArticleType(type, parentId) {
  120. let sql =
  121. `INSERT INTO hfy_news_type (
  122. date_time, type_name, type_key, type_sort, type_logo, seo_key, sub_text,parent_id)
  123. VALUES (?, ?, ?, ?, ?, ?, ?)`;
  124. let values = [];
  125. values.push(time.getUnixTimeStamp());
  126. values.push(type.type_name);
  127. values.push(type.type_key);
  128. values.push(type.type_sort);
  129. values.push(type.type_logo);
  130. values.push(type.seo_key);
  131. values.push(type.sub_text);
  132. values.push(parentId);
  133. return mysql.pq(sql,values);
  134. }
  135. module.exports = {
  136. addReadNum,
  137. loadTypes,
  138. searchAllNewsMini,
  139. getNewsById,
  140. getTypeByKey,
  141. addArticle,
  142. editArticle,
  143. editArticleType,
  144. deleteArticleType,
  145. addArticleType,
  146. }