pg_order_good_dao.go 5.7 KB
package dao

import (
	"fmt"
	"github.com/go-pg/pg/v10"
	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/pg/models"
)

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, queryLimit string
	if _, ok := queryOptions["beginTime"]; ok {
		queryTime = fmt.Sprintf("and created_at>='%v' and created_at<'%v'", queryOptions["beginTime"], queryOptions["endTime"])
	}
	if v, ok := queryOptions["offset"]; ok {
		queryLimit += fmt.Sprintf("offset %v \n", v)
	}
	if v, ok := queryOptions["limit"]; ok {
		queryLimit += fmt.Sprintf("limit %v \n", v)
	}
	sql := fmt.Sprintf(`select order_good_name good_name,sum(coalesce(order_good_amount,0)) good_amount from order_goods 
where company_id=? and org_id = ? and deleted_at is null %v
GROUP BY order_good_name
order by good_amount desc
%v 
`, queryTime, queryLimit)
	_, err := tx.Query(&goods, sql, queryOptions["companyId"], queryOptions["orgId"])
	return goods, err
}

// CooperationCompanyModeStatistics  共创模式统计
//
// queryOptions 查询参数
// 	- companyId 企业Id
// 	- orgId     组织Id
func (dao *OrderGoodDao) CooperationCompanyModeStatistics(queryOptions map[string]interface{}) ([]*domain.CooperationModeStatisticsDto, error) {
	tx := dao.transactionContext.PgTx
	var goods []*domain.CooperationModeStatisticsDto
	//	sql := fmt.Sprintf(`select count(0) cooperation_people,sum(a.actually_paid_amount) dividends_estimate,b.cooperation_mode_number,sum(good_amount_count) order_amount
	//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 a.deleted_at is null and b.status = 1
	//group by b.cooperation_mode_number
	//`)
	sql := fmt.Sprintf(`select count(DISTINCT dividends_user->>'userId') cooperation_people,sum(a.dividends_amount) dividends_estimate,b.cooperation_mode_number      
from dividends_estimates a inner join cooperation_contracts b on a.cooperation_contract_number = b.cooperation_contract_number
where a.company->>'companyId' = '?' and a.org->>'orgId' = '?' and a.is_canceled is null and a.deleted_at is null and b.status = 1 
group by b.cooperation_mode_number
`)
	_, err := tx.Query(&goods, sql, queryOptions["companyId"], queryOptions["orgId"])
	return goods, err
}

// 共创用户模式统计
func (dao *OrderGoodDao) CooperationUserModeStatistics(queryOptions map[string]interface{}) ([]*domain.CooperationModeStatisticsDto, error) {
	tx := dao.transactionContext.PgTx
	var goods []*domain.CooperationModeStatisticsDto
	creditAccount := new(models.DividendsEstimate)
	query := tx.Model(creditAccount)
	query.ColumnExpr("count(0) cooperation_people")
	query.ColumnExpr("sum(dividends_amount) dividends_estimate")
	//query.ColumnExpr("sum(good_amount_count) order_amount")
	//query.ColumnExpr("sum(actually_paid_amount) settlement_amount")
	query.ColumnExpr("a.cooperation_mode_number cooperation_mode_number")
	query.Join("inner join cooperation_contracts as a").JoinOn("a.cooperation_contract_number = dividends_estimate.cooperation_contract_number")
	//query.Join("inner join cooperation_contracts as a").JoinOn("a.cooperation_contract_number = credit_account.cooperation_contract_number")
	if v, ok := queryOptions["userId"]; ok && v.(int64) > 0 {
		query.Where(fmt.Sprintf(`dividends_estimate.dividends_user->>'userId'='%v' `, v))
	}
	if v, ok := queryOptions["orgId"]; ok && v.(int64) > 0 {
		query.Where(fmt.Sprintf(` "dividends_estimate".org->>'orgId'= '%v'`, v))
		query.Where(fmt.Sprintf(` a.org->>'orgId'= '%v'`, v))
	}
	query.Where("dividends_estimate.is_canceled is null")
	query.Where("dividends_estimate.deleted_at is null")
	query.Group("cooperation_mode_number")
	err := query.Select(&goods)
	return goods, err
}

// CalculateDividendsOrderAmount 计算分红订单金额
func (dao *DividendsOrderDao) CalculateGoodOrderAmount(queryOptions map[string]interface{}) (float64, error) {
	tx := dao.transactionContext.PgTx
	var orderGood = new(models.OrderGood)
	query := tx.Model(orderGood)
	query.ColumnExpr("sum(case when dividends_order_number is null then -order_good_amount else order_good_amount end) as order_good_amount")
	if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
		query.Where("company_id = '?'", companyId)
	}
	if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
		query.Where("org_id = '?'", orgId)
	}
	if beginTime, ok := queryOptions["beginTime"]; ok {
		query.Where("order_time>= ?", beginTime)
	}
	if endTime, ok := queryOptions["endTime"]; ok {
		query.Where("order_time< ?", endTime)
	}
	if cooperationContractNumbers, ok := queryOptions["cooperationContractNumbers"]; ok && len(cooperationContractNumbers.([]string)) != 0 {
		query.Where("cooperation_contract_number in (?)", pg.In(cooperationContractNumbers))
	}
	err := query.Select()
	if err != nil {
		return 0, err
	}
	return orderGood.OrderGoodAmount, nil
}

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