diff --git a/internal/dao/bundleDao.go b/internal/dao/bundleDao.go index fb630ec..df3be93 100644 --- a/internal/dao/bundleDao.go +++ b/internal/dao/bundleDao.go @@ -8,6 +8,8 @@ import ( "micro-bundle/pkg/app" commonErr "micro-bundle/pkg/err" "micro-bundle/pkg/msg" + "micro-bundle/pkg/utils" + "strings" "time" "gorm.io/gorm" @@ -391,64 +393,124 @@ func AddBundleExtendRecord(data model.BundleExtensionRecords) 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 - FROM - fiee_bundle.bundle_order_value_add AS bova - ` + "LEFT JOIN `micro-account`.`user` u ON u.id = bova.customer_id" + ` - - 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 - FROM - fiee_bundle.bundle_extension_records ber - ` + "LEFT JOIN `micro-account`.`user` u ON u.id = ber.user_id" + ` - ORDER BY created_at DESC - LIMIT ? OFFSET ?; + 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 + 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" + ` + LEFT JOIN ` + "`micro-account`.`user`" + ` u ON u.id = bova.customer_id + ` + bovaCWhereClause + ` UNION ALL - SELECT ber.created_at + 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" + ` + LEFT JOIN ` + "`micro-account`.`user`" + ` u ON u.id = ber.user_id + ` + berCWhereClause + ` ) AS total_table; ` - // 查询总数 - err = app.ModuleClients.BundleDB.Raw(countSql).Scan(&total).Error - if err != nil { - return nil, 0, err - } if req.Page == 0 { req.Page = 1 } @@ -457,8 +519,15 @@ func GetBundleExtendRecordList(req *bundle.BundleExtendRecordsListRequest) (data } 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 + } + // 查询数据 - err = app.ModuleClients.BundleDB.Raw(sql, req.PageSize, offset).Scan(&data).Error + argsWithPage := append(append(bovaArgs, berArgs...), req.PageSize, offset) + err = app.ModuleClients.BundleDB.Raw(sql, argsWithPage...).Scan(&data).Error return } @@ -473,7 +542,7 @@ func GetBundleBalance(req *bundle.GetBundleBalanceReq) (data []model.BundleBalan if req.PageSize != 0 && req.Page != 0 { subQuery = subQuery.Limit(int(req.PageSize)).Offset(int(req.Page-1) * int(req.PageSize)) } - app.ModuleClients.BundleDB.Table("fiee_bundle.bundle_order_records AS bor"). + session := app.ModuleClients.BundleDB.Table("fiee_bundle.bundle_order_records AS bor"). Select(` bor.customer_id as user_id, u.nickname as user_name, @@ -500,8 +569,19 @@ func GetBundleBalance(req *bundle.GetBundleBalanceReq) (data []model.BundleBalan 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 != ?", ""). - // Where("") - Scan(&data) + 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 }