2024-04-15 15:17:30 +08:00
package dao
import (
"database/sql"
"fmt"
"service/bizcommon/util"
"service/dbstruct"
"service/library/logger"
"time"
2024-04-16 07:41:52 +08:00
"github.com/gin-gonic/gin"
"github.com/jmoiron/sqlx"
2024-04-15 15:17:30 +08:00
)
// 获取空间解锁信息
func ( m * Mysql ) GetZoneUnlock ( ctx * gin . Context , tx * sqlx . Tx , mid , zid int64 ) ( zu * dbstruct . ZoneUnlock , err error ) {
var tmpZu dbstruct . ZoneUnlock
if tx != nil {
err = tx . GetContext ( ctx , & tmpZu , fmt . Sprintf ( "select * from %s where mid=? and zid=?" , TableVasZoneUnlock ) , mid , zid )
} else {
db := m . getDBVas ( )
err = db . GetContext ( ctx , & tmpZu , fmt . Sprintf ( "select * from %s where mid=? and zid=?" , TableVasZoneUnlock ) , mid , zid )
}
if err != nil {
return
}
zu = & tmpZu
return
}
2024-04-16 07:41:52 +08:00
// 获取空间解锁信息 by mid
func ( m * Mysql ) GetZoneUnlockListByMid ( ctx * gin . Context , tx * sqlx . Tx , mid int64 ) ( list [ ] * dbstruct . ZoneUnlock , err error ) {
list = make ( [ ] * dbstruct . ZoneUnlock , 0 )
if tx != nil {
err = tx . SelectContext ( ctx , & list , fmt . Sprintf ( "select * from %s where mid=?" , TableVasZoneUnlock ) , mid )
} else {
db := m . getDBVas ( )
err = db . SelectContext ( ctx , & list , fmt . Sprintf ( "select * from %s where mid=?" , TableVasZoneUnlock ) , mid )
}
if err == sql . ErrNoRows {
err = nil
return
}
if err != nil {
return
}
return
}
2024-04-15 15:17:30 +08:00
// 获取空间解锁信息 by zid
func ( m * Mysql ) GetZoneUnlockListByZid ( ctx * gin . Context , tx * sqlx . Tx , zid int64 ) ( list [ ] * dbstruct . ZoneUnlock , err error ) {
list = make ( [ ] * dbstruct . ZoneUnlock , 0 )
if tx != nil {
err = tx . SelectContext ( ctx , & list , fmt . Sprintf ( "select * from %s where zid=?" , TableVasZoneUnlock ) , zid )
} else {
db := m . getDBVas ( )
err = db . SelectContext ( ctx , & list , fmt . Sprintf ( "select * from %s where zid=?" , TableVasZoneUnlock ) , zid )
}
if err == sql . ErrNoRows {
err = nil
return
}
if err != nil {
return
}
return
}
// 获取空间解锁信息 by mid, zids
func ( m * Mysql ) GetZoneUnlockListByMidZids ( ctx * gin . Context , tx * sqlx . Tx , mid int64 , zids [ ] int64 ) ( list [ ] * dbstruct . ZoneUnlock , err error ) {
list = make ( [ ] * dbstruct . ZoneUnlock , 0 )
if tx != nil {
err = tx . SelectContext ( ctx , & list , fmt . Sprintf ( "select * from %s where mid=? and zid in (%s)" , TableVasZoneUnlock , util . Convert2SqlArr ( zids ) ) , mid )
} else {
db := m . getDBVas ( )
err = db . SelectContext ( ctx , & list , fmt . Sprintf ( "select * from %s where mid=? and zid in (%s)" , TableVasZoneUnlock , util . Convert2SqlArr ( zids ) ) , mid )
}
if err == sql . ErrNoRows {
err = nil
return
}
if err != nil {
return
}
return
}
// 初始化空间解锁信息
func ( m * Mysql ) CreateZoneUnlock ( ctx * gin . Context , tx * sqlx . Tx , mid , zid int64 ) error {
var err error
sqlStr := "insert into " + TableVasZoneUnlock +
2024-04-16 20:20:53 +08:00
" (mid, zid, consume, " +
" admission_ct, admission_until, admission_order_id, admission_unlock_type, " +
" ironfanship_ct, ironfanship_until, ironfanship_order_id, ironfanship_unlock_type, " +
" superfanship_ct, superfanship_until, superfanship_order_id, superfanship_unlock_type) " +
" values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
args := [ ] any { mid , zid , 0 , 0 , 0 , "" , 0 , 0 , 0 , "" , 0 , 0 , 0 , "" , 0 }
2024-04-15 15:17:30 +08:00
if tx != nil {
2024-04-16 20:20:53 +08:00
_ , err = tx . ExecContext ( ctx , sqlStr , args ... )
2024-04-15 15:17:30 +08:00
} else {
db := m . getDBVas ( )
2024-04-16 20:20:53 +08:00
_ , err = db . ExecContext ( ctx , sqlStr , args ... )
2024-04-15 15:17:30 +08:00
}
if err != nil {
logger . Error ( "CreateZoneUnlock fail, mid: %v, zid: %v, err: %v" , mid , zid , err )
return err
}
return nil
}
// 解锁空间动态
func ( m * Mysql ) UnlockZoneMoment ( ctx * gin . Context , tx * sqlx . Tx , mid , zid , momentId int64 , orderId string ) error {
var err error
sqlStr := "update " + TableVasZoneMomentUnlock + " set moment_id=?, `status`=?, ct=?, order_id=? where mid=? and zid=?"
if tx != nil {
_ , err = tx . ExecContext ( ctx , sqlStr , momentId , dbstruct . ZoneMomentUnlockStatusUnlock , time . Now ( ) . Unix ( ) , orderId , mid , zid )
} else {
db := m . getDBVas ( )
_ , err = db . ExecContext ( ctx , sqlStr , momentId , dbstruct . ZoneMomentUnlockStatusUnlock , time . Now ( ) . Unix ( ) , orderId , mid , zid )
}
if err != nil {
return err
}
return err
}
// 获取动态解锁信息 by mid, moment_ids
func ( m * Mysql ) GetZoneMomentUnlockListByMidMomentIds ( ctx * gin . Context , tx * sqlx . Tx , mid int64 , momentIds [ ] int64 ) ( list [ ] * dbstruct . ZoneMomentUnlock , err error ) {
list = make ( [ ] * dbstruct . ZoneMomentUnlock , 0 )
sqlStr := fmt . Sprintf ( "select * from %s where mid=? and moment_id in (%s)" , TableVasZoneMomentUnlock , util . Convert2SqlArr ( momentIds ) )
if tx != nil {
err = tx . SelectContext ( ctx , & list , sqlStr , mid )
} else {
db := m . getDBVas ( )
err = db . SelectContext ( ctx , & list , sqlStr , mid )
}
if err == sql . ErrNoRows {
err = nil
return
}
if err != nil {
return
}
return
}
// 解锁空间普通会员
2024-04-16 20:20:53 +08:00
func ( m * Mysql ) UnlockZoneAdmission ( ctx * gin . Context , tx * sqlx . Tx , mid , zid , until int64 , orderId string , unlockType int32 ) error {
2024-04-15 15:17:30 +08:00
var err error
2024-04-16 20:20:53 +08:00
sqlStr := "update " + TableVasZoneUnlock + " set admission_ct=?, admission_until=?, admission_order_id=?, admission_unlock_type=? where mid=? and zid=?"
args := [ ] any { time . Now ( ) . Unix ( ) , until , orderId , unlockType , mid , zid }
2024-04-15 15:17:30 +08:00
if tx != nil {
2024-04-16 20:20:53 +08:00
_ , err = tx . ExecContext ( ctx , sqlStr , args ... )
2024-04-15 15:17:30 +08:00
} else {
db := m . getDBVas ( )
2024-04-16 20:20:53 +08:00
_ , err = db . ExecContext ( ctx , sqlStr , args ... )
2024-04-15 15:17:30 +08:00
}
if err != nil {
return err
}
return err
}
2024-04-16 09:50:54 +08:00
// 删除空间普通会员
func ( m * Mysql ) DeleteZoneAdmission ( ctx * gin . Context , tx * sqlx . Tx , mid , zid int64 ) error {
var err error
sqlStr := "update " + TableVasZoneUnlock + " set admission_ct=?, admission_until=? where mid=? and zid=?"
args := [ ] any { 0 , 0 , mid , zid }
if tx != nil {
_ , err = tx . ExecContext ( ctx , sqlStr , args ... )
} else {
db := m . getDBVas ( )
_ , err = db . ExecContext ( ctx , sqlStr , args ... )
}
if err != nil {
return err
}
return err
}
2024-04-15 15:17:30 +08:00
// 解锁空间铁粉
2024-04-16 20:20:53 +08:00
func ( m * Mysql ) UnlockZoneIronfanship ( ctx * gin . Context , tx * sqlx . Tx , mid , zid , until int64 , orderId string , unlockType int32 ) error {
2024-04-15 15:17:30 +08:00
var err error
2024-04-16 20:20:53 +08:00
sqlStr := "update " + TableVasZoneUnlock + " set ironfanship_ct=?, ironfanship_until=?, ironfanship_order_id=?, ironfanship_unlock_type=? where mid=? and zid=?"
args := [ ] any { time . Now ( ) . Unix ( ) , until , orderId , unlockType , mid , zid }
2024-04-15 15:17:30 +08:00
if tx != nil {
2024-04-16 20:20:53 +08:00
_ , err = tx . ExecContext ( ctx , sqlStr , args ... )
2024-04-15 15:17:30 +08:00
} else {
db := m . getDBVas ( )
2024-04-16 20:20:53 +08:00
_ , err = db . ExecContext ( ctx , sqlStr , args ... )
2024-04-15 15:17:30 +08:00
}
if err != nil {
return err
}
return err
}
// 解锁空间铁粉
2024-04-16 20:20:53 +08:00
func ( m * Mysql ) UnlockZoneSuperfanship ( ctx * gin . Context , tx * sqlx . Tx , mid , zid , until int64 , orderId string , unlockType int32 ) error {
2024-04-15 15:17:30 +08:00
var err error
2024-04-16 20:20:53 +08:00
sqlStr := "update " + TableVasZoneUnlock + " set superfanship_ct=?, superfanship_until=?, superfanship_order_id=?, superfanship_unlock_type=? where mid=? and zid=?"
args := [ ] any { time . Now ( ) . Unix ( ) , until , orderId , unlockType , mid , zid }
2024-04-15 15:17:30 +08:00
if tx != nil {
2024-04-16 20:20:53 +08:00
_ , err = tx . ExecContext ( ctx , sqlStr , args ... )
2024-04-15 15:17:30 +08:00
} else {
db := m . getDBVas ( )
2024-04-16 20:20:53 +08:00
_ , err = db . ExecContext ( ctx , sqlStr , args ... )
2024-04-15 15:17:30 +08:00
}
if err != nil {
return err
}
return err
}
// 增加空间消费详情
func ( m * Mysql ) CreateZoneConsumeHis ( ctx * gin . Context , tx * sqlx . Tx , zch * dbstruct . ZoneConsumeHis ) error {
var err error
sqlStr := "insert into " + TableVasZoneConsumeHistory +
2024-04-16 09:50:54 +08:00
" (mid, zid, consume, ct, order_id, product_id) " +
" values (?,?,?,?,?,?)"
2024-04-15 15:17:30 +08:00
if tx != nil {
2024-04-16 09:50:54 +08:00
_ , err = tx . ExecContext ( ctx , sqlStr , zch . GetMid ( ) , zch . GetZid ( ) , zch . GetConsume ( ) , time . Now ( ) . Unix ( ) , zch . GetOrderId ( ) , zch . GetProductId ( ) )
2024-04-15 15:17:30 +08:00
} else {
db := m . getDBVas ( )
2024-04-16 09:50:54 +08:00
_ , err = db . ExecContext ( ctx , sqlStr , zch . GetMid ( ) , zch . GetZid ( ) , zch . GetConsume ( ) , time . Now ( ) . Unix ( ) , zch . GetOrderId ( ) , zch . GetProductId ( ) )
2024-04-15 15:17:30 +08:00
}
if err != nil {
logger . Error ( "CreateZoneConsumeHis fail, zch: %v, err: %v" , util . ToJson ( zch ) , err )
return err
}
return nil
}
// 增加空间消费
func ( m * Mysql ) IncZoneConsume ( ctx * gin . Context , tx * sqlx . Tx , mid , zid , inc int64 ) error {
var err error
sqlStr := "update " + TableVasZoneUnlock + " set consume=consume+? where mid=? and zid=?"
if tx != nil {
_ , err = tx . ExecContext ( ctx , sqlStr , util . AbsInt64 ( inc ) , mid , zid )
} else {
db := m . getDBVas ( )
_ , err = db . ExecContext ( ctx , sqlStr , util . AbsInt64 ( inc ) , mid , zid )
}
if err != nil {
return err
}
return err
}
// 减少空间消费
func ( m * Mysql ) DecZoneConsume ( ctx * gin . Context , tx * sqlx . Tx , mid , zid , inc int64 ) error {
var err error
sqlStr := "update " + TableVasZoneUnlock + " set consume=consume-? where mid=? and zid=?"
if tx != nil {
_ , err = tx . ExecContext ( ctx , sqlStr , util . AbsInt64 ( inc ) , mid , zid )
} else {
db := m . getDBVas ( )
_ , err = db . ExecContext ( ctx , sqlStr , util . AbsInt64 ( inc ) , mid , zid )
}
if err != nil {
return err
}
return err
}
// 添加到空间成员
func ( m * Mysql ) AddZoneMember ( ctx * gin . Context , tx * sqlx . Tx , mid , zid int64 , memType int32 ) error {
var err error
2024-04-18 20:27:18 +08:00
// 先获取,没有再添加
var tmpZm * dbstruct . ZoneMember
if tx != nil {
err = tx . GetContext ( ctx , tmpZm , fmt . Sprintf ( "select * from %s where mid=? and zid=? and member_type=?" , TableVasZoneMember ) , mid , zid , memType )
} else {
db := m . getDBVas ( )
err = db . GetContext ( ctx , tmpZm , fmt . Sprintf ( "select * from %s where mid=? and zid=? and member_type=?" , TableVasZoneMember ) , mid , zid , memType )
}
if err == sql . ErrNoRows {
err = nil
}
if err != nil {
return err
}
if tmpZm != nil {
return nil
}
// 再添加
2024-04-15 15:17:30 +08:00
sqlStr := "insert into " + TableVasZoneMember + " (zid, mid, member_type, ct) " + " values (?,?,?,?)"
if tx != nil {
_ , err = tx . ExecContext ( ctx , sqlStr , zid , mid , memType , time . Now ( ) . Unix ( ) )
} else {
db := m . getDBVas ( )
_ , err = db . ExecContext ( ctx , sqlStr , zid , mid , memType , time . Now ( ) . Unix ( ) )
}
if err != nil {
return err
}
return err
}
2024-04-16 09:50:54 +08:00
// 删除空间成员
func ( m * Mysql ) DeleteZoneMember ( ctx * gin . Context , tx * sqlx . Tx , mid , zid int64 , memType int32 ) error {
var err error
sqlStr := fmt . Sprintf ( "delete from %s where mid=? and zid=? and member_type=?" , TableVasZoneMember )
args := [ ] any { mid , zid , memType }
if tx != nil {
_ , err = tx . ExecContext ( ctx , sqlStr , args ... )
} else {
db := m . getDBVas ( )
_ , err = db . ExecContext ( ctx , sqlStr , args ... )
}
if err != nil {
return err
}
return err
}
2024-04-15 15:17:30 +08:00
// 获取空间成员列表
func ( m * Mysql ) GetZoneMemberList ( ctx * gin . Context , tx * sqlx . Tx , zid int64 , memType int32 ) ( list [ ] * dbstruct . ZoneMember , err error ) {
list = make ( [ ] * dbstruct . ZoneMember , 0 )
2024-04-18 20:27:18 +08:00
sqlStr := fmt . Sprintf ( fmt . Sprintf ( "select * from %s where zid=? and member_type=? order by ct desc" , TableVasZoneMember ) )
2024-04-15 15:17:30 +08:00
if tx != nil {
err = tx . SelectContext ( ctx , & list , sqlStr , zid , memType )
} else {
db := m . getDBVas ( )
err = db . SelectContext ( ctx , & list , sqlStr , zid , memType )
}
if err == sql . ErrNoRows {
err = nil
return
}
if err != nil {
return
}
return
}
2024-04-16 10:49:02 +08:00
// 空间退款记录
func ( m * Mysql ) CreateZoneRefundHis ( ctx * gin . Context , tx * sqlx . Tx , o * dbstruct . ZoneRefundHis ) error {
var err error
sqlStr := "insert into " + TableVasZoneRefundHis +
" (mid, zid, ct, contact_name, contact_phone, note, order_id, product_id) " +
" values (?,?,?,?,?,?,?,?) "
args := [ ] any { o . GetMid ( ) , o . GetZid ( ) , time . Now ( ) . Unix ( ) , o . GetContactName ( ) , o . GetContactPhone ( ) , o . GetNote ( ) , o . GetOrderId ( ) , o . GetProductId ( ) }
if tx != nil {
_ , err = tx . ExecContext ( ctx , sqlStr , args ... )
} else {
db := m . getDBVas ( )
_ , err = db . ExecContext ( ctx , sqlStr , args ... )
}
if err != nil {
logger . Error ( "CreateZoneRefundHis fail, o: %v, err: %v" , util . ToJson ( o ) , err )
return err
}
return nil
}