pg_cooperation_person_statistics_service.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
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
package domain_service
import (
"github.com/go-pg/pg/v10"
"github.com/linmadan/egglib-go/utils/tool_funs"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/repository"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
"time"
)
// 共创公司统计
func (ptr *CooperationStatisticsService) CooperationCompanyStatistics(queryOptions map[string]interface{}) (interface{}, error) {
// 参数验证
var request = struct {
UserBaseId int64 `json:"userBaseId" valid:"Required"`
CompanyList []int64 `json:"companyList" valid:"Required"` //公司列表 待统计的公司列表
}{}
if err := LoadQueryObject(queryOptions, &request); err != nil {
return nil, err
}
queryOptions = tool_funs.SimpleStructToMap(&request)
var response = make([]*cooperationCompanyStatisticsResponse, 0)
var totalDividend float64
for i := range request.CompanyList {
orgId := request.CompanyList[i]
item, err := ptr.cooperationCompanyStatistics(request.UserBaseId, orgId)
if err != nil {
return response, err
}
totalDividend += item.DividendsIncome
response = append(response, item)
}
if totalDividend > 0 {
for i := range response {
response[i].DividendsRatio = utils.Round(response[i].DividendsIncome/totalDividend*100, 2)
}
}
return response, nil
}
func (ptr *CooperationStatisticsService) cooperationCompanyStatistics(userBaseId int64, orgId int64) (*cooperationCompanyStatisticsResponse, error) {
// 1.相关项目统计
cooperationApplicationRepository, _ := repository.NewCooperationApplicationRepository(ptr.transactionContext)
cooperationProjectCount, _, err := cooperationApplicationRepository.Find(map[string]interface{}{"userBaseId": userBaseId, "orgId": orgId,
"limit": 1, "cooperationApplicationStatus": int32(2)})
if err != nil {
return nil, err
}
// 2.相关合约统计
cooperationContractRelevantRepository, _ := repository.NewCooperationContractUndertakerRepository(ptr.transactionContext)
cooperationContractCount, _, err := cooperationContractRelevantRepository.Find(map[string]interface{}{"userBaseId": userBaseId,
"limit": 1})
if err != nil {
return nil, err
}
// 3.个人分红统计
type response struct {
Total float64 `json:"total"`
Accounting float64 `json:"accounting"`
Accounted float64 `json:"accounted"`
Paid float64 `json:"paid"`
}
creditAccountDao, _ := dao.NewCreditAccountDao(ptr.transactionContext)
var allDividends = &CreditAccountStatisticsResponse{}
if err := creditAccountDao.DividendsStatistics(map[string]interface{}{"userBaseId": userBaseId, "orgId": orgId}, allDividends); err != nil {
return nil, err
}
allDividends.Accounting = allDividends.Total - allDividends.Accounted
var ret = &cooperationCompanyStatisticsResponse{
OrgId: orgId,
CooperationProjectCount: cooperationProjectCount,
CooperationContractCount: cooperationContractCount,
DividendsIncome: utils.Round(allDividends.Total, 2),
}
return ret, nil
}
type cooperationCompanyStatisticsResponse struct {
// 当天统计的企业id
OrgId int64 `json:"orgId"`
// 共创项目数
CooperationProjectCount int64 `json:"cooperationProjectCount"`
// 共创合约数
CooperationContractCount int64 `json:"cooperationContractCount"`
// 分红占比
DividendsRatio float64 `json:"dividendsRatio"`
// 分红支出
DividendsIncome float64 `json:"dividendsIncome"`
}
// 个人-用户合约统计
func (ptr *CooperationStatisticsService) PersonCooperationContractStatistics(queryOptions map[string]interface{}) (interface{}, error) {
// 参数验证
var request = struct {
UserBaseId int64 `json:"userBaseId" valid:"Required"`
}{}
if err := LoadQueryObject(queryOptions, &request); err != nil {
return nil, err
}
queryOptions = tool_funs.SimpleStructToMap(&request)
cooperationContractUndertaker, _ := repository.NewCooperationContractUndertakerRepository(ptr.transactionContext)
_, contractUndertakers, err := cooperationContractUndertaker.Find(queryOptions)
if err != nil {
return nil, nil
}
var response = struct {
ContractSum int `json:"contractSum"`
ContractStoppedSum int `json:"contractStoppedSum"`
}{}
if len(contractUndertakers) == 0 {
return response, nil
}
var numbers []string
for i := range contractUndertakers {
numbers = append(numbers, contractUndertakers[i].CooperationContractNumber)
}
response.ContractSum = ptr.cooperationContractCount(numbers, 0)
response.ContractStoppedSum = ptr.cooperationContractCount(numbers, 2)
return response, nil
}
func (ptr *CooperationStatisticsService) cooperationContractCount(numbers []string, status int) int {
var total int
var contract = new(models.CooperationContract)
query := ptr.transactionContext.PgDd.Model(contract)
query.ColumnExpr("count(*) total")
query.Where("cooperation_contract_number in (?)", pg.In(numbers))
if status > 0 {
query.Where("status =? ")
}
query.Select(&total)
return total
}
// 个人 - 企业支付统计
func (ptr *CooperationStatisticsService) PersonCompanyPaymentHistoryStatistics(queryOptions map[string]interface{}) (interface{}, error) {
// 参数验证
var request = struct {
Limit int `json:"limit" valid:"Required"`
Offset int `json:"offset"`
UserBaseId int64 `json:"userBaseId" valid:"Required"`
OrgId int64 `json:"orgId" valid:"Required"`
SortByActuallyPaidAmount int `json:"sortByActuallyPaidAmount" valid:"Required"`
BeginTime time.Time `json:"beginTime"`
EndTime time.Time `json:"endTime"`
}{}
if err := LoadQueryObject(queryOptions, &request); err != nil {
return nil, err
}
queryOptions = tool_funs.SimpleStructToMap(&request)
type companyStatisticsResponse struct {
CooperationTime int64 `json:"cooperationTime"`
DividendsOrderAmount float64 `json:"dividendsOrderAmount"`
DividesAmount float64 `json:"dividesAmount"` // 分红金额
ActuallyPaidAmount float64 `json:"actuallyPaidAmount"` // 已支付金额
UnPaidAmount float64 `json:"unPaidAmount"` // 未支付金额
OrgId int64 `json:"orgId,string"`
OrgName string `json:"orgName"`
Participator interface{} `json:"participator"`
}
var responses []companyStatisticsResponse
creditAccountDao, _ := dao.NewCreditAccountDao(ptr.transactionContext)
if err := creditAccountDao.CooperationCompanyDividendsStatistics(queryOptions, &responses); err != nil {
return nil, err
}
var retMap = make([]interface{}, 0)
for i := range responses {
retMap = append(retMap, map[string]interface{}{
"paymentAmount": responses[i].ActuallyPaidAmount,
"org": map[string]interface{}{
"orgId": responses[i].OrgId,
"orgName": responses[i].OrgName,
},
})
}
return retMap, nil
}