pg_common_statistics_service.go 2.4 KB
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"
	"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总榜,默认展示年榜
func (ptr *PgCommonStatisticsService) CooperationGoodsStatistics(companyId, orgId int64, rankType int) ([]*domain.CooperationGoodsStatisticsDto, error) {
	orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext)
	queryOptions := make(map[string]interface{})
	queryOptions["companyId"] = companyId
	queryOptions["orgId"] = orgId
	y := time.Now().Year()
	m := time.Now().Month()
	var beginTime, endTime time.Time
	if rankType == 1 { //1月榜
		beginTime = time.Date(y, m, 1, 0, 0, 0, 0, time.Local)
		endTime = beginTime.AddDate(0, 1, 0)
		queryOptions["beginTime"] = beginTime
		queryOptions["endTime"] = endTime
	} else if rankType == 2 { //2年榜
		beginTime = time.Date(y, 1, 1, 0, 0, 0, 0, time.Local)
		endTime = beginTime.AddDate(1, 0, 0)
		queryOptions["beginTime"] = beginTime
		queryOptions["endTime"] = endTime
	}
	return orderGoodDao.CooperationGoodsStatistics(queryOptions)
}

func (ptr *PgCommonStatisticsService) 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]
		if v, ok := queryOption[k]; ok {
			res[k] = v
		} else {
			return nil, fmt.Errorf("参数 %v 不存在", k)
		}
	}
	return res, nil
}

type item struct {
	key string
	val interface{}
}