package data_manage import ( "eta/eta_api/global" "eta/eta_api/utils" "fmt" "strings" "time" ) // BaseFromRadishResearchClassify 萝卜投研原始数据分类表 type BaseFromRadishResearchClassify struct { BaseFromRadishResearchClassifyId int `orm:"column(base_from_radish_research_classify_id);pk" gorm:"primaryKey"` ClassifyName string `description:"分类名称"` ParentId int `description:"父级id"` Level int `description:"层级"` Sort int `description:"排序字段,越小越靠前,默认值:10"` SysUserId int `description:"创建人id"` SysUserRealName string `description:"创建人姓名"` LevelPath string `description:"层级路径"` RootId int `description:"顶级分类ID"` UniqueCode string `description:"唯一编码"` ModifyTime time.Time `description:"修改时间"` CreateTime time.Time `description:"创建时间"` } func (m *BaseFromRadishResearchClassify) TableName() string { return "base_from_radish_research_classify" } type BaseFromRadishResearchClassifyCols struct { PrimaryId string ClassifyName string ParentId string SysUserId string SysUserRealName string Level string Sort string RootId string LevelPath string UniqueCode string CreateTime string ModifyTime string } func (m *BaseFromRadishResearchClassify) Cols() BaseFromRadishResearchClassifyCols { return BaseFromRadishResearchClassifyCols{ PrimaryId: "base_from_radish_research_classify_id", ClassifyName: "classify_name", ParentId: "parent_id", SysUserId: "sys_user_id", SysUserRealName: "sys_user_real_name", Level: "level", Sort: "sort", RootId: "root_id", LevelPath: "level_path", UniqueCode: "unique_code", CreateTime: "create_time", ModifyTime: "modify_time", } } func (m *BaseFromRadishResearchClassify) Create() (err error) { o := global.DbMap[utils.DbNameIndex] err = o.Create(m).Error return } func (m *BaseFromRadishResearchClassify) CreateMulti(items []*BaseFromRadishResearchClassify) (err error) { if len(items) == 0 { return } o := global.DbMap[utils.DbNameIndex] err = o.CreateInBatches(items, utils.MultiAddNum).Error return } func (m *BaseFromRadishResearchClassify) Update(cols []string) (err error) { o := global.DbMap[utils.DbNameIndex] err = o.Select(cols).Updates(m).Error return } func (m *BaseFromRadishResearchClassify) Remove() (err error) { o := global.DbMap[utils.DbNameIndex] sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId) err = o.Exec(sql, m.BaseFromRadishResearchClassifyId).Error return } func (m *BaseFromRadishResearchClassify) MultiRemove(ids []int) (err error) { if len(ids) == 0 { return } o := global.DbMap[utils.DbNameIndex] sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids))) err = o.Exec(sql, ids).Error return } func (m *BaseFromRadishResearchClassify) RemoveByCondition(condition string, pars []interface{}) (err error) { if condition == "" { return } o := global.DbMap[utils.DbNameIndex] sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition) err = o.Exec(sql, pars...).Error return } func (m *BaseFromRadishResearchClassify) GetItemById(id int) (item *BaseFromRadishResearchClassify, err error) { o := global.DbMap[utils.DbNameIndex] sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId) err = o.Raw(sql, id).First(&item).Error return } func (m *BaseFromRadishResearchClassify) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *BaseFromRadishResearchClassify, err error) { o := global.DbMap[utils.DbNameIndex] 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...).First(&item).Error return } func (m *BaseFromRadishResearchClassify) GetCountByCondition(condition string, pars []interface{}) (count int, err error) { o := global.DbMap[utils.DbNameIndex] sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition) err = o.Raw(sql, pars...).Scan(&count).Error return } func (m *BaseFromRadishResearchClassify) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromRadishResearchClassify, err error) { o := global.DbMap[utils.DbNameIndex] 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...).Find(&items).Error return } func (m *BaseFromRadishResearchClassify) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BaseFromRadishResearchClassify, err error) { o := global.DbMap[utils.DbNameIndex] 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 = o.Raw(sql, pars...).Find(&items).Error return } // GetBaseFromRadishResearchClassify 获取所有分类 func GetBaseFromRadishResearchClassify() (items []*BaseFromRadishResearchClassify, err error) { sql := ` SELECT * FROM base_from_radish_research_classify ORDER BY parent_id ASC, sort ASC, base_from_radish_research_classify_id ASC` err = global.DbMap[utils.DbNameIndex].Raw(sql).Find(&items).Error return } // BaseFromRadishResearchClassifyItem 萝卜投研数据分类信息 type BaseFromRadishResearchClassifyItem struct { ClassifyId int `description:"分类ID"` ClassifyName string `description:"分类名称"` ParentId int `description:"父级id"` Level int `description:"层级"` Sort int `description:"排序字段"` LevelPath string `description:"层级路径"` UniqueCode string `description:"唯一编码"` CreateTime string `description:"创建时间"` ModifyTime string `description:"修改时间"` Children []*BaseFromRadishResearchClassifyItem `description:"子分类"` } func (m *BaseFromRadishResearchClassify) Format2Item() (item *BaseFromRadishResearchClassifyItem) { item = new(BaseFromRadishResearchClassifyItem) item.ClassifyId = m.BaseFromRadishResearchClassifyId 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.CreateTime = utils.TimeTransferString(utils.FormatDateTime, m.CreateTime) item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, m.ModifyTime) return } type RadishResearchClassifyAddReq struct { ClassifyName string `description:"分类名称"` ParentId int `description:"父级ID, 第一级传0"` Level int `description:"层级, 第一级传0, 其余传上一级的层级"` } type RadishResearchClassifyEditReq struct { ClassifyId int `description:"分类ID"` ClassifyName string `description:"分类名称"` } func (m *BaseFromRadishResearchClassify) GetSortMax(parentId int) (sort int, err error) { o := global.DbMap[utils.DbNameIndex] sql := fmt.Sprintf(`SELECT COALESCE(Max(%s), 0) AS sort FROM %s WHERE %s = ?`, m.Cols().Sort, m.TableName(), m.Cols().ParentId) err = o.Raw(sql, parentId).Scan(&sort).Error return } type RadishResearchClassifyRemoveReq struct { ClassifyId int `description:"分类ID"` } type BaseFromRadishResearchClassifyListItem struct { NodeType int `description:"节点类型: 0-分类; 1-指标"` ClassifyId int `description:"分类ID"` ClassifyName string `description:"分类名称"` IndexId int `description:"指标ID"` IndexCode string `description:"指标编码"` IndexName string `description:"指标名称"` ParentId int `description:"父级ID"` Level int `description:"层级"` Sort int `description:"排序"` UniqueCode string `description:"唯一编码, 指标的话用indexCode"` Children []*BaseFromRadishResearchClassifyListItem `description:"子分类"` } type BaseFromRadishResearchClassifyMoveReq struct { ClassifyId int `description:"分类ID"` ParentClassifyId int `description:"父级分类ID"` PrevClassifyId int `description:"上一个兄弟节点分类ID"` NextClassifyId int `description:"下一个兄弟节点分类ID"` ItemId int `description:"指标ID, 如果指标ID有值,则移动对象为指标,否则认为移动对象为分类"` PrevItemId int `description:"上一个指标ID"` NextItemId int `description:"下一个指标ID"` } func GetRadishResearchClassifyById(classifyId int) (item *BaseFromRadishResearchClassify, err error) { o := global.DbMap[utils.DbNameIndex] sql := `SELECT * FROM base_from_radish_research_classify WHERE base_from_radish_research_classify_id = ?` err = o.Raw(sql, classifyId).First(&item).Error return } func GetRadishResearchClassifyByRootIdLevel(rootId int, orderStr string) (items []*BaseFromRadishResearchClassify, err error) { o := global.DbMap[utils.DbNameIndex] sql := ` SELECT * FROM base_from_radish_research_classify WHERE root_id = ? ` if orderStr != "" { sql += orderStr } else { sql += ` order by level desc, sort asc, base_from_radish_research_classify_id asc` } err = o.Raw(sql, rootId).Find(&items).Error return } // UpdateRadishResearchClassifySortByParentId 根据父类id更新排序 func UpdateRadishResearchClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string) (err error) { o := global.DbMap[utils.DbNameIndex] sql := ` update base_from_radish_research_classify set sort = ` + updateSort + ` WHERE parent_id = ? AND sort > ? ` if classifyId > 0 { sql += ` or ( base_from_radish_research_classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)` } err = o.Exec(sql, parentId, nowSort).Error return } // GetFirstRadishResearchClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据 func GetFirstRadishResearchClassifyByParentId(parentId int) (item *BaseFromRadishResearchClassify, err error) { o := global.DbMap[utils.DbNameIndex] sql := ` SELECT * FROM base_from_radish_research_classify WHERE parent_id = ? ORDER BY sort ASC,base_from_radish_research_classify_id ASC LIMIT 1` err = o.Raw(sql, parentId).First(&item).Error return } func UpdateRadishResearchClassifyChildByParentClassifyId(classifyIds []int, rootId int, levelStep int) (err error) { o := global.DbMap[utils.DbNameIndex] var pars []interface{} pars = append(pars, rootId, levelStep) pars = append(pars, classifyIds) // 更新相关联的二级分类的parentId,和classify_name_second sql := `update base_from_radish_research_classify SET root_id = ?, level = level+? where base_from_radish_research_classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)` err = o.Exec(sql, pars...).Error return } // UpdateLevelPath 更新层级路径 func (m *BaseFromRadishResearchClassify) UpdateLevelPath(classifyId int, levelPath string) (err error) { sql := fmt.Sprintf(`UPDATE %s SET %s = ? WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().LevelPath, m.Cols().PrimaryId) err = global.DbMap[utils.DbNameIndex].Exec(sql, levelPath, classifyId).Error return } // GetRadishResearchClassifyByParentIdAndName 根据父级ID和名称获取分类 func GetRadishResearchClassifyByParentIdAndName(parentId int, classifyName string, classifyId int) (item *EdbClassify, err error) { sql := `SELECT * FROM base_from_radish_research_classify WHERE parent_id = ? AND classify_name = ? AND base_from_radish_research_classify_id <> ? LIMIT 1` err = global.DbMap[utils.DbNameIndex].Raw(sql, parentId, classifyName, classifyId).First(&item).Error return }