|
|
package models
import ( "errors" "fmt" "hudongzhuanjia/utils/define" "net/url" "reflect" "strings" "time"
"github.com/ouxuanserver/osmanthuswine/src/core" "github.com/xormplus/xorm" )
const TableNamePrefix = "ox_"
type Model struct { Id int `json:"id" xorm:"not null pk autoincr INT(11)"` IsDelete bool `json:"is_delete" xorm:"default(0)" description:"是否删除"` CreatedAt time.Time `json:"created_at" xorm:"created" description:"创建时间"` UpdatedAt time.Time `json:"updated_at" xorm:"updated" description:"更新时间"` }
func ParseToken(token string) (_type string, id int, err error) { token, _ = url.PathUnescape(token) tokens := strings.SplitN(token, ":", 2) if len(tokens) < 2 { err = errors.New("token失效") return } _type = tokens[0] if tokens[0] == define.TYPE_USER || tokens[0] == define.TYPE_LIVEUSER { // h5用户
user := &User{} err = user.GetByToken(tokens[1]) if err != nil { return } id = user.Id } else if tokens[0] == define.TYPE_CUSTOMER { // 客户
customer := &Customer{} err = customer.GetByToken(tokens[1]) if err != nil { return } id = customer.Id } else if tokens[0] == define.TYPE_ENTRYPEOPLE { // 录入人员
entry := &OrderEntryPerson{} err = entry.GetByToken(tokens[1]) if err != nil { return } id = entry.Id } else { err = errors.New("token失效") } return }
func (m *Model) TableName(bean interface{}) string { return core.GetXormAuto().TableName(bean) }
func (m *Model) Alias(bean interface{}, alias string) string { tn := m.TableName(bean) return fmt.Sprintf("%s as %s", tn, alias) }
func (m *Model) Add(bean interface{}) (int64, error) { return core.GetXormAuto().InsertOne(bean) }
func (m *Model) Get(bean interface{}, id interface{}) (bool, error) { return core.GetXormAuto().NoAutoCondition().Where("is_delete=0 and id=?", id).Get(bean) }
func (m *Model) Del(bean interface{}, id interface{}) (int64, error) { return core.GetXormAuto().NoAutoCondition().Where("is_delete=0 and id=?", id).Delete(bean) }
func (m *Model) Update(bean, id interface{}, field ...string) (int64, error) { s := core.GetXormAuto().NoAutoCondition().ID(id) defer s.Close()
if len(field) > 0 { if field[0] == "*" { s = s.AllCols() } else if field[0] == "#" { s = s.MustCols(field[1:]...) } else { s = s.Cols(field...) } } return s.Update(bean) }
func (m *Model) Exist(bean, id interface{}) (bool, error) { return core.GetXormAuto().NoAutoCondition().Where("is_delete=0 and id=?", id).Exist(bean) }
func init() { err := core.GetXormAuto().Sync2( new(Activity), new(ActivityModuleService), new(AreaStore), new(AuctionHistory), new(AuctionResultRecord), new(AuctionPlayer), 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 Alias(bean interface{}, alias string) string { tn := GetTableName(bean) return fmt.Sprintf("%s as %s", tn, alias) }
func Get(bean interface{}, id interface{}) (bool, error) { return core.GetXormAuto().NoAutoCondition().Where("is_delete=0 and id=?", id).Get(bean) }
func Del(bean interface{}, id interface{}) (int64, error) { return core.GetXormAuto().NoAutoCondition().ID(id).Delete(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).NoAutoCondition().Cols(field...).Update(bean) } else { return core.GetXormAuto().ID(id).NoAutoCondition().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() }
|