pg_order_good_dao.go 3.5 KB
package dao

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

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 _, ok := queryOptions["limit"]; ok {
		queryLimit = fmt.Sprintf("limit %v", queryOptions["limit"])
	}
	sql := fmt.Sprintf(`select order_good_name good_name,sum(order_good_amount) good_amount from order_goods 
where company_id=? and org_id = ?  %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
}

// CooperationModeStatistics  共创模式统计
//
// queryOptions 查询参数
// 	- companyId 企业Id
// 	- orgId     组织Id
func (dao *OrderGoodDao) CooperationModeStatistics(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,0 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 b.status = 1
group by b.cooperation_mode_number
`)
	_, err := tx.Query(&goods, sql, queryOptions["companyId"], queryOptions["orgId"])
	return goods, err
}

// DividendsStatistics  企业分红统计
//
// queryOptions 查询参数
// 	- beginTime 开始时间
// 	- endTime 结束时间
// 	- companyId 企业Id
// 	- orgId     组织Id
//  - paymentStatus 支付状态
func (dao *OrderGoodDao) CompanyDividendsStatistics(queryOptions map[string]interface{}) (*domain.DividendStatisticsDto, error) {
	tx := dao.transactionContext.PgTx
	var queryTime, queryPaymentStatus string
	if _, ok := queryOptions["beginTime"]; ok {
		queryTime = fmt.Sprintf("and created_at>='%s' and created_at<'%s'", (queryOptions["beginTime"].(time.Time)).Format(time.RFC3339), (queryOptions["endTime"].(time.Time)).Format(time.RFC3339))
	}
	if _, ok := queryOptions["paymentStatus"]; ok {
		queryPaymentStatus = fmt.Sprintf(" and payment_status=%v", queryOptions["paymentStatus"])
	}
	sql := fmt.Sprintf(`select sum(actually_paid_amount) dividends_estimate,0 order_amount
from credit_accounts 
where company->>'companyId' = '?' and org->>'orgId' = '?' and deleted_at is null  %v %v 
`, queryTime, queryPaymentStatus)
	var s = &domain.DividendStatisticsDto{}
	_, err := tx.Query(s, sql, queryOptions["companyId"], queryOptions["orgId"])
	return s, err
}

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