pg_order_good_dao.go 1.4 KB
package dao

import (
	"fmt"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
)

type OrderGoodDao struct {
	transactionContext *pgTransaction.TransactionContext
}

// CooperationGoodsStatistics  共创产品统计数据
//
// queryOptions 查询参数
// 	- beginTime 开始时间
// 	- endTime 结束时间
// 	- companyId 企业Id
// 	- orgId     组织Id
func (dao *OrderGoodDao) CooperationGoodsStatistics(queryOptions map[string]interface{}) ([]*domain.CooperationGoodsStatisticsDto, error) {
	tx := dao.transactionContext.PgTx
	var goods []*domain.CooperationGoodsStatisticsDto
	var queryTime string
	if _, ok := queryOptions["beginTime"]; ok {
		queryTime = fmt.Sprintf("and created_at>='%v' and created_at<'%v'", queryOptions["beginTime"], queryOptions["endTime"])
	}
	sql := fmt.Sprintf(`select order_good_name goodName,sum(order_good_amount) goodAmount from order_goods 
where company_id=? and org_id = ? %v
GROUP BY order_good_name
order by goodAmount desc`, queryTime)
	_, err := tx.Query(&goods, sql, queryOptions["companyId"], queryOptions["orgId"])
	return goods, err
}

func NewOrderGoodDao(transactionContext *pgTransaction.TransactionContext) (*OrderGoodDao, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &OrderGoodDao{
			transactionContext: transactionContext,
		}, nil
	}
}