workshop_plan_completion_dao.go
3.0 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
package dao
import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
"time"
)
type WorkshopPlanCompletionRecordDao struct {
transactionContext *pgTransaction.TransactionContext
}
func (dao *WorkshopPlanCompletionRecordDao) Records(companyId, orgId int, workshopId int, begin, end time.Time, records interface{}) error {
tx := dao.transactionContext.PgTx
sql := fmt.Sprintf(`
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
--,created_at
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,8)
and created_at>=?
and created_at<?
), product_record_sum as(
select sum(weight) weight,plan_id from product_record
GROUP BY plan_id
)
select
plan_devoted->>'weight' plan_weight -- 计划重量
, coalesce(b.weight,0) real_weight --批次实际产能
,a.product_plan_id
,a.product_date
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 >=?
--and created_at<?
--order by created_at desc`)
if _, err := tx.Query(records, sql,
companyId, orgId, workshopId, begin, end,
companyId, orgId, workshopId, begin, end); err != nil {
return err
}
return nil
}
func (dao *WorkshopPlanCompletionRecordDao) FindOne(companyId, orgId int, workshopId int, begin time.Time) (*models.WorkshopPlanCompletionRecord, error) {
tx := dao.transactionContext.PgTx
var record = new(models.WorkshopPlanCompletionRecord)
q := tx.Model(record)
q.Where("company_id=?", companyId)
q.Where("org_id=?", orgId)
q.Where("workshop_id=?", workshopId)
q.Where("created_at=?", begin)
err := q.First()
if err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, domain.ErrorNotFound
} else {
return nil, err
}
}
return record, nil
}
func (dao *WorkshopPlanCompletionRecordDao) Save(record *models.WorkshopPlanCompletionRecord) error {
tx := dao.transactionContext.PgTx
if _, err := tx.Model(record).Insert(); err != nil {
return err
}
return nil
}
func (dao *WorkshopPlanCompletionRecordDao) Update(record *models.WorkshopPlanCompletionRecord) error {
tx := dao.transactionContext.PgTx
if _, err := tx.Model(record).WherePK().Update(); err != nil {
return err
}
return nil
}
func NewWorkshopPlanCompletionRecordDao(transactionContext *pgTransaction.TransactionContext) (*WorkshopPlanCompletionRecordDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &WorkshopPlanCompletionRecordDao{
transactionContext: transactionContext,
}, nil
}
}