yangys
2024-10-24 16f093ea132a823e255f446c225ce6b63fdb9eeb
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
<?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.qianwen.mdc.mapper.MachineMapper">
  <resultMap id="BaseResultMap" type="com.qianwen.mdc.domain.Machine">
  
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="ip" jdbcType="VARCHAR" property="ip" />
    <result column="port" jdbcType="INTEGER" property="port" />
    <result column="protocol_id" jdbcType="INTEGER" property="typeId" />
    <result column="workshop_id" jdbcType="INTEGER" property="workshopId" />
    <result column="section_id" jdbcType="INTEGER" property="sectionId" />
    <result column="trans_mode" jdbcType="VARCHAR" property="transMode" />
    
    <result column="is_collect" jdbcType="INTEGER" property="isCollect" />
    <result column="concern" jdbcType="INTEGER" property="concern" />
    <result column="dnc_name" jdbcType="VARCHAR" property="dncName" />
    <result column="shift_type" jdbcType="INTEGER" property="shiftType" />
  </resultMap>
    <!-- 
  <select id="queryMachineListPortNotNUll" resultType="com.qianwen.mdc.domain.Machine">
        select
            a.id,
            a.name,
            a.ip,
            a.port,
            a.position,
            a.protocol_id typeId,
            a.workshop_id workshopId,
            a.section_id sectionId,
            a.monitor_port monitorPort,
            a.is_collect isCollect,
            a.shift_type shiftType,
            b.name workshop,
            c.name section,
            d.state status,
            d.var1 cycleCount,
            d.var2 cycleTime
        from machine a
        left join workshop b
        on a.workshop_id = b.id
        left join section c
        on a.section_id = c.id
        left join (select max(id) as id,machine_id,state,var1,var2 from machine_status group by machine_id) d
        on a.id = d.machine_id
        where a.monitor_port is not null
         <if test="workshopId != null">
            and a.workshop_id = #{workshopId}
         </if>
         <if test="sectionId != null">
            and a.section_id = #{sectionId}
         </if>
         
         <if test="concernFlag != null">
            and a.concern = #{concernFlag}
         </if>
         <if test="id != null">
            and a.id = #{id}
         </if>
        order by a.id
    </select>
     -->
    <!-- 原方法名称:queryMachineList,由新方法listWithStatus替换 -->
  <select id="listWithStatus" resultType="com.qianwen.mdc.dto.machine.MachineFullDTO">
        select
            a.id,
            acc.machine_name as machineName,
            a.ip,
            a.port,
            a.protocol_id typeId,
            a.workshop_id workshopId,
            a.section_id sectionId,
            a.is_collect isCollect,
            a.concern concern,
            a.shift_type shiftType,
            acc.pic,
            b.name workshop,
            c.name section,
            e.status status,
            e.cycle_count cycleCount,
            e.cycle_time cycleTime,
            e.machine_on_time machineOnTime,
            e.var4,
            e.var5
        from machine a
        inner join machine_account acc on a.id=acc.machine_id
        left join workshop b
        on a.workshop_id = b.id
        left join section c
        on a.section_id = c.id
        left join (select max(id) as id,machine_id from machine_status group by machine_id) d on a.id = d.machine_id
        left join machine_status e on e.id = d.id        
        <where>
          <if test="plantId != null">
          and plant_id=#{plantId}
          </if>
        <if test="machineName != null and machineName != ''">
            <bind name="name" value="'%' + machineName + '%'" />
            and acc.machine_name like #{machineName}
         </if>
         <if test="workshopId != null">
            and a.workshop_id = #{workshopId}
         </if>
         
         <if test="concernFlag != null">
            and a.concern = #{concernFlag}
         </if>
         
         <if test="status != null and status != ''">
            and e.status = #{status}
         </if>
        </where>
        order by a.id
    </select>
    
    <select id="queryPage" resultType="com.qianwen.mdc.dto.machine.MachineFullDTO">
          select
            a.id,
            acc.machine_name machineName,
            a.ip,
            a.port,
            a.dnc_name dncName,
            acc.type,
            acc.category,
            a.protocol_id protocolId,
            p.name protocolName,
            p.version protocolVersion,
            a.workshop_id workshopId,
            a.section_id sectionId,
            a.is_collect isCollect,
            a.concern concern,
            a.shift_type shiftType,
            a.trans_mode transMode,
            b.name workshop,
            c.name section
        from machine a
        inner join machine_account acc on a.id=acc.machine_id
        left join workshop b on a.workshop_id = b.id
        left join section c on a.section_id = c.id      
        left join machine_protocol p on a.protocol_id=p.id
        <where>
            <if test="dto.plantId != null">
               a.plant_id=#{dto.plantId}
             </if>
              
             <if test="dto.machineName != null and dto.machineName != ''">
                <bind name="machineName" value="'%' + dto.machineName + '%'" />
                and acc.machine_name like #{machineName}
             </if>
             <if test="dto.ip != null and dto.ip != ''">
                <bind name="ip" value="'%' + dto.ip + '%'" />
                and a.ip like #{ip}
             </if>
             <if test="dto.type != null and dto.type != ''">
                and acc.type=#{dto.type}
             </if>
             <if test="dto.transMode != null">
                and a.trans_mode=#{dto.transMode}
             </if>
             <if test="dto.category != null and dto.category != ''">
                and acc.category=#{dto.category}
             </if>
             <if test="dto.workshopId != null">
                and a.workshop_id = #{dto.workshopId}
             </if>
             <if test="dto.sectionId != null">
                and a.section_id = #{dto.sectionId}
             </if>
             
        </where>
        order by a.id
  </select>
  
  <update id="clearPlantIdInPlant">
           update machine
           set plant_id = null where plant_id=#{plantId,jdbcType=INTEGER}
    </update>
    
    <!-- 机床类型查询(查询记录数),计划不用了,注释 -->
    <!-- 
    <select id="selectCountOfMachineAndTypes" resultType="int">
        select
        count(*)
        from machine m
        left join mdc_machine_account a on a.machine_id = m.id
        left join mdc_machine_types t on t.id = a.types_id
        left join workshop w on w.id = m.workshop_id
        left join section s on s.id = m.section_id
        <where>
            <if test="workshop != null and workshop != ''">
                and w.name = #{workshop}
            </if>
            <if test="section != null and section != ''">
                and s.name = #{section}
            </if>
            <if test="list != null and list.size() > 0">
                and m.name in
                <foreach collection="list" open="(" separator="," close=")" item="machineName">
                    #{machineName}
                </foreach>
            </if>
        </where>
    </select>
     -->
    <!-- 机床类型查询,计划不用了,注释 -->
    <!-- 
    <select id="selectMachineAndTypes" resultType="java.util.HashMap">
        select
        m.id machineId,
        m.name machineName,
        w.name workshopName,
        s.name sectionName,
        t.id typesId,
        t.name typesName
        from machine m
        left join mdc_machine_account a on a.machine_id = m.id
        left join mdc_machine_types t on t.id = a.types_id
        left join workshop w on w.id = m.workshop_id
        left join section s on s.id = m.section_id
        <where>
            <if test="workshop != null and workshop != ''">
                and w.name = #{workshop}
            </if>
            <if test="section != null and section != ''">
                and s.name = #{section}
            </if>
            <if test="list != null and list.size() > 0">
                and m.name in
                <foreach collection="list" open="(" separator="," close=")" item="machineName">
                    #{machineName}
                </foreach>
            </if>
            <if test="machineType != null and machineType != ''">
                and t.name = #{machineType}
            </if>
        </where>
        order by w.id, s.id, m.name
        <if test="limit != null">
            limit #{limit}, #{offset}
        </if>
    </select>
 -->
    <select id="updateMachineAndTypes" resultType="int">
        update mdc_machine_types t,
            mdc_machine_account a
            left join mdc_machine m on m.id = a.machine_id
            left join mdc_workshop w on w.id = m.workshop_id
            left join mdc_section s on s.id = m.section_id
            set a.types_id = t.id
        <where>
            t.name = #{machineType}
            <if test="workshop != null and workshop != ''">
                and w.name = #{workshop}
            </if>
            <if test="section != null and section != ''">
                and s.name = #{section}
            </if>
            <if test="list != null and list.size() > 0">
                and m.name in
                <foreach collection="list" open="(" separator="," close=")" item="machineName">
                    #{machineName}
                </foreach>
            </if>
        </where>
    </select>
 
    <select id="getByWorkshopSection" resultType="String">
        select m.name from machine m
        left join workshop w
        on m.workshop_id = w.id
        left join section s
        on m.section_id = s.id
        <where>
            <if test="workshop != null and workshop != ''">
                and w.name = #{workshop}
            </if>
            <if test="section != null and section != ''">
                and s.name = #{section}
            </if>
        </where>
        order by m.name
    </select>
 
    <!-- 
    <insert id="addAlarmHistory" parameterType="com.qianwen.mdc.domain.MdcMachineAlarmHistory">
        insert into mdc_machine_alarm_history
        <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="id != null">
        id,
        </if>
        <if test="machineId != null">
        machine_id,
        </if>
        <if test="alarmType != null and alarmType != ''">
        alarm_type,
        </if>
        <if test="alarmHandleResult != null and alarmHandleResult != ''">
        alarm_handle_result,
        </if>
        create_date,
        <if test="createEmp != null and createEmp != ''">
        create_emp,
        </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
        <if test="id != null">
          #{id,jdbcType=INTEGER},
        </if>
        <if test="machineId != null">
          #{machineId,jdbcType=INTEGER},
          </if>
           <if test="alarmType != null and alarmType != ''">
          #{alarmType,jdbcType=VARCHAR},
          </if>
          <if test="alarmHandleResult != null and alarmHandleResult != ''">
          #{alarmHandleResult,jdbcType=VARCHAR},
          </if>
          NOW(),
          <if test="createEmp != null and createEmp != ''">
          #{createEmp,jdbcType=VARCHAR},
          </if>
        </trim>
     </insert>
 -->
     <update id="updateMachineStatus">
           update mdc_machine_status
           set state = #{state,jdbcType=VARCHAR}
           <where>
              <if test="machineId != null">
              AND machine_id = #{machineId,jdbcType=INTEGER}
              </if>
           </where>
    </update>
    
 
    <select id="selectMachineDetail" resultType="com.qianwen.mdc.domain.MachineStatus">
        select
            a.id,
            a.machine_id machineId,
            a.time,
            a.state,
            a.spindle_speed spindleSpeed,
            a.spindle_override spindleOverride,
            a.spindle_load spindleLoad,
            a.feedrate_override feedrateOverride,
            a.feed_rate feedRate,
            a.rapid_feed rapidFeed,
            a.prog_name progName,
            a.alarm_no alarmNo,
            a.alarm_msg alarmMsg,
            a.mode mode,
            a.tool_no toolNo,
            a.tool_name toolName,
            a.oper_id operId,
            a.var1 cycleCount,
            a.var2,
            a.var3,
            a.var4,
            a.var5,
            a.var6
        from mdc_machine_status a
        <where>
         <if test="machineId != null">
            and a.machine_id = #{machineId}
         </if>
        </where>
        order by id desc limit 1
    </select>
 
    <select id="selectAlarmList" resultType="com.qianwen.mdc.domain.MachineStatus">
        select
            a.prog_name progName,
            a.alarm_no alarmNo,
            a.alarm_msg alarmMsg
        from mdc_machine_status a
        <where>
         <if test="machineId != null">
            and a.machine_id = #{machineId}
         </if>
        </where>
        order by id desc
    </select>
 
    <select id="selectStatusMonitor" resultType="java.util.HashMap">
        select
            m.name,
            m.first_position position,
            m.shift_type shiftType,
            a.cnc_system cncSystem,
            a.types_id typesId,
            mt.name mdcType,
            s.state status,
            s.prog_name progName,
            s.var1 cycleCount,
            s.var2 cycleTime,
            convert(s.var2, signed)/(36 * 24) mdcRate,
            s.feed_rate feedRate,
            s.spindle_speed spindleSpeed,
            s.spindle_override spindleOverride,
            concat(w.name, '-', se.name) section,
            q.mdcTypeRate
        from
            machine m
            left join mdc_machine_account a on m.id = a.machine_id
            left join mdc_machine_types mt on a.types_id = mt.id
            left join mdc_machine_status s on m.id = s.machine_id
            left join mdc_workshop w on m.workshop_id = w.id
            left join mdc_section se on m.section_id = se.id
            left join (
                select
                    a.types_id typesid,
                    avg(convert(s.var2, signed)/(36 * 24)) mdcTypeRate
                from
                    mdc_machine_account a
                    left join mdc_machine_status s on a.machine_id = s.machine_id
                where
                    s.id in (select max(id) id from mdc_machine_status group by machine_id)
                group by
                    a.types_id
            ) q on q.typesid = a.types_id
        where
            s.id in (select max(id) id from mdc_machine_status group by machine_id)
    </select>
</mapper>