正在显示
5 个修改的文件
包含
158 行增加
和
7 行删除
| @@ -27,4 +27,5 @@ require ( | @@ -27,4 +27,5 @@ require ( | ||
| 27 | github.com/yudai/gojsondiff v1.0.0 // indirect | 27 | github.com/yudai/gojsondiff v1.0.0 // indirect |
| 28 | github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect | 28 | github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect |
| 29 | github.com/yudai/pp v2.0.1+incompatible // indirect | 29 | github.com/yudai/pp v2.0.1+incompatible // indirect |
| 30 | + github.com/shopspring/decimal v1.2.0 | ||
| 30 | ) | 31 | ) |
| @@ -11,3 +11,23 @@ type CooperationGoodsStatisticsDto struct { | @@ -11,3 +11,23 @@ type CooperationGoodsStatisticsDto struct { | ||
| 11 | // 排名 | 11 | // 排名 |
| 12 | Rank int32 `json:"rank"` | 12 | Rank int32 `json:"rank"` |
| 13 | } | 13 | } |
| 14 | + | ||
| 15 | +type CooperationModeStatisticsDto struct { | ||
| 16 | + // 共创人数 | ||
| 17 | + CooperationPeople float64 `json:"cooperationPeople"` | ||
| 18 | + // 分红预算 | ||
| 19 | + DividendsEstimate string `json:"dividendsEstimate"` | ||
| 20 | + // 订单金额 | ||
| 21 | + OrderAmount float64 `json:"orderAmount"` | ||
| 22 | + // 共创模式编号 | ||
| 23 | + CooperationModeNumber string `json:"cooperationModeNumber"` | ||
| 24 | + // 共创模式 | ||
| 25 | + // cooperationMode *CooperationMode | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +type DividendStatisticsDto struct { | ||
| 29 | + // 分红预算 | ||
| 30 | + DividendsEstimate string `json:"dividendsEstimate"` | ||
| 31 | + // 订单金额 | ||
| 32 | + OrderAmount float64 `json:"orderAmount"` | ||
| 33 | +} |
| @@ -20,18 +20,68 @@ type OrderGoodDao struct { | @@ -20,18 +20,68 @@ type OrderGoodDao struct { | ||
| 20 | func (dao *OrderGoodDao) CooperationGoodsStatistics(queryOptions map[string]interface{}) ([]*domain.CooperationGoodsStatisticsDto, error) { | 20 | func (dao *OrderGoodDao) CooperationGoodsStatistics(queryOptions map[string]interface{}) ([]*domain.CooperationGoodsStatisticsDto, error) { |
| 21 | tx := dao.transactionContext.PgTx | 21 | tx := dao.transactionContext.PgTx |
| 22 | var goods []*domain.CooperationGoodsStatisticsDto | 22 | var goods []*domain.CooperationGoodsStatisticsDto |
| 23 | - var queryTime string | 23 | + var queryTime,queryLimit string |
| 24 | if _, ok := queryOptions["beginTime"]; ok { | 24 | if _, ok := queryOptions["beginTime"]; ok { |
| 25 | queryTime = fmt.Sprintf("and created_at>='%v' and created_at<'%v'", queryOptions["beginTime"], queryOptions["endTime"]) | 25 | queryTime = fmt.Sprintf("and created_at>='%v' and created_at<'%v'", queryOptions["beginTime"], queryOptions["endTime"]) |
| 26 | } | 26 | } |
| 27 | + if _, ok := queryOptions["limit"]; ok { | ||
| 28 | + queryLimit = fmt.Sprintf("limit %v", queryOptions["limit"]) | ||
| 29 | + } | ||
| 27 | sql := fmt.Sprintf(`select order_good_name goodName,sum(order_good_amount) goodAmount from order_goods | 30 | sql := fmt.Sprintf(`select order_good_name goodName,sum(order_good_amount) goodAmount from order_goods |
| 28 | -where company_id=? and org_id = ? %v | 31 | +where company_id=? and org_id = ? %v |
| 29 | GROUP BY order_good_name | 32 | GROUP BY order_good_name |
| 30 | -order by goodAmount desc`, queryTime) | 33 | +order by goodAmount desc |
| 34 | +%v | ||
| 35 | +`, queryTime,queryLimit) | ||
| 31 | _, err := tx.Query(&goods, sql, queryOptions["companyId"], queryOptions["orgId"]) | 36 | _, err := tx.Query(&goods, sql, queryOptions["companyId"], queryOptions["orgId"]) |
| 32 | return goods, err | 37 | return goods, err |
| 33 | } | 38 | } |
| 34 | 39 | ||
| 40 | +// CooperationModeStatistics 共创模式统计 | ||
| 41 | +// | ||
| 42 | +// queryOptions 查询参数 | ||
| 43 | +// - companyId 企业Id | ||
| 44 | +// - orgId 组织Id | ||
| 45 | +func (dao *OrderGoodDao) CooperationModeStatistics(queryOptions map[string]interface{}) ([]*domain.CooperationModeStatisticsDto, error) { | ||
| 46 | + tx := dao.transactionContext.PgTx | ||
| 47 | + var goods []*domain.CooperationModeStatisticsDto | ||
| 48 | + sql := fmt.Sprintf(`select count(0) cooperationPeople,sum(a.actually_paid_amount) dividendsEstimate,b.cooperation_mode_number,0 orderAmount | ||
| 49 | +from credit_accounts a inner join cooperation_contracts b on a.cooperation_contract_number = b.cooperation_contract_number | ||
| 50 | +where a.company->>'companyId' = '?' and a.org->>'orgId' = '?' and b.status = 1 | ||
| 51 | +group by b.cooperation_mode_number | ||
| 52 | +`) | ||
| 53 | + _, err := tx.Query(&goods, sql, queryOptions["companyId"], queryOptions["orgId"]) | ||
| 54 | + return goods, err | ||
| 55 | +} | ||
| 56 | + | ||
| 57 | +// DividendsStatistics 企业分红统计 | ||
| 58 | +// | ||
| 59 | +// queryOptions 查询参数 | ||
| 60 | +// - beginTime 开始时间 | ||
| 61 | +// - endTime 结束时间 | ||
| 62 | +// - companyId 企业Id | ||
| 63 | +// - orgId 组织Id | ||
| 64 | +// - paymentStatus 支付状态 | ||
| 65 | +func (dao *OrderGoodDao) DividendsStatistics(queryOptions map[string]interface{}) (*domain.DividendStatisticsDto, error) { | ||
| 66 | + tx := dao.transactionContext.PgTx | ||
| 67 | + var queryTime, queryPaymentStatus string | ||
| 68 | + if _, ok := queryOptions["beginTime"]; ok { | ||
| 69 | + queryTime = fmt.Sprintf("and created_at>='%v' and created_at<'%v'", queryOptions["beginTime"], queryOptions["endTime"]) | ||
| 70 | + } | ||
| 71 | + if _, ok := queryOptions["paymentStatus"]; ok { | ||
| 72 | + queryPaymentStatus = fmt.Sprintf(" and payment_status=%v", queryOptions["paymentStatus"]) | ||
| 73 | + } | ||
| 74 | + sql := fmt.Sprintf(`select sum(actually_paid_amount) dividendsEstimate,0 orderAmount | ||
| 75 | +from credit_accounts | ||
| 76 | +where company->>'companyId' = '?' and org->>'orgId' = '?' %v %v | ||
| 77 | +`, queryTime, queryPaymentStatus) | ||
| 78 | + var s *domain.DividendStatisticsDto | ||
| 79 | + _, err := tx.Query(&s, sql, queryOptions["companyId"], queryOptions["orgId"]) | ||
| 80 | + return s, err | ||
| 81 | +} | ||
| 82 | + | ||
| 83 | + | ||
| 84 | + | ||
| 35 | func NewOrderGoodDao(transactionContext *pgTransaction.TransactionContext) (*OrderGoodDao, error) { | 85 | func NewOrderGoodDao(transactionContext *pgTransaction.TransactionContext) (*OrderGoodDao, error) { |
| 36 | if transactionContext == nil { | 86 | if transactionContext == nil { |
| 37 | return nil, fmt.Errorf("transactionContext参数不能为nil") | 87 | return nil, fmt.Errorf("transactionContext参数不能为nil") |
| @@ -40,4 +90,4 @@ func NewOrderGoodDao(transactionContext *pgTransaction.TransactionContext) (*Ord | @@ -40,4 +90,4 @@ func NewOrderGoodDao(transactionContext *pgTransaction.TransactionContext) (*Ord | ||
| 40 | transactionContext: transactionContext, | 90 | transactionContext: transactionContext, |
| 41 | }, nil | 91 | }, nil |
| 42 | } | 92 | } |
| 43 | -} | 93 | +} |
| @@ -5,6 +5,7 @@ import ( | @@ -5,6 +5,7 @@ import ( | ||
| 5 | pgTransaction "github.com/linmadan/egglib-go/transaction/pg" | 5 | pgTransaction "github.com/linmadan/egglib-go/transaction/pg" |
| 6 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain" | 6 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain" |
| 7 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao" | 7 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao" |
| 8 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils" | ||
| 8 | "time" | 9 | "time" |
| 9 | ) | 10 | ) |
| 10 | 11 | ||
| @@ -41,7 +42,8 @@ func (ptr *PgCommonStatisticsService) Scan(keyFlags []int, queryOption map[strin | @@ -41,7 +42,8 @@ func (ptr *PgCommonStatisticsService) Scan(keyFlags []int, queryOption map[strin | ||
| 41 | // totalOrganizationUser 统计组织用户 | 42 | // totalOrganizationUser 统计组织用户 |
| 42 | 43 | ||
| 43 | // rankType 排行榜类型,1月榜,2年榜 3总榜,默认展示年榜 | 44 | // rankType 排行榜类型,1月榜,2年榜 3总榜,默认展示年榜 |
| 44 | -func (ptr *PgCommonStatisticsService) CooperationGoodsStatistics(companyId, orgId int64, rankType int) ([]*domain.CooperationGoodsStatisticsDto, error) { | 45 | +// top 排名前n个 |
| 46 | +func (ptr *PgCommonStatisticsService) CooperationGoodsStatistics(companyId, orgId int64, rankType int, top int) ([]*domain.CooperationGoodsStatisticsDto, error) { | ||
| 45 | orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext) | 47 | orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext) |
| 46 | queryOptions := make(map[string]interface{}) | 48 | queryOptions := make(map[string]interface{}) |
| 47 | queryOptions["companyId"] = companyId | 49 | queryOptions["companyId"] = companyId |
| @@ -60,7 +62,75 @@ func (ptr *PgCommonStatisticsService) CooperationGoodsStatistics(companyId, orgI | @@ -60,7 +62,75 @@ func (ptr *PgCommonStatisticsService) CooperationGoodsStatistics(companyId, orgI | ||
| 60 | queryOptions["beginTime"] = beginTime | 62 | queryOptions["beginTime"] = beginTime |
| 61 | queryOptions["endTime"] = endTime | 63 | queryOptions["endTime"] = endTime |
| 62 | } | 64 | } |
| 63 | - return orderGoodDao.CooperationGoodsStatistics(queryOptions) | 65 | + if top >0{ |
| 66 | + queryOptions["limit"] = top | ||
| 67 | + } | ||
| 68 | + goods,err:= orderGoodDao.CooperationGoodsStatistics(queryOptions) | ||
| 69 | + if err!=nil{ | ||
| 70 | + return nil,err | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + // 2.计算百分比 | ||
| 74 | + var totalAmount float64 | ||
| 75 | + for i:=range goods{ | ||
| 76 | + goods[i].Rank = int32(i) | ||
| 77 | + totalAmount +=goods[i].GoodAmount | ||
| 78 | + } | ||
| 79 | + for i:=range goods{ | ||
| 80 | + goods[i].GoodRatio = utils.Round((goods[i].GoodAmount / totalAmount)*100,0) | ||
| 81 | + } | ||
| 82 | + return goods,nil | ||
| 83 | +} | ||
| 84 | + | ||
| 85 | +// CooperationModeStatistics 企业-共创模式统计 | ||
| 86 | +// | ||
| 87 | +// p1 p1_desc | ||
| 88 | +func (ptr *PgCommonStatisticsService) CooperationModeStatistics(companyId, orgId int64) ([]*domain.CooperationModeStatisticsDto, error){ | ||
| 89 | + orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext) | ||
| 90 | + queryOptions := make(map[string]interface{}) | ||
| 91 | + queryOptions["companyId"] = companyId | ||
| 92 | + queryOptions["orgId"] = orgId | ||
| 93 | + | ||
| 94 | + modeStatistics,err:= orderGoodDao.CooperationModeStatistics(queryOptions) | ||
| 95 | + if err!=nil{ | ||
| 96 | + return nil,err | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + return modeStatistics, nil | ||
| 100 | +} | ||
| 101 | + | ||
| 102 | +// DividendsStatistics 分红统计 | ||
| 103 | +// | ||
| 104 | +// action 1:当前月 | ||
| 105 | +func (ptr *PgCommonStatisticsService) DividendsStatistics(companyId, orgId int64,action int) (interface{}, error){ | ||
| 106 | + orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext) | ||
| 107 | + queryOptions := make(map[string]interface{}) | ||
| 108 | + queryOptions["companyId"] = companyId | ||
| 109 | + queryOptions["orgId"] = orgId | ||
| 110 | + var beginTime, endTime time.Time | ||
| 111 | + var res = make(map[string]interface{}) | ||
| 112 | + if action==1{ | ||
| 113 | + y := time.Now().Year() | ||
| 114 | + m := time.Now().Month() | ||
| 115 | + beginTime = time.Date(y, m, 1, 0, 0, 0, 0, time.Local) | ||
| 116 | + endTime = beginTime.AddDate(0, 1, 0) | ||
| 117 | + queryOptions["beginTime"] = beginTime | ||
| 118 | + queryOptions["endTime"] = endTime | ||
| 119 | + } | ||
| 120 | + totalDividends,err:= orderGoodDao.DividendsStatistics(queryOptions) | ||
| 121 | + if err!=nil{ | ||
| 122 | + return nil,err | ||
| 123 | + } | ||
| 124 | + res["creditAccount"] = totalDividends.DividendsEstimate | ||
| 125 | + res["orderAmount"] = totalDividends.OrderAmount | ||
| 126 | + | ||
| 127 | + queryOptions["paymentStatus"] = 2 | ||
| 128 | + dividendsEstimate,err:= orderGoodDao.DividendsStatistics(queryOptions) | ||
| 129 | + if err!=nil{ | ||
| 130 | + return nil,err | ||
| 131 | + } | ||
| 132 | + res["dividendsEstimate"] = dividendsEstimate.DividendsEstimate | ||
| 133 | + return res, nil | ||
| 64 | } | 134 | } |
| 65 | 135 | ||
| 66 | func (ptr *PgCommonStatisticsService) loadQueryOptions(queryOption map[string]interface{}, keys ...string) (map[string]interface{}, error) { | 136 | func (ptr *PgCommonStatisticsService) loadQueryOptions(queryOption map[string]interface{}, keys ...string) (map[string]interface{}, error) { |
| 1 | package utils | 1 | package utils |
| 2 | 2 | ||
| 3 | -import "strconv" | 3 | +import ( |
| 4 | + "strconv" | ||
| 5 | + "github.com/shopspring/decimal" | ||
| 6 | +) | ||
| 4 | 7 | ||
| 5 | // Intersect 返回两个数组的交集 | 8 | // Intersect 返回两个数组的交集 |
| 6 | func Intersect(nums1 []int64, nums2 []int64) []int64 { | 9 | func Intersect(nums1 []int64, nums2 []int64) []int64 { |
| @@ -68,3 +71,10 @@ func SliceAtoi(sa []string) ([]int64, error) { | @@ -68,3 +71,10 @@ func SliceAtoi(sa []string) ([]int64, error) { | ||
| 68 | } | 71 | } |
| 69 | return si, nil | 72 | return si, nil |
| 70 | } | 73 | } |
| 74 | + | ||
| 75 | +func Round(value float64, places int32) float64 { | ||
| 76 | + quantity := decimal.NewFromFloat(value) | ||
| 77 | + d := quantity.Round(places) | ||
| 78 | + rsp, _ := d.Float64() | ||
| 79 | + return rsp | ||
| 80 | +} |
-
请 注册 或 登录 后发表评论