优化套餐余额查询SQL,调整JOIN逻辑并简化查询条件

This commit is contained in:
lzh 2025-06-25 11:56:09 +08:00
parent 92c8e4d574
commit e4a9c3ac4d

View File

@ -97,25 +97,22 @@ func GetBundleExtendRecordList(req *bundle.BundleExtendRecordsListRequest) (data
}
func GetBundleBalanceList(req *bundle.GetBundleBalanceListReq) (data []model.BundleBalancePo, total int64, err error) {
subQuery := app.ModuleClients.BundleDB.Table("bundle_order_records").
Select("customer_id, MAX(created_at) AS max_created_time").
Group("customer_id")
subQuery := app.ModuleClients.BundleDB.Table("bundle_order_records as bor1").
Select("bor1.*").
Joins(`INNER JOIN (
SELECT customer_id, MAX(created_at) AS max_created_time
FROM bundle_order_records
GROUP BY customer_id
) bor2 ON bor1.customer_id = bor2.customer_id AND bor1.created_at = bor2.max_created_time`)
session := app.ModuleClients.BundleDB.Table("`micro-account`.`user` AS u").
Select(`
bb.*,
bor.expiration_time as expired_time,
bor.bundle_name,
bor.status,
bor.uuid as order_uuid,
rn.name as user_name,
u.tel_num as user_phone_number,
u.id as user_id
`).
Joins("LEFT JOIN fiee_bundle.bundle_balance AS bb on u.id = bb.user_id").
Joins("LEFT JOIN `micro-account`.`real_name` rn on u.real_name_id = rn.id").
Joins("LEFT JOIN bundle_order_records bor on bor.uuid = bb.order_uuid").
Joins("LEFT JOIN (?) AS t ON bor.customer_id = t.customer_id AND bor.created_at = t.max_created_time", subQuery).
Where("rn.name IS NOT NULL and u.deleted_at = 0").Group("u.id")
Select(`bb.*, bor.expiration_time as expired_time, bor.bundle_name, bor.status,
bor.uuid as order_uuid, rn.name as user_name,
u.tel_num as user_phone_number, u.id as user_id`).
Joins("LEFT JOIN `micro-account`.real_name rn ON u.real_name_id = rn.id").
Joins("LEFT JOIN (?) as bor ON bor.customer_id = u.id", subQuery).
Joins("LEFT JOIN fiee_bundle.bundle_balance bb ON u.id = bb.user_id AND bb.order_uuid = bor.uuid").
Where("rn.name IS NOT NULL").
Where("u.deleted_at = 0")
if req.UserName != "" {
if utils.IsPhoneNumber(req.UserName) {
session = session.Where("u.tel_num = ?", req.UserName)