pg_cooperation_project_dao.go
4.7 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
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 CooperationProjectDao struct {
transactionContext *pgTransaction.TransactionContext
}
// GenerateProjectNumber 生成共创项目编码
func (dao *CooperationProjectDao) GenerateProjectNumber(queryOptions map[string]interface{}) (string, error) {
tx := dao.transactionContext.PgTx
var cooperationProjectModels []*models.CooperationProject
query := tx.Model(&cooperationProjectModels)
currentTime := time.Now()
todayZeroTime := utils.GetZeroTime(currentTime)
nextDayZeroTime := utils.GetNextDayZeroTime(currentTime)
query.Where("cooperation_project.created_at >= ?", todayZeroTime)
query.Where("cooperation_project.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 := "XM" + 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)]
contractNumber := "XM" + timeString + "#" + countStr
return contractNumber, nil
}
}
}
// CheckUndertakerTypesUncheckedAvailable 校验项目承接对象是否可以删除
func (dao *CooperationProjectDao) CheckUndertakerTypesUncheckedAvailable(queryOptions map[string]interface{}) (bool, error) {
tx := dao.transactionContext.PgTx
var cooperationContractModels []*models.CooperationContract
// 获取和项目关联的合约
query := tx.Model(&cooperationContractModels)
if cooperationProjectNumber, ok := queryOptions["cooperationProjectNumber"]; ok && cooperationProjectNumber != "" {
query = query.Where("cooperation_project_number = ?", cooperationProjectNumber)
}
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 count, err := query.SelectAndCount(); err != nil {
return false, err
} else {
if count > 0 {
for _, cooperationContractModel := range cooperationContractModels {
// 获取合约相关承接人
var cooperationContractUndertakerModels []*models.CooperationContractUndertaker
queryUndertaker := tx.Model(&cooperationContractUndertakerModels)
queryUndertaker.Where("cooperation_contract_number = ?", cooperationContractModel.CooperationContractNumber)
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, err2 := queryUndertaker.Exists()
return !ok, err2
}
}
}
return false, nil
}
// CheckProjectNumberAvailable 校验项目编号唯一性
func (dao *CooperationProjectDao) CheckProjectNumberAvailable(queryOptions map[string]interface{}) (bool, error) {
tx := dao.transactionContext.PgTx
var cooperationProjectModels []*models.CooperationProject
query := tx.Model(&cooperationProjectModels)
if cooperationProjectNumber, ok := queryOptions["cooperationProjectNumber"]; ok && cooperationProjectNumber != "" {
query = query.Where("cooperation_project_number = ?", cooperationProjectNumber)
}
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query = query.Where(`cooperation_project.company @> '{"companyId":"?"}'`, companyId)
}
if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
query = query.Where(`cooperation_project.org @> '{"orgId":"?"}'`, orgId)
}
ok, err := query.Exists()
return !ok, err
}
func NewCooperationProjectDao(transactionContext *pgTransaction.TransactionContext) (*CooperationProjectDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &CooperationProjectDao{
transactionContext: transactionContext,
}, nil
}
}