d_news.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 * FROM hfy_news WHERE 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. sql += ` WHERE type_id = ?`;
  106. values.push(id);
  107. return mysql.pq(sql, values);
  108. }
  109. // 删除产品类型
  110. function deleteArticleType(id) {
  111. let sql = `DELETE FROM hfy_news_type WHERE type_id = ?`;
  112. return mysql.pq(sql, [id]);
  113. }
  114. // 新增产品类型
  115. function addArticleType(type, parentId) {
  116. let sql =
  117. `INSERT INTO hfy_news_type (
  118. date_time, type_name, type_key, type_sort, type_logo, seo_key, parent_id)
  119. VALUES (?, ?, ?, ?, ?, ?, ?)`;
  120. let values = [];
  121. values.push(time.getUnixTimeStamp());
  122. values.push(type.type_name);
  123. values.push(type.type_key);
  124. values.push(type.type_sort);
  125. values.push(type.type_logo);
  126. values.push(type.seo_key);
  127. values.push(parentId);
  128. return mysql.pq(sql,values);
  129. }
  130. module.exports = {
  131. addReadNum,
  132. loadTypes,
  133. searchAllNewsMini,
  134. getNewsById,
  135. getTypeByKey,
  136. addArticle,
  137. editArticle,
  138. editArticleType,
  139. deleteArticleType,
  140. addArticleType,
  141. }