374 lines
12 KiB
Go
374 lines
12 KiB
Go
package dao
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"service/bizcommon/util"
|
|
"service/dbstruct"
|
|
"service/library/logger"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jmoiron/sqlx"
|
|
)
|
|
|
|
// 获取空间解锁信息
|
|
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
|
|
}
|
|
|
|
// 获取空间解锁信息 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
|
|
}
|
|
|
|
// 获取空间解锁信息 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 +
|
|
" (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}
|
|
if tx != nil {
|
|
_, err = tx.ExecContext(ctx, sqlStr, args...)
|
|
} else {
|
|
db := m.getDBVas()
|
|
_, err = db.ExecContext(ctx, sqlStr, args...)
|
|
}
|
|
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
|
|
}
|
|
|
|
// 解锁空间普通会员
|
|
func (m *Mysql) UnlockZoneAdmission(ctx *gin.Context, tx *sqlx.Tx, mid, zid, until int64, orderId string, unlockType int32) error {
|
|
var err error
|
|
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}
|
|
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) RefundZoneAdmission(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.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}
|
|
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) UnlockZoneIronfanship(ctx *gin.Context, tx *sqlx.Tx, mid, zid, until int64, orderId string, unlockType int32) error {
|
|
var err error
|
|
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}
|
|
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) UnlockZoneSuperfanship(ctx *gin.Context, tx *sqlx.Tx, mid, zid, until int64, orderId string, unlockType int32) error {
|
|
var err error
|
|
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}
|
|
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) CreateZoneConsumeHis(ctx *gin.Context, tx *sqlx.Tx, zch *dbstruct.ZoneConsumeHis) error {
|
|
var err error
|
|
sqlStr := "insert into " + TableVasZoneConsumeHistory +
|
|
" (mid, zid, consume, ct, order_id, product_id) " +
|
|
" values (?,?,?,?,?,?)"
|
|
if tx != nil {
|
|
_, err = tx.ExecContext(ctx, sqlStr, zch.GetMid(), zch.GetZid(), zch.GetConsume(), time.Now().Unix(), zch.GetOrderId(), zch.GetProductId())
|
|
} else {
|
|
db := m.getDBVas()
|
|
_, err = db.ExecContext(ctx, sqlStr, zch.GetMid(), zch.GetZid(), zch.GetConsume(), time.Now().Unix(), zch.GetOrderId(), zch.GetProductId())
|
|
}
|
|
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
|
|
// 先获取,没有再添加
|
|
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.GetMid() == mid && tmpZm.GetZid() == zid && tmpZm.GetMemberType() == memType {
|
|
return nil
|
|
}
|
|
|
|
// 再添加
|
|
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
|
|
}
|
|
|
|
// 删除空间成员
|
|
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
|
|
}
|
|
|
|
// 获取空间成员列表
|
|
func (m *Mysql) GetZoneMemberList(ctx *gin.Context, tx *sqlx.Tx, zid int64, memType int32) (list []*dbstruct.ZoneMember, err error) {
|
|
list = make([]*dbstruct.ZoneMember, 0)
|
|
sqlStr := fmt.Sprintf(fmt.Sprintf("select * from %s where zid=? and member_type=? order by ct desc", TableVasZoneMember))
|
|
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
|
|
}
|
|
|
|
// 空间退款记录
|
|
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
|
|
}
|