ustcyc
2025-01-07 90e30e1461f48721442d0391e46c2edde170d9e7
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?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.zhitan.model.mapper.ModelNodeMapper">
 
    <resultMap type="com.zhitan.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.zhitan.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.zhitan.basicdata.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.zhitan.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.zhitan.basicdata.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.zhitan.basicdata.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.zhitan.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}
        <if test="code != null  and code != ''">
            and code like concat('%', #{code}, '%')
        </if>
        <if test="name != null  and name != ''">
            and name like concat('%', #{name}, '%')
        </if>
    </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.zhitan.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.zhitan.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.zhitan.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.zhitan.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.zhitan.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.zhitan.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.zhitan.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>
    <select id="selectIndexByModelCodeAndNodeId"
            resultType="com.zhitan.model.domain.vo.ModelNodeIndexInfor">
        SELECT mn.node_id nodeId,
               mn."name",
               ni.index_id indexId
        FROM model_node mn
                 LEFT JOIN node_index ni ON ni.node_id = mn.node_id
        WHERE mn.model_code = #{modelCode}
          AND mn.node_id = #{nodeId}
    </select>
 
    <select id="getSettingDeviceIndex" resultType="com.zhitan.basicdata.domain.MeterImplement">
        SELECT distinct
               m."id",
               m.code,
               m.meter_name AS meterName,
               m.meter_type    meterType,
               m.wire_diameter    wireDiameter,
               m.max_allowable_power    maxAllowablePower
        FROM meter_implement m
                 LEFT JOIN energy_index ei ON ei.meter_id = m."id"
                 LEFT JOIN node_index ni ON ni.index_id = ei.index_id
        WHERE ni.node_id = #{nodeId}
        <if test="energyType!=null and energyType!=''">
            AND M.energy_type = #{energyType}
        </if>
    </select>
 
    <delete id="delIndexNodeIdAndIndexType">
        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}
        );
    </delete>
    <insert id="setIndexAndNodeId">
        <foreach collection="indexIds" item="indexId" separator=";">
            insert into node_index (node_id, index_id)
            values (#{nodeId}, #{indexId})
        </foreach>
    </insert>
 
    <select id="getModelNodeIndexIdByNodeId" resultType="com.zhitan.model.domain.vo.ModelNodeIndexInfor">
        SELECT mn.node_id  nodeId,
               mn."name"   "name",
               ni.index_id indexId,
               ei.energy_id energyId
        FROM "model_node" mn
        LEFT JOIN "node_index" ni ON mn.node_id = ni.node_id
        LEFT JOIN energy_index ei on ni.index_id = ei.index_id
        WHERE ni.node_id = #{nodeId}
        <if test="energyType!=null and energyType!=''">
            and ei.energy_id = #{energyType}
        </if>
    </select>
    <select id="getModelNodeByParentId" resultType="com.zhitan.model.domain.vo.ModelNodeIndexInfor">
        SELECT mn.node_id  nodeId,
               mn."name"   "name",
               ni.index_id indexId,
               ei.energy_id
        FROM "model_node" mn
                 LEFT JOIN "node_index" ni ON mn.node_id = ni.node_id
                 left join energy_index ei on ni.index_id = ei.index_id
        WHERE mn.parent_id = #{parentId} and ei.index_type = 'STATISTIC'
    </select>
    <select id="getFirstModeNodeInfo" resultType="com.zhitan.model.domain.ModelNode">
        <include refid="selectModelNodeVo"/>
        where model_code = #{modelCode}  AND parent_id is null
        order by order_num
    </select>
    <select id="selectIndexByNodeIds" resultType="com.zhitan.model.domain.vo.ModelNodeIndexInfor">
        SELECT mn.node_id  nodeId,
        mn."name"   "name",
        ni.index_id indexId,
        ei.energy_id
        FROM "model_node" mn
        LEFT JOIN "node_index" ni ON mn.node_id = ni.node_id
        left join energy_index ei on ni.index_id = ei.index_id
        <where>
            mn.model_code = #{modelCode}
          AND mn.node_id in
        <foreach collection="nodeIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        <if test="modelCode!=null and modelCode!='' and nodeIds.size>0">
            and ei.index_type='STATISTIC'
        </if>
 
        </where>
 
    </select>
</mapper>