ArticleTweetImplDao.java 30 KB

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