pg_credit_account_dao.go
4.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
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 CreditAccountDao struct {
transactionContext *pgTransaction.TransactionContext
}
// GenerateCreditAccountNumber 生成账期结算单号
func (dao *CreditAccountDao) GenerateCreditAccountNumber() (string, error) {
tx := dao.transactionContext.PgTx
var creditAccountModels []*models.CreditAccount
query := tx.Model(&creditAccountModels)
currentTime := time.Now()
todayZeroTime := utils.GetZeroTime(currentTime)
nextDayZeroTime := utils.GetNextDayZeroTime(currentTime)
query.Where("credit_account.created_at >= ?", todayZeroTime)
query.Where("credit_account.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)]
creditAccountNumber := "JS" + timeString + "#" + countStr
return creditAccountNumber, nil
}
}
// CheckCreditAccountNumberAvailable 校验账期结算单号是否唯一
func (dao *CreditAccountDao) CheckCreditAccountNumberAvailable(queryOptions map[string]interface{}) (bool, error) {
tx := dao.transactionContext.PgTx
var creditAccountModels []*models.CreditAccount
query := tx.Model(&creditAccountModels)
if creditAccountNumber, ok := queryOptions["creditAccountNumber"]; ok && creditAccountNumber != "" {
query = query.Where("credit_account_number = ?", creditAccountNumber)
}
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query = query.Where(`credit_account.company @> '{"companyId":"?"}'`, companyId)
}
if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
query = query.Where(`credit_account.org @> '{"orgId":"?"}'`, orgId)
}
ok, err := query.Exists()
return !ok, err
}
// 分红统计(分红明细)
func (dao *CreditAccountDao) DividendsStatistics(queryOptions map[string]interface{}, v interface{}) error {
// sql := `
//select
//sum(settlement_amount) total,
//sum((case when payment_status = 1 then actually_paid_amount else 0 end)) paid,
//sum((case when settlement_time is not null then settlement_amount else 0 end)) accounted
//from credit_accounts
//where
//`
//
//
// if _, ok := queryOptions["beginTime"]; ok {
// sql += `created_at>? and created_at<? and `
// }
// if v, ok := queryOptions["userBaseId"]; ok && v.(int64) > 0 {
// sql += fmt.Sprintf(`participator->>'userBaseId'='%v' `, v)
// }
// if v, ok := queryOptions["orgId"]; ok && v.(int64) > 0 {
// sql += fmt.Sprintf(` org->>'orgId'= '%v'`, v)
// }
// _, err := dao.transactionContext.PgDd.Query(v, sql, queryOptions["beginTime"], queryOptions["endTime"])
creditAccount := new(models.CreditAccount)
query := dao.transactionContext.PgTx.Model(creditAccount)
query.ColumnExpr(`sum(settlement_amount) total`)
query.ColumnExpr(`sum((case when payment_status = 1 then actually_paid_amount else 0 end)) paid`)
query.ColumnExpr(`sum((case when settlement_time is not null then settlement_amount else 0 end)) accounted `)
if _, ok := queryOptions["beginTime"]; ok {
query.Where(`created_at>? `, queryOptions["beginTime"])
query.Where(`created_at<? `, queryOptions["endTime"])
}
if v, ok := queryOptions["userBaseId"]; ok && v.(int64) > 0 {
query.Where(fmt.Sprintf(`participator->>'userBaseId'='%v' `, v))
}
if v, ok := queryOptions["orgId"]; ok && v.(int64) > 0 {
query.Where(fmt.Sprintf(` org->>'orgId'= '%v'`, v))
}
err := query.Select(v)
return err
}
func NewCreditAccountDao(transactionContext *pgTransaction.TransactionContext) (*CreditAccountDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为空")
} else {
return &CreditAccountDao{
transactionContext: transactionContext,
}, nil
}
}