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
}
2024-04-29 15:47:45 +08:00
// 解锁空间动态
2024-04-15 15:17:30 +08:00
func ( m * Mysql ) UnlockZoneMoment ( ctx * gin . Context , tx * sqlx . Tx , mid , zid , momentId int64 , orderId string ) error {
var err error
2024-04-19 23:33:00 +08:00
// 先获取,没有再添加
var tmpZm dbstruct . ZoneMomentUnlock
2024-04-15 15:17:30 +08:00
if tx != nil {
2024-04-19 23:33:00 +08:00
err = tx . GetContext ( ctx , & tmpZm , fmt . Sprintf ( "select * from %s where mid=? and zid=? and moment_id=?" , TableVasZoneMomentUnlock ) , mid , zid , momentId )
2024-04-15 15:17:30 +08:00
} else {
db := m . getDBVas ( )
2024-04-19 23:33:00 +08:00
err = db . GetContext ( ctx , & tmpZm , fmt . Sprintf ( "select * from %s where mid=? and zid=? and moment_id=?" , TableVasZoneMomentUnlock ) , mid , zid , momentId )
}
if err == sql . ErrNoRows {
err = nil
}
if err != nil {
return err
}
if tmpZm . IsUnlock ( ) {
return nil
}
// 再添加
2024-04-19 23:41:18 +08:00
sqlStr := "insert into " + TableVasZoneMomentUnlock + " (mid,zid,moment_id,status,ct,order_id) " + " values (?,?,?,?,?,?)"
2024-04-19 23:54:56 +08:00
args := [ ] any { mid , zid , momentId , dbstruct . ZoneMomentUnlockStatusUnlock , time . Now ( ) . Unix ( ) , orderId }
2024-04-19 23:33:00 +08:00
if tx != nil {
_ , err = tx . ExecContext ( ctx , sqlStr , args ... )
} else {
db := m . getDBVas ( )
_ , err = db . ExecContext ( ctx , sqlStr , args ... )
2024-04-15 15:17:30 +08:00
}
if err != nil {
return err
}
return err
}
2024-04-29 15:47:45 +08:00
// 退款空间动态
func ( m * Mysql ) RefundZoneMoment ( ctx * gin . Context , tx * sqlx . Tx , mid , zid , momentId int64 ) error {
var err error
sqlStr := fmt . Sprintf ( "delete from %s where mid=? and zid=? and moment_id=?" , TableVasZoneMomentUnlock )
args := [ ] any { mid , zid , momentId }
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
// 获取动态解锁信息 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-18 21:17:41 +08:00
// 退款空间普通会员
func ( m * Mysql ) RefundZoneAdmission ( ctx * gin . Context , tx * sqlx . Tx , mid , zid int64 ) error {
2024-04-16 09:50:54 +08:00
var err error
2024-04-18 21:17:41 +08:00
sqlStr := "update " + TableVasZoneUnlock + " set admission_ct=?, admission_until=?, admission_unlock_type=? where mid=? and zid=?"
args := [ ] any { 0 , 0 , dbstruct . ZoneUnlockTypeRefund , 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
}
// 退出空间
func ( m * Mysql ) ExitZoneAdmission ( ctx * gin . Context , tx * sqlx . Tx , mid , zid int64 ) error {
var err error
sqlStr := "update " + TableVasZoneUnlock + " set admission_ct=?, admission_until=?, admission_unlock_type=? where mid=? and zid=?"
args := [ ] any { 0 , 0 , dbstruct . ZoneUnlockTypeExit , mid , zid }
2024-04-16 09:50:54 +08:00
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-29 15:56:42 +08:00
// 铁粉退款
func ( m * Mysql ) RefundZoneIronfanship ( ctx * gin . Context , tx * sqlx . Tx , mid , zid int64 ) error {
var err error
sqlStr := "update " + TableVasZoneUnlock + " set ironfanship_ct=?, ironfanship_until=?, ironfanship_unlock_type=? where mid=? and zid=?"
args := [ ] any { 0 , 0 , dbstruct . ZoneUnlockTypeRefund , 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-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
}
2024-04-29 15:47:45 +08:00
// 超粉退款
func ( m * Mysql ) RefundZoneSuperfanship ( ctx * gin . Context , tx * sqlx . Tx , mid , zid int64 ) error {
var err error
sqlStr := "update " + TableVasZoneUnlock + " set superfanship_ct=?, superfanship_until=?, superfanship_unlock_type=? where mid=? and zid=?"
args := [ ] any { 0 , 0 , dbstruct . ZoneUnlockTypeRefund , 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
// 增加空间消费详情
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
}
2024-04-29 15:47:45 +08:00
// 获取空间消费详情
func ( m * Mysql ) GetZoneConsumeHisByOrderId ( ctx * gin . Context , tx * sqlx . Tx , orderId string ) ( list [ ] * dbstruct . ZoneConsumeHis , err error ) {
list = make ( [ ] * dbstruct . ZoneConsumeHis , 0 )
2024-04-29 16:08:51 +08:00
sqlStr := fmt . Sprintf ( "select * from %s where order_id=?" , TableVasZoneConsumeHistory )
2024-04-29 15:47:45 +08:00
if tx != nil {
err = tx . SelectContext ( ctx , & list , sqlStr , orderId )
} else {
db := m . getDBVas ( )
err = db . SelectContext ( ctx , & list , sqlStr , orderId )
}
if err == sql . ErrNoRows {
err = nil
return
}
if err != nil {
return
}
return
}
2024-04-15 15:17:30 +08:00
// 增加空间消费
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
// 先获取,没有再添加
2024-04-18 20:31:43 +08:00
var tmpZm dbstruct . ZoneMember
2024-04-18 20:27:18 +08:00
if tx != nil {
2024-04-18 20:31:43 +08:00
err = tx . GetContext ( ctx , & tmpZm , fmt . Sprintf ( "select * from %s where mid=? and zid=? and member_type=?" , TableVasZoneMember ) , mid , zid , memType )
2024-04-18 20:27:18 +08:00
} else {
db := m . getDBVas ( )
2024-04-18 20:31:43 +08:00
err = db . GetContext ( ctx , & tmpZm , fmt . Sprintf ( "select * from %s where mid=? and zid=? and member_type=?" , TableVasZoneMember ) , mid , zid , memType )
2024-04-18 20:27:18 +08:00
}
if err == sql . ErrNoRows {
err = nil
}
if err != nil {
return err
}
2024-04-18 20:31:43 +08:00
if tmpZm . GetMid ( ) == mid && tmpZm . GetZid ( ) == zid && tmpZm . GetMemberType ( ) == memType {
2024-04-18 20:27:18 +08:00
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
}
2024-04-29 17:39:49 +08:00
// 获取空间退款记录
func ( m * Mysql ) GetZoneRefundHisList ( ctx * gin . Context , tx * sqlx . Tx , mid , zid int64 , orderId string , st , et int64 , offset , limit int ) ( list [ ] * dbstruct . ZoneRefundHis , err error ) {
list = make ( [ ] * dbstruct . ZoneRefundHis , 0 )
var (
sqlStr string
args = make ( [ ] any , 0 )
)
switch {
case mid > 0 :
sqlStr = fmt . Sprintf ( fmt . Sprintf ( "select * from %s where mid=? order by ct desc" , TableVasZoneRefundHis ) )
args = [ ] any { mid }
case zid > 0 :
sqlStr = fmt . Sprintf ( fmt . Sprintf ( "select * from %s where zid=? order by ct desc" , TableVasZoneRefundHis ) )
args = [ ] any { zid }
case len ( orderId ) > 0 :
sqlStr = fmt . Sprintf ( fmt . Sprintf ( "select * from %s where order_id=? order by ct desc" , TableVasZoneRefundHis ) )
args = [ ] any { orderId }
default :
sqlStr = fmt . Sprintf ( "select * from %s where ct>=%d and ct<%d limit %d offset %d" , TableVasZoneRefundHis , st , et , limit , offset )
}
if tx != nil {
err = tx . SelectContext ( ctx , & list , sqlStr , args ... )
} else {
db := m . getDBVas ( )
err = db . SelectContext ( ctx , & list , sqlStr , args ... )
}
if err == sql . ErrNoRows {
err = nil
return
}
if err != nil {
return
}
return
}