| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- const mysql = require('./mysql');
- const {searchSql,limitSql} = require("../tools/searchSql");
- const {getUnixTimeStamp} = require("../tools/time_cjs");
- const time = 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 ORDER BY type_sort DESC`;
- 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 getTypeById(id){
- let sql = `SELECT * FROM hfy_news_type WHERE type_id = ? limit 1`;
- return mysql.pq(sql,[id]);
- }
- // 轻量搜索接口
- 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 ,
- news.title,
- news.remark ,
- news.author,
- news.image,
- news.date_time ,
- n_type.type_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)
- }
- sql += ` order by news.sort DESC , news.date_time DESC`
- return searchSql(mysql.pq,type,sql,values,limit,page);
- }
- function getNewsById(id){
- 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`;
- return mysql.pq(sql,[id]);
- }
- function getMiniNewsById(id){
- let sql = `
- SELECT
- news.title, news.id, news.remark, news.author, news.image, news.date_time,
- 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`;
- return mysql.pq(sql,[id]);
- }
- function getArticleTotalByTypeId(id){
- let sql = `SELECT count(*) as total FROM hfy_news WHERE type_id = ?`;
- 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);
- }
- function editArticle(article, typeId){
- let sql = `UPDATE hfy_news SET
- title=?, remark=?, author=?, content=?,
- image=?, type_id=?
- WHERE id = ?`;
- let values = [];
- values.push(article.title);
- values.push(article.remark);
- values.push(article.author);
- values.push(article.content);
- values.push(article.cover);
- values.push(typeId);
- values.push(article.id);
- return mysql.pq(sql,values);
- }
- function delArticle(id){
- let sql = `DELETE FROM hfy_news WHERE id = ? limit 1`;
- return mysql.pq(sql,[id]);
- }
- // 编辑产品类型
- function editArticleType(id, typeChange) {
- let sql = ``
- let values = [];
- sql += `UPDATE hfy_news_type SET date_time = ?`;
- values.push(time.getUnixTimeStamp());
- if(typeChange.type_name){
- sql += `,type_name = ?`;
- values.push(typeChange.type_name);
- }
- if(typeChange.type_key){
- sql += `,type_key = ?`;
- values.push(typeChange.type_key);
- }
- if(typeChange.type_sort){
- sql += `,type_sort = ?`;
- values.push(typeChange.type_sort);
- }
- if(typeChange.type_logo){
- sql += `,type_logo = ?`;
- values.push(typeChange.type_logo);
- }
- if(typeChange.seo_key){
- sql += `,seo_key = ?`;
- values.push(typeChange.seo_key);
- }
- if(typeChange.sub_text){
- sql += `,sub_text = ?`;
- values.push(typeChange.sub_text);
- }
- sql += ` WHERE type_id = ?`;
- values.push(id);
- return mysql.pq(sql, values);
- }
- // 删除产品类型
- function deleteArticleType(id) {
- let sql = `DELETE FROM hfy_news_type WHERE type_id = ?`;
- return mysql.pq(sql, [id]);
- }
- // 新增产品类型
- function addArticleType(type, parentId) {
- let sql =
- `INSERT INTO hfy_news_type (
- date_time, type_name, type_key, type_sort, type_logo, seo_key, sub_text, parent_type)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?)`;
- let values = [];
- values.push(time.getUnixTimeStamp());
- values.push(type.type_name);
- values.push(type.type_key);
- values.push(type.type_sort);
- values.push(type.type_logo);
- values.push(type.seo_key);
- values.push(type.sub_text);
- values.push(parentId);
- return mysql.pq(sql,values);
- }
- module.exports = {
- addReadNum,
- loadTypes,
- searchAllNewsMini,
- getNewsById,
- getMiniNewsById,
- getTypeByKey,
- getTypeById,
- addArticle,
- getArticleTotalByTypeId,
- editArticle,
- delArticle,
- editArticleType,
- deleteArticleType,
- addArticleType,
- }
|