作者 yangfu

统计修改

package domain
import (
"bytes"
"fmt"
"github.com/linmadan/egglib-go/utils/json"
"time"
)
... ... @@ -89,11 +89,20 @@ func (creditAccount *CreditAccount) Update(data map[string]interface{}) error {
}
func ConditionInContractNumbers(numbers []string) string {
var response = make([]map[string]string, 0)
var sqlBuilder = bytes.NewBuffer(nil)
l := len(numbers)
for i := range numbers {
response = append(response, map[string]string{
"cooperationContractNumber": numbers[i],
})
sqlBuilder.WriteString(fmt.Sprintf(`account_detail @> '[{"cooperationContractNumber":"%v"}]' `, numbers[i]))
if i < (l - 1) {
sqlBuilder.WriteString(" OR ")
}
}
return fmt.Sprintf("account_detail <@'%v'", json.MarshalToString(response))
return sqlBuilder.String()
//var response = make([]map[string]string, 0)
//for i := range numbers {
// response = append(response, map[string]string{
// "cooperationContractNumber": numbers[i],
// })
//}
//return fmt.Sprintf("account_detail <@'%v'", json.MarshalToString(response))
}
... ...
... ... @@ -84,6 +84,9 @@ func (dao *DividendsOrderDao) CalculateDividendsOrderAmount(queryOptions map[str
if endTime, ok := queryOptions["endTime"]; ok {
query.Where("order_time< ?", endTime)
}
if cooperationContractNumber, ok := queryOptions["cooperationContractNumber"]; ok && cooperationContractNumber != "" {
query.Where("cooperation_contract_number = ?", cooperationContractNumber)
}
err := query.Select()
if err != nil {
return 0, err
... ...
... ... @@ -65,23 +65,24 @@ group by b.cooperation_mode_number
func (dao *OrderGoodDao) CooperationUserModeStatistics(queryOptions map[string]interface{}) ([]*domain.CooperationModeStatisticsDto, error) {
tx := dao.transactionContext.PgTx
var goods []*domain.CooperationModeStatisticsDto
creditAccount := new(models.CreditAccount)
creditAccount := new(models.DividendsEstimate)
query := tx.Model(creditAccount)
query.ColumnExpr("count(0) cooperation_people")
query.ColumnExpr("sum(actually_paid_amount) dividends_estimate")
query.ColumnExpr("sum(good_amount_count) order_amount")
query.ColumnExpr("sum(actually_paid_amount) settlement_amount")
query.ColumnExpr("sum(dividends_amount) dividends_estimate")
//query.ColumnExpr("sum(good_amount_count) order_amount")
//query.ColumnExpr("sum(actually_paid_amount) settlement_amount")
query.ColumnExpr("a.cooperation_mode_number cooperation_mode_number")
query.Join("inner join cooperation_contracts as a").JoinOn("a.cooperation_contract_number = credit_account.cooperation_contract_number")
query.Join("inner join cooperation_contracts as a").JoinOn("a.cooperation_contract_number = dividends_estimate.cooperation_contract_number")
//query.Join("inner join cooperation_contracts as a").JoinOn("a.cooperation_contract_number = credit_account.cooperation_contract_number")
if v, ok := queryOptions["userId"]; ok && v.(int64) > 0 {
query.Where(fmt.Sprintf(`credit_account.participator->>'userId'='%v' `, v))
query.Where(fmt.Sprintf(`dividends_estimate.dividends_user->>'userId'='%v' `, v))
}
if v, ok := queryOptions["orgId"]; ok && v.(int64) > 0 {
query.Where(fmt.Sprintf(` "credit_account".org->>'orgId'= '%v'`, v))
query.Where(fmt.Sprintf(` "dividends_estimate".org->>'orgId'= '%v'`, v))
query.Where(fmt.Sprintf(` a.org->>'orgId'= '%v'`, v))
}
query.Where("credit_account.deleted_at is null")
query.Where("dividends_estimate.is_canceled is null")
query.Where("dividends_estimate.deleted_at is null")
query.Group("cooperation_mode_number")
err := query.Select(&goods)
return goods, err
... ...
... ... @@ -392,10 +392,10 @@ func (ptr *CooperationStatisticsService) CooperationUserModeStatistics(queryOpti
m := cooperModes[i]
if modeStatistics, ok := mapModeStatistics[m.CooperationModeNumber]; ok {
totalOrderAmount += modeStatistics.OrderAmount
totalDividendAmount += modeStatistics.SettlementAmount
totalDividendAmount += modeStatistics.DividendsEstimate
dividendsExpenseByTypes = append(dividendsExpenseByTypes, map[string]interface{}{
"dividendsTypeName": m.CooperationModeName + "分红支出",
"dividendsExpense": modeStatistics.SettlementAmount, //实际已支付金额
"dividendsExpense": modeStatistics.DividendsEstimate, //实际已支付金额
})
orderAmountByTypes = append(orderAmountByTypes, map[string]interface{}{
"orderAmount": modeStatistics.OrderAmount,
... ...
... ... @@ -93,7 +93,7 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma
var contracts []*domain.CooperationContract
var err error
if request.UserBaseId > 0 {
contracts, err = ptr.getUserContracts(request.UserBaseId, queryOptions)
contracts, err = ptr.getUserContracts(request.OrgId, request.UserBaseId, queryOptions)
} else if request.OrgId > 0 {
queryOptions["orgId"] = request.OrgId
contracts, err = ptr.getCompanyContracts(request.OrgId, request.UserId, queryOptions)
... ... @@ -103,6 +103,8 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma
}
// 2.根据合约查询分红预算
orderGoodDao, _ := dao.NewDividendsOrderDao(ptr.transactionContext)
dividendsEstimateDao, _ := dao.NewDividendsEstimateDao(ptr.transactionContext)
var numbers []string
var results = make([]*searchContractDividendsResult, 0)
for i := range contracts {
... ... @@ -114,31 +116,34 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma
Status: item.Status,
CreatedAt: item.CreatedAt.Unix() * 1000,
}
resultItem.DividendsOrderAmount, _ = orderGoodDao.CalculateGoodOrderAmount(map[string]interface{}{"orgId": request.OrgId, "cooperationContractNumbers": []string{item.CooperationContractNumber}})
resultItem.DividendsAmount, _ = dividendsEstimateDao.CountDividendsEstimateDividendsAmount(map[string]interface{}{"orgId": request.OrgId, "userBaseId": request.UserBaseId, "cooperationContractNumbers": []string{item.CooperationContractNumber}})
results = append(results, resultItem)
numbers = append(numbers, item.CooperationContractNumber)
}
mapCreditAccount, err := ptr.getContractsCreditAccount(numbers)
if err != nil {
return nil, err
}
for i := range results {
if v, ok := mapCreditAccount[results[i].CooperationContractNumber]; ok {
results[i].DividendsAmount = v.SettlementAmount
results[i].DividendsOrderAmount = v.GoodAmountCount
}
}
//mapCreditAccount, err := ptr.getContractsCreditAccount(numbers)
//if err != nil {
// return nil, err
//}
//for i := range results {
// if v, ok := mapCreditAccount[results[i].CooperationContractNumber]; ok {
// results[i].DividendsAmount = v.SettlementAmount
// results[i].DividendsOrderAmount = v.GoodAmountCount
// }
//}
return results, nil
}
// getUserContracts 获取用户的合约列表
//
// p1 p1_desc
func (ptr *CooperationStatisticsService) getUserContracts(userBaseId int64, queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
func (ptr *CooperationStatisticsService) getUserContracts(orgId, userBaseId int64, queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
var contracts = make([]*domain.CooperationContract, 0)
if userBaseId == 0 {
return contracts, nil
}
contractNumbers, err := ptr.getRelevantContracts(map[string]interface{}{"userBaseId": userBaseId})
contractNumbers, err := ptr.getRelevantContracts(map[string]interface{}{"orgId": orgId, "userBaseId": userBaseId})
if len(contractNumbers) == 0 {
return contracts, nil
}
... ...
... ... @@ -206,7 +206,7 @@ func (repository *CreditAccountRepository) Find(queryOptions map[string]interfac
query.Where(`(credit_account.participator->>'userName')::text LIKE ?`, fmt.Sprintf("%%%s%%", participatorName))
}
if cooperationContractNumber, ok := queryOptions["cooperationContractNumber"]; ok && cooperationContractNumber != "" {
query.Where(`cooperation_contract_number = ?`, fmt.Sprintf("%s", cooperationContractNumber))
query.Where(fmt.Sprintf(`account_detail @>'[{"cooperationContractNumber":"%v"}]'`, cooperationContractNumber))
}
if v, ok := queryOptions["cooperationContractNumbers"]; ok && len(v.([]string)) > 0 {
//query.Where("cooperation_contract_number in (?)", pg.In(v))
... ...