service/app/mix/dao/mysql_zone.go

559 lines
18 KiB
Go

package dao
import (
"database/sql"
"fmt"
"service/bizcommon/util"
"service/dbstruct"
"service/library/logger"
"strings"
"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
// 先获取,没有再添加
var tmpZm dbstruct.ZoneMomentUnlock
if tx != nil {
err = tx.GetContext(ctx, &tmpZm, fmt.Sprintf("select * from %s where mid=? and zid=? and moment_id=?", TableVasZoneMomentUnlock), mid, zid, momentId)
} else {
db := m.getDBVas()
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
}
// 再添加
sqlStr := "insert into " + TableVasZoneMomentUnlock + " (mid,zid,moment_id,status,ct,order_id) " + " values (?,?,?,?,?,?)"
args := []any{mid, zid, momentId, dbstruct.ZoneMomentUnlockStatusUnlock, time.Now().Unix(), orderId}
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) 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
}
// 获取动态解锁信息 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) 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
}
// 解锁空间超粉
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) 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
}
// 增加空间消费详情
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) GetZoneConsumeHisByOrderId(ctx *gin.Context, tx *sqlx.Tx, orderId string) (list []*dbstruct.ZoneConsumeHis, err error) {
list = make([]*dbstruct.ZoneConsumeHis, 0)
sqlStr := fmt.Sprintf("select * from %s where order_id=?", TableVasZoneConsumeHistory)
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
}
// 增加空间消费
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
}
// 获取空间退款记录
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
}
// 获取空间动态购买记录
func (m *Mysql) GetZoneMomentUnlockList(ctx *gin.Context, tx *sqlx.Tx, momentId int64, offset, limit int) (list []*dbstruct.ZoneMomentUnlock, err error) {
list = make([]*dbstruct.ZoneMomentUnlock, 0)
sqlStr := fmt.Sprintf("select * from %s where moment_id=? limit ? offset ?", TableVasZoneMomentUnlock)
args := []any{momentId, 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
}
// 获取时段内空间进入人数、总入账金额、各业务分入账金额
func (m *Mysql) GetLastHourZoneProfit(ctx *gin.Context, tx *sqlx.Tx, st, et int64) (list []*dbstruct.ZoneProfit, err error) {
var sql strings.Builder
sql.WriteString(fmt.Sprintf("select uid as mid, oid1 as zid, product_id, count(1) as num, sum(pay_amount) as amount from %s", TableOrder))
sql.WriteString(fmt.Sprintf(" where ct >=%d and ct<=%d and product_id in(?,?,?,?) and order_status = 1 group by product_id, uid, oid1", st, et))
list = make([]*dbstruct.ZoneProfit, 0)
if tx != nil {
err = tx.SelectContext(ctx, &list, sql.String(), dbstruct.ProductIdH5ZoneMoment, dbstruct.ProductIdH5ZoneAdmission, dbstruct.ProductIdH5ZoneIronfanship, dbstruct.ProductIdH5ZoneSuperfanship)
} else {
db := m.getDBVas()
err = db.SelectContext(ctx, &list, sql.String(), dbstruct.ProductIdH5ZoneMoment, dbstruct.ProductIdH5ZoneAdmission, dbstruct.ProductIdH5ZoneIronfanship, dbstruct.ProductIdH5ZoneSuperfanship)
}
if err != nil {
return
}
return
}
func (m *Mysql) GetLastHourZoneRefund(ctx *gin.Context, tx *sqlx.Tx, st, et int64) (list []*dbstruct.ZoneProfit, err error) {
var sql strings.Builder
sql.WriteString(fmt.Sprintf("select t1.uid as mid, t1.oid1 as zid, count(1) as num, sum(t1.pay_amount) as amount from %s t1 join %s t2 on t1.id = t2.order_id", TableOrder, TableVasZoneRefundHis))
sql.WriteString(fmt.Sprintf(" where t2.ct >=%d and t2.ct<=%d and t1.ct <%d group by t1.uid, t1.oid1", st, et, st))
list = make([]*dbstruct.ZoneProfit, 0)
if tx != nil {
err = tx.SelectContext(ctx, &list, sql.String())
} else {
db := m.getDBVas()
err = db.SelectContext(ctx, &list, sql.String())
}
if err != nil {
return
}
return
}