123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- package data_manage
- import (
- "eta_gn/eta_api/global"
- "eta_gn/eta_api/utils"
- "fmt"
- "strings"
- "time"
- )
- // EdbCollectClassify 指标收藏分类
- type EdbCollectClassify struct {
- EdbCollectClassifyId int `gorm:"primaryKey;autoIncrement;column:edb_collect_classify_id;type:int(10) unsigned;not null"`
- ClassifyName string `gorm:"column:classify_name;type:varchar(255);not null;default:''"` // 分类名称
- ParentId int `gorm:"column:parent_id;type:int(10) unsigned;not null;default:0"` // 父级Id
- SysUserId int `gorm:"column:sys_user_id;type:int(10) unsigned;not null;default:0"` // 创建人Id
- SysUserRealName string `gorm:"column:sys_user_real_name;type:varchar(128);not null"` // 创建人姓名
- UniqueCode string `gorm:"unique;column:unique_code;type:varchar(64);not null;default:''"` // 唯一编码
- Level int `gorm:"column:level;type:int(10) unsigned;not null;default:0"` // 层级
- Sort int `gorm:"column:sort;type:int(10) unsigned;not null;default:0"` // 排序
- RootId int `gorm:"column:root_id;type:int(10) unsigned;not null;default:0"` // 顶级Id
- LevelPath string `gorm:"column:level_path;type:varchar(255);not null;default:''"` // 层级路径,逗号分隔
- CreateTime time.Time `gorm:"column:create_time;type:datetime"` // 创建时间
- ModifyTime time.Time `gorm:"column:modify_time;type:datetime"` // 修改时间
- }
- func (m *EdbCollectClassify) TableName() string {
- return "edb_collect_classify"
- }
- type EdbCollectClassifyCols struct {
- PrimaryId string
- ClassifyName string
- ParentId string
- SysUserId string
- SysUserRealName string
- UniqueCode string
- Level string
- Sort string
- RootId string
- LevelPath string
- CreateTime string
- ModifyTime string
- }
- func (m *EdbCollectClassify) Cols() EdbCollectClassifyCols {
- return EdbCollectClassifyCols{
- PrimaryId: "edb_collect_classify_id",
- ClassifyName: "classify_name",
- ParentId: "parent_id",
- SysUserId: "sys_user_id",
- SysUserRealName: "sys_user_real_name",
- UniqueCode: "unique_code",
- Level: "level",
- Sort: "sort",
- RootId: "root_id",
- LevelPath: "level_path",
- CreateTime: "create_time",
- ModifyTime: "modify_time",
- }
- }
- func (m *EdbCollectClassify) Create() (err error) {
- err = global.DmSQL["data"].Create(m).Error
- return
- }
- func (m *EdbCollectClassify) CreateMulti(items []*EdbCollectClassify) (err error) {
- if len(items) == 0 {
- return
- }
- err = global.DmSQL["data"].CreateInBatches(items, utils.MultiAddNum).Error
- return
- }
- func (m *EdbCollectClassify) Update(cols []string) (err error) {
- err = global.DmSQL["data"].Select(cols).Updates(m).Error
- return
- }
- func (m *EdbCollectClassify) Remove() (err error) {
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
- err = global.DmSQL["data"].Exec(sql, m.EdbCollectClassifyId).Error
- return
- }
- func (m *EdbCollectClassify) MultiRemove(ids []int) (err error) {
- if len(ids) == 0 {
- return
- }
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
- err = global.DmSQL["data"].Exec(sql, ids).Error
- return
- }
- func (m *EdbCollectClassify) RemoveByCondition(condition string, pars []interface{}) (err error) {
- if condition == "" {
- return
- }
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
- err = global.DmSQL["data"].Exec(sql, pars...).Error
- return
- }
- func (m *EdbCollectClassify) GetItemById(id int) (item *EdbCollectClassify, err error) {
- sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
- err = global.DmSQL["data"].Raw(sql, id).First(&item).Error
- return
- }
- func (m *EdbCollectClassify) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *EdbCollectClassify, err error) {
- 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 = global.DmSQL["data"].Raw(sql, pars...).First(&item).Error
- return
- }
- func (m *EdbCollectClassify) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
- sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
- err = global.DmSQL["data"].Raw(sql, pars...).Scan(&count).Error
- return
- }
- func (m *EdbCollectClassify) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbCollectClassify, err error) {
- 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 = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error
- return
- }
- func (m *EdbCollectClassify) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*EdbCollectClassify, err error) {
- 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)
- pars = append(pars, startSize, pageSize)
- err = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error
- return
- }
- func (m *EdbCollectClassify) GetSortMax(parentId int) (sort int, err error) {
- sql := fmt.Sprintf(`SELECT COALESCE(MAX(%s), 0) FROM %s WHERE %s = ?`, m.Cols().Sort, m.TableName(), m.Cols().ParentId)
- err = global.DmSQL["data"].Raw(sql, parentId).Scan(&sort).Error
- return
- }
- // EdbCollectClassifyItem 指标收藏分类
- type EdbCollectClassifyItem struct {
- ClassifyId int `description:"收藏ID"`
- ClassifyName string `description:"指标收藏分类ID"`
- ParentId int `description:"指标ID"`
- Level int `description:"指标ID"`
- Sort int `description:"指标ID"`
- LevelPath string `description:"层级路径"`
- UniqueCode string `description:"唯一编码"`
- Children []*EdbCollectClassifyItem `description:"子分类"`
- }
- func (m *EdbCollectClassify) Format2Item() (item *EdbCollectClassifyItem) {
- item = new(EdbCollectClassifyItem)
- item.ClassifyId = m.EdbCollectClassifyId
- item.ClassifyName = m.ClassifyName
- item.ParentId = m.ParentId
- item.Level = m.Level
- item.Sort = m.Sort
- item.LevelPath = m.LevelPath
- item.UniqueCode = m.UniqueCode
- //item.Children = make([]*EdbCollectClassifyItem, 0)
- return
- }
- // EdbCollectClassifyListItem 指标收藏分类列表
- type EdbCollectClassifyListItem struct {
- NodeType int `description:"类型: 1-分类; 2-指标"`
- ClassifyId int `description:"分类ID"`
- ClassifyName string `description:"分类名称"`
- EdbInfoId int `description:"指标ID"`
- EdbCode string `description:"指标编码"`
- EdbName string `description:"指标名称"`
- ParentId int `description:"父级ID"`
- Level int `description:"层级"`
- Sort int `description:"排序"`
- UniqueCode string `description:"唯一编码, 指标的话用indexCode"`
- Children []*EdbCollectClassifyListItem `description:"子分类"`
- }
- // EdbCollectClassifyAddReq 新增分类
- type EdbCollectClassifyAddReq struct {
- ClassifyName string `description:"分类名称"`
- ParentId int `description:"父级ID"`
- Level int `description:"层级"`
- }
- // EdbCollectClassifyEditReq 编辑分类
- type EdbCollectClassifyEditReq struct {
- ClassifyId int `description:"分类ID"`
- ClassifyName string `description:"分类名称"`
- }
- // EdbCollectClassifyRemoveReq 删除分类
- type EdbCollectClassifyRemoveReq struct {
- ClassifyId int `description:"分类ID"`
- }
- // RemoveClassifyAndCollect 移除分类及收藏
- func (m *EdbCollectClassify) RemoveClassifyAndCollect(classifyIds []int) (err error) {
- if len(classifyIds) == 0 {
- return
- }
- tx := global.DmSQL["data"].Begin()
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- return
- }
- _ = tx.Commit()
- }()
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(classifyIds)))
- e := tx.Exec(sql, classifyIds).Error
- if e != nil {
- err = fmt.Errorf("remove classify err: %v", e)
- return
- }
- collectOb := new(EdbCollect)
- sql = fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, collectOb.TableName(), collectOb.Cols().EdbCollectClassifyId, utils.GetOrmInReplace(len(classifyIds)))
- e = tx.Exec(sql, classifyIds).Error
- if e != nil {
- err = fmt.Errorf("remove collect err: %v", e)
- return
- }
- return
- }
- // EdbCollectClassifyMoveReq 移动分类
- type EdbCollectClassifyMoveReq struct {
- ClassifyId int `description:"分类ID"`
- PrevClassifyId int `description:"上一个兄弟节点分类ID"`
- NextClassifyId int `description:"下一个兄弟节点分类ID"`
- }
- // GetEdbCollectClassifyById 获取分类
- func GetEdbCollectClassifyById(adminId, classifyId int) (item *EdbCollectClassify, err error) {
- sql := `SELECT * FROM edb_collect_classify WHERE sys_user_id = ? AND edb_collect_classify_id = ? `
- err = global.DmSQL["data"].Raw(sql, adminId, classifyId).First(&item).Error
- return
- }
- // GetFirstEdbCollectClassifyByAdminId 获取当前账号下,排序第一条的分类数据
- func GetFirstEdbCollectClassifyByAdminId(adminId int) (item *EdbCollectClassify, err error) {
- sql := `SELECT * FROM edb_collect_classify WHERE sys_user_id = ? ORDER BY sort ASC, edb_collect_classify_id ASC LIMIT 1`
- err = global.DmSQL["data"].Raw(sql, adminId).First(&item).Error
- return
- }
- // UpdateEdbCollectClassifySortByClassifyId 更新排序
- func UpdateEdbCollectClassifySortByClassifyId(adminId, classifyId, nowSort int, updateSort string) (err error) {
- sql := fmt.Sprintf(`UPDATE edb_collect_classify SET sort = %d WHERE sys_user_id = ? and sort > ? `, updateSort)
- if classifyId > 0 {
- sql += ` OR ( edb_collect_classify_id > ` + fmt.Sprint(classifyId) + ` AND sort = ` + fmt.Sprint(nowSort) + `)`
- }
- err = global.DmSQL["data"].Exec(sql, adminId, nowSort).Error
- return
- }
|