作者 yangfu

企业首页统计

... ... @@ -27,4 +27,5 @@ require (
github.com/yudai/gojsondiff v1.0.0 // indirect
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
github.com/yudai/pp v2.0.1+incompatible // indirect
github.com/shopspring/decimal v1.2.0
)
... ...
... ... @@ -11,3 +11,23 @@ type CooperationGoodsStatisticsDto struct {
// 排名
Rank int32 `json:"rank"`
}
type CooperationModeStatisticsDto struct {
// 共创人数
CooperationPeople float64 `json:"cooperationPeople"`
// 分红预算
DividendsEstimate string `json:"dividendsEstimate"`
// 订单金额
OrderAmount float64 `json:"orderAmount"`
// 共创模式编号
CooperationModeNumber string `json:"cooperationModeNumber"`
// 共创模式
// cooperationMode *CooperationMode
}
type DividendStatisticsDto struct {
// 分红预算
DividendsEstimate string `json:"dividendsEstimate"`
// 订单金额
OrderAmount float64 `json:"orderAmount"`
}
\ No newline at end of file
... ...
... ... @@ -20,18 +20,68 @@ type OrderGoodDao struct {
func (dao *OrderGoodDao) CooperationGoodsStatistics(queryOptions map[string]interface{}) ([]*domain.CooperationGoodsStatisticsDto, error) {
tx := dao.transactionContext.PgTx
var goods []*domain.CooperationGoodsStatisticsDto
var queryTime string
var queryTime,queryLimit string
if _, ok := queryOptions["beginTime"]; ok {
queryTime = fmt.Sprintf("and created_at>='%v' and created_at<'%v'", queryOptions["beginTime"], queryOptions["endTime"])
}
if _, ok := queryOptions["limit"]; ok {
queryLimit = fmt.Sprintf("limit %v", queryOptions["limit"])
}
sql := fmt.Sprintf(`select order_good_name goodName,sum(order_good_amount) goodAmount from order_goods
where company_id=? and org_id = ? %v
where company_id=? and org_id = ? %v
GROUP BY order_good_name
order by goodAmount desc`, queryTime)
order by goodAmount desc
%v
`, queryTime,queryLimit)
_, err := tx.Query(&goods, sql, queryOptions["companyId"], queryOptions["orgId"])
return goods, err
}
// CooperationModeStatistics 共创模式统计
//
// queryOptions 查询参数
// - companyId 企业Id
// - orgId 组织Id
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) cooperationPeople,sum(a.actually_paid_amount) dividendsEstimate,b.cooperation_mode_number,0 orderAmount
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
`)
_, err := tx.Query(&goods, sql, queryOptions["companyId"], queryOptions["orgId"])
return goods, err
}
// DividendsStatistics 企业分红统计
//
// queryOptions 查询参数
// - beginTime 开始时间
// - endTime 结束时间
// - companyId 企业Id
// - orgId 组织Id
// - paymentStatus 支付状态
func (dao *OrderGoodDao) DividendsStatistics(queryOptions map[string]interface{}) (*domain.DividendStatisticsDto, error) {
tx := dao.transactionContext.PgTx
var queryTime, queryPaymentStatus string
if _, ok := queryOptions["beginTime"]; ok {
queryTime = fmt.Sprintf("and created_at>='%v' and created_at<'%v'", queryOptions["beginTime"], queryOptions["endTime"])
}
if _, ok := queryOptions["paymentStatus"]; ok {
queryPaymentStatus = fmt.Sprintf(" and payment_status=%v", queryOptions["paymentStatus"])
}
sql := fmt.Sprintf(`select sum(actually_paid_amount) dividendsEstimate,0 orderAmount
from credit_accounts
where company->>'companyId' = '?' and org->>'orgId' = '?' %v %v
`, queryTime, queryPaymentStatus)
var s *domain.DividendStatisticsDto
_, err := tx.Query(&s, sql, queryOptions["companyId"], queryOptions["orgId"])
return s, err
}
func NewOrderGoodDao(transactionContext *pgTransaction.TransactionContext) (*OrderGoodDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
... ... @@ -40,4 +90,4 @@ func NewOrderGoodDao(transactionContext *pgTransaction.TransactionContext) (*Ord
transactionContext: transactionContext,
}, nil
}
}
}
\ No newline at end of file
... ...
... ... @@ -5,6 +5,7 @@ import (
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"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/utils"
"time"
)
... ... @@ -41,7 +42,8 @@ func (ptr *PgCommonStatisticsService) Scan(keyFlags []int, queryOption map[strin
// totalOrganizationUser 统计组织用户
// rankType 排行榜类型,1月榜,2年榜 3总榜,默认展示年榜
func (ptr *PgCommonStatisticsService) CooperationGoodsStatistics(companyId, orgId int64, rankType int) ([]*domain.CooperationGoodsStatisticsDto, error) {
// top 排名前n个
func (ptr *PgCommonStatisticsService) CooperationGoodsStatistics(companyId, orgId int64, rankType int, top int) ([]*domain.CooperationGoodsStatisticsDto, error) {
orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext)
queryOptions := make(map[string]interface{})
queryOptions["companyId"] = companyId
... ... @@ -60,7 +62,75 @@ func (ptr *PgCommonStatisticsService) CooperationGoodsStatistics(companyId, orgI
queryOptions["beginTime"] = beginTime
queryOptions["endTime"] = endTime
}
return orderGoodDao.CooperationGoodsStatistics(queryOptions)
if top >0{
queryOptions["limit"] = top
}
goods,err:= orderGoodDao.CooperationGoodsStatistics(queryOptions)
if err!=nil{
return nil,err
}
// 2.计算百分比
var totalAmount float64
for i:=range goods{
goods[i].Rank = int32(i)
totalAmount +=goods[i].GoodAmount
}
for i:=range goods{
goods[i].GoodRatio = utils.Round((goods[i].GoodAmount / totalAmount)*100,0)
}
return goods,nil
}
// CooperationModeStatistics 企业-共创模式统计
//
// p1 p1_desc
func (ptr *PgCommonStatisticsService) CooperationModeStatistics(companyId, orgId int64) ([]*domain.CooperationModeStatisticsDto, error){
orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext)
queryOptions := make(map[string]interface{})
queryOptions["companyId"] = companyId
queryOptions["orgId"] = orgId
modeStatistics,err:= orderGoodDao.CooperationModeStatistics(queryOptions)
if err!=nil{
return nil,err
}
return modeStatistics, nil
}
// DividendsStatistics 分红统计
//
// action 1:当前月
func (ptr *PgCommonStatisticsService) DividendsStatistics(companyId, orgId int64,action int) (interface{}, error){
orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext)
queryOptions := make(map[string]interface{})
queryOptions["companyId"] = companyId
queryOptions["orgId"] = orgId
var beginTime, endTime time.Time
var res = make(map[string]interface{})
if action==1{
y := time.Now().Year()
m := time.Now().Month()
beginTime = time.Date(y, m, 1, 0, 0, 0, 0, time.Local)
endTime = beginTime.AddDate(0, 1, 0)
queryOptions["beginTime"] = beginTime
queryOptions["endTime"] = endTime
}
totalDividends,err:= orderGoodDao.DividendsStatistics(queryOptions)
if err!=nil{
return nil,err
}
res["creditAccount"] = totalDividends.DividendsEstimate
res["orderAmount"] = totalDividends.OrderAmount
queryOptions["paymentStatus"] = 2
dividendsEstimate,err:= orderGoodDao.DividendsStatistics(queryOptions)
if err!=nil{
return nil,err
}
res["dividendsEstimate"] = dividendsEstimate.DividendsEstimate
return res, nil
}
func (ptr *PgCommonStatisticsService) loadQueryOptions(queryOption map[string]interface{}, keys ...string) (map[string]interface{}, error) {
... ...
package utils
import "strconv"
import (
"strconv"
"github.com/shopspring/decimal"
)
// Intersect 返回两个数组的交集
func Intersect(nums1 []int64, nums2 []int64) []int64 {
... ... @@ -68,3 +71,10 @@ func SliceAtoi(sa []string) ([]int64, error) {
}
return si, nil
}
func Round(value float64, places int32) float64 {
quantity := decimal.NewFromFloat(value)
d := quantity.Round(places)
rsp, _ := d.Float64()
return rsp
}
\ No newline at end of file
... ...