DeviceMapper.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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.ruoyi.base.mapper.DeviceMapper">
  6. <resultMap type="com.ruoyi.base.domain.Device" id="DeviceResult">
  7. <result property="id" column="id" />
  8. <result property="deviceId" column="device_id" />
  9. <result property="deviceName" column="device_name" />
  10. <result property="deviceTypeId" column="device_type_id" />
  11. <result property="farmId" column="farm_id" />
  12. <result property="model" column="model" />
  13. <result property="manufacturer" column="manufacturer" />
  14. <result property="installDate" column="install_date" />
  15. <result property="status" column="status" />
  16. <result property="lastActiveTime" column="last_active_time" />
  17. <result property="createTime" column="create_time" />
  18. <result property="updateTime" column="update_time" />
  19. <result property="bindStatus" column="bindStatus" />
  20. <result property="bindTime" column="binding_time" />
  21. <result property="fieldName" column="field_name" />
  22. <result property="longitude" column="longitude" />
  23. <result property="latitude" column="latitude" />
  24. <result property="remark" column="remark" />
  25. <association property="dept" javaType="com.ruoyi.system.api.domain.SysDept" resultMap="deptResult" />
  26. <collection property="roles" javaType="java.util.List" resultMap="RoleResult" />
  27. </resultMap>
  28. <resultMap type="FieldPersonnel" id="FieldPersonnelResult">
  29. <result property="id" column="id" />
  30. <result property="fieldId" column="field_id" />
  31. <result property="userId" column="user_id" />
  32. <result property="roleType" column="role_type" />
  33. <result property="bindingTime" column="binding_time" />
  34. <result property="status" column="status" />
  35. <result property="createTime" column="create_time" />
  36. <result property="updateTime" column="update_time" />
  37. <result property="createdBy" column="created_by" />
  38. <result property="updatedBy" column="updated_by" />
  39. <result property="isDeleted" column="is_deleted" />
  40. <result property="userName" column="user_name" />
  41. <result property="postName" column="post_name" />
  42. <result property="phonenumber" column="phonenumber" />
  43. </resultMap>
  44. <resultMap id="deptResult" type="com.ruoyi.system.api.domain.SysDept">
  45. <id property="deptId" column="dept_id" />
  46. <result property="parentId" column="parent_id" />
  47. <result property="deptName" column="dept_name" />
  48. <result property="ancestors" column="ancestors" />
  49. <result property="orderNum" column="order_num" />
  50. <result property="leader" column="leader" />
  51. <result property="status" column="dept_status" />
  52. </resultMap>
  53. <resultMap id="RoleResult" type="com.ruoyi.system.api.domain.SysDept">
  54. <id property="roleId" column="role_id" />
  55. <result property="roleName" column="role_name" />
  56. <result property="roleKey" column="role_key" />
  57. <result property="roleSort" column="role_sort" />
  58. <result property="dataScope" column="data_scope" />
  59. <result property="status" column="role_status" />
  60. </resultMap>
  61. <sql id="selectDeviceVo">
  62. select id, device_id, device_name, device_type_id, farm_id, model, manufacturer, install_date, status, last_active_time, create_time, update_time, remark from device
  63. </sql>
  64. <select id="selectDeviceList" parameterType="com.ruoyi.base.domain.Device" resultMap="DeviceResult">
  65. select de.id, de.device_id, de.device_name, de.device_type_id, de.farm_id, de.model,
  66. de.manufacturer, de.install_date, de.status, de.last_active_time, de.create_time, de.update_time, de.remark,d.dept_name, d.leader,f.field_name
  67. from device de
  68. left join sys_dept d on de.farm_id = d.dept_id
  69. LEFT JOIN field_device fd ON de.id = fd.device_id AND fd.status = '0'
  70. left join field f on fd.field_id = f.id
  71. <where>
  72. (fd.status = 0 or fd.status is null)
  73. <if test="deviceId != null and deviceId != ''"> and de.device_id = #{deviceId}</if>
  74. <if test="deviceName != null and deviceName != ''"> and de.device_name like concat('%', #{deviceName}, '%')</if>
  75. <if test="deptIdList != null and deptIdList.size() != 0"> and de.farm_id in (<foreach collection="deptIdList" item="deptId" separator=",">#{deptId}</foreach>)</if>
  76. <if test="fieldIdList != null and fieldIdList.size() != 0"> and f.id in (<foreach collection="fieldIdList" item="deptId" separator=",">#{deptId}</foreach>)</if>
  77. <if test="deviceTypeId != null and deviceTypeId != ''"> and de.device_type_id = #{deviceTypeId}</if>
  78. <if test="farmId != null and farmId != ''"> and de.farm_id = #{farmId}</if>
  79. <if test="model != null and model != ''"> and de.model = #{model}</if>
  80. <if test="manufacturer != null and manufacturer != ''"> and de.manufacturer = #{manufacturer}</if>
  81. <if test="installDate != null "> and de.install_date = #{installDate}</if>
  82. <if test="status != null "> and de.status = #{status}</if>
  83. <if test="lastActiveTime != null "> and de.last_active_time = #{lastActiveTime}</if>
  84. ${params.dataScope}
  85. </where>
  86. </select>
  87. <select id="selectDeviceById" parameterType="Long" resultMap="DeviceResult">
  88. select de.id, de.device_id, de.device_name, de.device_type_id, de.farm_id, de.model,
  89. de.manufacturer, de.install_date, de.status, de.last_active_time,d.dept_name,f.field_name,de.remark,longitude,latitude
  90. from device de
  91. left join sys_dept d on de.farm_id = d.dept_id
  92. left join field_device fd on de.id = fd.device_id and fd.status = '0'
  93. left join field f on fd.field_id = f.id
  94. where de.id = #{id} and (fd.status = 0 or fd.status is null);
  95. </select>
  96. <select id="selectDeviceByField" resultMap="DeviceResult">
  97. select d.*,fd.status as bindStatus,fd.binding_time as binding_time
  98. from field f
  99. left join field_device fd on f.id = fd.field_id
  100. left join device d on fd.device_id = d.id where f.id = #{id} and fd.status = 0;
  101. </select>
  102. <select id="selectUserById" resultMap="FieldPersonnelResult">
  103. select u.user_name,p.post_name,u.phonenumber,fp.binding_time,u.user_id
  104. from field f
  105. join field_personnel fp on f.id = fp.field_id
  106. left join sys_user u on fp.user_id = u.user_id
  107. left join sys_user_post sup on u.user_id = sup.user_id
  108. left join sys_post p on sup.post_id = p.post_id
  109. where f.id = #{id} and fp.status = 0
  110. </select>
  111. <select id="selectDeviceByunField" resultMap="DeviceResult">
  112. select d.*
  113. from device d
  114. left join field f on d.farm_id = f.farm_id
  115. where d.id not in (select device_id from field_device f where f.field_id = #{id} and f.status = 0) and
  116. d.farm_id = #{farmId}
  117. </select>
  118. <select id="selectDeviceByunUser" resultMap="FieldPersonnelResult">
  119. select u.user_name,p.post_name,u.phonenumber,u.user_id,sd.dept_name
  120. from sys_user u
  121. left join field_personnel fp on u.user_id = fp.user_id
  122. left join field f on f.id = fp.field_id
  123. left join sys_user_post sup on u.user_id = sup.user_id
  124. left join sys_post p on sup.post_id = p.post_id
  125. left join sys_dept sd on u.dept_id = sd.dept_id
  126. where u.user_id not in (select user_id from field_personnel where field_id = #{id} and status = 0)
  127. and u.dept_id = #{deptId}
  128. group by u.user_id,p.post_name,sd.dept_name
  129. </select>
  130. <insert id="insertDevice" parameterType="com.ruoyi.base.domain.Device" useGeneratedKeys="true" keyProperty="id">
  131. insert into device
  132. <trim prefix="(" suffix=")" suffixOverrides=",">
  133. <if test="deviceId != null and deviceId != ''">device_id,</if>
  134. <if test="deviceName != null and deviceName != ''">device_name,</if>
  135. <if test="deviceTypeId != null and deviceTypeId != ''">device_type_id,</if>
  136. <if test="farmId != null">farm_id,</if>
  137. <if test="model != null">model,</if>
  138. <if test="manufacturer != null">manufacturer,</if>
  139. <if test="longitude != null">longitude,</if>
  140. <if test="latitude != null">latitude,</if>
  141. <if test="installDate != null">install_date,</if>
  142. <if test="status != null">status,</if>
  143. <if test="lastActiveTime != null">last_active_time,</if>
  144. <if test="createTime != null">create_time,</if>
  145. <if test="updateTime != null">update_time,</if>
  146. <if test="remark != null">remark,</if>
  147. </trim>
  148. <trim prefix="values (" suffix=")" suffixOverrides=",">
  149. <if test="deviceId != null and deviceId != ''">#{deviceId},</if>
  150. <if test="deviceName != null and deviceName != ''">#{deviceName},</if>
  151. <if test="deviceTypeId != null and deviceTypeId != ''">#{deviceTypeId},</if>
  152. <if test="farmId != null">#{farmId},</if>
  153. <if test="model != null">#{model},</if>
  154. <if test="manufacturer != null">#{manufacturer},</if>
  155. <if test="longitude != null">#{longitude},</if>
  156. <if test="latitude != null">#{latitude},</if>
  157. <if test="installDate != null">#{installDate},</if>
  158. <if test="status != null">#{status},</if>
  159. <if test="lastActiveTime != null">#{lastActiveTime},</if>
  160. <if test="createTime != null">#{createTime},</if>
  161. <if test="updateTime != null">#{updateTime},</if>
  162. <if test="remark != null">#{remark},</if>
  163. </trim>
  164. </insert>
  165. <update id="updateDevice" parameterType="com.ruoyi.base.domain.Device">
  166. update device
  167. <trim prefix="SET" suffixOverrides=",">
  168. <if test="deviceId != null and deviceId != ''">device_id = #{deviceId},</if>
  169. <if test="deviceName != null and deviceName != ''">device_name = #{deviceName},</if>
  170. <if test="deviceTypeId != null and deviceTypeId != ''">device_type_id = #{deviceTypeId},</if>
  171. <if test="farmId != null">farm_id = #{farmId},</if>
  172. <if test="model != null">model = #{model},</if>
  173. <if test="manufacturer != null">manufacturer = #{manufacturer},</if>
  174. <if test="installDate != null">install_date = #{installDate},</if>
  175. <if test="status != null">status = #{status},</if>
  176. <if test="lastActiveTime != null">last_active_time = #{lastActiveTime},</if>
  177. <if test="createTime != null">create_time = #{createTime},</if>
  178. <if test="updateTime != null">update_time = #{updateTime},</if>
  179. <if test="remark != null">remark = #{remark},</if>
  180. </trim>
  181. where id = #{id}
  182. </update>
  183. <delete id="deleteDeviceById" parameterType="Long">
  184. delete from device where id = #{id}
  185. </delete>
  186. <delete id="deleteDeviceByIds" parameterType="String">
  187. delete from device where id in
  188. <foreach item="id" collection="array" open="(" separator="," close=")">
  189. #{id}
  190. </foreach>
  191. </delete>
  192. <select id="selectUniappDeviceList" parameterType="com.ruoyi.base.domain.dto.DeviceDto" resultMap="DeviceResult">
  193. select d.id, d.device_id, d.device_name, d.device_type_id, d.farm_id, d.model, d.longitude,
  194. d.latitude, d.manufacturer, d.install_date, d.status, d.last_active_time, d.create_time,
  195. d.update_time, d.remark, f.field_name
  196. from device d
  197. INNER JOIN field_device fd ON fd.device_id = d.id AND fd.status = 0
  198. INNER JOIN field f ON f.id = fd.field_id
  199. INNER JOIN field_personnel fp ON fp.field_id = f.id AND fp.status = 0
  200. <where>
  201. <if test="deviceId != null and deviceId != ''"> and d.device_id = #{deviceId}</if>
  202. <if test="fieldId != null and fieldId != ''"> and fd.field_id = #{fieldId}</if>
  203. <if test="userId != null and userId != ''"> and fp.user_id = #{userId}</if>
  204. <if test="deviceName != null and deviceName != ''"> and d.device_name like concat('%', #{deviceName}, '%')</if>
  205. <if test="deviceTypeId != null and deviceTypeId != ''">
  206. and d.device_type_id in
  207. <foreach item="id" collection="deviceTypeId.split(',')" open="(" separator="," close=")">
  208. #{id}
  209. </foreach>
  210. </if>
  211. <if test="status != null "> and d.status = #{status}</if>
  212. <if test="deviceQueryParams != null and deviceQueryParams != ''">
  213. and (
  214. d.device_name like concat('%', #{deviceQueryParams}, '%') OR
  215. d.device_id like concat('%', #{deviceQueryParams}, '%')
  216. )
  217. </if>
  218. </where>
  219. order by create_time desc
  220. </select>
  221. <select id="countDeviceStatistics" resultType="com.ruoyi.base.domain.vo.DeviceOverviewVO">
  222. SELECT
  223. COUNT(DISTINCT d.id) AS totalDevices,
  224. SUM(CASE WHEN d.status = 1 THEN 1 ELSE 0 END) AS onlineDevices,
  225. SUM(CASE WHEN d.status = 0 THEN 1 ELSE 0 END) AS offlineDevices
  226. FROM device d
  227. INNER JOIN field_device fd ON fd.device_id = d.id AND fd.status = 0
  228. INNER JOIN field f ON f.id = fd.field_id
  229. INNER JOIN field_personnel fp ON fp.field_id = f.id AND fp.status = 0
  230. <where>
  231. <if test="fieldId != null and fieldId != ''">
  232. AND fd.field_id = #{fieldId}
  233. </if>
  234. <if test="userId != null and userId != ''">
  235. AND fp.user_id = #{userId}
  236. </if>
  237. </where>
  238. </select>
  239. <select id="countDevicesByType" resultType="java.util.Map">
  240. SELECT
  241. d.device_type_id AS deviceTypeId,
  242. SUM( CASE WHEN d.STATUS = 1 THEN 1 ELSE 0 END ) AS online,
  243. SUM( CASE WHEN d.STATUS = 0 THEN 1 ELSE 0 END ) AS offline
  244. FROM
  245. device d
  246. LEFT JOIN field_device fd ON fd.device_id = d.id
  247. LEFT JOIN field_personnel fp ON fd.field_id = fp.field_id
  248. <where>
  249. <if test="fieldId != null and fieldId != ''">
  250. AND fd.field_id = #{fieldId}
  251. </if>
  252. <if test="userId != null and userId != ''">
  253. AND fp.user_id = #{userId}
  254. </if>
  255. AND fd.`status` = 0
  256. AND fp.`status` = 0
  257. </where>
  258. GROUP BY device_type_id
  259. </select>
  260. <select id="countAlertsByType" resultType="java.util.Map">
  261. SELECT
  262. d.device_type_id as deviceTypeId,
  263. COUNT(a.id) as alerts
  264. FROM
  265. device d
  266. LEFT JOIN
  267. device_alert a ON d.device_id = a.device_id AND a.status = 0
  268. <where>
  269. <if test="farmId != null and farmId != ''">
  270. AND d.farm_id = #{farmId}
  271. </if>
  272. <if test="fieldId != null and fieldId != ''">
  273. AND d.field_id = #{fieldId}
  274. </if>
  275. </where>
  276. GROUP BY d.device_type_id
  277. </select>
  278. </mapper>