| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- const mysql = require('./mysql');
- const {searchSql,limitSql} = require("../tools/searchSql");
- const {getUnixTimeStamp} = require("../tools/time_cjs");
- const log = require("../logger").logger("d_news","info");
- function addReadNum(id){
- let sql = `UPDATE hfy_news SET hits = hits + 1 WHERE id = ? limit 1`;
- let values = [id];
- return mysql.pq(sql,values);
- }
- function loadTypes() {
- let sql = `SELECT * FROM hfy_news_type`;
- return mysql.pq(sql, []);
- }
- function getTypeByKey(key){
- let sql = `SELECT * FROM hfy_news_type WHERE type_key = ? limit 1`;
- return mysql.pq(sql,[key]);
- }
- // 轻量搜索接口
- function searchAllNewsMini(type='array',searchParam,sort,page,limit){
- let sql;
- let values = [];
- if(type === 'count'){
- sql = `select count(*) as total `;
- }else{
- sql = `select
- news.id as id ,
- news.title as name,
- n_type.type_key
- `;
- }
- sql += `
- from
- hfy_news as news ,
- hfy_news_type as n_type
- `
- sql += ` where news.type_id = n_type.type_id`
- if(searchParam.key){
- sql += ` and news.title like '%${searchParam.key}%'`
- }
- if(searchParam.type){
- sql += ` and n_type.type_key = ?`
- values.push(searchParam.type)
- }
- return searchSql(mysql.pq,type,sql,values,limit,page);
- }
- function getNewsById(id){
- let sql = `SELECT * FROM hfy_news WHERE id = ? limit 1`;
- return mysql.pq(sql,[id]);
- }
- function addArticle(article, typeId){
- let sql = `INSERT INTO hfy_news (title, remark, author, content, type_id, image, date_time) VALUES (?, ?, ?, ?, ?, ?, ?)`;
- let values = [];
- values.push(article.title);
- values.push(article.remark);
- values.push(article.author);
- values.push(article.content);
- values.push(typeId);
- values.push(article.cover);
- values.push(getUnixTimeStamp());
- log.info(`[新增文章] sql=${sql}, values=${values}`);
- return mysql.pq(sql,values);
- }
- module.exports = {
- addReadNum,
- loadTypes,
- searchAllNewsMini,
- getNewsById,
- getTypeByKey,
- addArticle
- }
|