pg_cooperation_contract_dao.go
7.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
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"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
"time"
)
type CooperationContractDao struct {
transactionContext *pgTransaction.TransactionContext
}
// GenerateContractNumber 生成共创合约编号
func (dao *CooperationContractDao) GenerateContractNumber() (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 count, err := query.AllWithDeleted().SelectAndCount(); err != nil {
return "", err
} else {
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
}
}
// 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 NewCooperationContractDao(transactionContext *pgTransaction.TransactionContext) (*CooperationContractDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &CooperationContractDao{
transactionContext: transactionContext,
}, nil
}
}