2025-02-20 12:40:39 +00:00
package dao
import (
2025-05-28 01:34:43 +00:00
"errors"
"fmt"
2025-02-20 12:40:39 +00:00
"micro-bundle/internal/model"
"micro-bundle/pb/bundle"
"micro-bundle/pkg/app"
commonErr "micro-bundle/pkg/err"
"micro-bundle/pkg/msg"
2025-06-09 05:58:27 +00:00
"micro-bundle/pkg/utils"
2025-05-28 01:34:43 +00:00
"time"
"gorm.io/gorm"
2025-02-20 12:40:39 +00:00
)
2025-02-22 10:53:35 +00:00
func CreateBundle ( req * model . BundleProfile ) ( res * bundle . CommonResponse , err error ) {
2025-02-20 12:40:39 +00:00
res = new ( bundle . CommonResponse )
2025-02-22 10:53:35 +00:00
err = app . ModuleClients . BundleDB . Model ( & model . BundleProfile { } ) . Create ( & req ) . Error
2025-02-20 12:40:39 +00:00
if err != nil {
res . Msg = msg . ErrorCreateBundleInfo
return res , commonErr . ReturnError ( err , msg . ErrorCreateBundleInfo , "创建套餐信息失败: " )
}
res . Msg = msg . SuccessCreateBundleInfo
return
}
2025-02-22 10:53:35 +00:00
func UpdateBundle ( req * model . BundleProfile ) ( res * bundle . CommonResponse , err error ) {
2025-02-20 12:40:39 +00:00
res = new ( bundle . CommonResponse )
2025-05-28 01:34:43 +00:00
err = app . ModuleClients . BundleDB . Model ( & model . BundleProfile { } ) . Where ( "uuid = ? and language= ?" , req . UUID , req . Language ) . Updates ( req ) . Error
2025-02-20 12:40:39 +00:00
if err != nil {
res . Msg = msg . ErrorUpdateBundleInfo
return res , commonErr . ReturnError ( err , msg . ErrorUpdateBundleInfo , "更新套餐信息失败: " )
}
res . Msg = msg . SuccessUpdateBundleInfo
return
}
func DeleteBundle ( uuid string ) ( res * bundle . CommonResponse , err error ) {
res = new ( bundle . CommonResponse )
err = app . ModuleClients . BundleDB . Where ( "uuid = ?" , uuid ) . Delete ( & model . BundleProfile { } ) . Error
if err != nil {
res . Msg = msg . ErrorDeleteBundleInfo
return res , commonErr . ReturnError ( err , msg . ErrorDeleteBundleInfo , "删除套餐信息失败: " )
}
res . Msg = msg . SuccessDeleteBundleInfo
return
}
func BundleList ( req * bundle . BundleListRequest ) ( res * bundle . BundleListResponse , err error ) {
res = new ( bundle . BundleListResponse )
res . Bundles = make ( [ ] * bundle . BundleProfile , 0 )
bundles := make ( [ ] * model . BundleProfile , 0 )
query := app . ModuleClients . BundleDB . Model ( & model . BundleProfile { } )
if req . Name != "" {
query = query . Where ( "name like ?" , "%" + req . Name + "%" )
}
if req . Content != "" {
query = query . Where ( "content like ?" , "%" + req . Content + "%" )
}
2025-02-22 11:05:19 +00:00
if req . Language != "" {
query = query . Where ( "language like ?" , req . Language )
}
2025-02-20 12:40:39 +00:00
count := * query
2025-02-22 12:12:34 +00:00
if req . PageSize != 0 && req . Page != 0 {
query = query . Limit ( int ( req . PageSize ) ) . Offset ( int ( req . Page - 1 ) * int ( req . PageSize ) )
}
if err = query . Find ( & bundles ) . Error ; err != nil {
2025-02-20 12:40:39 +00:00
return res , commonErr . ReturnError ( err , msg . ErrorGetBundleList , "获取套餐列表失败: " )
}
2025-02-22 10:58:52 +00:00
for _ , bundleProfile := range bundles {
2025-02-22 11:05:19 +00:00
res . Bundles = append ( res . Bundles , & bundle . BundleProfile {
2025-03-12 07:54:22 +00:00
Uuid : bundleProfile . UUID ,
Name : bundleProfile . Name ,
Price : bundleProfile . Price ,
PriceType : bundleProfile . PriceType ,
Contract : bundleProfile . Contract ,
Content : bundleProfile . Content ,
Language : bundleProfile . Language ,
CreatedAt : bundleProfile . CreatedAt . String ( ) ,
UpdatedAt : bundleProfile . UpdatedAt . String ( ) ,
CompanySign : bundleProfile . CompanySign ,
ContractDuration : int64 ( bundleProfile . ContractDuration ) ,
2025-03-28 20:59:56 +00:00
BundleCommonUid : bundleProfile . BundleCommonUid ,
2025-02-22 11:05:19 +00:00
} )
2025-02-22 10:58:52 +00:00
}
2025-02-22 10:56:39 +00:00
2025-02-22 11:16:11 +00:00
// _ = copier.CopyWithOption(&res.Bundles, bundles, copier.Option{DeepCopy: true})
2025-02-20 12:40:39 +00:00
2025-02-22 11:16:11 +00:00
// fmt.Printf("res.Bundles %+v\n", res.Bundles)
2025-02-22 10:56:39 +00:00
2025-02-20 12:40:39 +00:00
var total int64
count . Count ( & total )
res . Total = int32 ( total )
return
}
func BundleDetail ( uuid string ) ( res * bundle . BundleProfile , err error ) {
res = new ( bundle . BundleProfile )
2025-02-23 06:04:06 +00:00
bundleProfile := new ( model . BundleProfile )
err = app . ModuleClients . BundleDB . Where ( "uuid = ?" , uuid ) . First ( & bundleProfile ) . Error
2025-02-20 12:40:39 +00:00
if err != nil {
return res , commonErr . ReturnError ( err , msg . ErrorGetBundleInfo , "获取套餐信息失败: " )
}
2025-02-23 06:04:06 +00:00
//_ = copier.CopyWithOption(&res, bundle, copier.Option{DeepCopy: true})
res = & bundle . BundleProfile {
2025-03-12 07:54:22 +00:00
Uuid : bundleProfile . UUID ,
Name : bundleProfile . Name ,
Price : bundleProfile . Price ,
PriceType : bundleProfile . PriceType ,
Contract : bundleProfile . Contract ,
Content : bundleProfile . Content ,
Language : bundleProfile . Language ,
CreatedAt : bundleProfile . CreatedAt . String ( ) ,
UpdatedAt : bundleProfile . UpdatedAt . String ( ) ,
CompanySign : bundleProfile . CompanySign ,
ContractDuration : int64 ( bundleProfile . ContractDuration ) ,
2025-03-28 21:41:02 +00:00
BundleCommonUid : bundleProfile . BundleCommonUid ,
2025-02-23 06:04:06 +00:00
}
2025-02-20 12:40:39 +00:00
return
}
2025-05-28 01:34:43 +00:00
func BundleListV2 ( req * bundle . BundleListRequest ) ( res * bundle . BundleListResponse , err error ) {
res = new ( bundle . BundleListResponse )
res . Bundles = make ( [ ] * bundle . BundleProfile , 0 )
bundles := make ( [ ] * model . BundleProfile , 0 )
query := app . ModuleClients . BundleDB . Model ( & model . BundleProfile { } ) . Preload ( "BundleProfileLang" )
//query = query.Where("shelf_status = ?", 1) //上架的
if req . Name != "" {
query = query . Where ( "name like ?" , "%" + req . Name + "%" )
}
if req . Content != "" {
query = query . Where ( "content like ?" , "%" + req . Content + "%" )
}
if req . Language != "" {
query = query . Where ( "language like ?" , req . Language )
}
count := * query
// 排序: sort 升序,相同 sort 按 created_at 倒序
query = query . Order ( "sort ASC" ) . Order ( "created_at DESC" )
if req . PageSize != 0 && req . Page != 0 {
query = query . Limit ( int ( req . PageSize ) ) . Offset ( int ( req . Page - 1 ) * int ( req . PageSize ) )
}
if err = query . Find ( & bundles ) . Error ; err != nil {
return res , commonErr . ReturnError ( err , msg . ErrorGetBundleList , "获取套餐列表失败: " )
}
if bundles != nil && len ( bundles ) > 0 {
for _ , bundleProfile := range bundles {
selectValueAddService := make ( [ ] * bundle . SelectValueAddService , 0 )
// 通过中间表拼接增值服务数据
if bundleProfile . UUID != "" {
bundleToValueAddServices , err := GetBundleToValueAddServiceByBundleUuid ( bundleProfile . UUID )
if err != nil {
return res , commonErr . ReturnError ( err , msg . ErrorGetBundleInfo , "获取增值服务信息失败: " )
}
for _ , v := range bundleToValueAddServices {
valueAddDetail , _ := ValueAddServiceDetail ( v . ValueUid , "" )
selectValueAddService = append ( selectValueAddService , & bundle . SelectValueAddService {
ValueAddUuid : v . ValueUid ,
IsDisplay : v . IsDisplay ,
ServiceName : valueAddDetail . ServiceName ,
} )
}
}
bundleProfileLang := [ ] * bundle . BundleProfileLang { }
if bundleProfile . BundleProfileLang != nil && len ( bundleProfile . BundleProfileLang ) > 0 {
for _ , lang := range bundleProfile . BundleProfileLang {
bpl := & bundle . BundleProfileLang {
Uuid : lang . UUID ,
Name : lang . Name ,
Price : lang . Price ,
PriceType : lang . PriceType ,
Content : lang . Content ,
Language : lang . Language ,
CreatedAt : time . Unix ( lang . CreatedAt , 0 ) . Format ( "2006-01-02 15:04:05" ) ,
UpdatedAt : time . Unix ( int64 ( lang . UpdatedAt ) , 0 ) . Format ( "2006-01-02 15:04:05" ) ,
}
bundleProfileLang = append ( bundleProfileLang , bpl )
}
}
res . Bundles = append ( res . Bundles , & bundle . BundleProfile {
Uuid : bundleProfile . UUID ,
Name : bundleProfile . Name ,
Content : bundleProfile . Content ,
Price : bundleProfile . Price ,
PriceType : bundleProfile . PriceType ,
Contract : bundleProfile . Contract ,
Language : bundleProfile . Language ,
CreatedAt : bundleProfile . CreatedAt . Format ( "2006-01-02 15:04:05" ) ,
UpdatedAt : bundleProfile . UpdatedAt . Format ( "2006-01-02 15:04:05" ) ,
CompanySign : bundleProfile . CompanySign ,
ContractDuration : int64 ( bundleProfile . ContractDuration ) ,
Sort : bundleProfile . Sort ,
BgImg1 : bundleProfile . BgImg1 ,
BgImg2 : bundleProfile . BgImg2 ,
SelectValueAddService : selectValueAddService ,
BundleProfileLang : bundleProfileLang ,
ShelfStatus : int64 ( bundleProfile . ShelfStatus ) ,
} )
}
}
var total int64
count . Count ( & total )
res . Total = int32 ( total )
return
}
func BundleDetailV2 ( req * bundle . BundleDetailRequest ) ( res * bundle . BundleDetailResponseV2 , err error ) {
if req . Uuid == "" {
return res , errors . New ( "uuid不能为空" )
}
if req . Language == "" {
return res , errors . New ( "language不能为空" )
}
res = new ( bundle . BundleDetailResponseV2 )
bundleProfile := new ( model . BundleProfile )
err = app . ModuleClients . BundleDB . Model ( & model . BundleProfile { } ) . Where ( "uuid = ?" , req . Uuid ) . Preload ( "BundleProfileLang" ) . First ( & bundleProfile ) . Error
if err != nil {
return res , commonErr . ReturnError ( err , msg . ErrorGetBundleInfo , "获取套餐信息失败: " )
}
bundleProfileLang := & bundle . BundleProfileLang {
BgImg1 : bundleProfile . BgImg1 ,
BgImg2 : bundleProfile . BgImg2 ,
Sort : bundleProfile . Sort ,
}
if bundleProfile != nil && bundleProfile . BundleProfileLang != nil && len ( bundleProfile . BundleProfileLang ) > 0 {
for _ , lang := range bundleProfile . BundleProfileLang {
if lang . Language == req . Language {
bundleProfileLang = & bundle . BundleProfileLang {
Uuid : lang . UUID ,
Name : lang . Name ,
Price : lang . Price ,
PriceType : lang . PriceType ,
Content : lang . Content ,
Language : lang . Language ,
CreatedAt : time . Unix ( lang . CreatedAt , 0 ) . Format ( "2006-01-02 15:04:05" ) ,
UpdatedAt : time . Unix ( int64 ( lang . UpdatedAt ) , 0 ) . Format ( "2006-01-02 15:04:05" ) ,
}
}
}
}
selectValueAddService := make ( [ ] * bundle . SelectValueAddService , 0 ) //已选增值服务
// 通过中间表拼接增值服务数据
if bundleProfile . UUID != "" {
bundleToValueAddServices , err := GetBundleToValueAddServiceByBundleUuid ( bundleProfile . UUID )
if err != nil {
return res , commonErr . ReturnError ( err , msg . ErrorGetBundleInfo , "获取增值服务信息失败: " )
}
for _ , valueAddService := range bundleToValueAddServices {
valueAddDeatilData , err := ValueAddServiceDetailByUuidAndLanguage ( valueAddService . ValueUid , req . Language )
if err != nil {
return res , commonErr . ReturnError ( err , msg . ErrorGetBundleInfo , "获取增值服务信息失败: " )
}
ValueAddServiceLang := & bundle . ValueAddServiceLang {
Uuid : valueAddDeatilData . UUID ,
ServiceName : valueAddDeatilData . ServiceName ,
ServiceType : int32 ( valueAddDeatilData . ServiceType ) ,
PriceMode : int32 ( valueAddDeatilData . PriceMode ) ,
PriceType : int64 ( valueAddDeatilData . PriceType ) ,
OriginalPrice : fmt . Sprintf ( "%.2f" , float32 ( valueAddDeatilData . OriginalPrice ) ) ,
Unit : string ( valueAddDeatilData . Unit ) ,
Language : valueAddDeatilData . Language ,
CreatedAt : time . Unix ( valueAddDeatilData . CreatedAt , 0 ) . Format ( "2006-01-02 15:04:05" ) ,
UpdatedAt : time . Unix ( valueAddDeatilData . UpdatedAt , 0 ) . Format ( "2006-01-02 15:04:05" ) ,
}
bundleProfileLang . ValueAddServiceLang = append ( bundleProfileLang . ValueAddServiceLang , ValueAddServiceLang )
selectValueAddService = append ( selectValueAddService , & bundle . SelectValueAddService {
ValueAddUuid : valueAddService . ValueUid ,
ServiceName : valueAddDeatilData . ServiceName ,
IsDisplay : valueAddService . IsDisplay ,
} )
}
}
if selectValueAddService != nil && len ( selectValueAddService ) > 0 {
res . SelectValueAddService = selectValueAddService
}
res . Bundle = bundleProfileLang
return
}
// 套餐上下架
func HandShelf ( uuid string , shelfStatus int64 ) ( res * bundle . CommonResponse , err error ) {
res = new ( bundle . CommonResponse )
err = app . ModuleClients . BundleDB . Model ( & model . BundleProfile { } ) . Where ( "uuid = ?" , uuid ) . Update ( "shelf_status" , shelfStatus ) . Error
if err != nil {
res . Msg = "套餐上下架操作失败"
return res , err
}
res . Msg = "套餐上下架操作成功"
return res , nil
}
// 通过uuid和language查询套餐语言表
func BundleDetailByUuidAndLanguage ( uuid string , language string ) ( bundleProfileLang * model . BundleProfileLang , err error ) {
err = app . ModuleClients . BundleDB . Where ( "uuid = ? AND language = ?" , uuid , language ) . First ( & bundleProfileLang ) . Error
if err != nil {
return nil , err
}
return
}
// 套餐主表创建
func TxCreateBundle ( tx * gorm . DB , req * model . BundleProfile ) ( err error ) {
err = tx . Model ( & model . BundleProfile { } ) . Create ( & req ) . Error
if err != nil {
return commonErr . ReturnError ( err , msg . ErrorCreateBundleInfo , "创建套餐信息失败: " )
}
return
}
// 套餐语言表创建
func TxCreateBundleLang ( tx * gorm . DB , req * model . BundleProfileLang ) ( err error ) {
err = tx . Model ( & model . BundleProfileLang { } ) . Create ( & req ) . Error
if err != nil {
return commonErr . ReturnError ( err , msg . ErrorCreateBundleInfo , "创建套餐信息失败: " )
}
return
}
func TxUpdateBundle ( tx * gorm . DB , uuid string , columns map [ string ] interface { } ) ( err error ) {
err = tx . Model ( & model . BundleProfile { } ) . Where ( "uuid =?" , uuid ) . Updates ( columns ) . Error
if err != nil {
return commonErr . ReturnError ( err , msg . ErrorUpdateBundleInfo , "更新套餐信息失败: " )
}
return
}
func TxUpdateBundleLang ( tx * gorm . DB , uuid string , language string , columns map [ string ] interface { } ) ( err error ) {
err = tx . Model ( & model . BundleProfileLang { } ) . Where ( "uuid =? and language=?" , uuid , language ) . Updates ( columns ) . Error
if err != nil {
return commonErr . ReturnError ( err , msg . ErrorUpdateBundleInfo , "更新套餐信息失败: " )
}
return
}
func CreateBundleToValueAddService ( tx * gorm . DB , records [ ] * model . BundleToValueAddService ) error {
return tx . Model ( & model . BundleToValueAddService { } ) . Create ( & records ) . Error
}
func DeleteBundleToValueAddService ( tx * gorm . DB , bundleUuid , valueUid string ) error {
return tx . Where ( "bundle_uuid = ? AND value_uid = ?" , bundleUuid , valueUid ) . Delete ( & model . BundleToValueAddService { } ) . Error
}
func GetValueAddServiceUuidsByBundleUuid ( bundleUuid string ) ( [ ] string , error ) {
var uuids [ ] string
err := app . ModuleClients . BundleDB . Model ( & model . BundleToValueAddService { } ) . Where ( "bundle_uuid = ?" , bundleUuid ) . Pluck ( "value_uid" , & uuids ) . Error
return uuids , err
}
// GetBundleToValueAddServiceByBundleUuid 根据套餐UUID获取所有关联的增值服务uuid
func GetBundleToValueAddServiceByBundleUuid ( bundleUuid string ) ( [ ] * model . BundleToValueAddService , error ) {
var result [ ] * model . BundleToValueAddService
err := app . ModuleClients . BundleDB . Model ( & model . BundleToValueAddService { } ) .
Where ( "bundle_uuid = ?" , bundleUuid ) .
Find ( & result ) . Error
return result , err
}
func GetBundleLangsByUuid ( uuid string ) ( [ ] * model . BundleProfileLang , error ) {
var result [ ] * model . BundleProfileLang
err := app . ModuleClients . BundleDB . Model ( & model . BundleProfileLang { } ) .
Where ( "uuid =?" , uuid ) .
Find ( & result ) . Error
return result , err
}
2025-06-09 05:58:27 +00:00
func AddBundleExtendRecord ( data model . BundleExtensionRecords ) error {
return app . ModuleClients . BundleDB . Create ( & data ) . Error
}
func GetBundleExtendRecordList ( req * bundle . BundleExtendRecordsListRequest ) ( [ ] model . BundleExtendRecordItemPo , int64 , error ) {
session := app . ModuleClients . BundleDB . Select ( "*,mu.nickname as user_name,mu.tel_num as user_phone_number,fu.tel_num as operator_phone_number,fu.nickname as operator_name" ) .
Model ( & model . BundleExtensionRecords { } ) .
Joins ( "left join `fontree-account`.`user` fu on operator_id = fu.id left join `micro-account`.`user` mu on user_id = mu.id " )
if req . EndTime != 0 {
session . Where ( "bundle_extension_records.created_at < ?" , time . Unix ( int64 ( req . EndTime ) , 0 ) )
}
if req . StartTime != 0 {
session . Where ( "bundle_extension_records.created_at > ?" , time . Unix ( int64 ( req . StartTime ) , 0 ) )
}
if req . Operator != "" {
if utils . IsPhoneNumber ( req . Operator ) {
session . Where ( "fu.tel_num = ?" , req . Operator )
} else {
session . Where ( "fu.nickname = ?" , req . Operator )
}
}
if req . User != "" {
if utils . IsPhoneNumber ( req . User ) {
session . Where ( "mu.tel_num = ?" , req . User )
} else {
session . Where ( "mu.nickname = ?" , req . User )
}
}
if req . AssociatedOrderNumber != "" {
session . Where ( "associated_order_number = ?" , req . AssociatedOrderNumber )
}
if req . Type != 0 {
session . Where ( "`type` = ?" , req . Type )
}
var total int64
res := [ ] model . BundleExtendRecordItemPo { }
session . Limit ( int ( req . PageSize ) ) . Offset ( int ( req . Page - 1 ) * int ( req . PageSize ) ) . Find ( & res ) . Count ( & total )
return res , total , session . Error
}