123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- package models
- import (
- "eta/eta_mini_crm/utils"
- "fmt"
- "strings"
- "time"
- "github.com/beego/beego/v2/client/orm"
- )
- type Classify struct {
- Id int `orm:"column(id);pk"`
- ClassifyName string `description:"分类名称"`
- Sort int `description:"排序"`
- ParentId int `description:"父级ID"`
- Enabled int `description:"状态:0-禁用;1-正常"`
- Level int `description:"分类层级"`
- CreateTime time.Time `description:"创建时间"`
- ModifyTime time.Time `description:"修改时间"`
- }
- func (m *Classify) TableName() string {
- return "classify"
- }
- type ClassifyCols struct {
- PrimaryId string
- ClassifyName string
- Sort string
- ParentId string
- Enabled string
- Level string
- CreateTime string
- ModifyTime string
- }
- func (m *Classify) Cols() ClassifyCols {
- return ClassifyCols{
- PrimaryId: "id",
- ClassifyName: "classify_name",
- Sort: "sort",
- ParentId: "parent_id",
- Enabled: "enabled",
- CreateTime: "create_time",
- ModifyTime: "modify_time",
- }
- }
- func (m *Classify) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*Classify, err error) {
- o := orm.NewOrmUsingDB("rddp")
- 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
- }
- type ClassifyItem struct {
- Id int `orm:"column(id);pk"`
- ClassifyName string `description:"分类名称"`
- Sort int `description:"排序"`
- ParentId int `description:"父级ID"`
- Enabled int `description:"状态:0-禁用;1-正常"`
- Level int `description:"分类层级"`
- CreateTime string `description:"创建时间"`
- ModifyTime string `description:"修改时间"`
- Child []*ClassifyItem `description:"子分类"`
- }
- func (m *Classify) Format2Item() (item *ClassifyItem) {
- item = new(ClassifyItem)
- item.Id = m.Id
- item.ClassifyName = m.ClassifyName
- item.Sort = m.Sort
- item.ParentId = m.ParentId
- item.Enabled = m.Enabled
- item.Level = m.Level
- item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, m.CreateTime)
- item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, m.ModifyTime)
- item.Child = make([]*ClassifyItem, 0)
- return
- }
- func GetClassifyList() (items []*ClassifyItem, err error) {
- o := orm.NewOrmUsingDB("rddp")
- sql := `SELECT * FROM classify WHERE enabled=1`
- _, err = o.Raw(sql).QueryRows(&items)
- return
- }
- func GetClassifyById(classifyId int) (item *ClassifyItem, err error) {
- o := orm.NewOrmUsingDB("rddp")
- sql := `SELECT * FROM classify WHERE enabled=1 AND id=?`
- err = o.Raw(sql, classifyId).QueryRow(&item)
- return
- }
- func GetClassifyListByIds(ids []string) (items []*ClassifyItem, err error) {
- if len(ids) == 0 {
- return
- }
- o := orm.NewOrmUsingDB("rddp")
- sql := `SELECT * FROM classify WHERE enabled=1 AND id IN (` + utils.GetOrmReplaceHolder(len(ids)) + `)`
- _, err = o.Raw(sql, ids).QueryRows(&items)
- return
- }
|