作者 yangfu

企业统计

... ... @@ -2,48 +2,17 @@ package domain_service
import (
"fmt"
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"
)
// PgCommonStatisticsService 通用统计服务
type PgCommonStatisticsService struct {
transactionContext *pgTransaction.TransactionContext
}
const (
// 共创产品统计数据
CooperationGoodsStatistics = iota + 1
)
var (
MapKey = map[int]string{
CooperationGoodsStatistics: "cooperationGoodsStatistics",
}
)
// Scan 扫描需要统计的项
//
// keyFlags 统计项标识符号
// queryOption 查询参数
func (ptr *PgCommonStatisticsService) Scan(keyFlags []int, queryOption map[string]interface{}) (interface{}, error) {
var res = make(map[string]interface{})
for i := range keyFlags {
switch keyFlags[i] {
case CooperationGoodsStatistics:
}
}
return res, nil
}
// totalOrganizationUser 统计组织用户
// rankType 排行榜类型,1月榜,2年榜 3总榜,默认展示年榜
// top 排名前n个
func (ptr *PgCommonStatisticsService) CooperationGoodsStatistics(companyId, orgId int64, rankType int, top int) ([]*domain.CooperationGoodsStatisticsDto, error) {
func (ptr *CooperationStatisticsService) CooperationGoodsStatistics(companyId, orgId int64, rankType int, top int) ([]*domain.CooperationGoodsStatisticsDto, error) {
orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext)
queryOptions := make(map[string]interface{})
queryOptions["companyId"] = companyId
... ... @@ -62,38 +31,38 @@ func (ptr *PgCommonStatisticsService) CooperationGoodsStatistics(companyId, orgI
queryOptions["beginTime"] = beginTime
queryOptions["endTime"] = endTime
}
if top >0{
if top > 0 {
queryOptions["limit"] = top
}
goods,err:= orderGoodDao.CooperationGoodsStatistics(queryOptions)
if err!=nil{
return nil,err
goods, err := orderGoodDao.CooperationGoodsStatistics(queryOptions)
if err != nil {
return nil, err
}
// 2.计算百分比
var totalAmount float64
for i:=range goods{
for i := range goods {
goods[i].Rank = int32(i)
totalAmount +=goods[i].GoodAmount
totalAmount += goods[i].GoodAmount
}
for i:=range goods{
goods[i].GoodRatio = utils.Round((goods[i].GoodAmount / totalAmount)*100,0)
for i := range goods {
goods[i].GoodRatio = utils.Round((goods[i].GoodAmount/totalAmount)*100, 0)
}
return goods,nil
return goods, nil
}
// CooperationModeStatistics 企业-共创模式统计
//
// p1 p1_desc
func (ptr *PgCommonStatisticsService) CooperationModeStatistics(companyId, orgId int64) ([]*domain.CooperationModeStatisticsDto, error){
func (ptr *CooperationStatisticsService) 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
modeStatistics, err := orderGoodDao.CooperationModeStatistics(queryOptions)
if err != nil {
return nil, err
}
return modeStatistics, nil
... ... @@ -102,14 +71,14 @@ func (ptr *PgCommonStatisticsService) CooperationModeStatistics(companyId, orgId
// DividendsStatistics 分红统计
//
// action 1:当前月
func (ptr *PgCommonStatisticsService) DividendsStatistics(companyId, orgId int64,action int) (interface{}, error){
func (ptr *CooperationStatisticsService) CompanyDividendsStatistics(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{
if action == 1 {
y := time.Now().Year()
m := time.Now().Month()
beginTime = time.Date(y, m, 1, 0, 0, 0, 0, time.Local)
... ... @@ -117,23 +86,23 @@ func (ptr *PgCommonStatisticsService) DividendsStatistics(companyId, orgId int64
queryOptions["beginTime"] = beginTime
queryOptions["endTime"] = endTime
}
totalDividends,err:= orderGoodDao.DividendsStatistics(queryOptions)
if err!=nil{
return nil,err
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
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) {
func LoadQueryOptions(queryOption map[string]interface{}, keys ...string) (map[string]interface{}, error) {
var res = make(map[string]interface{})
for i := 0; i < len(keys); i++ {
k := keys[i]
... ...
package domain_service
import (
"encoding/json"
"fmt"
"github.com/go-pg/pg/v10"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
... ... @@ -13,6 +14,13 @@ const (
SearchContractDividends = "SearchContractDividends"
// 获取分红合约详情
GetContractDividends = "GetContractDividends"
// 企业-商品统计
CooperationGoodsStatistics = "cooperationGoodsStatistics"
// 企业-共创模式统计
CooperationModeStatistics = "CooperationModeStatistics"
// 企业-分红统计
CompanyDividendsStatistics = "CompanyDividendsStatistics"
)
// CooperationStatisticsService 共创统计服务
... ... @@ -20,6 +28,16 @@ type CooperationStatisticsService struct {
transactionContext *pgTransaction.TransactionContext
}
func NewCooperationStatisticsService(transactionContext *pgTransaction.TransactionContext) (*CooperationStatisticsService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &CooperationStatisticsService{
transactionContext: transactionContext,
}, nil
}
}
/***** 1.合约分红模块 *****/
/*1.1 分红合约搜索*/
... ... @@ -34,14 +52,15 @@ type CooperationStatisticsService struct {
// - orgId
// 按个人
// - userBaseId
func (svr *CooperationStatisticsService) SearchContractDividends(queryOptions map[string]interface{}) (interface{}, error) {
func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions map[string]interface{}) (interface{}, error) {
// 1.根据个人、企业查询合约列表
var contracts []*domain.CooperationContract
var err error
if _, ok := queryOptions["userBaseId"]; ok {
contracts, err = svr.getUserContracts(queryOptions)
} else if _, ok := queryOptions["orgId"]; ok {
contracts, err = svr.getCompanyContracts(queryOptions)
contracts, err = ptr.getUserContracts(queryOptions)
} else if v, ok := queryOptions["orgId"]; ok {
queryOptions["orgId"], _ = (v.(json.Number)).Int64()
contracts, err = ptr.getCompanyContracts(queryOptions)
}
if err != nil {
return nil, err
... ... @@ -62,7 +81,7 @@ func (svr *CooperationStatisticsService) SearchContractDividends(queryOptions ma
results = append(results, resultItem)
numbers = append(numbers, item.CooperationContractNumber)
}
mapEstimate, err := svr.getContractsDividendsEstimate(numbers)
mapEstimate, err := ptr.getContractsDividendsEstimate(numbers)
if err != nil {
return nil, err
}
... ... @@ -79,8 +98,8 @@ func (svr *CooperationStatisticsService) SearchContractDividends(queryOptions ma
// getUserContracts 获取用户的合约列表
//
// p1 p1_desc
func (svr *CooperationStatisticsService) getUserContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
undertakerRepository, _ := repository.NewCooperationContractUndertakerRepository(svr.transactionContext)
func (ptr *CooperationStatisticsService) getUserContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
undertakerRepository, _ := repository.NewCooperationContractUndertakerRepository(ptr.transactionContext)
_, undertakers, err := undertakerRepository.Find(queryOptions)
var numbers []string
for i := range undertakers {
... ... @@ -90,7 +109,7 @@ func (svr *CooperationStatisticsService) getUserContracts(queryOptions map[strin
return []*domain.CooperationContract{}, nil
}
queryOptions["inCooperationContractNumber"] = numbers
contractRepository, _ := repository.NewCooperationContractRepository(svr.transactionContext)
contractRepository, _ := repository.NewCooperationContractRepository(ptr.transactionContext)
// TODO: 参数查询条件
_, contracts, err := contractRepository.Find(queryOptions)
return contracts, err
... ... @@ -99,21 +118,21 @@ func (svr *CooperationStatisticsService) getUserContracts(queryOptions map[strin
// getCompanyContracts 获取组织合约列表
//
// p1 p1_desc
func (svr *CooperationStatisticsService) getCompanyContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
contractRepository, _ := repository.NewCooperationContractRepository(svr.transactionContext)
func (ptr *CooperationStatisticsService) getCompanyContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
contractRepository, _ := repository.NewCooperationContractRepository(ptr.transactionContext)
// TODO: 参数查询条件
_, contracts, err := contractRepository.Find(queryOptions)
return contracts, err
}
// getContractsDividendsEstimate 合约分红预算
func (svr *CooperationStatisticsService) getContractsDividendsEstimate(numbers []string) (map[string]*domain.DividendsEstimate, error) {
func (ptr *CooperationStatisticsService) getContractsDividendsEstimate(numbers []string) (map[string]*domain.DividendsEstimate, error) {
var estimates []*domain.DividendsEstimate
var resMap = make(map[string]*domain.DividendsEstimate)
if len(numbers) == 0 {
return resMap, nil
}
_, err := svr.transactionContext.PgDd.Query(&estimates, `select cooperation_contract_number,sum(dividends_amount) dividends_amount from dividends_estimates
_, err := ptr.transactionContext.PgDd.Query(&estimates, `select cooperation_contract_number,sum(dividends_amount) dividends_amount from dividends_estimates
where cooperation_contract_number in (?)
group by cooperation_contract_number
`, pg.In(numbers))
... ... @@ -144,16 +163,6 @@ type searchContractDividendsResult struct {
CreatedAt int64 `json:"createdAt"`
}
func NewCooperationStatisticsService(transactionContext *pgTransaction.TransactionContext) (*CooperationStatisticsService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &CooperationStatisticsService{
transactionContext: transactionContext,
}, nil
}
}
/*1.2 分红合约详情*/
// 分红合约搜索
... ... @@ -164,9 +173,9 @@ func NewCooperationStatisticsService(transactionContext *pgTransaction.Transacti
// - orgId
// 按个人
// - userBaseId
func (svr *CooperationStatisticsService) GetContractDividends(queryOptions map[string]interface{}) (interface{}, error) {
func (ptr *CooperationStatisticsService) GetContractDividends(queryOptions map[string]interface{}) (interface{}, error) {
// 1.合约详情
contractRepository, _ := repository.NewCooperationContractRepository(svr.transactionContext)
contractRepository, _ := repository.NewCooperationContractRepository(ptr.transactionContext)
if _, ok := queryOptions["contractId"]; !ok {
return nil, fmt.Errorf("合约ID(contractId)不能为空")
}
... ...