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 }