PropelDaoImpl.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package com.happy.dao.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.happy.Model.*;
  4. import com.happy.dao.PropelDao;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  7. import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
  8. import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
  9. import org.springframework.stereotype.Repository;
  10. import java.text.SimpleDateFormat;
  11. import java.util.ArrayList;
  12. import java.util.Date;
  13. import java.util.List;
  14. @Repository("PropelDao")
  15. public class PropelDaoImpl implements PropelDao {
  16. @Autowired
  17. private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
  18. public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
  19. return namedParameterJdbcTemplate;
  20. }
  21. public void setNamedParameterJdbcTemplate(
  22. NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
  23. this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
  24. }
  25. public List<JSONObject> query(){
  26. List<JSONObject> lj = new ArrayList<JSONObject>();
  27. String build = "case when build='1栋' then 'qs01' \n" +
  28. "when build='2栋' then 'qs02' \n" +
  29. "when build='3栋' then 'qs03'\n" +
  30. "when build='4栋' then 'qs04'\n" +
  31. "when build='5栋' then 'qs05'\n" +
  32. "when build='6栋' then 'qs06'\n" +
  33. "when build='7栋' then 'qs07'\n" +
  34. "when build='8栋' then 'qs08'\n" +
  35. "when build='9栋' then 'qs09'\n" +
  36. "when build='10栋' then 'qs10'\n" +
  37. "when build='11栋' then 'qs11'\n" +
  38. "when build='12栋' then 'qs12' end as build";
  39. String sql = "select "+build+", CONVERT(SUM(use_size), decimal(10, 2)) as size from (select a.id, a.`begin_time`, a.`device_code`, a.`use_size`, b.`build` from `consume` a left join `build_water` b on a.`device_code`=b.`user_id` where b.build is not null ) a group by build";
  40. MapSqlParameterSource sps = new MapSqlParameterSource();
  41. List<Propel> list = namedParameterJdbcTemplate.query(sql, sps,
  42. new BeanPropertyRowMapper<Propel>(Propel.class));
  43. SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  44. String time = sf.format(new Date());
  45. if (list != null && list.size() > 0) {
  46. for (int i=0; i<list.size();i++){
  47. JSONObject json = new JSONObject();
  48. json.put("buildCode", list.get(i).getBuild());
  49. json.put("currentAggr", list.get(i).getSize());
  50. json.put("energyType", "3");
  51. json.put("updateTime", time);
  52. lj.add(json);
  53. }
  54. return lj;
  55. }
  56. return null;
  57. }
  58. public List<Build_elec> queryEt(){
  59. String sql="select * from build_elec where pointid!='0'";
  60. MapSqlParameterSource sps = new MapSqlParameterSource();
  61. List<Build_elec> list=namedParameterJdbcTemplate.query(sql, sps,new BeanPropertyRowMapper<Build_elec>(Build_elec.class));
  62. if(list != null && list.size() >0){
  63. return list;
  64. }
  65. return null;
  66. }
  67. public int updateUseSize(String pointid, double use_size){
  68. String sql = "update build_elec set use_size=:use_size where pointid=:pointid";
  69. MapSqlParameterSource sps = new MapSqlParameterSource();
  70. sps.addValue("pointid", pointid);
  71. sps.addValue("use_size", use_size);
  72. return namedParameterJdbcTemplate.update(sql, sps);
  73. }
  74. public List<JSONObject> queryElecT(){
  75. List<JSONObject> lj = new ArrayList<JSONObject>();
  76. String build = "case when build='1栋' then 'qs01' \n" +
  77. "when build='2栋' then 'qs02' \n" +
  78. "when build='3栋' then 'qs03'\n" +
  79. "when build='4栋' then 'qs04'\n" +
  80. "when build='5栋' then 'qs05'\n" +
  81. "when build='6栋' then 'qs06'\n" +
  82. "when build='7栋' then 'qs07'\n" +
  83. "when build='8栋' then 'qs08'\n" +
  84. "when build='9栋' then 'qs09'\n" +
  85. "when build='10栋' then 'qs10'\n" +
  86. "when build='11栋' then 'qs11'\n" +
  87. "when build='12栋' then 'qs12' end as build";
  88. String sql = "select "+build+", CONVERT(SUM(use_size), decimal(10, 2)) as use_size from `build_elec` group by build";
  89. MapSqlParameterSource sps = new MapSqlParameterSource();
  90. List<ElecTotal> list = namedParameterJdbcTemplate.query(sql, sps,
  91. new BeanPropertyRowMapper<ElecTotal>(ElecTotal.class));
  92. SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  93. String time = sf.format(new Date());
  94. if (list != null && list.size() > 0) {
  95. for (int i=0; i<list.size();i++){
  96. JSONObject json = new JSONObject();
  97. json.put("buildCode", list.get(i).getBuild());
  98. json.put("currentAggr", list.get(i).getUse_size());
  99. json.put("energyType", "1");
  100. json.put("updateTime", time);
  101. lj.add(json);
  102. }
  103. return lj;
  104. }
  105. return null;
  106. }
  107. // 查询水表信息
  108. public List<WaterTj> queryWaterTj(){
  109. String sql="select * from waterTongji where `state`=1";
  110. MapSqlParameterSource sps = new MapSqlParameterSource();
  111. List<WaterTj> list=namedParameterJdbcTemplate.query(sql, sps,new BeanPropertyRowMapper<WaterTj>(WaterTj.class));
  112. if(list != null && list.size() >0){
  113. return list;
  114. }
  115. return null;
  116. }
  117. // 更新冷水使用
  118. public int updateWaterUseSize(String pointid, double use_size){
  119. String sql = "update waterTongji set use_size=:use_size where pointid=:pointid";
  120. MapSqlParameterSource sps = new MapSqlParameterSource();
  121. sps.addValue("pointid", pointid);
  122. sps.addValue("use_size", use_size);
  123. return namedParameterJdbcTemplate.update(sql, sps);
  124. }
  125. // 按楼栋查水
  126. public List<JSONObject> queryWaterT(){
  127. List<JSONObject> lj = new ArrayList<JSONObject>();
  128. String build = "case when build='1栋' then 'qs01' \n" +
  129. "when build='2栋' then 'qs02' \n" +
  130. "when build='3栋' then 'qs03'\n" +
  131. "when build='4栋' then 'qs04'\n" +
  132. "when build='5栋' then 'qs05'\n" +
  133. "when build='6栋' then 'qs06'\n" +
  134. "when build='7栋' then 'qs07'\n" +
  135. "when build='8栋' then 'qs08'\n" +
  136. "when build='9栋' then 'qs09'\n" +
  137. "when build='10栋' then 'qs10'\n" +
  138. "when build='11栋' then 'qs11'\n" +
  139. "when build='12栋' then 'qs12' end as build";
  140. String sql = "select "+build+", CONVERT(SUM(use_size), decimal(10, 2)) as use_size from `waterTongji` group by build";
  141. MapSqlParameterSource sps = new MapSqlParameterSource();
  142. List<ElecTotal> list = namedParameterJdbcTemplate.query(sql, sps,
  143. new BeanPropertyRowMapper<ElecTotal>(ElecTotal.class));
  144. SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  145. String time = sf.format(new Date());
  146. if (list != null && list.size() > 0) {
  147. for (int i=0; i<list.size();i++){
  148. JSONObject json = new JSONObject();
  149. json.put("buildCode", list.get(i).getBuild());
  150. json.put("currentAggr", list.get(i).getUse_size());
  151. json.put("energyType", "2");
  152. json.put("updateTime", time);
  153. lj.add(json);
  154. }
  155. return lj;
  156. }
  157. return null;
  158. }
  159. // 有条件的查询
  160. public List<Detail_elc> findAll(String sqlx) {
  161. MapSqlParameterSource sps = new MapSqlParameterSource();
  162. List<Detail_elc> list=namedParameterJdbcTemplate.query(sqlx, sps,new BeanPropertyRowMapper<Detail_elc>(Detail_elc.class));
  163. return list != null && list.size() > 0 ? list : null;
  164. }
  165. // 查询总记录数
  166. public int total(String sqlx) {
  167. MapSqlParameterSource sps = new MapSqlParameterSource();
  168. return namedParameterJdbcTemplate.queryForInt(sqlx, sps);
  169. }
  170. }