互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.2 KiB

package models
import (
"fmt"
"github.com/ouxuanserver/osmanthuswine/src/core"
"github.com/xormplus/xorm"
"reflect"
"strings"
)
const TableNamePrefix = "ox_"
func init() {
err := core.GetXormAuto().Sync2(
new(Activity),
new(ActivityModuleService),
new(AreaStore),
new(AuctionHistory),
new(AuctionResultRecord),
new(BaheTeam),
new(BaheTeamMember),
new(BarrageHistory),
new(BullyScreenHistory),
new(BullyScreenServer),
new(BullyScreenWalletHistory),
new(Customer),
new(CustomerGoods),
new(CustomerOperation),
new(CustomerOrder),
new(DanMuServer),
new(Invitation),
new(InvitationLetter),
new(LotteryDrawActivity),
new(LotteryDrawEnvelopeWallet),
new(LotteryDrawRecord),
new(LotteryDrawRule),
new(LotteryDrawRuleLadder),
new(LotteryDrawWalletHistory),
new(ModuleService),
new(ModuleServiceHistory),
new(ModuleStyle),
new(NewAuctionActivity),
new(NewVoteActivity),
new(NewVoteActivityHistory),
new(NewVoteActivityLadder),
new(OrderDrawActivity),
new(OrderDrawRecord),
new(OrderDrawRule),
new(OrderDrawRuleLadder),
new(OrderDrawStyle),
new(RewardHistory),
new(RewardServer),
new(RewardWallet),
new(RewardWalletHistory),
new(Sensitive),
new(ShakeRedEnvelopeActivity),
new(ShakeRedEnvelopeRecord),
new(ShakeRedEnvelopeRule),
new(ShakeRedEnvelopeRuleLadder),
new(SignHistory),
new(SignUp),
new(TugOfWar),
new(UpperWall),
new(User),
new(UserWalletHistory),
new(NewVoteActivity),
new(NewVoteActivityHistory),
new(OrderEntryPerson),
new(Rehearsal),
new(MsgWallServer),
new(ShakeRedEnvelopeUser),
new(OrderGift),
new(UserPrize),
new(Calorie),
new(CalorieUser),
new(UserOrder),
new(UserRedPack),
new(RealSignList),
//new(RealSignHistory),
new(LiveViewer),
new(LiveConfig),
new(LiveRedEnvelopeRule),
new(UserTransfer),
new(CustomerOrderSub),
)
fmt.Printf("error=======>%v\n\n", err)
}
func Condition(condition map[string]interface{}) *xorm.Session {
session := core.GetXormAuto().NewSession()
for k, v := range condition {
if strings.Contains(k, " not in") {
session = session.NotIn(strings.TrimRight(k, " not in"), v)
} else if strings.Contains(k, " in") {
session = session.In(strings.TrimRight(k, " in"), v)
} else if strings.HasPrefix(strings.ToLower(k), "order by") {
session = session.OrderBy(v.(string))
} else if strings.HasSuffix(k, "<") || strings.HasSuffix(k, ">") ||
strings.HasSuffix(k, "<=") || strings.HasSuffix(k, ">=") ||
strings.HasSuffix(k, "<>") || strings.HasSuffix(k, "=") ||
strings.HasSuffix(k, "!=") {
session = session.Where(fmt.Sprintf("%s?", k), v)
} else {
session = session.Where(fmt.Sprintf("%s=?", k), v)
}
}
return session
}
func Save(condition map[string]interface{}, obj interface{}, filed ...string) error {
session := Condition(condition)
defer session.Close()
if condition != nil && Exist(Condition(condition), reflect.New(reflect.TypeOf(obj).Elem()).Interface()) {
//存在则更新
_, err := session.Cols(filed...).Update(obj)
return err
} else {
//不存在则插入
_, err := core.GetXormAuto().InsertOne(obj)
return err
}
}
func Exist(session *xorm.Session, obj interface{}) (result bool) {
defer session.Close()
result, _ = session.Exist(obj)
return result
}
func GetTableName(bean interface{}) string {
if table, ok := bean.(string); ok {
return table
}
return core.GetXormAuto().TableName(bean)
}
func AliasTableName(bean interface{}, alias string) string {
tn := GetTableName(bean)
return fmt.Sprintf("%s as %s", tn, alias)
}
func Get(bean interface{}, id int64) (bool, error) {
return core.GetXormAuto().Where("is_delete=0 and id=?", id).Get(bean)
}
func Add(bean interface{}) (int64, error) {
return core.GetXormAuto().InsertOne(bean)
}
func Update(id, bean interface{}, field ...string) (int64, error) {
if len(field) > 0 {
return core.GetXormAuto().ID(id).Cols(field...).Update(bean)
} else {
return core.GetXormAuto().ID(id).Update(bean)
}
}
func Commit(f func(s *xorm.Session) error) error {
session := core.GetXormAuto().NewSession()
defer session.Close()
err := session.Begin()
if err != nil {
return err
}
err = f(session)
if err != nil {
session.Rollback()
return err
}
return session.Commit()
}