board_show_dao.go
15.1 KB
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
431
432
433
434
435
package dao
import (
"fmt"
"github.com/go-pg/pg/v10"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
)
//车间看板(生产)的展示数据
type BoardShowDao struct {
transactionContext *pgTransaction.TransactionContext
}
func NewBoardShowDao(transactionContext *pgTransaction.TransactionContext) (*BoardShowDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &BoardShowDao{
transactionContext: transactionContext,
}, nil
}
}
type BoardWorkshopDesc struct {
WorkshopName string `json:"workshopName"`
Uname string `json:"uname"`
Lines string `json:"lines"`
Workon string `json:"workon"`
}
// WorkshopDesc 车间描述
func (d *BoardShowDao) WorkshopDesc(companyId int, orgId int, workshopId int) (*BoardWorkshopDesc, error) {
sqlStr := `with workshop as (
select a.workshop_name,a.uname,a.line->>'lineName' linename from (
select workshop_name,principal->>'userName' uname
, jsonb_array_elements(product_lines) line
from manufacture.workshop where company_id = ?
and org_id = ?
and workshop_id=?
) a
where a.line->>'removed' = '1'
)
select max(workshop_name) workshop_name
,max(uname) uname
,string_agg(linename, '\') lines,
'全天' workon
from workshop`
tx := d.transactionContext.PgTx
var result BoardWorkshopDesc
_, err := tx.QueryOne(&result, sqlStr, companyId, orgId, workshopId)
if err != nil {
return nil, err
}
return &result, nil
}
// 在岗人数
func (d *BoardShowDao) OnDutyCount(companyId int, orgId int, workshopId int) (int, error) {
sqlStr := `select count(distinct product_worker->>'userId') from manufacture.product_attendance_record
where company_id = ?
and org_id = ?
and work_station->>'workshopId'='?'
and sign_in>to_date(to_char(now(), 'YYYY-MM-DD'),'YYYY-MM-DD')`
var cnt int
tx := d.transactionContext.PgTx
_, err := tx.QueryOne(pg.Scan(&cnt), sqlStr, companyId, orgId, workshopId)
return cnt, err
}
// 员工效率排名
type BoardEmployeeWorkpieceRatio struct {
Uname string `json:"uname"`
Today string `json:"today"`
Yesterday string `json:"yesterday"`
History string `json:"history"`
}
// 员工效率排名
func (d *BoardShowDao) EmployeeWorkpieceRatio(companyId int, orgId int, workshopId int) (
[]BoardEmployeeWorkpieceRatio, error) {
sqlStr := `with employee_plan_product as(
select product_worker->>'userName' as uname,
(product_weigh - second_level_weigh - CAST(product_record_info->>'totalOtherSecondLevelWeigh' as decimal)) today
,CAST(product_record_info->>'yesterdayOutputWeight' as decimal) yesterday
,CAST(product_record_info->>'bestOutputWeight' as decimal) history
from manufacture.employee_product_record
where
company_id = ?
and org_id = ?
and work_station->>'workshopId'='?'
and work_station->>'sectionName' = '包装'
and product_record_info->>'productDate'=to_char(now(), 'YYYY-MM-DD')
),group_employee_plan_product as (
select uname,sum(today) today,sum(yesterday) yesterday,sum(history) history from employee_plan_product
group by uname
)
select * from group_employee_plan_product
order by today desc`
result := make([]BoardEmployeeWorkpieceRatio, 0)
tx := d.transactionContext.PgTx
_, err := tx.Query(&result, sqlStr, companyId, orgId, workshopId)
if err != nil {
return nil, err
}
return result, nil
}
// 时段产能
type BoardTimeSectionProductRecord struct {
Ts string `json:"ts"`
Total string `json:"total"`
Line string `json:"line"`
}
// TimeSectionProductRecord 时段产能
func (d *BoardShowDao) TimeSectionProductRecord(companyId int, orgId int, workshopId int, lineIdA int, lineIdB int) (
[]BoardTimeSectionProductRecord, error,
) {
sqlStr := `(WITH ts_product as(
select sum(a.weight) total,a.ts,max(line) line from (
select
cast(device_running_record_info->>'count' as DECIMAL) weight,
"replace"(to_char(created_at at time ZONE 'Asia/shanghai', 'HH24:') || cast(date_part('minute',created_at) as integer)/30*30, ':0', ':00') ts,
work_station->>'lineName' line
from manufacture.device_running_record
where
company_id = ?
and org_id = ?
and work_station->>'workshopId'='?'
and work_station->>'lineId' = '?'
and device_running_record_info->>'deviceType'='CCJ'
and created_at >to_date(to_char(now(), 'YYYY-MM-DD'),'YYYY-MM-DD')
) a
group by a.ts
order by ts
)
, ts_product_list as (
select d.ts,ts_product.total,line from (
select to_char(c.ts::timestamp,'HH24:MI') ts from (
select generate_series(a.end - interval '2.5 hour',
"replace"(to_char(a.end, 'yyyy-mm-dd HH24:') || cast(date_part('minute',a.end) as integer)/30*30+30, ':0', ':00')::timestamp,
'30 minute') ts from (
select to_timestamp(to_char(now() at time ZONE 'Asia/shanghai','yyyy-mm-dd HH24'),'yyyy-mm-dd HH24') as end
) a
) c
) d left join ts_product on d.ts = ts_product.ts
)
SELECT ts, coalesce(total,0) total ,'南瓜饼1线' line
from ts_product_list
)
union all
(WITH ts_product as(
select sum(a.weight) total,a.ts,max(line) line from (
select
cast(device_running_record_info->>'count' as DECIMAL) weight,
"replace"(to_char(created_at at time ZONE 'Asia/shanghai', 'HH24:') || cast(date_part('minute',created_at) as integer)/30*30, ':0', ':00') ts,
work_station->>'lineName' line
from manufacture.device_running_record
where
company_id = ?
and org_id = ?
and work_station->>'workshopId'='?'
and work_station->>'lineId' = '?'
and device_running_record_info->>'deviceType'='CCJ'
and created_at >to_date(to_char(now(), 'YYYY-MM-DD'),'YYYY-MM-DD')
) a
group by a.ts
order by ts
)
, ts_product_list as (
select d.ts,ts_product.total,line from (
select to_char(c.ts::timestamp,'HH24:MI') ts from (
select generate_series(a.end - interval '2.5 hour',
"replace"(to_char(a.end, 'yyyy-mm-dd HH24:') || cast(date_part('minute',a.end) as integer)/30*30+30, ':0', ':00')::timestamp,
'30 minute') ts from (
select to_timestamp(to_char(now() at time ZONE 'Asia/shanghai','yyyy-mm-dd HH24'),'yyyy-mm-dd HH24') as end
) a
) c
) d left join ts_product on d.ts = ts_product.ts
)
SELECT ts, coalesce(total,0) total ,'南瓜饼2线' line
from ts_product_list
)`
condition := []interface{}{
companyId, orgId, workshopId, lineIdA,
companyId, orgId, workshopId, lineIdB,
}
result := make([]BoardTimeSectionProductRecord, 0)
tx := d.transactionContext.PgTx
_, err := tx.Query(&result, sqlStr, condition...)
return result, err
}
// 今日昨日历史最佳
type BoardTopShow struct {
Today string `json:"today"`
Yesterday string `json:"yesterday"`
History string `json:"history"`
}
// TopShop 今日昨日历史最佳
func (d *BoardShowDao) TopShow(companyId int, orgId int, workshopId int) (*BoardTopShow, error) {
sqlStr := `with today_product as (
select
(case when product_record_type=1 then cast(product_record_info->>'weigh' as DECIMAL)
ELSE -(cast(product_record_info->>'weigh' as DECIMAL))
END) as weight,
cast(product_record_info->>'productPlanId' as INTEGER) plan_id
from manufacture.product_records
where company_id = ?
and org_id = ?
and work_station->>'workshopId'='?'
and product_record_type in (1,2)
and created_at>=to_date(to_char(now(), 'YYYY-MM-DD'),'YYYY-MM-DD')
) ,tmp_today_product as(
select
(select COALESCE(sum(weight),0) weight from today_product) today,
(select COALESCE(sum(real),0) from manufacture.workshop_plan_completion_record where
company_id =?
and org_id =?
and created_at > CURRENT_DATE -1
and workshop_id = ?
limit 1
) yesterday,
(select max(real) from manufacture.workshop_plan_completion_record where workshop_id = ?) history
)
select today,
cast(yesterday as DECIMAL) yesterday,
(case when today > history then today else history end) history
from tmp_today_product`
condition := []interface{}{
companyId, orgId, workshopId,
companyId, orgId, workshopId, workshopId,
}
var result BoardTopShow
tx := d.transactionContext.PgTx
_, err := tx.QueryOne(&result, sqlStr, condition...)
return &result, err
}
// 二级品占比
type BoardProportionOfSecondLevel struct {
Sname string `json:"sname"` //品名
Rate string `json:"rate"` //占比
}
// 二级品占比
func (d *BoardShowDao) ProportionOfSecondLevel(companyId int, orgId int, workshopId int) (
[]BoardProportionOfSecondLevel, error) {
sqlStr := `with item_product as (
select sum(a.weight) item_total,max(sname) sname from (
select cast(product_record_info->>'weigh' as DECIMAL) weight
,work_station->>'sectionName' sname
,work_station->>'workStationId' workStationId
from manufacture.product_records
where company_id = ?
and org_id = ?
and work_station->>'workshopId'='?'
and product_record_type = 8
and product_record_info->>'approveStatus'='2'
and created_at >=to_date(to_char(now(), 'YYYY-MM-DD'),'YYYY-MM-DD')
and work_station->>'sectionName' in ('制模','成型','穿串','包装')
) a
group by a.workStationId
)
,item_product_rate as(
select sname,round(item_total/(select sum(item_total) from item_product)*100, 0) as rate from item_product
)
select b.sname, coalesce(b.rate,0) rate from item_product_rate b`
condition := []interface{}{
companyId, orgId, workshopId,
}
result := make([]BoardProportionOfSecondLevel, 0)
tx := d.transactionContext.PgTx
_, err := tx.Query(&result, sqlStr, condition...)
return result, err
}
// 工段生产信息
type BoardSectionProductInfo struct {
Sname string `json:"sname"`
ProductWeight string `json:"productWeight"`
SecondLevelWeight string `json:"secondLevelWeight"`
TotalWeigh string `json:"totalWeigh"`
Qu string `json:"qu"`
Sort string `json:"sort"` //排名
}
// 工段生产信息
func (d *BoardShowDao) SectionProductInfo(companyId int, orgId int, workshopId int) (
[]BoardSectionProductInfo, error) {
sqlStr := `with section_product as(
select
work_station->>'sectionName' section, --工段
CAST(product_record_info->>'outputWeight' as decimal) product_weigh, --产出量
CAST(product_record_info->>'secondLevelWeight' as decimal) second_level_weigh,
CAST(product_record_info->>'qualificationRate' as decimal) qu
--二级品
from manufacture.workshop_product_record
where company_id = ?
and org_id = ?
and work_station->>'workshopId'='?'
and product_date =to_char(now(), 'YYYY-MM-DD')
),
all_section_product as(
select b.sname,
COALESCE(a.product_weigh,0) product_weigh,
COALESCE(a.second_level_weigh,0) second_level_weigh,
COALESCE(a.qu,0) qu
from section_product a right join (
select unnest(ARRAY ['制模','成型','穿串','包装']) sname
) b on a.section = b.sname
),group_section_product as(
select
sname,
sum(product_weigh) product_weigh,
sum(second_level_weigh) second_level_weigh
,sum(product_weigh+second_level_weigh) total_weigh
from all_section_product
GROUP BY sname
)
select *,
(case when product_weigh=0 or total_weigh=0 then 0 else round(product_weigh*100/total_weigh,1) end) qu,
(case when sname = '制模' then 1
when sname='成型' then 2
when sname='穿串' then 3 else 4 end) sort
from group_section_product
order by sort`
condition := []interface{}{
companyId, orgId, workshopId,
}
result := make([]BoardSectionProductInfo, 0)
tx := d.transactionContext.PgTx
_, err := tx.Query(&result, sqlStr, condition...)
return result, err
}
// 当前计划达成率
func (d *BoardShowDao) WorkshopPlanCompletionCurrent(companyId int, orgId int, workshopId int) (string, error) {
sqlStr := `select rate rate from manufacture.workshop_plan_completion_record where
company_id = ?
and org_id = ?
and workshop_id=?
order by created_at desc
limit 1`
condition := []interface{}{
companyId, orgId, workshopId,
}
var result string
tx := d.transactionContext.PgTx
_, err := tx.QueryOne(pg.Scan(&result), sqlStr, condition...)
return result, err
}
type PlanCompletion5Day struct {
Ts string `jsonL:"ts"`
Total string `json:"total"`
}
// 近5天 计划达成率
func (d *BoardShowDao) WorkshopPlanCompletion5Day(companyId int, orgId int, workshopId int) ([]PlanCompletion5Day, error) {
sqlStr := `with ts_product as (
select rate,
to_char(created_at,'mm-dd') ts
from manufacture.workshop_plan_completion_record
where company_id = ?
and org_id = ?
and workshop_id= ?
and created_at > (now()-interval '6d')
)
,ts_product_list as (
select d.ts,ts_product.rate from (
select to_char(c.ts::timestamp,'mm-dd') ts from (
select generate_series(now() - interval '4 day',now(),'1 day') ts
) c ) d left join ts_product on d.ts = ts_product.ts
)
SELECT ts, coalesce(rate,0) total
from ts_product_list`
result := make([]PlanCompletion5Day, 0)
condition := []interface{}{
companyId, orgId, workshopId,
}
tx := d.transactionContext.PgTx
_, err := tx.Query(&result, sqlStr, condition...)
return result, err
}
// product_plan 批次产能
func (d *BoardShowDao) ProductPlan(companyId int, orgId int, workshopId int) ([]PlanCompletion5Day, error) {
sqlStr := `with product_record as (
select
(case when product_record_type=1 then cast(product_record_info->>'weigh' as DECIMAL)
ELSE -(cast(product_record_info->>'weigh' as DECIMAL))
END) as weight,
cast(product_record_info->>'productPlanId' as INTEGER) plan_id
from manufacture.product_records
where company_id =?
and org_id = ?
and work_station->>'workshopId'='?'
and work_station->>'sectionName'='包装'
and product_record_type in (1,2)
and created_at>to_date(to_char((now() -interval '3 day'), 'YYYY-MM-DD'),'YYYY-MM-DD') --'2022-01-01' --
), product_record_sum as(
select sum(weight) weight,plan_id
from product_record
GROUP BY plan_id
)
select
a.product_plan_id
,batch_number --批次号
,plan_product_name --品名
,machine --机台
,coalesce(plan_devoted->>'unit','') unit --规格
,cast(coalesce(plan_devoted->>'quantity','0') as DECIMAL) quantity -- 数量
,cast(coalesce(plan_devoted->>'weight','0')as DECIMAL) plan_weight -- 计划重量
, coalesce(b.weight,0) real_weight --批次实际产能
from manufacture.product_plan a left join product_record_sum b on a.product_plan_id = b.plan_id
where company_id = ?
and org_id = ?
and workshop->>'workshopId'='?'
and product_date >to_date(to_char(now() -interval '3 day', 'YYYY-MM-DD'),'YYYY-MM-DD')
and cast(coalesce(plan_devoted->>'weight','0')as DECIMAL) >0
order by created_at desc`
result := make([]PlanCompletion5Day, 0)
condition := []interface{}{
companyId, orgId, workshopId,
companyId, orgId, workshopId,
}
tx := d.transactionContext.PgTx
_, err := tx.Query(&result, sqlStr, condition...)
return result, err
}