MonthReportMapper.xml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.huaxu.dao.MonthReportMapper">
  6. <resultMap type="MonthReportEntity" id="MonthReportResult">
  7. <result property="id" column="id"/>
  8. <result property="tenantId" column="tenant_id"/>
  9. <result property="year" column="year"/>
  10. <result property="month" column="month"/>
  11. <result property="day" column="day"/>
  12. <result property="hour" column="hour"/>
  13. <result property="parentSceneId" column="parent_scene_id"/>
  14. <result property="parentSceneName" column="parent_scene_name"/>
  15. <result property="sceneId" column="scene_id"/>
  16. <result property="sceneName" column="scene_name"/>
  17. <result property="deviceId" column="device_id"/>
  18. <result property="deviceName" column="device_name"/>
  19. <result property="deviceCode" column="device_code"/>
  20. <result property="attributeId" column="attribute_id"/>
  21. <result property="attributeName" column="attribute_name"/>
  22. <result property="minValue" column="min_value"/>
  23. <result property="maxValue" column="max_value"/>
  24. <result property="avgValue" column="avg_value"/>
  25. <result property="sumValue" column="sum_value"/>
  26. <result property="latestValue" column="latest_value"/>
  27. <result property="collectDate" column="collect_date"/>
  28. <result property="dateCreate" column="date_create"/>
  29. <result property="createBy" column="create_by"/>
  30. <result property="dateUpdate" column="date_update"/>
  31. <result property="updateBy" column="update_by"/>
  32. </resultMap>
  33. <!-- 实体栏位 -->
  34. <sql id="monthReportColumns">
  35. a.id as "id" ,
  36. a.tenant_id as "tenantId" ,
  37. a.year as "year" ,
  38. a.month as "month" ,
  39. a.day as "day" ,
  40. a.hour as "hour" ,
  41. a.parent_scene_id as "parentSceneId" ,
  42. a.parent_scene_name as "parentSceneName" ,
  43. a.scene_id as "sceneId" ,
  44. a.scene_name as "sceneName" ,
  45. a.device_id as "deviceId" ,
  46. a.device_name as "deviceName" ,
  47. a.device_code as "deviceCode" ,
  48. a.attribute_id as "attributeId" ,
  49. a.attribute_name as "attributeName" ,
  50. a.min_value as "minValue" ,
  51. a.max_value as "maxValue" ,
  52. a.avg_value as "avgValue" ,
  53. a.sum_value as "sumValue" ,
  54. a.latest_value as "latestValue" ,
  55. a.collect_date as "collectDate" ,
  56. a.date_create as "dateCreate" ,
  57. a.create_by as "createBy" ,
  58. a.date_update as "dateUpdate" ,
  59. a.update_by as "updateBy"
  60. </sql>
  61. <!-- 根据主键获取实体 -->
  62. <select id="findMonthReportById" resultType="com.huaxu.entity.MonthReportEntity">
  63. SELECT
  64. <include refid="monthReportColumns"/>
  65. FROM sms_month_report a
  66. WHERE a.id = #{id}
  67. </select>
  68. <!-- 根据获取实体List -->
  69. <select id="findList" resultType="com.huaxu.entity.MonthReportEntity">
  70. SELECT
  71. <include refid="monthReportColumns"/>
  72. FROM sms_month_report a
  73. <where>
  74. <if test="tenantId != null and tenantId != ''">and a.tenant_id = #{tenantId}</if>
  75. <if test="year != null ">and a.year = #{year}</if>
  76. <if test="month != null ">and a.month = #{month}</if>
  77. <if test="day != null ">and a.day = #{day}</if>
  78. <if test="hour != null ">and a.hour = #{hour}</if>
  79. <if test="parentSceneId != null ">and a.parent_scene_id = #{parentSceneId}</if>
  80. <if test="sceneId != null ">and a.scene_id = #{sceneId}</if>
  81. <if test="deviceId != null ">and a.device_id = #{deviceId}</if>
  82. <if test="deviceCode != null and deviceCode != ''">and a.device_code = #{deviceCode}</if>
  83. <if test="attributeId != null ">and a.attribute_id = #{attributeId}</if>
  84. <if test="collectDate != null ">and a.collect_date = #{collectDate}</if>
  85. </where>
  86. </select>
  87. <!-- 根据获取实体 page -->
  88. <select id="findPage" resultType="com.huaxu.dto.ReportDto">
  89. SELECT
  90. distinct
  91. a.tenant_id as "tenantId" ,
  92. a.year as "year" ,
  93. a.month as "month" ,
  94. a.day as "day" ,
  95. b.parent_scene_id as "parentSceneId",
  96. c.SCENE_NAME as "parentSceneName"
  97. FROM sms_month_report a
  98. inner join sms_device_parm b on a.DEVICE_ID = b.DEVICE_ID and a.ATTRIBUTE_ID=b.ATTRIBUTE_ID and b.`STATUS`=1
  99. inner join sms_scene c on b.PARENT_SCENE_ID=c.id
  100. <where>
  101. b.IS_REPORT=1
  102. <if test="monthReport.tenantId != null and monthReport.tenantId != ''">and a.tenant_id =
  103. #{monthReport.tenantId}
  104. </if>
  105. <if test="monthReport.year != null ">and a.year = #{monthReport.year}</if>
  106. <if test="monthReport.month != null ">and a.month = #{monthReport.month}</if>
  107. and b.parent_scene_id in
  108. <foreach collection = "monthReport.parentSceneIds" item = "dramaId" open = "(" close = ")" separator = "," >
  109. #{dramaId}
  110. </foreach>
  111. </where>
  112. order by b.parent_scene_id,a.year,a.month,a.day
  113. </select>
  114. <select id="findAttributeList" resultType="com.huaxu.dto.ReportAttributeDto">
  115. SELECT tab.attributeName,tab.ATTRIBUTE_TYPE,min(tab.minValue) as "minValue",
  116. max(tab.maxValue) as "maxValue",AVG(tab.avgValue)as "avgValue",
  117. AVG(tab.sumValue)as "sumValue",AVG(tab.latestValue) as "latestValue"
  118. from
  119. (SELECT IFNULL(a.REMARK,b.`NAME`) AS "attributeName",
  120. c.min_value as "minValue" ,c.max_value as "maxValue" ,c.avg_value as "avgValue" ,
  121. c.sum_value as "sumValue" ,c.latest_value as "latestValue",
  122. a.PARM_TYPE ATTRIBUTE_TYPE
  123. FROM sms_device_parm a
  124. INNER JOIN sms_device_attribute b on a.ATTRIBUTE_ID=b.ID
  125. INNER JOIN sms_month_report c on a.ATTRIBUTE_ID=c.ATTRIBUTE_ID and a.DEVICE_ID=c.DEVICE_ID
  126. where
  127. a.IS_REPORT=1 and a.`STATUS`=1
  128. <if test="report.deviceIds != null and report.deviceIds.size() > 0">
  129. and a.DEVICE_ID in
  130. <foreach collection="report.deviceIds" item="dramaId" open="(" close=")" separator=",">
  131. #{dramaId.id}
  132. </foreach>
  133. </if>
  134. <if test="report.year != null ">and c.`YEAR`=#{report.year}</if>
  135. <if test="report.month != null ">and c.`MONTH`=#{report.month}</if>
  136. <if test="report.day != null ">and c.`DAY`=#{report.day}</if>
  137. <if test="report.parentSceneId != null ">and a.PARENT_SCENE_ID=#{report.parentSceneId}</if>
  138. ORDER BY a.SEQ
  139. ) as tab
  140. group by tab.attributeName,tab.ATTRIBUTE_TYPE
  141. </select>
  142. <select id="findReport" resultType="com.huaxu.dto.ReportDto">
  143. select
  144. tab.year,tab.month,tab.day,
  145. sum(ifnull(intakeWaterUsage,0))as "intakeWaterUsage",
  146. sum(ifnull(yieldWaterUsage,0))as "yieldWaterUsage",
  147. sum(ifnull(powerUsage,0))as "powerUsage",
  148. sum(ifnull(drugUsage,0))as "drugUsage"
  149. from
  150. (SELECT
  151. c.year,c.month,c.day,
  152. case when a.PARM_TYPE=4 then
  153. c.SUM_VALUE end as "intakeWaterUsage",
  154. case when a.PARM_TYPE=3 then
  155. c.SUM_VALUE end as "yieldWaterUsage",
  156. case when a.PARM_TYPE=5 then
  157. c.SUM_VALUE end as "powerUsage",
  158. case when a.PARM_TYPE=6 then
  159. c.SUM_VALUE end as "drugUsage"
  160. FROM sms_device_parm a
  161. INNER JOIN sms_device_attribute b on a.ATTRIBUTE_ID=b.ID
  162. INNER JOIN sms_month_report c on a.ATTRIBUTE_ID=c.ATTRIBUTE_ID and a.DEVICE_ID=c.DEVICE_ID
  163. where
  164. <if test="report.deviceIds != null and report.deviceIds.size() > 0">
  165. a.DEVICE_ID in
  166. <foreach collection="report.deviceIds" item="dramaId" open="(" close=")" separator=",">
  167. #{dramaId.id}
  168. </foreach>
  169. </if>
  170. <if test="report.parentSceneIds != null">
  171. and a.PARENT_SCENE_ID in
  172. <foreach collection="report.parentSceneIds" item="dramaId" open="(" close=")" separator=",">
  173. #{dramaId}
  174. </foreach>
  175. </if>
  176. <if test="report.year != null ">and c.`YEAR`=#{report.year}</if>
  177. <if test="report.month != null ">and c.`MONTH`=#{report.month}</if>
  178. <if test="report.parentSceneId != null ">and c.PARENT_SCENE_ID=#{report.parentSceneId}</if>
  179. and (a.PARM_TYPE =3 or a.PARM_TYPE =4 or a.PARM_TYPE =5 or a.PARM_TYPE =6) and a.`STATUS`=1) tab
  180. group by tab.year,tab.month,tab.day
  181. order by tab.year desc,tab.month desc,tab.day desc
  182. </select>
  183. <!-- 根据获取实体 page -->
  184. <select id="findDevicePressPage" resultType="com.huaxu.dto.DevicePressReportAttributeDto">
  185. SELECT
  186. a.DEVICE_CODE as "deviceCode",
  187. a.DEVICE_NAME as "deviceName",
  188. a.min_value as "minValue" ,a.max_value as "maxValue" ,a.avg_value as "avgValue" ,
  189. a.sum_value as "sumValue" ,a.latest_value as "latestValue",
  190. a.COLLECT_DATE as "collectDate"
  191. FROM sms_month_report a
  192. inner join sms_device d on d.id = a.DEVICE_ID
  193. inner join sms_device_parm b on a.DEVICE_ID = b.DEVICE_ID and a.ATTRIBUTE_ID=b.ATTRIBUTE_ID and b.`STATUS`=1
  194. <if test="monthReport.type == 1">
  195. and b.PARM_TYPE = 13
  196. </if>
  197. <if test="monthReport.type == 2">
  198. and b.PARM_TYPE = 14
  199. </if>
  200. <where>
  201. <if test="monthReport.tenantId != null and monthReport.tenantId != ''">and a.tenant_id =
  202. #{monthReport.tenantId}
  203. </if>
  204. <if test="monthReport.year != null ">and a.year = #{monthReport.year}</if>
  205. <if test="monthReport.month != null ">and a.month = #{monthReport.month}</if>
  206. and a.DEVICE_ID in
  207. <foreach collection="monthReport.deviceIds" item="dramaId" open="(" close=")" separator=",">
  208. #{dramaId.id}
  209. </foreach>
  210. </where>
  211. order by a.DEVICE_ID,a.year,a.month,a.day
  212. </select>
  213. <!-- 根据获取实体 page -->
  214. <select id="findDeviceWaterPage" resultType="com.huaxu.dto.DeviceWaterReportAttributeDto">
  215. select tab.deviceCode,tab.deviceName,tab.collectDate,
  216. sum(tab.minPHValue) as minPHValue,sum(tab.maxPHValue) as maxPHValue,sum(tab.avgPHValue) as avgPHValue,sum(tab.sumPHValue) as sumPHValue,sum(tab.latestPHValue) as latestPHValue,
  217. sum(tab.minQuValue) as minQuValue,sum(tab.maxQuValue) as maxQuValue,sum(tab.avgQuValue) as avgQuValue,sum(tab.sumQuValue) as sumQuValue,sum(tab.latestQuValue) as latestQuValue,
  218. sum(tab.minCLValue) as minCLValue,sum(tab.maxCLValue) as maxCLValue,sum(tab.avgCLValue) as avgCLValue,sum(tab.sumCLValue) as sumCLValue,sum(tab.latestCLValue) as latestCLValue
  219. from (
  220. SELECT a.DEVICE_ID,a.id,a.DEVICE_CODE as "deviceCode", a.DEVICE_NAME as "deviceName",
  221. case when b.PARM_TYPE=7 then a.min_value end as "minPHValue" ,
  222. case when b.PARM_TYPE=7 then a.max_value end as "maxPHValue" ,
  223. case when b.PARM_TYPE=7 then a.avg_value end as "avgPHValue" ,
  224. case when b.PARM_TYPE=7 then a.sum_value end as "sumPHValue" ,
  225. case when b.PARM_TYPE=7 then a.latest_value end as "latestPHValue",
  226. case when b.PARM_TYPE=9 then a.min_value end as "minQuValue" ,
  227. case when b.PARM_TYPE=9 then a.max_value end as "maxQuValue" ,
  228. case when b.PARM_TYPE=9 then a.avg_value end as "avgQuValue" ,
  229. case when b.PARM_TYPE=9 then a.sum_value end as "sumQuValue" ,
  230. case when b.PARM_TYPE=9 then a.latest_value end as "latestQuValue",
  231. case when b.PARM_TYPE=11 then a.min_value end as "minCLValue" ,
  232. case when b.PARM_TYPE=11 then a.max_value end as "maxCLValue" ,
  233. case when b.PARM_TYPE=11 then a.avg_value end as "avgCLValue" ,
  234. case when b.PARM_TYPE=11 then a.sum_value end as "sumCLValue" ,
  235. case when b.PARM_TYPE=11 then a.latest_value end as "latestCLValue",
  236. a.COLLECT_DATE as "collectDate" ,
  237. a.year,a.month,a.day
  238. FROM sms_month_report a
  239. inner join sms_device d on d.id = a.DEVICE_ID
  240. inner join sms_device_parm b on a.DEVICE_ID = b.DEVICE_ID and a.ATTRIBUTE_ID=b.ATTRIBUTE_ID and b.`STATUS`=1
  241. and (b.PARM_TYPE = 7 or b.PARM_TYPE = 9 or b.PARM_TYPE = 11)
  242. <where>
  243. <if test="monthReport.tenantId != null and monthReport.tenantId != ''">and a.tenant_id =
  244. #{monthReport.tenantId}
  245. </if>
  246. <if test="monthReport.year != null ">and a.year = #{monthReport.year}</if>
  247. <if test="monthReport.month != null ">and a.month = #{monthReport.month}</if>
  248. and a.DEVICE_ID in
  249. <foreach collection="monthReport.deviceIds" item="dramaId" open="(" close=")" separator=",">
  250. #{dramaId.id}
  251. </foreach>
  252. </where>) as tab
  253. group by tab.DEVICE_ID,tab.year,tab.month,tab.day,tab.deviceCode,tab.deviceName,tab.collectDate
  254. order by tab.DEVICE_ID,tab.year,tab.month,tab.day
  255. </select>
  256. <select id="findAmountBySceneIds" resultType="com.huaxu.entity.MonthReportEntity">
  257. SELECT a.year as "year",a.`MONTH` as "month",a.`DAY` as "day",sum(a.LATEST_VALUE) as "latestValue",
  258. sum(a.SUM_VALUE) as "sumValue"
  259. FROM sms_month_report a
  260. INNER JOIN sms_device_parm b on a.DEVICE_ID=b.DEVICE_ID and a.ATTRIBUTE_ID=b.ATTRIBUTE_ID and b.`STATUS`=1
  261. where DATE_SUB(CURDATE(), INTERVAL 30 DAY) &lt;= date(a.COLLECT_DATE)
  262. and b.PARM_TYPE=3 and b.PARENT_SCENE_ID in
  263. <foreach collection="monthReport.parentSceneLists" item="dramaId" open="(" close=")" separator=",">
  264. #{dramaId.id}
  265. </foreach>
  266. group by a.year,a.`MONTH`,a.`DAY`
  267. order by a.year desc,a.`MONTH` desc,a.`DAY` desc
  268. </select>
  269. <select id="findAmountTotalBySceneIds" resultType="com.huaxu.entity.MonthReportEntity">
  270. select sum(a.LATEST_VALUE) as "latestValue"
  271. from sms_month_report a
  272. inner join sms_device_parm b on a.DEVICE_ID=b.DEVICE_ID and b.ATTRIBUTE_ID=a.ATTRIBUTE_ID and b.`STATUS`=1
  273. inner join (
  274. select max(a.`DAY`) as day,a.DEVICE_ID
  275. from sms_month_report a
  276. inner join sms_device_parm b on a.DEVICE_ID=b.DEVICE_ID and b.ATTRIBUTE_ID=a.ATTRIBUTE_ID and b.`STATUS`=1
  277. where b.PARM_TYPE=3 and b.PARENT_SCENE_ID in
  278. <foreach collection="monthReport.parentSceneLists" item="dramaId" open="(" close=")" separator=",">
  279. #{dramaId.id}
  280. </foreach>
  281. <if test="monthReport.year != null ">and a.year = #{monthReport.year}</if>
  282. <if test="monthReport.month != null ">and a.month = #{monthReport.month}</if>
  283. GROUP BY a.DEVICE_ID
  284. )as tab on tab.DEVICE_ID=a.DEVICE_ID and a.`DAY`=tab.`day`
  285. where b.PARM_TYPE=3 and b.PARENT_SCENE_ID in
  286. <foreach collection="monthReport.parentSceneLists" item="dramaId" open="(" close=")" separator=",">
  287. #{dramaId.id}
  288. </foreach>
  289. <if test="monthReport.year != null ">and a.year = #{monthReport.year}</if>
  290. <if test="monthReport.month != null ">and a.month = #{monthReport.month}</if>
  291. </select>
  292. <select id="findAttributeListForMonth" resultType="com.huaxu.dto.ReportAttributeDto">
  293. SELECT tab.attributeName,tab.ATTRIBUTE_TYPE,sum(tab.sumValue)as "sumValue"
  294. from
  295. (SELECT IFNULL(a.REMARK,b.`NAME`) AS "attributeName",
  296. c.min_value as "minValue" ,c.max_value as "maxValue" ,c.avg_value as "avgValue" ,
  297. c.sum_value as "sumValue" ,c.latest_value as "latestValue",
  298. a.PARM_TYPE ATTRIBUTE_TYPE
  299. FROM sms_device_parm a
  300. INNER JOIN sms_device_attribute b on a.ATTRIBUTE_ID=b.ID
  301. INNER JOIN sms_month_report c on a.ATTRIBUTE_ID=c.ATTRIBUTE_ID and a.DEVICE_ID=c.DEVICE_ID
  302. where a.`STATUS`=1 and (a.PARM_TYPE=3 or a.PARM_TYPE=4)
  303. <if test="report.deviceIds != null and report.deviceIds.size() > 0">
  304. and a.DEVICE_ID in
  305. <foreach collection="report.deviceIds" item="dramaId" open="(" close=")" separator=",">
  306. #{dramaId.id}
  307. </foreach>
  308. </if>
  309. <if test="report.year != null ">and c.`YEAR`=#{report.year}</if>
  310. <if test="report.month != null ">and c.`MONTH`=#{report.month}</if>
  311. <if test="report.day != null ">and c.`DAY`=#{report.day}</if>
  312. <if test="report.parentSceneId != null ">and a.PARENT_SCENE_ID=#{report.parentSceneId}</if>
  313. ) as tab
  314. group by tab.attributeName,tab.ATTRIBUTE_TYPE
  315. </select>
  316. </mapper>