作者 yangfu

共创订单金额统计

... ... @@ -79,7 +79,7 @@ func (dao *CreditAccountDao) CooperationUsersDividendsStatistics(queryOptions ma
creditAccount := new(models.CreditAccount)
query := dao.transactionContext.PgTx.Model(creditAccount)
query.ColumnExpr(`0 cooperation_time`)
query.ColumnExpr(`0 dividends_order_amount`)
query.ColumnExpr(`sum(good_amount_count) dividends_order_amount`)
query.ColumnExpr(`sum(settlement_amount) divides_amount`)
query.ColumnExpr(`sum((case when payment_status = 1 then actually_paid_amount else 0 end)) actually_paid_amount`)
query.ColumnExpr(`max(participator->>'userId') user_id`)
... ...
... ... @@ -46,7 +46,7 @@ order by good_amount desc
func (dao *OrderGoodDao) CooperationModeStatistics(queryOptions map[string]interface{}) ([]*domain.CooperationModeStatisticsDto, error) {
tx := dao.transactionContext.PgTx
var goods []*domain.CooperationModeStatisticsDto
sql := fmt.Sprintf(`select count(0) cooperation_people,sum(a.actually_paid_amount) dividends_estimate,b.cooperation_mode_number,0 order_amount
sql := fmt.Sprintf(`select count(0) cooperation_people,sum(a.actually_paid_amount) dividends_estimate,b.cooperation_mode_number,sum(good_amount_count) order_amount
from credit_accounts a inner join cooperation_contracts b on a.cooperation_contract_number = b.cooperation_contract_number
where a.company->>'companyId' = '?' and a.org->>'orgId' = '?' and b.status = 1
group by b.cooperation_mode_number
... ...
... ... @@ -222,14 +222,8 @@ func (ptr *CooperationStatisticsService) CompanyCooperationUsersStatistics(query
for i := range responses {
responses[i].CooperationTime = time.Now().Unix() * 1000
unPaidAmount := responses[i].DividesAmount - responses[i].ActuallyPaidAmount
responses[i].Participator = map[string]interface{}{
"userId": responses[i].UserId,
"userInfo": map[string]interface{}{
"userName": responses[i].UserName,
},
}
retMap = append(retMap, map[string]interface{}{
"dividendsOrderAmount": 0,
"dividendsOrderAmount": responses[i].DividendsOrderAmount,
"dividesAmount": responses[i].DividesAmount,
"unPaidAmount": unPaidAmount,
"cooperationTime": time.Now().Unix() * 1000,
... ...
... ... @@ -9,6 +9,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/repository"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
"strconv"
"time"
)
... ... @@ -103,21 +104,16 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma
results = append(results, resultItem)
numbers = append(numbers, item.CooperationContractNumber)
}
mapEstimate, err := ptr.getContractsDividendsEstimate(numbers)
mapCreditAccount, err := ptr.getContractsCreditAccount(numbers)
if err != nil {
return nil, err
}
for i := range results {
if v, ok := mapEstimate[results[i].CooperationContractNumber]; ok {
results[i].DividendsAmount = v.DividendsAmount
if v, ok := mapCreditAccount[results[i].CooperationContractNumber]; ok {
results[i].DividendsAmount = v.SettlementAmount
results[i].DividendsOrderAmount = v.GoodAmountCount
}
}
// TODO: 3.根据合约查询订单金额
//for i:=range results{
// queryOptions["cooperationContractNumber"] =results[i].CooperationContractNumber
// results[i].DividendsOrderAmount = ptr.CalculateOrderAmount(queryOptions)
//}
return results, nil
}
... ... @@ -171,6 +167,33 @@ group by cooperation_contract_number
return resMap, nil
}
// getContractsDividendsEstimate 合约分红预算
func (ptr *CooperationStatisticsService) getContractsCreditAccount(numbers []string) (map[string]*domain.CreditAccount, error) {
var creditAccounts []*domain.CreditAccount
var resMap = make(map[string]*domain.CreditAccount)
if len(numbers) == 0 {
return resMap, nil
}
var creditAccount = new(domain.CreditAccount)
query := ptr.transactionContext.PgDd.Model(creditAccount)
query.Column("cooperation_contract_number")
query.ColumnExpr("sum(good_amount_count) good_amount_count")
query.ColumnExpr("sum(settlement_amount) settlement_amount")
query.Where("cooperation_contract_number in (?)", pg.In(numbers))
query.Where("deleted_at is null")
query.Group("cooperation_contract_number")
err := query.Select(&creditAccounts)
if err != nil {
return nil, err
}
for i := range creditAccounts {
resMap[creditAccounts[i].CooperationContractNumber] = creditAccounts[i]
}
return resMap, nil
}
type searchContractDividendsResult struct {
// 共创合约ID
CooperationContractId int64 `json:"cooperationContractId,string"`
... ... @@ -220,17 +243,40 @@ func (ptr *CooperationStatisticsService) GetContractDividends(queryOptions map[s
if err != nil {
return res, err
}
// 3.取订单商品列表
var orderGoodIds []int64
for i := range creditAccounts {
a := creditAccounts[i]
for j := range a.AccountDetail {
if a.AccountDetail[j].OrderGoodId > 0 {
orderGoodIds = append(orderGoodIds, a.AccountDetail[j].OrderGoodId)
}
}
}
orderGoodRepository, _ := repository.NewOrderGoodRepository(ptr.transactionContext)
_, orderGoods, err := orderGoodRepository.Find(map[string]interface{}{"orderGoodIds": orderGoodIds})
if err != nil {
return res, err
}
var mapOrderGoods = make(map[int64]*domain.OrderGood)
for i := range orderGoods {
mapOrderGoods[orderGoods[i].OrderGoodId] = orderGoods[i]
}
var dividends = make([]interface{}, 0)
for i := range creditAccounts {
a := creditAccounts[i]
for j := range a.AccountDetail {
participateType, _ := strconv.Atoi(a.ParticipateType)
detail := a.AccountDetail[j]
item := map[string]interface{}{
"creditAccountId": a.CreditAccountId,
"orderGoodName": "",
"dividendsType": 0,
"dividendsRatio": 0,
"dividendsAmount": a.AccountDetail[j].DividendsAmount,
"dividendsAmount": detail.DividendsAmount,
"dividendsUser": map[string]interface{}{
"userInfo": a.Participator.UserInfo,
"userId": a.Participator.UserId,
... ... @@ -240,7 +286,14 @@ func (ptr *CooperationStatisticsService) GetContractDividends(queryOptions map[s
"dividendsEstimateTime": a.CreatedAt.Unix() * 1000,
"orderOrReturnedOrderNum": a.CreditAccountOrderNum,
}
if detail.OrderGoodAmount > 0 {
item["dividendsRatio"] = utils.Round(detail.DividendsAmount/detail.OrderGoodAmount*100, 2)
}
item["dividendsType"] = a.AccountDetail[j].DividendsType
if v, ok := mapOrderGoods[detail.OrderGoodId]; ok {
item["orderGoodName"] = v.OrderGoodName
}
dividends = append(dividends, item)
}
... ...