ArticleTweetImplDao.java 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. package com.happy.dao.impl;
  2. import com.happy.Model.*;
  3. import com.happy.Until.SqlUtil;
  4. import com.happy.Until.UUIDUtil;
  5. import com.happy.dao.ArticleTweetDao;
  6. import com.happy.vo.*;
  7. import org.apache.commons.lang.StringUtils;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  10. import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
  11. import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
  12. import org.springframework.jdbc.core.namedparam.SqlParameterSource;
  13. import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
  14. import org.springframework.jdbc.support.GeneratedKeyHolder;
  15. import org.springframework.jdbc.support.KeyHolder;
  16. import org.springframework.stereotype.Repository;
  17. import java.util.List;
  18. @Repository("ArticleTweetDao")
  19. public class ArticleTweetImplDao implements ArticleTweetDao {
  20. @Autowired
  21. private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
  22. public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
  23. return namedParameterJdbcTemplate;
  24. }
  25. public void setNamedParameterJdbcTemplate(NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
  26. this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
  27. }
  28. @Override
  29. public ArticleTweet queryArticleById(String id, Integer userId) {
  30. String caseSql = userId == null ? "" : "case when ac.id is null then 0 else 1 end is_collect,";
  31. String leftSql = userId == null ? "" : "left join article_collect ac on ac.is_lose = 0 and ac.article_id = at.id and ac.user_id = " + userId + " ";
  32. String sql = "SELECT " + caseSql + "(select IFNULL(count(*),0) from article_collect where article_id = :id and is_lose = 0) as collect_num,hd.name as location_name,at.* " +
  33. "FROM article_tweet at " +
  34. "left join hotel_dict hd on hd.code = 10 and hd.id = at.location_id " +
  35. leftSql +
  36. "WHERE at.id = :id ";
  37. MapSqlParameterSource sps = new MapSqlParameterSource();
  38. sps.addValue("id", id);
  39. List<ArticleTweet> list = null;
  40. try {
  41. list = namedParameterJdbcTemplate.query(sql, sps,
  42. new BeanPropertyRowMapper<>(ArticleTweet.class));
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. }
  46. if (list != null && list.size() > 0) {
  47. return list.get(0);
  48. }
  49. return null;
  50. }
  51. @Override
  52. public int insert(ArticleTweet articleTweet) {
  53. KeyHolder key = new GeneratedKeyHolder();
  54. String sql = "insert into `article_tweet` (user_id,user_name,user_photo,title,content,location_id,hotel_id,create_id,create_date,status,approve) " +
  55. "values(:userId,:userName,:userPhoto,:title,:content,:locationId,:hotelId,:createId,:createDate,:status,:approve) ";
  56. MapSqlParameterSource sps = new MapSqlParameterSource();
  57. sps.addValue("userId", articleTweet.getUserId());
  58. sps.addValue("userName", articleTweet.getUserName());
  59. sps.addValue("userPhoto", articleTweet.getUserPhoto());
  60. sps.addValue("title", articleTweet.getTitle());
  61. sps.addValue("content", articleTweet.getContent());
  62. sps.addValue("locationId", articleTweet.getLocationId());
  63. sps.addValue("hotelId", articleTweet.getHotelId());
  64. sps.addValue("createId", articleTweet.getCreateId());
  65. sps.addValue("createDate", articleTweet.getCreateDate());
  66. sps.addValue("status", articleTweet.getStatus());
  67. sps.addValue("approve", articleTweet.getApprove());
  68. int num = 0;
  69. try {
  70. num = namedParameterJdbcTemplate.update(sql, sps, key);
  71. num = key.getKey().intValue();
  72. } catch (Exception e) {
  73. e.printStackTrace();
  74. }
  75. return num;
  76. }
  77. @Override
  78. public int updateArticleApprove(ArticleTweet articleTweet) {
  79. String sql = "update article_tweet set approve = :approve where id = :id";
  80. MapSqlParameterSource sps = new MapSqlParameterSource();
  81. sps.addValue("approve", articleTweet.getApprove());
  82. sps.addValue("id", articleTweet.getId());
  83. int num = 0;
  84. try {
  85. num = namedParameterJdbcTemplate.update(sql, sps);
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. }
  89. return num;
  90. }
  91. @Override
  92. public int insertArticleFileBatch(List<ArticleFileInfo> articleFileInfos) {
  93. SqlParameterSource[] params = SqlParameterSourceUtils.createBatch(articleFileInfos.toArray());
  94. String insertSql = "insert into article_file_info (link_id, url, name ,create_date ,type) " +
  95. "VALUES (:linkId, :url, :name ,:createDate ,:type) ";
  96. int[] m = namedParameterJdbcTemplate.batchUpdate(insertSql, params);
  97. return m.length;
  98. }
  99. @Override
  100. public List<HotelVo> queryHotelPage(String sqlx, int page, int rows) {
  101. SqlUtil.filterKeyword(sqlx);
  102. int start = (page - 1) * rows;// 每页的起始下标
  103. String sql = "select h.id,h.hname as name,h.cover_img,h.score_hotel as score,am.type " +
  104. "from admin_manager am\n" +
  105. "inner join hotel h on h.status = 1 and h.manager_id = am.id\n" +
  106. "where am.status = 1 and am." + sqlx + " ORDER BY name asc limit :start,:rows ";
  107. MapSqlParameterSource sps = new MapSqlParameterSource();
  108. sps.addValue("start", start);
  109. sps.addValue("rows", rows);
  110. List<HotelVo> list = namedParameterJdbcTemplate.query(sql, sps,
  111. new BeanPropertyRowMapper<>(HotelVo.class));
  112. if (list != null && list.size() > 0) {
  113. return list;
  114. }
  115. return null;
  116. }
  117. @Override
  118. public int queryHotelTotal(String sqlx) {
  119. SqlUtil.filterKeyword(sqlx);
  120. String sql = "select count(*) from admin_manager am\n" +
  121. "inner join hotel h on h.status = 1 and h.manager_id = am.id\n" +
  122. "where am.status = 1 and am." + sqlx;
  123. MapSqlParameterSource sps = new MapSqlParameterSource();
  124. return namedParameterJdbcTemplate.queryForInt(sql, sps);
  125. }
  126. @Override
  127. public List<HotelVo> queryHotels(String sqlx) {
  128. SqlUtil.filterKeyword(sqlx);
  129. String sql = "select h.manager_id,(select IFNULL(count(*),0) from booking_comment where status = 1 and hotel_id = h.id) as comment,h.id,h.hname as name,h.cover_img,h.score_hotel as score,am.type " +
  130. "from hotel h\n" +
  131. "left join admin_manager am on am.status = 1 and h.manager_id = am.id\n" +
  132. "where h.status = 1 and h." + sqlx + " ORDER BY name asc limit 0,2";
  133. MapSqlParameterSource sps = new MapSqlParameterSource();
  134. List<HotelVo> list = namedParameterJdbcTemplate.query(sql, sps,
  135. new BeanPropertyRowMapper<>(HotelVo.class));
  136. if (list != null && list.size() > 0) {
  137. return list;
  138. }
  139. return null;
  140. }
  141. @Override
  142. public List<HotelVo> queryHotelPageByHotleId(String sqlx, int page, int rows) {
  143. SqlUtil.filterKeyword(sqlx);
  144. int start = (page - 1) * rows;// 每页的起始下标
  145. String sql =
  146. "select h.manager_id,(select IFNULL(count(*),0) from booking_comment where status = 1 and hotel_id = h.id) as comment,h.id,h.hname as name,h.cover_img,h.score_hotel as score,am.type " +
  147. "from hotel h\n" +
  148. "left join admin_manager am on am.status = 1 and h.manager_id = am.id\n" +
  149. "where h.status = 1 and h." + sqlx + " ORDER BY name asc limit :start,:rows ";
  150. MapSqlParameterSource sps = new MapSqlParameterSource();
  151. sps.addValue("start", start);
  152. sps.addValue("rows", rows);
  153. List<HotelVo> list = namedParameterJdbcTemplate.query(sql, sps,
  154. new BeanPropertyRowMapper<>(HotelVo.class));
  155. if (list != null && list.size() > 0) {
  156. return list;
  157. }
  158. return null;
  159. }
  160. @Override
  161. public int queryHotelTotalByHotleId(String sqlx) {
  162. SqlUtil.filterKeyword(sqlx);
  163. String sql = "select count(*) from hotel h\n" +
  164. "where h.status = 1 and h." + sqlx;
  165. MapSqlParameterSource sps = new MapSqlParameterSource();
  166. return namedParameterJdbcTemplate.queryForInt(sql, sps);
  167. }
  168. @Override
  169. public List<FileInfo> queryList(String sqlx) {
  170. SqlUtil.filterKeyword(sqlx);
  171. String sql = "SELECT id,link_id,url,name " +
  172. "FROM `article_file_info` WHERE type=1 " + sqlx;
  173. List<FileInfo> list = null;
  174. try {
  175. list = namedParameterJdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FileInfo.class));
  176. } catch (Exception e) {
  177. e.printStackTrace();
  178. }
  179. if (list != null && list.size() > 0) {
  180. return list;
  181. }
  182. return null;
  183. }
  184. @Override
  185. public UserCollect queryUserCollect(Integer parentId, Integer userId) {
  186. String sql = "SELECT * FROM user_collect " +
  187. "WHERE is_lose=0 and parent_userid = :parentId and user_id = :userId";
  188. MapSqlParameterSource sps = new MapSqlParameterSource();
  189. sps.addValue("parentId", parentId);
  190. sps.addValue("userId", userId);
  191. List<UserCollect> list = null;
  192. try {
  193. list = namedParameterJdbcTemplate.query(sql, sps,
  194. new BeanPropertyRowMapper<>(UserCollect.class));
  195. } catch (Exception e) {
  196. e.printStackTrace();
  197. }
  198. if (list != null && list.size() > 0) {
  199. return list.get(0);
  200. }
  201. return null;
  202. }
  203. @Override
  204. public List<LikeListVo> queryLikesPage(Integer articleId, int page, int rows) {
  205. int start = (page - 1) * rows;// 每页的起始下标
  206. String sql = "select (select IFNULL(count(*),0) from article_tweet where user_id = al.like_id) as article_number,(select IFNULL(count(*),0) from user_collect where is_lose = 0 and parent_userid = al.like_id) as fans_number,al.like_id as id,al.like_image as image,al.like_name as name " +
  207. "from article_likes al\n" +
  208. "where al.is_lose = 0 and al.article_id = :articleId order by al.create_date desc limit :start,:rows";
  209. MapSqlParameterSource sps = new MapSqlParameterSource();
  210. sps.addValue("start", start);
  211. sps.addValue("rows", rows);
  212. sps.addValue("articleId", articleId);
  213. List<LikeListVo> list = null;
  214. try {
  215. list = namedParameterJdbcTemplate.query(sql, sps, new BeanPropertyRowMapper<>(LikeListVo.class));
  216. } catch (Exception e) {
  217. e.printStackTrace();
  218. }
  219. if (list != null && list.size() > 0) {
  220. return list;
  221. }
  222. return null;
  223. }
  224. @Override
  225. public int queryLikesTotal(Integer articleId) {
  226. String sql ="select count(*) from article_likes " +
  227. "where is_lose = 0 and article_id = :articleId order by create_date desc";
  228. MapSqlParameterSource sps = new MapSqlParameterSource();
  229. sps.addValue("articleId", articleId);
  230. return namedParameterJdbcTemplate.queryForInt(sql, sps);
  231. }
  232. @Override
  233. public List<LikeListVo> queryArticleLikes(Integer articleId) {
  234. String sql = "select (select IFNULL(count(*),0) from article_tweet where user_id = al.like_id) as article_number,(select IFNULL(count(*),0) from user_collect where is_lose = 0 and parent_userid = al.like_id) as fans_number,al.like_id as id,al.like_image as image,al.like_name as name " +
  235. "from article_likes al\n" +
  236. "where al.is_lose = 0 and al.article_id = :articleId order by al.create_date desc";
  237. MapSqlParameterSource sps = new MapSqlParameterSource();
  238. sps.addValue("articleId", articleId);
  239. List<LikeListVo> list = null;
  240. try {
  241. list = namedParameterJdbcTemplate.query(sql, sps, new BeanPropertyRowMapper<>(LikeListVo.class));
  242. } catch (Exception e) {
  243. e.printStackTrace();
  244. }
  245. if (list != null && list.size() > 0) {
  246. return list;
  247. }
  248. return null;
  249. }
  250. @Override
  251. public List<ArticleCommentVo> queryArticleComment(Integer articleId) {
  252. String sql = "select id,comment_parent_id as parent_id,comment_id as user_id,comment_name as user_name,comment_image as image,content,create_date as date " +
  253. "from article_comment " +
  254. "where article_id = :articleId and comment_parent_id = 0 " +
  255. "ORDER BY create_date desc limit 0,5";
  256. MapSqlParameterSource sps = new MapSqlParameterSource();
  257. sps.addValue("articleId", articleId);
  258. List<ArticleCommentVo> list = null;
  259. try {
  260. list = namedParameterJdbcTemplate.query(sql, sps, new BeanPropertyRowMapper<>(ArticleCommentVo.class));
  261. } catch (Exception e) {
  262. e.printStackTrace();
  263. }
  264. if (list != null && list.size() > 0) {
  265. return list;
  266. }
  267. return null;
  268. }
  269. @Override
  270. public int queryArticleCommentTotal(Integer articleId) {
  271. String sql = "select Count(*) from article_comment " +
  272. "where article_id = :articleId";
  273. MapSqlParameterSource sps = new MapSqlParameterSource();
  274. sps.addValue("articleId", articleId);
  275. return namedParameterJdbcTemplate.queryForInt(sql, sps);
  276. }
  277. @Override
  278. public List<ArticleCommentVo> queryCommentPageByArticle(Integer articleId, int page, int rows) {
  279. int start = (page - 1) * rows;// 每页的起始下标
  280. String sql = "select id,comment_parent_id as parent_id,comment_id as user_id,comment_name as user_name,comment_image as image,content,create_date as date " +
  281. "from article_comment " +
  282. "where article_id = :articleId and comment_parent_id = 0 " +
  283. "ORDER BY create_date desc limit :start,:rows";
  284. MapSqlParameterSource sps = new MapSqlParameterSource();
  285. sps.addValue("start", start);
  286. sps.addValue("rows", rows);
  287. sps.addValue("articleId", articleId);
  288. List<ArticleCommentVo> list = namedParameterJdbcTemplate.query(sql, sps,
  289. new BeanPropertyRowMapper<>(ArticleCommentVo.class));
  290. if (list != null && list.size() > 0) {
  291. return list;
  292. }
  293. return null;
  294. }
  295. @Override
  296. public int queryCommentTotalByArticle(Integer articleId) {
  297. String sql = "select count(*) from article_comment " +
  298. "where article_id = :articleId and comment_parent_id = 0";
  299. MapSqlParameterSource sps = new MapSqlParameterSource();
  300. sps.addValue("articleId", articleId);
  301. return namedParameterJdbcTemplate.queryForInt(sql, sps);
  302. }
  303. @Override
  304. public List<ArticleCommentVo> queryCommentsByArticle(Integer articleId) {
  305. String sql = "select id,comment_parent_id as parent_id,comment_id as user_id,comment_name as user_name,comment_image as image,content,create_date as date " +
  306. "from article_comment " +
  307. "where article_id = :articleId and comment_parent_id > 0 " +
  308. "order BY create_date desc";
  309. MapSqlParameterSource sps = new MapSqlParameterSource();
  310. sps.addValue("articleId", articleId);
  311. List<ArticleCommentVo> list = namedParameterJdbcTemplate.query(sql, sps,
  312. new BeanPropertyRowMapper<>(ArticleCommentVo.class));
  313. if (list != null && list.size() > 0) {
  314. return list;
  315. }
  316. return null;
  317. }
  318. @Override
  319. public articleUserVo queryUserInfo(Integer userId) {
  320. String sql = "select id,head_photo as image,user_name,descript,\n" +
  321. "(select count(*) from user_collect where is_lose = 0 and parent_userid = :userId) as fans_num,\n" +
  322. "(select count(*) from user_collect where is_lose = 0 and user_id = :userId) as follow_num,\n" +
  323. "(select count(*) from article_likes where is_lose = 0 and article_id in (select id from article_tweet where user_id = :userId)) as like_num\n" +
  324. "from users where id = :userId";
  325. MapSqlParameterSource sps = new MapSqlParameterSource();
  326. sps.addValue("userId", userId);
  327. List<articleUserVo> list = null;
  328. try {
  329. list = namedParameterJdbcTemplate.query(sql, sps,
  330. new BeanPropertyRowMapper<>(articleUserVo.class));
  331. } catch (Exception e) {
  332. e.printStackTrace();
  333. }
  334. if (list != null && list.size() > 0) {
  335. return list.get(0);
  336. }
  337. return null;
  338. }
  339. @Override
  340. public List<OwnerArticleVo> queryOwnerArticlePage(Integer userId, String sqlWhere, int page, int rows) {
  341. int start = (page - 1) * rows;// 每页的起始下标
  342. String sql = "select case when al.id is null then 0 else 1 end is_like,at.approve,at.id,at.title,at.create_date as date,at.location_id as town_id,\n" +
  343. "hd.name as town_name,(select count(*) from article_likes where article_id = at.id and is_lose = 0) as like_num,\n" +
  344. "(select count(*) from article_comment where article_id = at.id) as comment_num\n" +
  345. "from article_tweet at\n" +
  346. "left join hotel_dict hd on hd.id = at.location_id\n" +
  347. "left join article_likes al on al.is_lose = 0 and al.article_id = at.id and al.like_id = :userId " +
  348. "where at.user_id = :userId " + sqlWhere +
  349. "ORDER BY at.create_date desc limit :start,:rows";
  350. MapSqlParameterSource sps = new MapSqlParameterSource();
  351. sps.addValue("start", start);
  352. sps.addValue("rows", rows);
  353. sps.addValue("userId", userId);
  354. List<OwnerArticleVo> list = namedParameterJdbcTemplate.query(sql, sps,
  355. new BeanPropertyRowMapper<>(OwnerArticleVo.class));
  356. if (list != null && list.size() > 0) {
  357. return list;
  358. }
  359. return null;
  360. }
  361. @Override
  362. public int queryOwnerArticleTotal(Integer userId, String sqlWhere) {
  363. String sql = "select count(*) from article_tweet at \n" +
  364. "where at.user_id = :userId " + sqlWhere;
  365. MapSqlParameterSource sps = new MapSqlParameterSource();
  366. sps.addValue("userId", userId);
  367. return namedParameterJdbcTemplate.queryForInt(sql, sps);
  368. }
  369. @Override
  370. public List<ArticleListVo> queryArticlesPage(String keyWord, String townId, Integer userId, Integer type, int page, int rows) {
  371. int start = (page - 1) * rows;// 每页的起始下标
  372. String innSql = "";
  373. String leftSql = "";
  374. String caseSql = "";
  375. if (type.intValue() == 0) {//发现
  376. caseSql = "case when ac.id is null then 0 else 1 end is_collect,";
  377. leftSql = "left join article_collect ac on ac.is_lose = 0 and ac.article_id = at.id and ac.user_id = :userId\n";
  378. } else if (type.intValue() == 1) {//关注
  379. caseSql = "case when ac.id is null then 0 else 1 end is_collect,";
  380. leftSql = "left join article_collect ac on ac.is_lose = 0 and ac.article_id = at.id and ac.user_id = :userId\n";
  381. innSql = "inner join user_collect uc on uc.is_lose = 0 and uc.parent_userid = at.user_id and uc.user_id = :userId\n";//关注
  382. } else if (type.intValue() == 2) {//收藏
  383. caseSql = "1 as is_collect,";
  384. innSql = "inner join article_collect acc on acc.is_lose = 0 and acc.article_id = at.id and acc.user_id = :userId\n";//收藏
  385. }
  386. String sqlWhere = townId == null ? "" : "and at.location_id = '" + townId + "' ";
  387. sqlWhere = sqlWhere + (keyWord == null ? "" : "and (at.user_name like '%" + keyWord + "%' or at.title like '%" + keyWord + "%' or at.content like '%" + keyWord + "%') ");
  388. String sql = "select at.id,at.location_id as town_id,hd.name as town_name,at.title,at.user_id,at.user_name,at.user_photo,\n" + caseSql +
  389. "(select count(*) from article_collect where is_lose = 0 and article_id = at.id) as collect_num\n" +
  390. "from article_tweet at\n" +
  391. "left join hotel_dict hd on hd.id = at.location_id\n" +
  392. leftSql +
  393. innSql +
  394. "where at.approve = 2 " + sqlWhere +
  395. "order by at.create_date desc ";
  396. MapSqlParameterSource sps = new MapSqlParameterSource();
  397. sps.addValue("start", start);
  398. sps.addValue("rows", rows);
  399. sps.addValue("userId", userId);
  400. List<ArticleListVo> list = namedParameterJdbcTemplate.query(sql, sps,
  401. new BeanPropertyRowMapper<>(ArticleListVo.class));
  402. if (list != null && list.size() > 0) {
  403. return list;
  404. }
  405. return null;
  406. }
  407. @Override
  408. public int queryArticlesTotal(String keyWord, String townId, Integer userId, Integer type) {
  409. String innSql = "";
  410. if (type.intValue() == 1) {//关注
  411. innSql = "inner join user_collect uc on uc.is_lose = 0 and uc.parent_userid = at.user_id and uc.user_id = :userId\n";//关注
  412. } else if (type.intValue() == 2) {//收藏
  413. innSql = "inner join article_collect acc on acc.is_lose = 0 and acc.article_id = at.id and acc.user_id = :userId\n";//收藏
  414. }
  415. String sqlWhere = townId == null ? "" : "and at.location_id = '" + townId + "' ";
  416. sqlWhere = sqlWhere + (keyWord == null ? "" : "and (at.user_name like '%" + keyWord + "%' or at.title like '%" + keyWord + "%' or at.content like '%" + keyWord + "%') ");
  417. String sql = "select count(*) from article_tweet at\n" +
  418. innSql +
  419. "where at.approve = 2 " + sqlWhere;
  420. MapSqlParameterSource sps = new MapSqlParameterSource();
  421. sps.addValue("userId", userId);
  422. sps.addValue("townId", townId);
  423. return namedParameterJdbcTemplate.queryForInt(sql, sps);
  424. }
  425. @Override
  426. public List<ArticleListVo> relatedTweetPage(Integer userId, String townId, Integer articleId, int page, int rows) {
  427. int start = (page - 1) * rows;// 每页的起始下标
  428. String sql = "select at.id,at.location_id as town_id,hd.name as town_name,at.title,at.user_id,at.user_name,at.user_photo,case when ac.id is null then 0 else 1 end is_collect,\n" +
  429. "(select count(*) from article_collect where is_lose = 0 and article_id = at.id) as collect_num\n" +
  430. "from article_tweet at\n" +
  431. "left join hotel_dict hd on hd.id = at.location_id\n" +
  432. "left join article_collect ac on ac.is_lose = 0 and ac.article_id = at.id and ac.user_id = :userId\n" +
  433. "where at.approve = 2 and at.location_id = :townId and at.id != :articleId\n" +
  434. "order by at.create_date desc ";
  435. MapSqlParameterSource sps = new MapSqlParameterSource();
  436. sps.addValue("start", start);
  437. sps.addValue("rows", rows);
  438. sps.addValue("userId", userId);
  439. sps.addValue("townId", townId);
  440. sps.addValue("articleId", articleId);
  441. List<ArticleListVo> list = namedParameterJdbcTemplate.query(sql, sps,
  442. new BeanPropertyRowMapper<>(ArticleListVo.class));
  443. if (list != null && list.size() > 0) {
  444. return list;
  445. }
  446. return null;
  447. }
  448. @Override
  449. public int relatedTweetTotal(String townId, Integer articleId) {
  450. String sql = "select count(*) from article_tweet at\n" +
  451. "where at.approve = 2 and at.location_id = :townId and at.id != :articleId";
  452. MapSqlParameterSource sps = new MapSqlParameterSource();
  453. sps.addValue("townId", townId);
  454. sps.addValue("articleId", articleId);
  455. return namedParameterJdbcTemplate.queryForInt(sql, sps);
  456. }
  457. @Override
  458. public ArticleCollect queryArticleCollect(Integer articleId, Integer userId) {
  459. String sql = "select * from article_collect\n" +
  460. "where article_id = :articleId and user_id = :userId ";
  461. MapSqlParameterSource sps = new MapSqlParameterSource();
  462. sps.addValue("articleId", articleId);
  463. sps.addValue("userId", userId);
  464. List<ArticleCollect> list = null;
  465. try {
  466. list = namedParameterJdbcTemplate.query(sql, sps,
  467. new BeanPropertyRowMapper<>(ArticleCollect.class));
  468. } catch (Exception e) {
  469. e.printStackTrace();
  470. }
  471. if (list != null && list.size() > 0) {
  472. return list.get(0);
  473. }
  474. return null;
  475. }
  476. @Override
  477. public int updateArticleCollect(ArticleCollect articleCollect) {
  478. String sql = "";
  479. MapSqlParameterSource sps = new MapSqlParameterSource();
  480. if (articleCollect.getId() == null) {
  481. sql = "insert into article_collect (article_id,user_id,is_lose,create_id,create_date,status) " +
  482. "values(:articleId,:userId,:isLose,:createId,:createDate,:status) ";
  483. sps.addValue("articleId", articleCollect.getArticleId());
  484. sps.addValue("userId", articleCollect.getUserId());
  485. sps.addValue("isLose", articleCollect.getIsLose());
  486. sps.addValue("createId", articleCollect.getCreateId());
  487. sps.addValue("createDate", articleCollect.getCreateDate());
  488. sps.addValue("status", articleCollect.getStatus());
  489. } else {
  490. sql = "update article_collect set is_lose = :isLose where id = :id";
  491. sps.addValue("isLose", articleCollect.getIsLose());
  492. sps.addValue("id", articleCollect.getId());
  493. }
  494. int num = 0;
  495. try {
  496. num = namedParameterJdbcTemplate.update(sql, sps);
  497. } catch (Exception e) {
  498. e.printStackTrace();
  499. }
  500. return num;
  501. }
  502. @Override
  503. public int insertArticleComment(ArticleComment articleComment) {
  504. MapSqlParameterSource sps = new MapSqlParameterSource();
  505. String sql = "insert into article_comment (article_id,comment_parent_id,comment_id,comment_name,comment_image,content,create_id,create_date,status) " +
  506. "values(:articleId,:commentParentId,:commentId,:commentName,:commentImage,:content,:createId,:createDate,:status) ";
  507. sps.addValue("articleId", articleComment.getArticleId());
  508. sps.addValue("commentParentId", articleComment.getCommentParentId());
  509. sps.addValue("commentId", articleComment.getCommentId());
  510. sps.addValue("commentName", articleComment.getCommentName());
  511. sps.addValue("commentImage", articleComment.getCommentImage());
  512. sps.addValue("content", articleComment.getContent());
  513. sps.addValue("createId", articleComment.getCreateId());
  514. sps.addValue("createDate", articleComment.getCreateDate());
  515. sps.addValue("status", articleComment.getStatus());
  516. int num = 0;
  517. try {
  518. num = namedParameterJdbcTemplate.update(sql, sps);
  519. } catch (Exception e) {
  520. e.printStackTrace();
  521. }
  522. return num;
  523. }
  524. @Override
  525. public UserCollect queryUserCollectNoLose(Integer authorId, Integer userId) {
  526. String sql = "select * from user_collect\n" +
  527. "where parent_userid = :authorId and user_id = :userId ";
  528. MapSqlParameterSource sps = new MapSqlParameterSource();
  529. sps.addValue("authorId", authorId);
  530. sps.addValue("userId", userId);
  531. List<UserCollect> list = null;
  532. try {
  533. list = namedParameterJdbcTemplate.query(sql, sps,
  534. new BeanPropertyRowMapper<>(UserCollect.class));
  535. } catch (Exception e) {
  536. e.printStackTrace();
  537. }
  538. if (list != null && list.size() > 0) {
  539. return list.get(0);
  540. }
  541. return null;
  542. }
  543. @Override
  544. public int updateUserCollect(UserCollect userCollect) {
  545. String sql = "";
  546. MapSqlParameterSource sps = new MapSqlParameterSource();
  547. if (userCollect.getId() == null) {
  548. sql = "insert into user_collect (parent_userid,user_id,is_lose,create_id,create_date,status) " +
  549. "values(:parentUserid,:userId,:isLose,:createId,:createDate,:status) ";
  550. sps.addValue("parentUserid", userCollect.getParentUserid());
  551. sps.addValue("userId", userCollect.getUserId());
  552. sps.addValue("isLose", userCollect.getIsLose());
  553. sps.addValue("createId", userCollect.getCreateId());
  554. sps.addValue("createDate", userCollect.getCreateDate());
  555. sps.addValue("status", userCollect.getStatus());
  556. } else {
  557. sql = "update user_collect set is_lose = :isLose where id = :id";
  558. sps.addValue("isLose", userCollect.getIsLose());
  559. sps.addValue("id", userCollect.getId());
  560. }
  561. int num = 0;
  562. try {
  563. num = namedParameterJdbcTemplate.update(sql, sps);
  564. } catch (Exception e) {
  565. e.printStackTrace();
  566. }
  567. return num;
  568. }
  569. @Override
  570. public ArticleLikes queryArticleLike(Integer articleId, Integer userId) {
  571. String sql = "select * from article_likes\n" +
  572. "where article_id = :articleId and like_id = :userId ";
  573. MapSqlParameterSource sps = new MapSqlParameterSource();
  574. sps.addValue("articleId", articleId);
  575. sps.addValue("userId", userId);
  576. List<ArticleLikes> list = null;
  577. try {
  578. list = namedParameterJdbcTemplate.query(sql, sps,
  579. new BeanPropertyRowMapper<>(ArticleLikes.class));
  580. } catch (Exception e) {
  581. e.printStackTrace();
  582. }
  583. if (list != null && list.size() > 0) {
  584. return list.get(0);
  585. }
  586. return null;
  587. }
  588. @Override
  589. public int updateArticleLike(ArticleLikes articleLikes) {
  590. String sql = "";
  591. MapSqlParameterSource sps = new MapSqlParameterSource();
  592. if (articleLikes.getId() == null) {
  593. sql = "insert into article_likes (article_id,like_id,like_name,like_image,is_lose,create_id,create_date,status) " +
  594. "values(:articleId,:likeId,:likeName,:likeImage,:isLose,:createId,:createDate,:status) ";
  595. sps.addValue("articleId", articleLikes.getArticleId());
  596. sps.addValue("likeId", articleLikes.getLikeId());
  597. sps.addValue("likeName", articleLikes.getLikeName());
  598. sps.addValue("likeImage", articleLikes.getLikeImage());
  599. sps.addValue("isLose", articleLikes.getIsLose());
  600. sps.addValue("createId", articleLikes.getCreateId());
  601. sps.addValue("createDate", articleLikes.getCreateDate());
  602. sps.addValue("status", articleLikes.getStatus());
  603. } else {
  604. sql = "update article_likes set is_lose = :isLose where id = :id";
  605. sps.addValue("isLose", articleLikes.getIsLose());
  606. sps.addValue("id", articleLikes.getId());
  607. }
  608. int num = 0;
  609. try {
  610. num = namedParameterJdbcTemplate.update(sql, sps);
  611. } catch (Exception e) {
  612. e.printStackTrace();
  613. }
  614. return num;
  615. }
  616. @Override
  617. public int updateDescript(Integer authorId, String descript) {
  618. String sql = "update users set descript = :descript where id = :authorId";
  619. MapSqlParameterSource sps = new MapSqlParameterSource();
  620. sps.addValue("descript", descript);
  621. sps.addValue("authorId", authorId);
  622. int num = 0;
  623. try {
  624. num = namedParameterJdbcTemplate.update(sql, sps);
  625. } catch (Exception e) {
  626. e.printStackTrace();
  627. }
  628. return num;
  629. }
  630. /**
  631. * 获取点赞数最多的最新推文数据
  632. */
  633. @Override
  634. public ArticleLikeMaxVo queryMaxArticleLike() {
  635. String sql = "select t.id,t.townId,t.townName,t.title,t.content from (select at.id,at.location_id as townId,hd.name as townName,at.title,at.content,(select Count(*) from article_likes where is_lose = 0 and article_id = at.id) as like_num,at.create_date\n" +
  636. "from article_tweet at\n" +
  637. "left join hotel_dict hd on hd.id = at.location_id\n" +
  638. "where at.approve = 2) t\n" +
  639. "order by t.like_num desc,t.create_date desc LIMIT 1";
  640. MapSqlParameterSource sps = new MapSqlParameterSource();
  641. List<ArticleLikeMaxVo> list = null;
  642. try {
  643. list = namedParameterJdbcTemplate.query(sql, sps,
  644. new BeanPropertyRowMapper<>(ArticleLikeMaxVo.class));
  645. } catch (Exception e) {
  646. e.printStackTrace();
  647. }
  648. if (list != null && list.size() > 0) {
  649. return list.get(0);
  650. }
  651. return null;
  652. }
  653. @Override
  654. public List<WalkthroughVo> walkthroughPage(String strSql, Integer page, Integer rows) {
  655. int start = (page - 1) * rows;// 每页的起始下标
  656. String sql=" SELECT AT.id AS id, AT.location_id AS locationId, AT\n" +
  657. "\t.user_name AS userName,\n" +
  658. "\tAT.title AS title,\n" +
  659. "\tAT.content AS content,\n" +
  660. "\tAT.create_date AS createDate,\n" +
  661. "\tafi2.urls AS urls \n" +
  662. "FROM\n" +
  663. "\t`article_tweet`\n" +
  664. "\tAT LEFT JOIN ( SELECT afi.link_id, GROUP_CONCAT( afi.url ) AS urls FROM article_file_info afi GROUP BY afi.link_id ) afi2 ON afi2.link_id = AT.id \n" +
  665. "WHERE\n" +
  666. "\tAT.approve = 2 " +strSql+"ORDER BY AT.create_date limit :page,:rows ";
  667. MapSqlParameterSource sps = new MapSqlParameterSource();
  668. sps.addValue("page", start);
  669. sps.addValue("rows", rows);
  670. List<WalkthroughVo> list = namedParameterJdbcTemplate.query(sql, sps,
  671. new BeanPropertyRowMapper<>(WalkthroughVo.class));
  672. if (list != null && list.size() > 0) return list;
  673. return null;
  674. }
  675. @Override
  676. public int walkthroughTotal(String strSql) {
  677. String sql="SELECT \n" +
  678. "\tCOUNT(*)\n" +
  679. "FROM\n" +
  680. "\t`article_tweet`\n" +
  681. "\tAT LEFT JOIN ( SELECT afi.link_id, GROUP_CONCAT( afi.url ) AS urls FROM article_file_info afi GROUP BY afi.link_id ) afi2 ON afi2.link_id = AT.id \n" +
  682. "WHERE\n" +
  683. "\tAT.approve = 2 " +strSql;
  684. MapSqlParameterSource sps = new MapSqlParameterSource();
  685. return namedParameterJdbcTemplate.queryForInt(sql, sps);
  686. }
  687. }