pg_cooperation_contract_dao.go
11.3 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
package dao
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/transform"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
"time"
)
type CooperationContractDao struct {
transactionContext *pgTransaction.TransactionContext
}
// GenerateContractNumber 生成共创合约编号
func (dao *CooperationContractDao) GenerateContractNumber(queryOptions map[string]interface{}) (string, error) {
tx := dao.transactionContext.PgTx
var cooperationContractModels []*models.CooperationContract
query := tx.Model(&cooperationContractModels)
currentTime := time.Now()
todayZeroTime := utils.GetZeroTime(currentTime)
nextDayZeroTime := utils.GetNextDayZeroTime(currentTime)
query.Where("cooperation_contract.created_at >= ?", todayZeroTime)
query.Where("cooperation_contract.created_at < ?", nextDayZeroTime)
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query = query.Where(`cooperation_contract.company @> '{"companyId":"?"}'`, companyId)
}
if count, err := query.AllWithDeleted().SelectAndCount(); err != nil {
return "", err
} else {
if count < 1000 {
countStr := fmt.Sprintf("%03d", count+1)
timestamp := currentTime.Unix()
timeNow := time.Unix(timestamp, 0)
timeString := timeNow.Format("20060102")
timeString = timeString[2:len(timeString)]
contractNumber := "HY" + timeString + "#" + countStr
return contractNumber, nil
} else {
countStr := fmt.Sprintf("%d", count+1)
timestamp := currentTime.Unix()
timeNow := time.Unix(timestamp, 0)
timeString := timeNow.Format("20060102")
timeString = timeString[2:len(timeString)]
dividendsOrderNumber := "HY" + timeString + "#" + countStr
return dividendsOrderNumber, nil
}
}
}
// CheckUndertakerTypesUncheckedAvailable 校验合约承接对象是否可以删除
func (dao *CooperationContractDao) CheckUndertakerTypesUncheckedAvailable(queryOptions map[string]interface{}) (bool, error) {
tx := dao.transactionContext.PgTx
var cooperationContractUndertakerModels []*models.CooperationContractUndertaker
query := tx.Model(&cooperationContractUndertakerModels)
if cooperationContractNumber, ok := queryOptions["cooperationContractNumber"]; ok && cooperationContractNumber != "" {
query = query.Where("cooperation_contract_number = ?", cooperationContractNumber)
}
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query = query.Where(`cooperation_contract.company @> '{"companyId":"?"}'`, companyId)
}
if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
query = query.Where(`cooperation_contract.org @> '{"orgId":"?"}'`, orgId)
}
if user, ok := queryOptions["user"]; ok && user.(bool) != false {
query = query.Where("user_type = ?", 1)
}
if partner, ok := queryOptions["partner"]; ok && partner.(bool) != false {
query = query.Where("user_type = ?", 2)
}
ok, err := query.Exists()
return !ok, err
}
// CheckContractNumberAvailable 检验合约编号唯一性
func (dao *CooperationContractDao) CheckContractNumberAvailable(queryOptions map[string]interface{}) (bool, error) {
tx := dao.transactionContext.PgTx
var cooperationContractModels []*models.CooperationContract
query := tx.Model(&cooperationContractModels)
if cooperationContractNumber, ok := queryOptions["cooperationContractNumber"]; ok && cooperationContractNumber != "" {
query = query.Where("cooperation_contract_number = ?", cooperationContractNumber)
}
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query = query.Where(`cooperation_contract.company @> '{"companyId":"?"}'`, companyId)
}
if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
query = query.Where(`cooperation_contract.org @> '{"orgId":"?"}'`, orgId)
}
ok, err := query.Exists()
return !ok, err
}
// CheckContractNumberExist 检验合约编号是否存在
func (dao *CooperationContractDao) CheckContractNumberExist(queryOptions map[string]interface{}) (bool, error) {
tx := dao.transactionContext.PgTx
var cooperationContractModels []*models.CooperationContract
query := tx.Model(&cooperationContractModels)
if cooperationContractNumber, ok := queryOptions["cooperationContractNumber"]; ok && cooperationContractNumber != "" {
query = query.Where("cooperation_contract_number = ?", cooperationContractNumber)
}
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query = query.Where(`cooperation_contract.company @> '{"companyId":"?"}'`, companyId)
}
if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
query = query.Where(`cooperation_contract.org @> '{"orgId":"?"}'`, orgId)
}
ok, err := query.Exists()
return ok, err
}
// SearchCooperationContractByUndertaker 查询共创合约并根据承接人返回
func (dao *CooperationContractDao) SearchCooperationContractByUndertaker(queryOptions map[string]interface{}) (int, []*models.CooperationContractByUndertaker, error) {
tx := dao.transactionContext.PgTx
var cooperationContractByUndertakerModels []*models.CooperationContractByUndertaker
var cooperationUndertakerModels []*models.CooperationContractUndertaker
query := tx.Model(&cooperationUndertakerModels)
query.ColumnExpr("cooperation_contract_undertaker.cooperation_contract_undertaker_id")
query.ColumnExpr("cooperation_contract_undertaker.user_info->>'userCode' AS undertaker_code")
query.ColumnExpr("cooperation_contract_undertaker.user_info->>'userName' AS undertaker_name")
query.ColumnExpr("cooperation_contract_undertaker.user_info->>'userPhone' AS undertaker_phone")
query.ColumnExpr("cooperation_contract_undertaker.contract_attachment")
query.ColumnExpr("A.cooperation_contract_sponsor")
query.ColumnExpr("A.cooperation_contract_number")
query.ColumnExpr("A.cooperation_contract_name")
query.ColumnExpr("A.cooperation_contract_description")
query.ColumnExpr("A.department")
query.ColumnExpr("A.org")
query.ColumnExpr("A.status AS contract_status")
query.ColumnExpr("B.cooperation_mode_name")
if cooperationContractName, ok := queryOptions["cooperationContractName"]; ok && cooperationContractName != "" {
query = query.Where("A.cooperation_contract_name ilike ?", fmt.Sprintf("%%%s%%", cooperationContractName))
}
if sponsorName, ok := queryOptions["sponsorName"]; ok && sponsorName != "" {
query = query.Where(`A.cooperation_contract_sponsor->>'userName')::text LIKE ?`, fmt.Sprintf("%%%s%%", sponsorName))
}
query.Join("JOIN cooperation_contracts AS A ON A.cooperation_contract_number = cooperation_contract_undertaker.cooperation_contract_number")
query.Join("JOIN cooperation_modes AS B ON B.cooperation_mode_number = A.cooperation_mode_number")
query = query.Order("cooperation_contract_undertaker_id DESC")
if offset, ok := queryOptions["offset"]; ok && offset.(int64) != 0 {
offset := offset.(int64)
if offset > -1 {
query = query.Offset(int(offset))
}
} else {
query = query.Offset(0)
}
if limit, ok := queryOptions["limit"]; ok && limit.(int64) != 0 {
limit := limit.(int64)
if limit > -1 {
query = query.Limit(int(limit))
}
} else {
query = query.Limit(20)
}
if count, err := query.SelectAndCount(&cooperationContractByUndertakerModels); err != nil {
return 0, nil, err
} else {
return count, cooperationContractByUndertakerModels, nil
}
}
func (dao *CooperationContractDao) Find(queryOptions map[string]interface{}) (int64, []*domain.CooperationContract, error) {
tx := dao.transactionContext.PgTx
var cooperationContractModels []*models.CooperationContract
cooperationContracts := make([]*domain.CooperationContract, 0)
query := sqlbuilder.BuildQuery(tx.Model(&cooperationContractModels), queryOptions)
if cooperationContractNumber, ok := queryOptions["cooperationContractNumber"]; ok && cooperationContractNumber != "" {
query.Where("cooperation_contract_number ilike ?", fmt.Sprintf("%%%s%%", cooperationContractNumber))
}
if cooperationContractName, ok := queryOptions["cooperationContactName"]; ok && cooperationContractName != "" {
query.Where("cooperation_contract_name like ?", fmt.Sprintf("%%%s%%", cooperationContractName))
}
if departmentName, ok := queryOptions["departmentName"]; ok && departmentName != "" {
query.Where(`(cooperation_contract.department->>'departmentName')::test LIKE ?`, fmt.Sprintf("%%%s%%", departmentName))
}
if sponsorName, ok := queryOptions["sponsorName"]; ok && sponsorName != "" {
query.Where(`(cooperation_contract.cooperation_contract_sponsor->>'userName')::text LIKE ?`, fmt.Sprintf("%%%s%%", sponsorName))
}
if cooperationContractIds, ok := queryOptions["cooperationContractIds"]; ok && len(cooperationContractIds.([]int64)) != 0 {
query.Where("cooperation_contract_id in (?)", pg.In(cooperationContractIds))
}
if cooperationContractNumbers, ok := queryOptions["cooperationContractNumbers"]; ok && len(cooperationContractNumbers.([]string)) != 0 {
query.Where("cooperation_contract_number in (?)", pg.In(cooperationContractNumbers))
}
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query.Where("company->>'companyId' = '?'", companyId)
}
if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
query.Where("org->>'orgId' = '?'", orgId)
}
if orgIds, ok := queryOptions["orgIds"]; ok && len(orgIds.([]int64)) > 0 {
newOrgIds := utils.SliceItoa(orgIds.([]int64))
query.Where("org->>'orgId' in (?)", pg.In(newOrgIds))
}
if incentivesType, ok := queryOptions["incentivesType"]; ok && incentivesType.(int32) != 0 {
query.Where("incentives_type = ?", incentivesType)
}
offsetLimitFlag := true
if offsetLimit, ok := queryOptions["offsetLimit"]; ok {
offsetLimitFlag = offsetLimit.(bool)
}
if offsetLimitFlag {
query.SetOffsetAndLimit(20)
}
query.SetOrderDirect("cooperation_contract_id", "DESC")
var count int
var err error
if count, err = query.SelectAndCount(); err != nil {
return 0, cooperationContracts, err
}
for _, cooperationContractModel := range cooperationContractModels {
// 获取分红激励规则列表
var dividendsIncentivesRuleModels []*models.DividendsIncentivesRule
var moneyIncentivesRuleModels []*models.MoneyIncentivesRule
var cooperationContractUndertakerModels []*models.CooperationContractUndertaker
var cooperationContractRelevantModels []*models.CooperationContractRelevant
if cooperationContract, err := transform.TransformToCooperationContractDomainModelFromPgModels(
cooperationContractModel,
&models.CooperationMode{},
dividendsIncentivesRuleModels,
moneyIncentivesRuleModels,
cooperationContractRelevantModels,
cooperationContractUndertakerModels); err != nil {
return 0, cooperationContracts, err
} else {
cooperationContracts = append(cooperationContracts, cooperationContract)
}
}
return int64(count), cooperationContracts, nil
}
func NewCooperationContractDao(transactionContext *pgTransaction.TransactionContext) (*CooperationContractDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &CooperationContractDao{
transactionContext: transactionContext,
}, nil
}
}