liulingling.177216
2024-08-26 349f1cfc5fa77fbc636d542df0d8050fddec48c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dingzhuo.energy.data.model.mapper.ModelNodeMapper">
 
  <resultMap type="com.dingzhuo.energy.data.model.domain.ModelNode" id="ModelNodeResult">
    <result property="nodeId" column="node_id"/>
    <result property="code" column="code"/>
    <result property="name" column="name"/>
    <result property="parentId" column="parent_id"/>
    <result property="address" column="address"/>
    <result property="modelCode" column="model_code"/>
    <result property="nodeCategory" column="node_category"/>
  </resultMap>
 
  <sql id="selectModelNodeVo">
    select node_id, code, name, parent_id, address, model_code, node_category
    from model_node
  </sql>
 
  <select id="selectModelNodeList"
    parameterType="com.dingzhuo.energy.data.model.domain.ModelNode"
    resultMap="ModelNodeResult">
    <include refid="selectModelNodeVo"/>
    <where>
      <if test="code != null  and code != ''">and code like concat('%', #{code}, '%')</if>
      <if test="name != null  and name != ''">and name like concat('%', #{name}, '%')</if>
    </where>
    order by order_num
  </select>
 
  <select id="selectModelNodeById" parameterType="String" resultMap="ModelNodeResult">
    <include refid="selectModelNodeVo"/>
    where node_id = #{nodeId}
  </select>
  <select id="getModelNodeByModelCode" resultMap="ModelNodeResult">
    <include refid="selectModelNodeVo"/>
    where model_code = #{modelCode}
    order by order_num
  </select>
  <select id="getMaxOrder" resultType="java.lang.Integer">
    select COALESCE(max(order_num), 0)
    from model_node
    where parent_id = #{parentId}
  </select>
  <select id="modelNodeHasExist" resultType="java.lang.Integer">
    select count(*)
    from model_node
    where code = #{code}
      and model_code = #{modelCode};
  </select>
  <select id="modelNodeHasExistWhenUpdate" resultType="java.lang.Integer">
    select count(*)
    from model_node
    where code = #{code}
      and model_code = #{modelCode}
      AND node_id != #{nodeId};
  </select>
  <select id="getSettingDevice"
    resultType="com.dingzhuo.energy.basic.data.meter.domain.MeterImplement">
    select id, code, meter_name as meterName
    from meter_implement m
           left join node_device nd on m.id = nd.device_id
    where nd.node_id = #{nodeId};
  </select>
  <select id="getSettingIndex"
    resultType="com.dingzhuo.energy.data.model.domain.EnergyIndex">
    select ei.index_id as indexId, code, name, index_type as indexType
    from energy_index ei
           left join node_index ni on ei.index_id = ni.index_id
    where ni.node_id = #{nodeId}
  </select>
  <select id="getSettingEnergy"
    resultType="com.dingzhuo.energy.basic.data.enerInfoManage.domain.SysEnergy">
    select enerid, enersno, enername
    from sys_energy e
           left join node_energy ne on e.enerid = ne.energy_id
    where ne.node_id = #{nodeId};
  </select>
  <select id="getSettingProduct"
    resultType="com.dingzhuo.energy.basic.data.enerInfoManage.domain.SysProduct">
    select productid, productsno, productname
    from sys_product p
           left join node_product np on p.productid = np.product_id
    where np.node_id = #{nodeId};
  </select>
  <select id="getSettingIndexByType"
    resultType="com.dingzhuo.energy.data.model.domain.EnergyIndex">
    select ei.index_id as indexId, code, name, index_type as indexType
    from energy_index ei
           left join node_index ni on ei.index_id = ni.index_id
    where ni.node_id = #{nodeId}
      and ei.index_type = #{indexType}
  </select>
  <select id="getModelNodeByNodeCodes" resultMap="ModelNodeResult">
    <include refid="selectModelNodeVo"/>
    where code in
    <foreach item="nodeCode" collection="list" open="(" separator="," close=")">
      #{nodeCode}
    </foreach>
    order by order_num
  </select>
  <select id="getModelNodeByModelCodeWithAuth" resultMap="ModelNodeResult">
    <include refid="selectModelNodeVo"/>
    where model_code = #{modelCode}
    and (
      node_id in (
        select node_id from data_auth_user where user_id = #{userId} and model_code = #{modelCode}
      )
      or node_id in (
        select node_id from data_auth_role where role_id in (
          select cast(r.role_id as varchar) from sys_role r
          left join sys_user_role ur on ur.role_id = r.role_id
          left join sys_user u on u.user_id = ur.user_id
          where cast(u.user_id as VARCHAR) = #{userId}
        ) and model_code = #{modelCode}
      )
    )
    order by order_num
  </select>
 
  <insert id="insertModelNode"
    parameterType="com.dingzhuo.energy.data.model.domain.ModelNode">
    insert into model_node
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="nodeId != null  and nodeId != ''">node_id,</if>
      <if test="code != null  and code != ''">code,</if>
      <if test="name != null  and name != ''">name,</if>
      <if test="parentId != null  and parentId != ''">parent_id,</if>
      <if test="address != null  and address != ''">address,</if>
      <if test="modelCode != null  and modelCode != ''">model_code,</if>
      <if test="nodeCategory != null  and nodeCategory != ''">node_category,</if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="nodeId != null  and nodeId != ''">#{nodeId},</if>
      <if test="code != null  and code != ''">#{code},</if>
      <if test="name != null  and name != ''">#{name},</if>
      <if test="parentId != null  and parentId != ''">#{parentId},</if>
      <if test="address != null  and address != ''">#{address},</if>
      <if test="modelCode != null  and modelCode != ''">#{modelCode},</if>
      <if test="nodeCategory != null  and nodeCategory != ''">#{nodeCategory},</if>
    </trim>
  </insert>
  <insert id="setDevice">
    delete
    from node_device
    where node_id = #{nodeId};
    <foreach collection="deviceIds" item="deviceId" separator=";">
      insert into node_device (node_id, device_id)
      values (#{nodeId}, #{deviceId})
    </foreach>
  </insert>
  <insert id="setEnergy">
    delete
    from node_energy
    where node_id = #{nodeId};
    <foreach collection="energyIds" item="energyId" separator=";">
      insert into node_energy (node_id, energy_id)
      values (#{nodeId}, #{energyId})
    </foreach>
  </insert>
  <insert id="setProduct">
    delete
    from node_product
    where node_id = #{nodeId};
    <foreach collection="productIds" item="productId" separator=";">
      insert into node_product (node_id, product_id)
      values (#{nodeId}, #{productId})
    </foreach>
  </insert>
 
  <update id="updateModelNode"
    parameterType="com.dingzhuo.energy.data.model.domain.ModelNode">
    update model_node
    <trim prefix="SET" suffixOverrides=",">
      <if test="code != null  and code != ''">code = #{code},</if>
      <if test="name != null  and name != ''">name = #{name},</if>
      <if test="parentId != null  and parentId != ''">parent_id = #{parentId},</if>
      <if test="address != null  and address != ''">address = #{address},</if>
      <if test="modelCode != null  and modelCode != ''">model_code = #{modelCode},</if>
      <if test="nodeCategory != null and nodeCategory != ''">node_category = #{nodeCategory},</if>
    </trim>
    where node_id = #{nodeId}
  </update>
  <update id="updateModelNodeParent">
    update model_node
    set parent_id = #{parentId}
    where node_id = #{nodeId};
 
    update model_node
    set address = replace(address, (select address from model_node where node_id = #{nodeId}),
                          (select address ||
                                  (select code from model_node where node_id = #{nodeId}) ||
                                  ','
                           from model_node
                           where node_id = #{parentId}))
    where parent_id = #{nodeId}
       or node_id = #{nodeId};
  </update>
  <update id="updateModelNodeOrder">
    <foreach collection="orders" index="id" item="order" separator=";">
      update model_node
      set order_num = #{order}
      where node_id = #{id}
    </foreach>
  </update>
  <update id="setIndex">
    delete
    from node_index
    where node_id = #{nodeId} and index_id in (select ni.index_id
    from node_index ni left join energy_index ei on ni.index_id = ei.index_id
    where node_id = #{nodeId} and ei.index_type = #{indexType});
    <foreach collection="indexIds" item="indexId" separator=";">
      insert into node_index (node_id, index_id)
      values (#{nodeId}, #{indexId})
    </foreach>
  </update>
 
  <delete id="deleteModelNodeById" parameterType="String">
    delete
    from model_node
    where node_id = #{nodeId}
  </delete>
 
  <delete id="deleteModelNodeByIds" parameterType="String">
    delete from model_node where node_id in
    <foreach item="nodeId" collection="array" open="(" separator="," close=")">
      #{nodeId}
    </foreach>
  </delete>
  <delete id="delDevice">
    delete
    from node_device
    where node_id = #{nodeId} and device_id in
    <foreach item="deviceId" collection="deviceIds" open="(" separator="," close=")">
      #{deviceId}
    </foreach>
  </delete>
  <delete id="delEnergy">
    delete
    from node_energy
    where node_id = #{nodeId} and energy_id in
    <foreach item="energyId" collection="energyIds" open="(" separator="," close=")">
      #{energyId}
    </foreach>
  </delete>
  <delete id="delProduct">
    delete
    from node_product
    where node_id = #{nodeId} and product_id in
    <foreach item="productId" collection="productIds" open="(" separator="," close=")">
      #{productId}
    </foreach>
  </delete>
  <delete id="delIndex">
    delete
    from node_index
    where node_id = #{nodeId} and index_id in
    <foreach item="indexId" collection="indexIds" open="(" separator="," close=")">
      #{indexId}
    </foreach>
  </delete>
 
  <select id="getSettingIndexByWhere"
          resultType="com.dingzhuo.energy.data.model.domain.EnergyIndex">
    select ei.index_id as indexId, code, name, index_type as indexType
    from energy_index ei
           left join node_index ni on ei.index_id = ni.index_id
    where ni.node_id = #{nodeId}
    <if test="indexName!=null and indexName!=''">
      and ei.name like concat('%', #{indexName}, '%')
    </if>
  </select>
 
  <select id="listIndesxByCodeList" resultType="java.lang.String">
    SELECT
      index_id
    FROM
      "node_index"
    WHERE
      node_id IN
    <foreach item="nodeId" collection="nodeIds" open="(" separator="," close=")">
      #{nodeId}
    </foreach>
  </select>
 
  <select id="getModelNodeIndexIdRelationInforByCode"
          resultType="com.dingzhuo.energy.data.model.domain.vo.ModelNodeIndexInfor">
    SELECT
      mn.node_id nodeId,
      mn."name" name,
      ni.index_id indexId
    FROM
      "model_node" mn
      LEFT JOIN "node_index" ni ON mn.node_id = ni.node_id
    WHERE
      mn.model_code = #{code}
      AND   mn.parent_id IS NOT NULL
  </select>
 
  <select id="listModelNodeIndexIdRelationInforByParentId" resultType="com.dingzhuo.energy.data.model.domain.vo.ModelNodeIndexInfor">
    SELECT
      mn.node_id nodeId,
      mn."name" name,
      ni.index_id indexId
    FROM
      "model_node" mn
      LEFT JOIN "node_index" ni ON mn.node_id = ni.node_id
    WHERE
      mn.parent_id = #{parentId}
  </select>
 
  <select id="getModelNodeIndexIdRelationInforByNodeId"
          resultType="com.dingzhuo.energy.data.model.domain.vo.ModelNodeIndexInfor">
    SELECT
      mn.node_id nodeId,
      mn."name" name,
      ni.index_id indexId
    FROM
      "model_node" mn
      LEFT JOIN "node_index" ni ON mn.node_id = ni.node_id
    WHERE
      mn.node_id = #{nodeId}
  </select>
 
  <select id="getModelNodeByModelCodeByIndexCode" resultType="com.dingzhuo.energy.data.model.domain.ModelNode">
    SELECT
      node_id nodeId,
      code,
      "name",
      parent_id parentId,
      address,
      model_code modelCode,
      node_category nodeCategory
    FROM
      "model_node"
    WHERE
      model_code = #{indexCode}
      AND parent_id IS NULL
      LIMIT 1;
  </select>
 
</mapper>