d_news.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. function getTypeById(id){
  20. let sql = `SELECT * FROM hfy_news_type WHERE type_id = ? limit 1`;
  21. return mysql.pq(sql,[id]);
  22. }
  23. // 轻量搜索接口
  24. function searchAllNewsMini(type='array',searchParam,sort,page,limit){
  25. let sql;
  26. let values = [];
  27. if(type === 'count'){
  28. sql = `select count(*) as total `;
  29. }else{
  30. sql = `select
  31. news.id ,
  32. news.title,
  33. news.remark ,
  34. news.author,
  35. news.image,
  36. news.date_time ,
  37. n_type.type_name,
  38. n_type.type_key`;
  39. }
  40. sql += `
  41. from
  42. hfy_news as news ,
  43. hfy_news_type as n_type
  44. `
  45. sql += ` where news.type_id = n_type.type_id`
  46. if(searchParam.key){
  47. sql += ` and news.title like '%${searchParam.key}%'`
  48. }
  49. if(searchParam.type){
  50. sql += ` and n_type.type_key = ?`
  51. values.push(searchParam.type)
  52. }
  53. sql += ` order by news.sort DESC , news.date_time DESC`
  54. return searchSql(mysql.pq,type,sql,values,limit,page);
  55. }
  56. function getNewsById(id){
  57. 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`;
  58. return mysql.pq(sql,[id]);
  59. }
  60. function getMiniNewsById(id){
  61. let sql = `
  62. SELECT
  63. news.title, news.id, news.remark, news.author, news.image, news.date_time,
  64. n_type.type_key as type_key
  65. FROM
  66. hfy_news as news,
  67. hfy_news_type as n_type
  68. WHERE
  69. news.type_id = n_type.type_id
  70. and id = ? limit 1`;
  71. return mysql.pq(sql,[id]);
  72. }
  73. function getArticleTotalByTypeId(id){
  74. let sql = `SELECT count(*) as total FROM hfy_news WHERE type_id = ?`;
  75. return mysql.pq(sql,[id]);
  76. }
  77. function addArticle(article, typeId){
  78. let sql = `INSERT INTO hfy_news (title, remark, author, content, type_id, image, date_time) VALUES (?, ?, ?, ?, ?, ?, ?)`;
  79. let values = [];
  80. values.push(article.title);
  81. values.push(article.remark);
  82. values.push(article.author);
  83. values.push(article.content);
  84. values.push(typeId);
  85. values.push(article.cover);
  86. values.push(getUnixTimeStamp());
  87. log.info(`[新增文章] sql=${sql}, values=${values}`);
  88. return mysql.pq(sql,values);
  89. }
  90. function editArticle(article, typeId){
  91. let sql = `UPDATE hfy_news SET
  92. title=?, remark=?, author=?, content=?,
  93. image=?, type_id=?
  94. WHERE id = ?`;
  95. let values = [];
  96. values.push(article.title);
  97. values.push(article.remark);
  98. values.push(article.author);
  99. values.push(article.content);
  100. values.push(article.cover);
  101. values.push(typeId);
  102. values.push(article.id);
  103. return mysql.pq(sql,values);
  104. }
  105. function delArticle(id){
  106. let sql = `DELETE FROM hfy_news WHERE id = ? limit 1`;
  107. return mysql.pq(sql,[id]);
  108. }
  109. // 编辑产品类型
  110. function editArticleType(id, typeChange) {
  111. let sql = ``
  112. let values = [];
  113. sql += `UPDATE hfy_news_type SET date_time = ?`;
  114. values.push(time.getUnixTimeStamp());
  115. if(typeChange.type_name){
  116. sql += `,type_name = ?`;
  117. values.push(typeChange.type_name);
  118. }
  119. if(typeChange.type_key){
  120. sql += `,type_key = ?`;
  121. values.push(typeChange.type_key);
  122. }
  123. if(typeChange.type_sort){
  124. sql += `,type_sort = ?`;
  125. values.push(typeChange.type_sort);
  126. }
  127. if(typeChange.type_logo){
  128. sql += `,type_logo = ?`;
  129. values.push(typeChange.type_logo);
  130. }
  131. if(typeChange.seo_key){
  132. sql += `,seo_key = ?`;
  133. values.push(typeChange.seo_key);
  134. }
  135. if(typeChange.sub_text){
  136. sql += `,sub_text = ?`;
  137. values.push(typeChange.sub_text);
  138. }
  139. sql += ` WHERE type_id = ?`;
  140. values.push(id);
  141. return mysql.pq(sql, values);
  142. }
  143. // 删除产品类型
  144. function deleteArticleType(id) {
  145. let sql = `DELETE FROM hfy_news_type WHERE type_id = ?`;
  146. return mysql.pq(sql, [id]);
  147. }
  148. // 新增产品类型
  149. function addArticleType(type, parentId) {
  150. let sql =
  151. `INSERT INTO hfy_news_type (
  152. date_time, type_name, type_key, type_sort, type_logo, seo_key, sub_text, parent_type)
  153. VALUES (?, ?, ?, ?, ?, ?, ?, ?)`;
  154. let values = [];
  155. values.push(time.getUnixTimeStamp());
  156. values.push(type.type_name);
  157. values.push(type.type_key);
  158. values.push(type.type_sort);
  159. values.push(type.type_logo);
  160. values.push(type.seo_key);
  161. values.push(type.sub_text);
  162. values.push(parentId);
  163. return mysql.pq(sql,values);
  164. }
  165. module.exports = {
  166. addReadNum,
  167. loadTypes,
  168. searchAllNewsMini,
  169. getNewsById,
  170. getMiniNewsById,
  171. getTypeByKey,
  172. getTypeById,
  173. addArticle,
  174. getArticleTotalByTypeId,
  175. editArticle,
  176. delArticle,
  177. editArticleType,
  178. deleteArticleType,
  179. addArticleType,
  180. }