pg_credit_account_dao.go 9.2 KB
package dao

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

type CreditAccountDao struct {
	transactionContext *pgTransaction.TransactionContext
}

// GenerateCreditAccountNumber 生成账期结算单号
func (dao *CreditAccountDao) GenerateCreditAccountNumber(queryOptions map[string]interface{}) (string, error) {
	tx := dao.transactionContext.PgTx
	var creditAccountModels []*models.CreditAccount
	query := tx.Model(&creditAccountModels)
	currentTime := time.Now()
	todayZeroTime := utils.GetZeroTime(currentTime)
	nextDayZeroTime := utils.GetNextDayZeroTime(currentTime)
	query.Where("credit_account.created_at >= ?", todayZeroTime)
	query.Where("credit_account.created_at < ?", nextDayZeroTime)
	if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
		query = query.Where(`credit_account.company @> '{"companyId":"?"}'`, companyId)
	}
	if count, err := query.AllWithDeleted().SelectAndCount(); 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)]
			creditAccountNumber := "JS" + timeString + "#" + countStr
			return creditAccountNumber, 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)]
			creditAccountNumber := "JS" + timeString + "#" + countStr
			return creditAccountNumber, nil
		}
	}
}

// CheckCreditAccountNumberAvailable 校验账期结算单号是否唯一
func (dao *CreditAccountDao) CheckCreditAccountNumberAvailable(queryOptions map[string]interface{}) (bool, error) {
	tx := dao.transactionContext.PgTx
	var creditAccountModels []*models.CreditAccount
	query := tx.Model(&creditAccountModels)
	if creditAccountNumber, ok := queryOptions["creditAccountNumber"]; ok && creditAccountNumber != "" {
		query = query.Where("credit_account_number = ?", creditAccountNumber)
	}
	if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
		query = query.Where(`credit_account.company @> '{"companyId":"?"}'`, companyId)
	}
	if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
		query = query.Where(`credit_account.org @> '{"orgId":"?"}'`, orgId)
	}
	ok, err := query.Exists()
	return !ok, err
}

// 分红统计(分红明细)
func (dao *CreditAccountDao) DividendsStatistics(queryOptions map[string]interface{}, v interface{}) error {
	creditAccount := new(models.CreditAccount)
	query := dao.transactionContext.PgTx.Model(creditAccount)
	query.ColumnExpr(`sum(settlement_amount) total`)
	query.ColumnExpr(`sum((case when payment_status = 2 then actually_paid_amount else 0 end)) paid`)
	query.ColumnExpr(`sum((case when payment_status = 1 then settlement_amount else 0 end)) unpaid`)
	query.ColumnExpr(`sum((case when settlement_time is not null then settlement_amount else 0 end)) accounted `)
	if v, ok := queryOptions["beginTime"]; ok && !(v.(time.Time).IsZero()) {
		query.Where(`created_at>=? `, queryOptions["beginTime"])
		query.Where(`created_at<? `, queryOptions["endTime"])
	}
	if v, ok := queryOptions["userBaseId"]; ok && v.(int64) > 0 {
		query.Where(fmt.Sprintf(`participator->>'userBaseId'='%v' `, v))
	}
	if v, ok := queryOptions["userId"]; ok && v.(int64) > 0 {
		query.Where(fmt.Sprintf(`participator->>'userId'='%v' `, v))
	}
	if v, ok := queryOptions["orgId"]; ok && v.(int64) > 0 {
		query.Where(fmt.Sprintf(` org->>'orgId'= '%v'`, v))
	}
	if v, ok := queryOptions["paymentBeginTime"]; ok && !(v.(time.Time).IsZero()) {
		query.Where(`payment_time>=? `, queryOptions["paymentBeginTime"])
	}
	if v, ok := queryOptions["paymentEndTime"]; ok && !(v.(time.Time).IsZero()) {
		query.Where(`payment_time<? `, queryOptions["paymentEndTime"])
	}
	if v, ok := queryOptions["cooperationContractNumbers"]; ok && len(v.([]string)) > 0 {
		//query.Where("cooperation_contract_number in (?)", pg.In(v))
		query.Where(domain.ConditionInContractNumbers(v.([]string)))
	}
	query.Where("deleted_at is null")
	err := query.Select(v)
	return err
}

// 分红统计(分红明细)
func (dao *CreditAccountDao) CooperationUsersDividendsStatistics(queryOptions map[string]interface{}, v interface{}) error {
	creditAccount := new(models.CreditAccount)
	query := dao.transactionContext.PgTx.Model(creditAccount)
	query.ColumnExpr(`0 cooperation_time`)
	query.ColumnExpr(`sum(good_amount_count) dividends_order_amount`)
	query.ColumnExpr(`sum(settlement_amount) divides_amount`)
	query.ColumnExpr(`sum((case when payment_status = 2 then actually_paid_amount else 0 end)) actually_paid_amount`)
	query.ColumnExpr(`sum((case when payment_status = 1 then settlement_amount else 0 end)) un_paid_amount`)
	query.ColumnExpr(`max(participator->>'userId') user_id`)
	query.ColumnExpr(`max(participator#>>'{userInfo,userName}') user_name`)
	//if _, ok := queryOptions["beginTime"]; ok && !queryOptions["beginTime"].(time.Time).IsZero() {
	//	query.Where(`created_at>? `, queryOptions["beginTime"])
	//	query.Where(`created_at<? `, queryOptions["endTime"])
	//}
	if v, ok := queryOptions["userBaseId"]; ok && v.(int64) > 0 {
		query.Where(fmt.Sprintf(`participator->>'userBaseId'='%v' `, v))
	}
	if v, ok := queryOptions["orgId"]; ok && v.(int64) > 0 {
		query.Where(fmt.Sprintf(` org->>'orgId'= '%v'`, v))
	}
	if v, ok := queryOptions["paymentStatus"]; ok && v.(int32) > 0 {
		query.Where("paymentStatus = ?", v)
	}
	if v, ok := queryOptions["beginTime"]; ok && !v.(time.Time).IsZero() {
		query.Where("created_at >= ?", v)
	}
	if v, ok := queryOptions["endTime"]; ok && !v.(time.Time).IsZero() {
		query.Where("created_at < ?", v)
	}
	if v, ok := queryOptions["paymentBeginTime"]; ok && !(v.(time.Time).IsZero()) {
		query.Where(`payment_time>=? `, queryOptions["paymentBeginTime"])
	}
	if v, ok := queryOptions["paymentEndTime"]; ok && !(v.(time.Time).IsZero()) {
		query.Where(`payment_time<? `, queryOptions["paymentEndTime"])
	}
	if v, ok := queryOptions["cooperationContractNumbers"]; ok && len(v.([]string)) > 0 {
		//query.Where("cooperation_contract_number in (?)", pg.In(v))
		query.Where(domain.ConditionInContractNumbers(v.([]string)))
	}
	query.Where("deleted_at is null")
	if v, ok := queryOptions["sortByActuallyPaidAmount"]; ok {
		vInt := v.(int)
		if vInt == 1 {
			query.Order("actually_paid_amount asc")
		} else {
			query.Order("actually_paid_amount desc")
		}
	}
	query.GroupExpr("participator->>'userId'")
	query.Limit(queryOptions["limit"].(int))
	query.Offset(queryOptions["offset"].(int))
	err := query.Select(v)
	return err
}

// 共创企业分红统计
func (dao *CreditAccountDao) CooperationCompanyDividendsStatistics(queryOptions map[string]interface{}, v interface{}) error {
	creditAccount := new(models.CreditAccount)
	query := dao.transactionContext.PgTx.Model(creditAccount)
	query.ColumnExpr(`0 cooperation_time`)
	query.ColumnExpr(`sum(good_amount_count) dividends_order_amount`)
	query.ColumnExpr(`sum(settlement_amount) divides_amount`)
	query.ColumnExpr(`sum((case when payment_status = 2 then actually_paid_amount else 0 end)) actually_paid_amount`)
	query.ColumnExpr(`max(org->>'orgId') org_id`)
	query.ColumnExpr(`max(org->>'orgName') org_name`)
	if _, ok := queryOptions["beginTime"]; ok {
		query.Where(`created_at>? `, queryOptions["beginTime"])
		query.Where(`created_at<? `, queryOptions["endTime"])
	}
	if v, ok := queryOptions["userBaseId"]; ok && v.(int64) > 0 {
		query.Where(fmt.Sprintf(`participator->>'userBaseId'='%v' `, v))
	}
	if v, ok := queryOptions["orgId"]; ok && v.(int64) > 0 {
		query.Where(fmt.Sprintf(` org->>'orgId'= '%v'`, v))
	}
	if v, ok := queryOptions["paymentStatus"]; ok && v.(int32) > 0 {
		query.Where("paymentStatus = ?", v)
	}
	if v, ok := queryOptions["beginTime"]; ok && !v.(time.Time).IsZero() {
		query.Where("created_at >= ?", v)
	}
	if v, ok := queryOptions["endTime"]; ok && !v.(time.Time).IsZero() {
		query.Where("created_at < ?", v)
	}
	if v, ok := queryOptions["paymentBeginTime"]; ok && !(v.(time.Time).IsZero()) {
		query.Where(`payment_time>=? `, queryOptions["paymentBeginTime"])
	}
	if v, ok := queryOptions["paymentEndTime"]; ok && !(v.(time.Time).IsZero()) {
		query.Where(`payment_time<? `, queryOptions["paymentEndTime"])
	}
	query.Where("deleted_at is null")
	if v, ok := queryOptions["sortByActuallyPaidAmount"]; ok {
		vInt := v.(int)
		if vInt == 1 {
			query.Order("actually_paid_amount asc")
		} else {
			query.Order("actually_paid_amount desc")
		}
	}
	query.GroupExpr("org->>'orgId'")
	query.Limit(queryOptions["limit"].(int))
	query.Offset(queryOptions["offset"].(int))
	err := query.Select(v)
	return err
}

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