attendance_record_dao.go
7.2 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
package dao
import (
"fmt"
"github.com/go-pg/pg/v10/orm"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
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"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/transform"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
"time"
)
type AttendanceRecordDao struct {
transactionContext *pgTransaction.TransactionContext
}
func NewAttendanceRecordDao(transactionContext *pgTransaction.TransactionContext) (*AttendanceRecordDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &AttendanceRecordDao{
transactionContext: transactionContext,
}, nil
}
}
// 生产工段的考勤记录
func (dao *AttendanceRecordDao) ProductWorkStationAttendanceRecord(companyId, orgId int, workStationId string, productTime time.Time) (int64, []*domain.ProductAttendanceRecord, error) {
tx := dao.transactionContext.PgTx
var productAttendanceRecordModels []*models.ProductAttendanceRecord
productAttendanceRecords := make([]*domain.ProductAttendanceRecord, 0)
query := sqlbuilder.BuildQuery(tx.Model(&productAttendanceRecordModels), map[string]interface{}{})
query.Where("company_id = ?", companyId)
query.Where("org_id = ?", orgId)
query.Where("work_station->>'workStationId' = ?", workStationId)
// 当天
query.Where("sign_in >= ?", utils.GetZeroTime(productTime))
query.Where("sign_in <?", utils.GetNextDayZeroTime(productTime))
query.Where("sign_in <= ?", productTime)
query.WhereGroup(func(q *orm.Query) (*orm.Query, error) {
query.WhereOr("sign_out >= ?", productTime)
query.WhereOr("sign_out=?", time.Time{})
return q, nil
})
query.DistinctOn("product_worker->>'userId',work_station->>'workStationId'")
query.SetOffsetAndLimit(domain.MaxQueryRow)
query.OrderExpr("product_worker->>'userId' asc ,work_station->>'workStationId' asc")
if count, err := query.SelectAndCount(); err != nil {
return 0, productAttendanceRecords, err
} else {
for _, productAttendanceRecordModel := range productAttendanceRecordModels {
if productAttendanceRecord, err := transform.TransformToProductAttendanceRecordDomainModelFromPgModels(productAttendanceRecordModel); err != nil {
return 0, productAttendanceRecords, err
} else {
productAttendanceRecords = append(productAttendanceRecords, productAttendanceRecord)
}
}
return int64(count), productAttendanceRecords, nil
}
}
// 近n天未审核的考勤记录
// fromLastHour 从前 n小时开始
// recentDay 近 n天未审核的数据
func (dao *AttendanceRecordDao) RecentUnApprovedAttendanceRecord(fromLastHour int, recentDay int) (int64, []*domain.ProductAttendanceRecord, error) {
tx := dao.transactionContext.PgTx
endTime := time.Now().Add(-(time.Hour * time.Duration(fromLastHour)))
beginTime := endTime.Add(-(time.Hour * time.Duration(recentDay*24)))
var productAttendanceRecordModels []*models.ProductAttendanceRecord
productAttendanceRecords := make([]*domain.ProductAttendanceRecord, 0)
query := sqlbuilder.BuildQuery(tx.Model(&productAttendanceRecordModels), map[string]interface{}{})
query.Where("sign_in <= ?", endTime)
query.Where("sign_in >= ?", beginTime)
query.Where("attendance_status = ?", domain.AttendanceNotApprove)
query.Where("work_time_before>0")
query.SetOffsetAndLimit(domain.MaxQueryRow)
query.SetOrderDirect("product_attendance_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
return 0, productAttendanceRecords, err
} else {
for _, productAttendanceRecordModel := range productAttendanceRecordModels {
if productAttendanceRecord, err := transform.TransformToProductAttendanceRecordDomainModelFromPgModels(productAttendanceRecordModel); err != nil {
return 0, productAttendanceRecords, err
} else {
productAttendanceRecords = append(productAttendanceRecords, productAttendanceRecord)
}
}
return int64(count), productAttendanceRecords, nil
}
}
func (dao *AttendanceRecordDao) WorkerAttendanceRecords(companyId, orgId, workerId int, workStationId string, beginTime, endTime time.Time) (int64, []*domain.ProductAttendanceRecord, error) {
tx := dao.transactionContext.PgTx
//endTime := time.Now()
//beginTime := endTime.Add(-(time.Hour * time.Duration(recentDay*24)))
var productAttendanceRecordModels []*models.ProductAttendanceRecord
productAttendanceRecords := make([]*domain.ProductAttendanceRecord, 0)
query := sqlbuilder.BuildQuery(tx.Model(&productAttendanceRecordModels), map[string]interface{}{})
query.Where("company_id =?", companyId)
query.Where("org_id =?", orgId)
query.Where("product_worker ->>'userId' = '?'", workerId)
query.Where("sign_in >= ?", beginTime)
query.Where("sign_in <= ?", endTime)
query.Where("attendance_status = ?", domain.AttendanceNotApprove)
if len(workStationId) > 0 {
query.Where("work_station ->>'workStationId' = ?", workStationId)
}
query.SetOffsetAndLimit(domain.MaxQueryRow)
query.SetOrderDirect("product_attendance_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
return 0, productAttendanceRecords, err
} else {
for _, productAttendanceRecordModel := range productAttendanceRecordModels {
if productAttendanceRecord, err := transform.TransformToProductAttendanceRecordDomainModelFromPgModels(productAttendanceRecordModel); err != nil {
return 0, productAttendanceRecords, err
} else {
productAttendanceRecords = append(productAttendanceRecords, productAttendanceRecord)
}
}
return int64(count), productAttendanceRecords, nil
}
}
func (dao *AttendanceRecordDao) WorkerAttendanceRecordsByProductDate(companyId, orgId, workerId int, productDate, beginTime, endTime time.Time) (int64, []*domain.ProductAttendanceRecord, error) {
tx := dao.transactionContext.PgTx
//endTime := time.Now()
//beginTime := endTime.Add(-(time.Hour * time.Duration(recentDay*24)))
var productAttendanceRecordModels []*models.ProductAttendanceRecord
productAttendanceRecords := make([]*domain.ProductAttendanceRecord, 0)
query := sqlbuilder.BuildQuery(tx.Model(&productAttendanceRecordModels), map[string]interface{}{})
query.Where("company_id =?", companyId)
query.Where("org_id =?", orgId)
query.Where("product_worker ->>'userId' = '?'", workerId)
query.Where("product_date = ?", productDate)
query.Where("sign_in >= ?", beginTime)
query.WhereGroup(func(query *orm.Query) (*orm.Query, error) {
query.Where("sign_out is null")
query.WhereOr("sign_out>?", endTime)
return query, nil
})
query.SetOrderDirect("product_attendance_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
return 0, productAttendanceRecords, err
} else {
for _, productAttendanceRecordModel := range productAttendanceRecordModels {
if productAttendanceRecord, err := transform.TransformToProductAttendanceRecordDomainModelFromPgModels(productAttendanceRecordModel); err != nil {
return 0, productAttendanceRecords, err
} else {
productAttendanceRecords = append(productAttendanceRecords, productAttendanceRecord)
}
}
return int64(count), productAttendanceRecords, nil
}
}