micro-bundle/internal/dao/bundleExtend.go
2025-06-13 14:12:09 +08:00

209 lines
6.5 KiB
Go

package dao
import (
"micro-bundle/internal/model"
"micro-bundle/pb/bundle"
"micro-bundle/pkg/app"
"micro-bundle/pkg/utils"
"strings"
"time"
)
func AddBundleExtendRecord(data model.BundleExtensionRecords) error {
return app.ModuleClients.BundleDB.Create(&data).Error
}
func GetBundleExtendRecordList(req *bundle.BundleExtendRecordsListRequest) (data []model.BundleExtendRecordItemPo, total int64, err error) {
var berConditions []string
var berArgs []any
var bovaConditions []string
var bovaArgs []any
// 动态条件拼接示例
if req.User != "" {
if utils.IsPhoneNumber(req.User) {
berConditions = append(berConditions, "u.tel_num = ?")
berArgs = append(berArgs, req.User)
} else {
berConditions = append(berConditions, "u.nickname LIKE ?")
berArgs = append(berArgs, "%"+req.User+"%")
}
}
if req.Operator != "" {
if utils.IsPhoneNumber(req.Operator) {
berConditions = append(berConditions, "operator_phone_number = ?")
berArgs = append(berArgs, "%"+req.User+"%")
} else {
berConditions = append(berConditions, "operator_name LIKE ?")
berArgs = append(berArgs, "%"+req.User+"%")
}
}
if req.Type != 0 {
switch req.Type {
case 1: // 手动创建
{
bovaConditions = append(berConditions, "bova_uuid IS NULL")
}
case 2: // 自行购买
{
berConditions = append(berConditions, "operator_name IS NULL")
}
}
}
if req.AssociatedOrderNumber != "" {
bovaConditions = append(bovaConditions, "order_uuid LIKE ?")
bovaArgs = append(bovaArgs, "%"+req.AssociatedOrderNumber+"%")
}
if req.StartTime != 0 {
berConditions = append(berConditions, "ber.created_at >= ?")
berArgs = append(berArgs, time.UnixMilli(int64(req.StartTime)))
bovaConditions = append(bovaConditions, "bova.created_at >= ?")
bovaArgs = append(bovaArgs, time.UnixMilli(int64(req.StartTime)))
}
if req.EndTime != 0 {
berConditions = append(berConditions, "ber.created_at <= ?")
berArgs = append(berArgs, time.UnixMilli(int64(req.EndTime)))
bovaConditions = append(bovaConditions, "bova.created_at <= ?")
bovaArgs = append(bovaArgs, time.UnixMilli(int64(req.EndTime)))
}
berCWhereClause := ""
if len(berConditions) > 0 {
berCWhereClause = " WHERE " + strings.Join(berConditions, " AND ")
}
bovaCWhereClause := ""
if len(berConditions) > 0 {
bovaCWhereClause = " WHERE " + strings.Join(bovaConditions, " AND ")
}
// 拼接数据查询SQL
sql := `
SELECT
u.nickname AS user_name,
u.tel_num AS user_phone_number,
bova.num AS service_num,
bova.service_type AS service_type,
bova.order_uuid,
bova.uuid AS bova_uuid,
NULL AS account_additional,
NULL AS images_additional,
NULL AS data_additional,
NULL AS video_additional,
NULL AS operator_name,
NULL AS operator_phone_number,
bova.created_at as created_at
FROM
fiee_bundle.bundle_order_value_add AS bova
LEFT JOIN ` + "`micro-account`.`user`" + ` u ON u.id = bova.customer_id
` + bovaCWhereClause + `
UNION ALL
SELECT
u.nickname AS user_name,
u.tel_num AS user_phone_number,
NULL AS service_num,
NULL AS service_type,
NULL AS order_uuid,
NULL AS bova_uuid,
ber.account_additional,
ber.images_additional,
ber.data_additional,
ber.video_additional,
ber.operator_name,
ber.operator_phone_number,
ber.created_at as created_at
FROM
fiee_bundle.bundle_extension_records ber
LEFT JOIN ` + "`micro-account`.`user`" + ` u ON u.id = ber.user_id
` + berCWhereClause + `
ORDER BY created_at DESC
LIMIT ? OFFSET ?;
`
// 拼接总数查询SQL
countSql := `
SELECT COUNT(*) AS total_count FROM (
SELECT bova.created_at as created_at
FROM fiee_bundle.bundle_order_value_add AS bova
LEFT JOIN ` + "`micro-account`.`user`" + ` u ON u.id = bova.customer_id
` + bovaCWhereClause + `
UNION ALL
SELECT ber.created_at as created_at
FROM fiee_bundle.bundle_extension_records ber
LEFT JOIN ` + "`micro-account`.`user`" + ` u ON u.id = ber.user_id
` + berCWhereClause + `
) AS total_table;
`
if req.Page == 0 {
req.Page = 1
}
if req.PageSize == 0 {
req.PageSize = 100
}
offset := (req.Page - 1) * req.PageSize
// 查询总数
err = app.ModuleClients.BundleDB.Raw(countSql, append(bovaArgs, berArgs...)...).Scan(&total).Error
if err != nil {
return nil, 0, err
}
// 查询数据
argsWithPage := append(append(bovaArgs, berArgs...), req.PageSize, offset)
err = app.ModuleClients.BundleDB.Raw(sql, argsWithPage...).Scan(&data).Error
return
}
func GetBundleBalance(req *bundle.GetBundleBalanceReq) (data []model.BundleBalancePo, total int64, err error) {
subQuery := app.ModuleClients.BundleDB.Table("fiee_bundle.bundle_order_records").
Select("customer_id, MAX(created_at) as max_created_at").
Group("customer_id")
err = subQuery.Count(&total).Error
if err != nil {
return
}
if req.PageSize != 0 && req.Page != 0 {
subQuery = subQuery.Limit(int(req.PageSize)).Offset(int(req.Page-1) * int(req.PageSize))
}
session := app.ModuleClients.BundleDB.Table("fiee_bundle.bundle_order_records AS bor").
Select(`
bor.customer_id as user_id,
u.nickname as user_name,
u.tel_num as user_phone_number,
bor.bundle_name as bundle_name,
bor.expiration_time as expiration_time,
bor.status as bundle_status,
bova.num as service_num,
bova.service_type as service_type,
cw.work_category as work_category,
cw.uuid as cw_uuid,
bova.uuid as bova_uuid,
cma.user_id as cma_uuid,
ber.id as ber_id,
bova.source as oder_source,
ber.account_additional,
ber.images_additional,
ber.data_additional,
ber.video_additional
`).
Joins("JOIN (?) t ON bor.customer_id = t.customer_id AND bor.created_at = t.max_created_at", subQuery).
Joins("LEFT JOIN fiee_bundle.bundle_order_value_add bova ON bova.order_uuid = bor.uuid").
Joins("LEFT JOIN fiee_bundle.cast_work cw ON cw.artist_uuid = bor.customer_id AND cw.created_at > UNIX_TIMESTAMP(bor.created_at)").
Joins("LEFT JOIN `micro-account`.`user` u ON u.id = bor.customer_id").
Joins("LEFT JOIN fiee_bundle.cast_media_account cma ON cma.artist_uuid = u.id").
Joins("LEFT JOIN fiee_bundle.bundle_extension_records ber ON ber.user_id = bor.customer_id").
Where("bor.customer_id != ?", "")
if req.BundleName != "" {
session.Where("bor.bundle_name like ?", "%"+req.BundleName+"%")
}
if req.UserName != "" {
session.Where("u.nickname like ?", "%"+req.UserName+"%")
}
if req.ExpiredTimeStart != 0 {
session.Where("bor.expiration_time >= ?", req.ExpiredTimeStart)
}
if req.ExpiredTimeEnd != 0 {
session.Where("bor.expiration_time <= ?", req.ExpiredTimeEnd)
}
err = session.Scan(&data).Error
return
}