From e4a9c3ac4d2bf3e40b9b64d44c30c4d8744d803f Mon Sep 17 00:00:00 2001 From: lzh <1625167628@qq.com> Date: Wed, 25 Jun 2025 11:56:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=A5=97=E9=A4=90=E4=BD=99?= =?UTF-8?q?=E9=A2=9D=E6=9F=A5=E8=AF=A2SQL=EF=BC=8C=E8=B0=83=E6=95=B4JOIN?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E5=B9=B6=E7=AE=80=E5=8C=96=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/dao/bundleExtend.go | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/internal/dao/bundleExtend.go b/internal/dao/bundleExtend.go index 38e61d8..66bded9 100644 --- a/internal/dao/bundleExtend.go +++ b/internal/dao/bundleExtend.go @@ -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)