package db import ( "applet/app/db/model" "applet/app/utils/logx" "time" "xorm.io/xorm" ) // 获取对应等级未过期的付费订单 func UserLevelOrderListByUIDByLevelTypeByExpire(Db *xorm.Engine, uid, levelID interface{}) ([]*model.UserLevelOrd, error) { var m []*model.UserLevelOrd err := Db.Where("uid = ? AND level_id = ? AND state = ? ", uid, levelID, 1).Desc("expire_at").Find(&m) if err != nil { return nil, err } return m, nil } // OrderListByPvdAndNoSettledWithPage is 查询未结算订单 func UserLevelOrderListByUIDByNoSettledWithPage(Db *xorm.Engine, page int) ([]*model.UserLevelOrd, error) { perPage := 100 startPlace := (page - 1) * perPage var o []*model.UserLevelOrd err := Db.Where("settle_at=?", 0).In("state", 1).Limit(perPage, startPlace).Find(&o) if err != nil { return nil, err } return o, nil } // 获取一个订单 func UserLevelOrderById(Db *xorm.Engine, id string) (*model.UserLevelOrd, error) { var m *model.UserLevelOrd has, err := Db.Where("id = ? ", id).Get(&m) if err != nil || has == false { return nil, err } return m, nil } //UserLevelOrderListByUIDByLevelByDateTypeByExpire is 获取某用户某等级的订单,按照过期时间降序排序 func UserLevelOrderListByUIDByLevelByDateTypeByExpire(Db *xorm.Engine, uid, levelID, t interface{}) ([]*model.UserLevelOrd, error) { var m []*model.UserLevelOrd err := Db.Where("uid = ? AND level_id = ? AND date_type = ? AND state = ? ", uid, levelID, t, 1).Desc("expire_at").Find(&m) if err != nil { return nil, err } return m, nil } //UserLevelOrderOneByUIDByLevelByDateTypeByExpire is 获取某用户某等级的订单,按照过期时间降序排序 func UserLevelOrderOneByUIDByLevelByDateTypeByExpire(Db *xorm.Engine, uid, levelID interface{}) ([]*model.UserLevelOrd, error) { var m []*model.UserLevelOrd err := Db.Where("uid = ? AND level_id = ? and state = 1", uid, levelID).Desc("expire_at").Find(&m) if err != nil { return nil, err } return m, nil } //UserLevelOrdInsertOne is 插入一个 等级付费升级的订单 func UserLevelOrdInsertOne(Db *xorm.Engine, order *model.UserLevelOrd) (int64, error) { affect, err := Db.InsertOne(order) if err != nil { return 0, err } return affect, nil } //UserLevelOrdUpdateOne is 更新付费升级订单 func UserLevelOrdUpdateOne(Db *xorm.Engine, order *model.UserLevelOrd) (int64, error) { affect, err := Db.ID(order.Id).Update(order) if err != nil { return 0, err } return affect, nil } func UserLevelOrdUpdateOneWithSession(Db *xorm.Session, order *model.UserLevelOrd) (int64, error) { affect, err := Db.ID(order.Id).Update(order) if err != nil { return 0, err } return affect, nil } func UserLevelOrdFindOneByOid(Db *xorm.Engine, id int64) (*model.UserLevelOrd, error) { var m model.UserLevelOrd has, err := Db.Where("id=? AND state=?", id, 1).Get(&m) if err != nil || !has { return nil, err } return &m, nil } func UserLevelOrdFindByUidStateLevelAndNotExpire(Db *xorm.Engine, uid int, state int, level string) (*model.UserLevelOrd, error) { var m model.UserLevelOrd //过期时间还没到 或者是永久的订单 if has, err := Db.Where("uid = ? AND level_id = ? AND state = ? AND (expire_at>= ? OR date_type=4)", uid, level, state, time.Now().Unix()).Get(&m); !has || err != nil { return nil, logx.Warn(err) } return &m, nil } // 查询订单状态 func UserLevelOrdFindByID(Db *xorm.Engine, id int) (*model.UserLevelOrd, error) { var m model.UserLevelOrd if has, err := Db.Where("id = ?", id).Get(&m); !has || err != nil { return nil, logx.Warn(err) } return &m, nil } // 获取对应id订单 func UserLevelOrdFindByIDs(Db *xorm.Engine, ids []int) ([]*model.UserLevelOrd, error) { var m []*model.UserLevelOrd err := Db.In("id", ids).Find(&m) if err != nil { return nil, err } return m, nil } //session事务修改订单 func UserLvUpOrderUpdateWithSession(session *xorm.Session, ordId int64, order *model.UserLevelOrd, forceCols ...string) (int64, error) { var ( affected int64 err error ) if forceCols != nil { affected, err = session.Where("id=?", ordId).Cols(forceCols...).Update(order) } else { affected, err = session.Where("id=?", ordId).Update(order) } if err != nil { return 0, logx.Warn(err) } return affected, nil }