作者 yangfu

直方图统计修改

... ... @@ -122,6 +122,9 @@ func (dao *CreditAccountDao) CooperationUsersDividendsStatistics(queryOptions ma
if v, ok := queryOptions["endTime"]; ok && !v.(time.Time).IsZero() {
query.Where("created_at < ?", v)
}
if v, ok := queryOptions["cooperationContractNumbers"]; ok && len(v.([]string)) > 0 {
query.Where("cooperation_contract_number in (?)", pg.In(v))
}
query.Where("deleted_at is null")
if v, ok := queryOptions["sortByActuallyPaidAmount"]; ok {
vInt := v.(int)
... ...
... ... @@ -317,6 +317,7 @@ func (ptr *CooperationStatisticsService) CompanyPaymentHistoryStatistics(queryOp
Limit int `json:"limit" valid:"Required"`
Offset int `json:"offset"`
OrgId int64 `json:"orgId" valid:"Required"`
UserId int64 `json:"userId" valid:"Required"`
SortByActuallyPaidAmount int `json:"sortByActuallyPaidAmount" valid:"Required"`
BeginTime time.Time `json:"beginTime"`
EndTime time.Time `json:"endTime"`
... ... @@ -326,13 +327,23 @@ func (ptr *CooperationStatisticsService) CompanyPaymentHistoryStatistics(queryOp
}
queryOptions = tool_funs.SimpleStructToMap(&request)
// 按关联相关人过滤
var retMap = make([]interface{}, 0)
contractNumbers, err := ptr.getRelevantContracts(map[string]interface{}{"userId": request.UserId})
if len(contractNumbers) == 0 {
return retMap, nil
}
if err != nil {
return retMap, err
}
queryOptions["cooperationContractNumbers"] = contractNumbers
var responses []usersStatisticsResponse
creditAccountDao, _ := dao.NewCreditAccountDao(ptr.transactionContext)
if err := creditAccountDao.CooperationUsersDividendsStatistics(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,
... ... @@ -353,22 +364,76 @@ func (ptr *CooperationStatisticsService) PaymentHistoryHistogramStatistics(query
creditAccountDao, _ := dao.NewCreditAccountDao(ptr.transactionContext)
var request = struct {
OrgId int64 `json:"orgId"`
UserBaseId int64 `json:"userBaseId"`
OrgId int64 `json:"orgId"`
UserId int64 `json:"userId"`
UserBaseId int64 `json:"userBaseId"`
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)
// 按关联相关人过滤
var contractNumbers []string
var err error
if request.UserId > 0 {
var retMap = make([]interface{}, 0)
contractNumbers, err = ptr.getRelevantContracts(map[string]interface{}{"userId": request.UserId})
if len(contractNumbers) == 0 {
return retMap, nil
}
if err != nil {
return retMap, err
}
queryOptions["cooperationContractNumbers"] = contractNumbers
}
var dividends = &CreditAccountStatisticsResponse{}
var xAxisData []string
var values []float64
var queryItems []queryItem
if !request.BeginTime.IsZero() && request.BeginTime.AddDate(0, 3, 0).Before(request.EndTime) {
queryItems, xAxisData = histogramStatisticsByYear()
} else {
queryItems, xAxisData = histogramStatisticsByMonth()
}
for i := range queryItems {
item := queryItems[i]
if len(contractNumbers) == 0 && request.UserId > 0 { //没有相关的合约 查看分红预算单为空
continue
}
queryOptions["beginTime"] = item.BeginTime
queryOptions["endTime"] = item.EndTime
if err := creditAccountDao.DividendsStatistics(queryOptions, dividends); err != nil {
return nil, err
}
values = append(values, dividends.Paid)
}
return map[string]interface{}{
"xAxis": map[string]interface{}{
"data": xAxisData,
},
"source": map[string]interface{}{
"value": values,
},
}, nil
}
type queryItem struct {
BeginTime time.Time
EndTime time.Time
}
func histogramStatisticsByMonth() ([]queryItem, []string) {
ret := make([]queryItem, 0)
year, month := time.Now().Year(), time.Now().Month()
var beginTime = time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
var endTime time.Time
var increaseDay = 5
var monthEnd = time.Date(year, month+1, 1, 0, 0, 0, 0, time.Local).Add(-time.Second)
var xAxisData []string
var values []float64
for {
if beginTime.AddDate(0, 0, increaseDay).After(monthEnd) {
endTime = monthEnd
... ... @@ -376,25 +441,30 @@ func (ptr *CooperationStatisticsService) PaymentHistoryHistogramStatistics(query
} else {
endTime = beginTime.AddDate(0, 0, increaseDay).Add(-time.Second)
}
xAxisData = append(xAxisData, endTime.Format("01-02"))
queryOptions["beginTime"] = beginTime
queryOptions["endTime"] = beginTime.AddDate(0, 0, increaseDay)
if err := creditAccountDao.DividendsStatistics(queryOptions, dividends); err != nil {
return nil, err
item := queryItem{
BeginTime: beginTime,
EndTime: beginTime.AddDate(0, 0, increaseDay).Add(-time.Second),
}
dividends.Accounting = dividends.Total - dividends.Accounted
values = append(values, dividends.Paid)
xAxisData = append(xAxisData, item.EndTime.Format("01-02"))
beginTime = endTime
if endTime == monthEnd {
break
}
ret = append(ret, item)
}
return map[string]interface{}{
"xAxis": map[string]interface{}{
"data": xAxisData,
},
"source": map[string]interface{}{
"value": values,
},
}, nil
return ret, xAxisData
}
func histogramStatisticsByYear() ([]queryItem, []string) {
ret := make([]queryItem, 0)
var xAxisData = []string{"3月", "6月", "9月", "12月"}
year, _ := time.Now().Year(), time.Now().Month()
var increase = 3
for i := 0; i < 4; i++ {
ret = append(ret, queryItem{
BeginTime: time.Date(year, time.Month(1+i*increase), 1, 0, 0, 0, 0, time.Local),
EndTime: time.Date(year, time.Month(1+(i+1)*increase), 1, 0, 0, 0, 0, time.Local),
})
}
return ret, xAxisData
}
... ...