pg_dividends_estimate_dao.go
3.5 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
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 DividendsEstimateDao struct {
transactionContext *pgTransaction.TransactionContext
}
// GenerateDividendsEstimateNumber 生成分红预算单号
func (dao *DividendsEstimateDao) GenerateDividendsEstimateNumber() (string, error) {
tx := dao.transactionContext.PgTx
var dividendsEstimateModels []*models.DividendsEstimate
query := tx.Model(÷ndsEstimateModels)
currentTime := time.Now()
todayZeroTime := utils.GetZeroTime(currentTime)
nextDayZeroTime := utils.GetNextDayZeroTime(currentTime)
query.Where("dividends_estimate.created_at >= ?", todayZeroTime)
query.Where("dividends_estimate.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)]
dividendsOrderNumber := "FH" + timeString + "#" + countStr
return dividendsOrderNumber, nil
}
}
// CheckDividendsEstimateOrderNumberAvailable 校验分红预算单号是否唯一
func (dao *DividendsEstimateDao) CheckDividendsEstimateOrderNumberAvailable(queryOptions map[string]interface{}) (bool, error) {
tx := dao.transactionContext.PgTx
var dividendsEstimateModels []*models.DividendsEstimate
query := tx.Model(÷ndsEstimateModels)
if dividendsEstimateOrderNumber, ok := queryOptions["dividendsEstimateOrderNumber"]; ok && dividendsEstimateOrderNumber != "" {
query = query.Where("dividends_estimate_order_number = ?", dividendsEstimateOrderNumber)
}
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query = query.Where(`dividends_estimate.company @> '{"companyId":"?"}'`, companyId)
}
if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
query = query.Where(`dividends_estimate.org @> '{"orgId":"?"}'`, orgId)
}
ok, err := query.Exists()
return !ok, err
}
// UserEstimated 判断金额激励用户是否已分红(已生成分红预算单)
func (dao *DividendsEstimateDao) UserEstimated(queryOptions map[string]interface{}) (bool, error) {
tx := dao.transactionContext.PgTx
var dividendsEstimateModels []*models.DividendsEstimate
query := tx.Model(÷ndsEstimateModels)
if undertakerUid, ok := queryOptions["undertakerUid"]; ok && undertakerUid.(int64) != 0 {
query = query.Where(`dividends_estimate.dividends_user @> '{"userId":"?"}'`, undertakerUid)
}
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query = query.Where(`dividends_estimate.company @> '{"companyId":"?"}'`, companyId)
}
if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
query = query.Where(`dividends_estimate.org @> '{"orgId":"?"}'`, orgId)
}
// 非取消的分红预算单
query.Where("dividends_estimate.is_canceled = ?", false)
// 过滤出金额激励
query.Where("dividends_estimate.dividends_type = ?", 3)
ok, err := query.Exists()
return ok, err
}
func NewDividendsEstimateDao(transactionContext *pgTransaction.TransactionContext) (*DividendsEstimateDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为空")
} else {
return &DividendsEstimateDao{
transactionContext: transactionContext,
}, nil
}
}