HotelDictImplDao.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package com.happy.dao.impl;
  2. import com.happy.Model.AdminManager;
  3. import com.happy.Model.HotelDict;
  4. import com.happy.Until.Func;
  5. import com.happy.Until.SqlUtil;
  6. import com.happy.Until.UUIDUtil;
  7. import com.happy.dao.HotelDictDao;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  10. import org.springframework.jdbc.core.namedparam.EmptySqlParameterSource;
  11. import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
  12. import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
  13. import org.springframework.stereotype.Repository;
  14. import java.util.List;
  15. @Repository("HotelDictDao")
  16. public class HotelDictImplDao implements HotelDictDao {
  17. @Autowired
  18. private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
  19. public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
  20. return namedParameterJdbcTemplate;
  21. }
  22. public void setNamedParameterJdbcTemplate(NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
  23. this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
  24. }
  25. @Override
  26. public int insertHotelDict(HotelDict hotelDict) {
  27. String sql = "INSERT INTO hotel_dict (id, code, name, file_url, create_id, create_date, modify_date, status) VALUES (:id, :code, :name, :file_url, :create_id, :create_date, :modify_date, :status);\n";
  28. MapSqlParameterSource sps = new MapSqlParameterSource();
  29. sps.addValue("code",hotelDict.getCode());
  30. sps.addValue("name",hotelDict.getName());
  31. sps.addValue("file_url",hotelDict.getFileUrl());
  32. sps.addValue("create_id",hotelDict.getCreateId());
  33. sps.addValue("create_date",UUIDUtil.getNewDate());
  34. sps.addValue("modify_date",hotelDict.getModifyDate());
  35. sps.addValue("status",1);
  36. if(hotelDict.getId()==null){
  37. sps.addValue("id", UUIDUtil.generateID());
  38. }else{
  39. sps.addValue("id", hotelDict.getId());
  40. }
  41. int num = 0;
  42. try{
  43. num = namedParameterJdbcTemplate.update(sql, sps);
  44. }
  45. catch(Exception e){
  46. e.printStackTrace();
  47. }
  48. return num;
  49. }
  50. @Override
  51. public int updateHotelDict(HotelDict hotelDict) {
  52. StringBuffer stringBuffer = new StringBuffer(" update `hotel_dict` set ");
  53. MapSqlParameterSource sps = new MapSqlParameterSource();
  54. // 将要修改的数据填充到查询语句中
  55. appendValue(hotelDict,stringBuffer,sps);
  56. stringBuffer.append(" where id=:id ");
  57. sps.addValue("id", hotelDict.getId());
  58. int num = 0;
  59. try{
  60. num = namedParameterJdbcTemplate.update(stringBuffer.toString(), sps);
  61. }
  62. catch(Exception e){
  63. e.printStackTrace();
  64. }
  65. return num;
  66. }
  67. @Override
  68. public int delHotelDict(int id) {
  69. String sql = "UPDATE `hotel_dict` SET status=0 WHERE id = :id ";
  70. MapSqlParameterSource sps = new MapSqlParameterSource();
  71. sps.addValue("id",id);
  72. int num = 0;
  73. try{
  74. num = namedParameterJdbcTemplate.update(sql, sps);
  75. }catch (Exception e){
  76. e.printStackTrace();
  77. }
  78. return num;
  79. }
  80. @Override
  81. public HotelDict getById(int id) {
  82. String sql = "SELECT * FROM `hotel_dict` WHERE id = :id ";
  83. MapSqlParameterSource sps = new MapSqlParameterSource();
  84. sps.addValue("id",id);
  85. List<HotelDict> list = null;
  86. try{
  87. list = namedParameterJdbcTemplate.query(sql, sps,
  88. new BeanPropertyRowMapper<>(HotelDict.class));
  89. }catch (Exception e){
  90. e.printStackTrace();
  91. }
  92. if(list != null && list.size()>0) return list.get(0);
  93. return null;
  94. }
  95. @Override
  96. public List<HotelDict> queryPage(String sqlx, int page, int rows) {
  97. SqlUtil.filterKeyword(sqlx);
  98. int start = (page - 1) * rows;// 每页的起始下标
  99. String sql = "SELECT * FROM `hotel_dict` WHERE status=1 "+sqlx+" ORDER BY id DESC limit :start,:rows ";
  100. MapSqlParameterSource sps = new MapSqlParameterSource();
  101. sps.addValue("start", start);
  102. sps.addValue("rows", rows);
  103. List<HotelDict> list = namedParameterJdbcTemplate.query(sql, sps,
  104. new BeanPropertyRowMapper<>(HotelDict.class));
  105. if (list != null && list.size() > 0) return list;
  106. return null;
  107. }
  108. @Override
  109. public int queryTotal(String sqlx) {
  110. SqlUtil.filterKeyword(sqlx);
  111. String sql = "SELECT count(*) FROM`hotel_dict` where status=1 "+sqlx;
  112. MapSqlParameterSource sps = new MapSqlParameterSource();
  113. return namedParameterJdbcTemplate.queryForInt(sql, sps);
  114. }
  115. @Override
  116. public List<HotelDict> queryList(String sqlx) {
  117. SqlUtil.filterKeyword(sqlx);
  118. String sql = "SELECT * FROM `hotel_dict` WHERE status=1 "+sqlx;
  119. List<HotelDict> list = null;
  120. try{
  121. list = namedParameterJdbcTemplate.query(sql, new BeanPropertyRowMapper<>(HotelDict.class));
  122. }catch (Exception e){
  123. e.printStackTrace();
  124. }
  125. if(list != null && list.size()>0) return list;
  126. return null;
  127. }
  128. @Override
  129. public String setConfig(String ids){
  130. String sql = "select GROUP_CONCAT(id) id from hotel_dict where status=1 and FIND_IN_SET(id,'"+ids+"')";
  131. MapSqlParameterSource sps = new MapSqlParameterSource();
  132. sps.addValue("ids", ids);
  133. String config = "";
  134. try{
  135. config = namedParameterJdbcTemplate.queryForObject(sql, EmptySqlParameterSource.INSTANCE, String.class);
  136. }catch (Exception e){
  137. e.printStackTrace();
  138. }
  139. return config;
  140. }
  141. public void appendValue(HotelDict hotelDict, StringBuffer stringBuffer, MapSqlParameterSource sps){
  142. if (!Func.checkNull(String.valueOf(hotelDict.getCode()))){
  143. stringBuffer.append(" code=:code ,");
  144. sps.addValue("code",hotelDict.getCode());
  145. }
  146. if (!Func.checkNull(hotelDict.getName())){
  147. stringBuffer.append(" name=:name ,");
  148. sps.addValue("name",hotelDict.getName());
  149. }
  150. if (!Func.checkNull(hotelDict.getFileUrl())){
  151. stringBuffer.append(" file_url=:file_url ,");
  152. sps.addValue("file_url",hotelDict.getFileUrl());
  153. }
  154. stringBuffer.append(" modify_date=:modify_date ");
  155. sps.addValue("modify_date", UUIDUtil.getNewDate());
  156. }
  157. }