123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- package models
- import (
- "eta/eta_mini_crm/utils"
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- "strings"
- "time"
- )
- // UserLevelMapping 用户等级分类关联表
- type UserLevelMapping struct {
- Id int `orm:"column(id);pk"`
- UserLevelId int `description:"用户等级ID"`
- ClassifyId int `description:"分类ID"`
- CreateTime time.Time `description:"创建时间"`
- }
- func (m *UserLevelMapping) TableName() string {
- return "user_level_mapping"
- }
- type UserLevelMappingCols struct {
- PrimaryId string
- UserLevelId string
- ClassifyId string
- CreateTime string
- }
- func (m *UserLevelMapping) Cols() UserLevelMappingCols {
- return UserLevelMappingCols{
- PrimaryId: "id",
- UserLevelId: "user_level_id",
- ClassifyId: "classify_id",
- CreateTime: "create_time",
- }
- }
- func (m *UserLevelMapping) Create() (err error) {
- o := orm.NewOrm()
- id, err := o.Insert(m)
- if err != nil {
- return
- }
- m.Id = int(id)
- return
- }
- func (m *UserLevelMapping) CreateMulti(items []*UserLevelMapping) (err error) {
- if len(items) == 0 {
- return
- }
- o := orm.NewOrm()
- _, err = o.InsertMulti(len(items), items)
- return
- }
- func (m *UserLevelMapping) Update(cols []string) (err error) {
- o := orm.NewOrm()
- _, err = o.Update(m, cols...)
- return
- }
- func (m *UserLevelMapping) Remove() (err error) {
- o := orm.NewOrm()
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
- _, err = o.Raw(sql, m.Id).Exec()
- return
- }
- func (m *UserLevelMapping) MultiRemove(ids []int) (err error) {
- if len(ids) == 0 {
- return
- }
- o := orm.NewOrm()
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
- _, err = o.Raw(sql, ids).Exec()
- return
- }
- func (m *UserLevelMapping) RemoveByCondition(condition string, pars []interface{}) (err error) {
- if condition == "" {
- return
- }
- o := orm.NewOrm()
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
- _, err = o.Raw(sql, pars).Exec()
- return
- }
- func (m *UserLevelMapping) GetItemById(id int) (item *UserLevelMapping, err error) {
- o := orm.NewOrm()
- sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 AND %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
- err = o.Raw(sql, id).QueryRow(&item)
- return
- }
- func (m *UserLevelMapping) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *UserLevelMapping, err error) {
- o := orm.NewOrm()
- order := ``
- if orderRule != "" {
- order = ` ORDER BY ` + orderRule
- }
- sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
- err = o.Raw(sql, pars).QueryRow(&item)
- return
- }
- func (m *UserLevelMapping) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
- o := orm.NewOrm()
- sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
- err = o.Raw(sql, pars).QueryRow(&count)
- return
- }
- func (m *UserLevelMapping) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*UserLevelMapping, err error) {
- o := orm.NewOrm()
- fields := strings.Join(fieldArr, ",")
- if len(fieldArr) == 0 {
- fields = `*`
- }
- order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
- if orderRule != "" {
- order = ` ORDER BY ` + orderRule
- }
- sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
- _, err = o.Raw(sql, pars).QueryRows(&items)
- return
- }
- func (m *UserLevelMapping) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*UserLevelMapping, err error) {
- o := orm.NewOrm()
- fields := strings.Join(fieldArr, ",")
- if len(fieldArr) == 0 {
- fields = `*`
- }
- order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
- if orderRule != "" {
- order = ` ORDER BY ` + orderRule
- }
- sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
- _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
- return
- }
|