pg_dividends_estimate_dao.go 11.9 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/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
	"time"
)

type DividendsEstimateDao struct {
	transactionContext *pgTransaction.TransactionContext
}

// GenerateDividendsEstimateNumber 生成分红预算单号
func (dao *DividendsEstimateDao) GenerateDividendsEstimateNumber(queryOptions map[string]interface{}) (string, error) {
	tx := dao.transactionContext.PgTx
	var dividendsEstimateModels []*models.DividendsEstimate
	query := tx.Model(&dividendsEstimateModels)
	currentTime := time.Now()
	todayZeroTime := utils.GetZeroTime(currentTime)
	nextDayZeroTime := utils.GetNextDayZeroTime(currentTime)
	query.Where("dividends_estimate.created_at >= ?", todayZeroTime)
	query.Where("dividends_estimate.created_at < ?", nextDayZeroTime)
	if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
		query = query.Where(`dividends_estimate.company @> '{"companyId":"?"}'`, companyId)
	}
	if count, err := query.AllWithDeleted().Count(); err != nil {
		return "", err
	} else {
		if count < 1000 {
			countStr := fmt.Sprintf("%03d", count+1)
			timestamp := currentTime.Unix()
			timeNow := time.Unix(timestamp, 0)
			timeString := timeNow.Format("20060102")
			timeString = timeString[2:len(timeString)]
			dividendsOrderNumber := "FH" + timeString + "#" + countStr
			return dividendsOrderNumber, nil
		} else {
			countStr := fmt.Sprintf("%d", count+1)
			timestamp := currentTime.Unix()
			timeNow := time.Unix(timestamp, 0)
			timeString := timeNow.Format("20060102")
			timeString = timeString[2:len(timeString)]
			dividendsOrderNumber := "FH" + timeString + "#" + countStr
			return dividendsOrderNumber, nil
		}

	}
}

// CheckDividendsEstimateOrderNumberAvailable 校验分红预算单号是否唯一
func (dao *DividendsEstimateDao) CheckDividendsEstimateOrderNumberAvailable(queryOptions map[string]interface{}) (bool, error) {
	tx := dao.transactionContext.PgTx
	var dividendsEstimateModels []*models.DividendsEstimate
	query := tx.Model(&dividendsEstimateModels)
	if dividendsEstimateOrderNumber, ok := queryOptions["dividendsEstimateOrderNumber"]; ok && dividendsEstimateOrderNumber != "" {
		query = query.Where("dividends_estimate_order_number = ?", dividendsEstimateOrderNumber)
	}
	if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
		query = query.Where(`dividends_estimate.company @> '{"companyId":"?"}'`, companyId)
	}
	if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
		query = query.Where(`dividends_estimate.org @> '{"orgId":"?"}'`, orgId)
	}
	ok, err := query.Exists()
	return !ok, err
}

// CheckDividendsEstimateOrderExist 判断是否存在分红预算单
func (dao *DividendsEstimateDao) CheckDividendsEstimateOrderExist(queryOptions map[string]interface{}) (bool, error) {
	tx := dao.transactionContext.PgTx
	var dividendsEstimateModels []*models.DividendsEstimate
	query := tx.Model(&dividendsEstimateModels)
	if orderOrReturnedOrderNum, ok := queryOptions["orderOrReturnedOrderNum"]; ok && orderOrReturnedOrderNum != "" {
		query = query.Where("order_or_returned_order_num = ?", orderOrReturnedOrderNum)
	}
	if isCanceled, ok := queryOptions["isCanceled"]; ok {
		query.Where("is_canceled = ?", isCanceled.(bool))
	}
	if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
		query = query.Where(`dividends_estimate.company @> '{"companyId":"?"}'`, companyId)
	}
	if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
		query = query.Where(`dividends_estimate.org @> '{"orgId":"?"}'`, orgId)
	}
	count, err := query.Count()
	if count > 0 {
		return true, err
	} else {
		return false, err
	}
}

// UserEstimated 判断金额激励用户是否已分红(已生成分红预算单)
func (dao *DividendsEstimateDao) UserEstimated(queryOptions map[string]interface{}) (bool, error) {
	tx := dao.transactionContext.PgTx
	var dividendsEstimateModels []*models.DividendsEstimate
	query := tx.Model(&dividendsEstimateModels)
	if undertakerUid, ok := queryOptions["undertakerUid"]; ok && undertakerUid.(int64) != 0 {
		query = query.Where(`dividends_estimate.dividends_user @> '{"userId":"?"}'`, undertakerUid)
	}
	if cooperationContractNumber, ok := queryOptions["cooperationContractNumber"]; ok && cooperationContractNumber != "" {
		query = query.Where("cooperation_contract_number = ?", cooperationContractNumber.(string))
	}
	if cooperationContractUndertakerId, ok := queryOptions["cooperationContractUndertakerId"]; ok && cooperationContractUndertakerId.(int64) != 0 {
		query = query.Where("cooperation_contract_undertaker_id = ?", cooperationContractUndertakerId)
	}
	if dividendsParticipateType, ok := queryOptions["dividendsParticipateType"]; ok && dividendsParticipateType.(int32) != 0 {
		query = query.Where("dividends_participate_type = ?", dividendsParticipateType)
	}
	if dividendsStage, ok := queryOptions["dividendsStage"]; ok && dividendsStage.(int32) != 0 {
		query = query.Where("dividends_stage = ?", dividendsStage)
	}
	if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
		query = query.Where(`dividends_estimate.company @> '{"companyId":"?"}'`, companyId)
	}
	if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
		query = query.Where(`dividends_estimate.org @> '{"orgId":"?"}'`, orgId)
	}
	//  非取消的分红预算单
	query.Where("dividends_estimate.is_canceled = ?", false)
	// 过滤出金额激励
	query.Where("dividends_estimate.dividends_type = ?", 3)
	ok, err := query.Exists()
	return ok, err
}

// CountDividendsEstimate 统计当前分红预算单总数
func (dao *DividendsEstimateDao) CountDividendsEstimate(queryOptions map[string]interface{}) (int, error) {
	tx := dao.transactionContext.PgTx
	var dividendsEstimateModels []*models.DividendsEstimate
	query := tx.Model(&dividendsEstimateModels)
	currentTime := time.Now()
	todayZeroTime := utils.GetZeroTime(currentTime)
	nextDayZeroTime := utils.GetNextDayZeroTime(currentTime)
	query.Where("dividends_estimate.created_at >= ?", todayZeroTime)
	query.Where("dividends_estimate.created_at < ?", nextDayZeroTime)
	if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
		query = query.Where(`dividends_estimate.company @> '{"companyId":"?"}'`, companyId)
	}
	if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
		query = query.Where(`dividends_estimate.org @> '{"orgId":"?"}'`, orgId)
	}
	if count, err := query.AllWithDeleted().Count(); err != nil {
		return 0, err
	} else {
		return count, nil
	}
}

// CountDividendsEstimateDividendsAmount 统计当前分红预算单总数
func (dao *DividendsEstimateDao) CountDividendsEstimateDividendsAmount(queryOptions map[string]interface{}) (float64, error) {
	tx := dao.transactionContext.PgTx
	var dividendsEstimateModels []*models.DividendsEstimate
	query := tx.Model(&dividendsEstimateModels)
	query.ColumnExpr("sum(dividends_amount) amount")
	if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
		query = query.Where(`dividends_estimate.company->>'companyId'='?'`, companyId)
	}
	if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
		query = query.Where(`dividends_estimate.org->>'orgId'='?'`, orgId)
	}
	if userBaseId, ok := queryOptions["userBaseId"]; ok && userBaseId.(int64) != 0 {
		query = query.Where(`dividends_user->>'userBaseId'='?'`, userBaseId)
	}
	if _, ok := queryOptions["beginTime"]; ok {
		query.Where(fmt.Sprintf("created_at>='%s' and created_at<'%s'", (queryOptions["beginTime"].(time.Time)).Format(time.RFC3339), (queryOptions["endTime"].(time.Time)).Format(time.RFC3339)))
	}
	if cooperationContractNumbers, ok := queryOptions["cooperationContractNumbers"]; ok && len(cooperationContractNumbers.([]string)) != 0 {
		query.Where("cooperation_contract_number in (?)", pg.In(cooperationContractNumbers))
	}
	query.AllWithDeleted()
	query.Where("is_canceled = false")
	var dividendsAmount float64
	if err := query.Select(&dividendsAmount); err != nil {
		return 0, err
	}
	return dividendsAmount, nil
}

// CountDividendsEstimate 统计当前分红预算单总数
func (dao *DividendsEstimateDao) DividendsEstimateStatistics(queryOptions map[string]interface{}, v interface{}) error {
	tx := dao.transactionContext.PgTx
	var dividendsEstimateModels []*models.DividendsEstimate
	query := tx.Model(&dividendsEstimateModels)
	query.ColumnExpr("sum(dividends_amount) total")
	query.ColumnExpr(`sum((case when dividends_account_status =1  then dividends_amount else 0 end)) accounting `)
	query.ColumnExpr(`sum((case when dividends_account_status =2  then dividends_amount else 0 end)) accounted `)
	if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
		query = query.Where(`dividends_estimate.company @> '{"companyId":"?"}'`, companyId)
	}
	if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
		query = query.Where(`dividends_estimate.org @> '{"orgId":"?"}'`, orgId)
	}
	if userBaseId, ok := queryOptions["userBaseId"]; ok && userBaseId.(int64) != 0 {
		query = query.Where(`dividends_user->>'userBaseId'='?'`, userBaseId)
	}
	if _, ok := queryOptions["beginTime"]; ok {
		query.Where(fmt.Sprintf("created_at>='%s' and created_at<'%s'", (queryOptions["beginTime"].(time.Time)).Format(time.RFC3339), (queryOptions["endTime"].(time.Time)).Format(time.RFC3339)))
	}
	if cooperationContractNumbers, ok := queryOptions["cooperationContractNumbers"]; ok && len(cooperationContractNumbers.([]string)) != 0 {
		query.Where("cooperation_contract_number in (?)", pg.In(cooperationContractNumbers))
	}
	//query.AllWithDeleted()
	query.Where("is_canceled =false")
	if err := query.Select(v); err != nil {
		return err
	}
	return nil
}

// CountDividendsEstimate 统计当前分红预算单总数
func (dao *DividendsEstimateDao) DividendsEstimateStatisticsGroup(queryOptions map[string]interface{}) (map[string]float64, error) {
	tx := dao.transactionContext.PgTx
	var dividendsEstimateModels []*models.DividendsEstimate
	query := tx.Model(&dividendsEstimateModels)
	query.Column("cooperation_contract_number")
	query.ColumnExpr("sum(dividends_amount) dividends_amount")
	//query.ColumnExpr(`sum((case when dividends_account_status =1  then dividends_amount else 0 end)) accounting `)
	//query.ColumnExpr(`sum((case when dividends_account_status =2  then dividends_amount else 0 end)) accounted `)
	if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
		query = query.Where(`dividends_estimate.company @> '{"companyId":"?"}'`, companyId)
	}
	if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
		query = query.Where(`dividends_estimate.org @> '{"orgId":"?"}'`, orgId)
	}
	if userBaseId, ok := queryOptions["userBaseId"]; ok && userBaseId.(int64) != 0 {
		query = query.Where(`dividends_user->>'userBaseId'='?'`, userBaseId)
	}
	if _, ok := queryOptions["beginTime"]; ok {
		query.Where(fmt.Sprintf("created_at>='%s' and created_at<'%s'", (queryOptions["beginTime"].(time.Time)).Format(time.RFC3339), (queryOptions["endTime"].(time.Time)).Format(time.RFC3339)))
	}
	if cooperationContractNumbers, ok := queryOptions["cooperationContractNumbers"]; ok && len(cooperationContractNumbers.([]string)) != 0 {
		query.Where("cooperation_contract_number in (?)", pg.In(cooperationContractNumbers))
	}
	query.Where("is_canceled =false")

	query.Group("cooperation_contract_number")
	err := query.Select(&dividendsEstimateModels)
	if err != nil {
		return nil, err
	}
	response := make(map[string]float64)
	for i := range dividendsEstimateModels {
		response[dividendsEstimateModels[i].CooperationContractNumber] = dividendsEstimateModels[i].DividendsAmount
	}

	return response, nil
}

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