李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
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
<?xml version="1.0" encoding="UTF-8"?>
 
<!--
  ~
  ~      Copyright (c) 2018-2025, ztt All rights reserved.
  ~
  ~  Redistribution and use in source and binary forms, with or without
  ~  modification, are permitted provided that the following conditions are met:
  ~
  ~ Redistributions of source code must retain the above copyright notice,
  ~  this list of conditions and the following disclaimer.
  ~  Redistributions in binary form must reproduce the above copyright
  ~  notice, this list of conditions and the following disclaimer in the
  ~  documentation and/or other materials provided with the distribution.
  ~  Neither the name of the pig4cloud.com developer nor the names of its
  ~  contributors may be used to endorse or promote products derived from
  ~  this software without specific prior written permission.
  ~  Author: ztt
  ~
  -->
 
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 
<mapper namespace="com.chinaztt.mes.aps.mapper.ApsCoreMapper">
 
 
    <resultMap id="operationBoMap" type="com.chinaztt.mes.aps.core.domain.OperationBo">
        <result property="id" column="id"/>
        <result property="no" column="no"/>
        <collection property="capabilityIds" ofType="long">
            <id column="capability_id"/>
        </collection>
    </resultMap>
 
    <resultMap id="ganttTaskVo" type="com.chinaztt.mes.aps.vo.GanttTaskVo">
        <result property="id" column="id"/>
        <result property="text" column="text"/>
        <result property="startDate" column="start_time"/>
        <result property="endDate" column="end_time"/>
        <result property="parent" column="resource_id"/>
        <result property="state" column="state"/>
        <result property="optaskNo" column="optask_no"/>
        <result property="partName" column="part_name"/>
        <result property="quantity" column="quantity"/>
        <result property="partNo" column="part_no"/>
        <result property="workCenter" column="work_center"/>
        <result property="customerOrderNo" column="customer_order_no"/>
        <result property="otcLineNo" column="otc_line_no"/>
        <result property="mpsNo" column="mps_no"/>
    </resultMap>
 
    <resultMap id="resourceVo" type="com.chinaztt.mes.aps.vo.ResourceVo">
        <result property="id" column="id"/>
        <result property="resName" column="resName"/>
        <result property="machineLoad" column="machine_load"/>
        <result property="workCenter" column="work_center"/>
    </resultMap>
 
    <select id="queryOperation" resultMap="operationBoMap">
        SELECT
            o.ID,
            o.operation_no "no",
            j.capability_id
        FROM
            technology_operation o
                LEFT JOIN aps_join_operation_capability j ON j.operation_id = o.ID
        WHERE
            o.id = #{operationId}
          and o.active = TRUE
    </select>
 
 
    <select id="queryOperationTaskByTimeRange" resultMap="ganttTaskVo">
        SELECT
        T.id,
        T.planned_start_date "start_time",
        t.planned_finish_date "end_time",
        T.resource_id,
        o."name" "text",
        t.state
        FROM
        production_operation_task T
        LEFT JOIN technology_routing_operation ro ON ro.ID = T.routing_operation_id
        LEFT JOIN technology_operation o ON o.ID = ro.operation_id
        where
        t.planned_start_date &lt; #{end}
        and t.planned_finish_date > #{start}
        <if test="resourceIds!=null and resourceIds.size>0">
            and t.resource_id in
            <foreach collection="resourceIds" item="id" index="index" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
    </select>
 
    <select id="querySceneTaskByTimeRange" resultMap="ganttTaskVo">
        SELECT
            t.id,
            t.start_time,
            t.end_time,
            t.resource_id,
            m.mo_no || ' ' || o."name" "text",
            'scene'                    state
        FROM aps_scene_task t
                 LEFT JOIN technology_routing_operation ro ON ro.ID = T.routing_operation_id
                 LEFT JOIN technology_operation o ON o.ID = ro.operation_id
                 LEFT JOIN plan_manufacturing_order m on m.id = t.order_id
        WHERE t.scene_id = #{sceneId}
          AND t.start_time &lt; #{end}
          AND t.end_time > #{start}
        <if test="resourceIds!=null and resourceIds.size>0">
            AND t.resource_id IN
            <foreach collection="resourceIds" item="id" index="index" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
    </select>
 
    <select id="queryResourceByGroupId" resultMap="resourceVo">
        WITH RECURSIVE T ( ID, rsc_group_name, parent_id ) AS (
        SELECT
            ID,
            rsc_group_name,
            parent_id
        FROM aps_resource_group WHERE ID = #{ groupId }
        UNION ALL
        SELECT
            t2.ID,
            t2.rsc_group_name,
            t2.parent_id
        FROM
            aps_resource_group t2
        JOIN T ON t2.parent_id = T.ID)
        SELECT DISTINCT
            r.ID,
            r.resource_name "resName"
        FROM
            T,
            aps_join_res_group j,
            aps_resource r
        WHERE
            T.ID = j.rsc_group_id
          AND j.resource_id = r.ID
          AND r.active = TRUE
        ORDER BY ID
    </select>
 
    <select id="getAllResourceVo" resultMap="resourceVo">
        SELECT DISTINCT
            r.ID,
            r.resource_name "resName"
        FROM
            aps_resource r
        WHERE r.active = TRUE
        ORDER BY ID
    </select>
 
    <select id="queryOperationTaskByTimeRangeGantt" resultMap="ganttTaskVo">
        SELECT
            opt.id,
            opt.planned_start_date  "start_time",
            opt.planned_finish_date "end_time",
            opt.workstation_id      resource_id,
            opt.optask_no,
            opt.planned_quantity    quantity,
            opt.work_center,
            bp.part_name,
            bp.part_no,
            too."name"              "text",
            pmps.mps_no,
            pco.customer_order_no,
            pco.otc_line_no,
            opt.state
        FROM
            production_operation_task opt
                LEFT JOIN technology_routing_operation ro ON ro.ID = opt.routing_operation_id
                LEFT JOIN production_operation_task_supply pots ON opt."id" = pots.operation_task_id
                LEFT JOIN plan_manufacturing_order pmo ON pots.mo_id = pmo."id"
                LEFT JOIN plan_master_production_schedule pmps ON pmps."id" = pmo.mps_id
                LEFT JOIN plan_join_model_customer pjmc
                          ON pjmc.model_id = pmps."id" AND pjmc.model = 'plan_master_production_schedule'
                LEFT JOIN plan_customer_order pco ON pco."id" = pjmc.customer_order_id
                LEFT JOIN technology_operation too ON too.ID = ro.operation_id
                LEFT JOIN basic_part bp on bp.id = opt.part_id
        WHERE
            opt.planned_start_date &lt; #{end}
          AND opt.planned_finish_date > #{start}
        <if test="workstationIds!=null and workstationIds.size>0">
            AND opt.workstation_id IN
            <foreach collection="workstationIds" item="id" index="index" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="stateList!=null and stateList.size>0">
            AND opt.state IN
            <foreach collection="stateList" item="id" index="index" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        ORDER BY opt.planned_finish_date:: DATE,opt.priority
    </select>
 
    <select id="queryResourceByGroupIdGantt" resultMap="resourceVo">
        SELECT DISTINCT
            bw.id,
            bw.name                                                                    AS "resName",
            bw.work_center,
            CASE
                WHEN EXTRACT(epoch FROM smax.planned_finish_date) -
                     EXTRACT(epoch FROM smin.planned_start_date) IS NULL OR
                     EXTRACT(epoch FROM smax.planned_finish_date) -
                     EXTRACT(epoch FROM smin.planned_start_date) = 0
                    THEN 0.00
                ELSE ROUND(CAST(ssum.work_hours * 100 / (EXTRACT(epoch FROM smax.planned_finish_date)
                    - EXTRACT(epoch FROM smin.planned_start_date)) AS NUMERIC), 2) END AS machine_load
        FROM
            basic_workstation bw
                LEFT JOIN (SELECT
                               MAX(planned_finish_date) planned_finish_date,
                               workstation_id
                           FROM production_operation_task
                           WHERE "state" IN ('07unsubmit', '01pending', '02inProgress', '03interrupted')
                           GROUP BY workstation_id) smax ON smax.workstation_id = bw.ID
                LEFT JOIN (SELECT
                               MIN(planned_start_date) planned_start_date,
                               workstation_id
                           FROM production_operation_task
                           WHERE "state" IN ('07unsubmit', '01pending', '02inProgress', '03interrupted')
                           GROUP BY workstation_id) smin ON smin.workstation_id = bw.ID
                LEFT JOIN (SELECT
                               SUM(EXTRACT(epoch FROM planned_finish_date) -
                                   EXTRACT(epoch FROM planned_start_date)) work_hours,
                               workstation_id
                           FROM
                               production_operation_task
                           WHERE
                               "state" IN ('07unsubmit', '01pending', '02inProgress', '03interrupted')
                           GROUP BY
                               workstation_id) ssum ON ssum.workstation_id = bw.ID
        WHERE bw.active = TRUE
        <if test="workStationList!=null and workStationList.size>0">
            AND bw."id" IN
            <foreach collection="workStationList" item="id" index="index" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="workcenter!=null ">
            AND bw.work_center = #{workcenter}
        </if>
        ORDER BY work_center
    </select>
 
    <select id="getAllResourceVoGantt" resultMap="resourceVo">
        SELECT DISTINCT
            r."id",
            r.name "resName"
        FROM
            basic_workstation r
        WHERE r.active = TRUE
        ORDER BY ID
    </select>
 
    <select id="getOperatioTaskMaxPlanEndTime" resultType="java.time.LocalDateTime">
        SELECT
            MAX(planned_finish_date)
        FROM production_operation_task
        WHERE "state" IN ('07unsubmit', '01pending', '02inProgress', '03interrupted')
          AND workstation_id = #{workstationId}
    </select>
 
    <select id="getWorkstationIdByWorkcenter" resultType="java.lang.Long">
        SELECT
            ar.workstation_id
        FROM aps_resource_duration ard
                 LEFT JOIN aps_resource ar ON ar."id" = ard.resource_id
        WHERE workstation_id IN (SELECT "id" FROM basic_workstation WHERE work_center = #{workCenter})
          AND ard.part_id = #{partId}
    </select>
</mapper>