package mysql import ( "context" "fmt" "strings" "github.com/Leufolium/test/dbstruct" "github.com/jmoiron/sqlx" ) type Mysql struct { dbVas *sqlx.DB } func (m *Mysql) getDBVas() *sqlx.DB { return m.dbVas } // mysql const ( DatabaseVas = "vas" TableOrder = "vas_order" // 订单表 TableWallet = "vas_wallet" // 钱包 TableCoinOrder = "vas_coin_order" // 金币订单 TableConsumeHistoryCost = "vas_ch_cost" // 消费明细 TableConsumeHistoryCharge = "vas_ch_charge" // 充值明细 TableConsumeHistoryIncome = "vas_ch_income" // 收入明细 TableConsumeHistoryWithdraw = "vas_ch_withdraw" // 提现明细 TableVasUserUnlock = "vas_user_unlock" // 用增解锁 TableWithdrawOrder = "vas_withdraw_order" // 提现订单表 TableWithdrawDiamondsHis = "vas_withdraw_diamonds_his" // 提现金币历史 TableVasUserMembershipUnlock = "vas_user_membership_unlock" // 会员资格解锁 TableVasZoneUnlock = "vas_zone_unlock" // 空间解锁 TableVasZoneConsumeHistory = "vas_zone_ch" // 空间消费记录 TableVasZoneMomentUnlock = "vas_zone_moment_unlock" // 空间动态解锁 TableVasZoneMember = "vas_zone_member" // 空间成员 TableVasZoneRefundHis = "vas_zone_refund_his" // 空间退款记录 DatabaseXxlJob = "xxl_job" TableXxlJobLog = "xxl_job_log" // xxl_job日志表 ) func NewMysql() (mysql *Mysql, err error) { mysql = new(Mysql) mysql.dbVas, err = NewMysqlDB() if err != nil { fmt.Printf("NewMysqlDB fail, err: %v\n", err) return } mysql.dbVas.SetMaxOpenConns(20) mysql.dbVas.SetMaxIdleConns(10) err = mysql.dbVas.Ping() if err != nil { fmt.Printf("MysqlDB ping fail, err: %v\n", err) return } return } func Convert2SqlArr(a ...any) string { return strings.Replace(strings.Trim(fmt.Sprint(a), "[]"), " ", ",", -1) } func (m *Mysql) GetZoneMemberCountGroupByZid(ctx context.Context, tx *sqlx.Tx, zids []int64) (list []*dbstruct.ZoneMemberCount, err error) { sql := fmt.Sprintf("select count(1) as num, zid from %s where zid in (%s) group by zid", TableVasZoneMember, Convert2SqlArr(zids)) list = make([]*dbstruct.ZoneMemberCount, 0) if tx != nil { err = tx.SelectContext(ctx, &list, sql) } else { db := m.getDBVas() err = db.SelectContext(ctx, &list, sql) } if err != nil { return } return }