package data_manage import ( "errors" "eta_gn/eta_api/global" "eta_gn/eta_api/utils" "fmt" "time" ) // ChartPublicClassify // @Description: 公共分类 type ChartPublicClassify struct { ChartPublicClassifyId int `gorm:"primaryKey" ` Source int `description:"分类来源: 1-图库; 2-商品价格曲线; 3-相关性图表; 6-拟合方程图表; 7-统计特征"` ChartPublicClassifyName string `description:"分类名称"` ParentId int `description:"父级id"` HasData int `description:"是否含有指标数据"` RootId int `description:"顶级id"` CreateTime time.Time `description:"创建时间"` ModifyTime time.Time `description:"修改时间"` SysUserId int `description:"创建人id"` SysUserRealName string `description:"创建人姓名"` Level int `description:"层级"` UniqueCode string `description:"唯一编码"` Sort int `description:"排序字段,越小越靠前,默认值:10"` LastModifyUserId int `description:"最后修改人ID"` LastModifyUserRealName string `description:"最后修改人姓名"` ChartPublicClassifyNamePath string `description:"分类名称的完整路径,格式为:父级分类名称|当前分类名称"` ChartPublicClassifyIdPath string `description:"分类的完整路径,格式为:父级ID,当前ID"` } func (m ChartPublicClassify) ToChartClassifyItems() *ChartClassifyItems { return &ChartClassifyItems{ ChartClassifyId: m.ChartPublicClassifyId, //ChartInfoId: 0, ChartClassifyName: m.ChartPublicClassifyName, ChartClassifyNameEn: m.ChartPublicClassifyName, ParentId: m.ParentId, Level: m.Level, Sort: m.Sort, UniqueCode: m.UniqueCode, Source: m.Source, //SourceName: "", SysUserId: m.SysUserId, SysUserRealName: m.SysUserRealName, //DateType: 0, //StartDate: "", //EndDate: "", //ChartType: 0, //Calendar: "", //SeasonStartDate: "", //SeasonEndDate: "", Children: nil, Button: ChartClassifyItemsButton{}, IsJoinPermission: 0, HaveOperaAuth: false, ChartClassifyIdPath: m.ChartPublicClassifyIdPath, } } // Add // @Description: 添加分类 // @author: Roc // @receiver m // @datetime 2024-12-04 17:00:18 // @return err error func (m *ChartPublicClassify) Add() (err error) { err = global.DmSQL["data"].Create(m).Error return } // Update // @Description: 更新分类基础信息 // @author: Roc // @receiver m // @datetime 2024-12-04 17:01:51 // @param cols []string // @return err error func (m *ChartPublicClassify) Update(cols []string) (err error) { err = global.DmSQL["data"].Select(cols).Updates(m).Error return } // GetChartClassifyById // @Description: 根据id获取分类 // @author: Roc // @receiver m // @datetime 2024-12-04 16:56:15 // @param classifyId int // @return item *ChartPublicClassify // @return err error func (m ChartPublicClassify) GetChartClassifyById(classifyId int) (item *ChartPublicClassify, err error) { sql := `SELECT * FROM chart_public_classify WHERE chart_public_classify_id=? ` err = global.DmSQL["data"].Raw(sql, classifyId).First(&item).Error return } // GetChartClassifyByIdList // @Description: 根据id列表获取分类列表 // @author: Roc // @receiver m // @datetime 2024-12-04 16:56:15 // @param classifyIdList []int // @return items []*ChartPublicClassify // @return err error func (m ChartPublicClassify) GetChartClassifyByIdList(classifyIdList []int) (items []*ChartPublicClassify, err error) { if len(classifyIdList) <= 0 { return } sql := `SELECT * FROM chart_public_classify WHERE chart_public_classify_id in (?) ` err = global.DmSQL["data"].Raw(sql, classifyIdList).Find(&items).Error return } // GetChartClassifyListByParentId // @Description: 根据父级id获取下级分类列表 // @author: Roc // @receiver m // @datetime 2024-12-03 13:57:48 // @param parentId int // @param source int8 // @return items []*ChartPublicClassify // @return err error func (m ChartPublicClassify) GetChartClassifyListByParentId(parentId int, source int8) (items []*ChartPublicClassify, err error) { o := global.DmSQL["data"] sql := `SELECT * FROM chart_public_classify WHERE parent_id=? AND source = ? order by sort asc,chart_public_classify_id asc ` err = o.Raw(sql, parentId, source).Find(&items).Error return } // GetChartClassifyItemsByParentId // @Description: 根据父级id获取下级分类列表(结构型的) // @author: Roc // @receiver m // @datetime 2024-12-03 13:56:18 // @param parentId int // @param source int8 // @return items []*ChartClassifyItems // @return err error func (m ChartPublicClassify) GetChartClassifyItemsByParentId(parentId int, source int8) (items []*ChartClassifyItems, err error) { list, err := m.GetChartClassifyListByParentId(parentId, source) if err != nil { return } for _, v := range list { items = append(items, v.ToChartClassifyItems()) } return } // GetChartClassifyListByParentId // @Description: 根据父级id获取下级分类列表 // @author: Roc // @receiver m // @datetime 2024-12-03 13:57:48 // @param parentId int // @param source int8 // @return items []*ChartPublicClassify // @return err error func (m ChartPublicClassify) GetChartClassifyListByType(source int8) (items []*ChartPublicClassify, err error) { o := global.DmSQL["data"] sql := `SELECT * FROM chart_public_classify WHERE source = ? order by sort asc,chart_public_classify_id asc ` err = o.Raw(sql, source).Find(&items).Error return } // GetAllChartClassifyByType 根据类型获取分类列表 func (m ChartPublicClassify) GetAllChartClassifyByType(source int8) (items []*ChartClassifyItems, err error) { list, err := m.GetChartClassifyListByType(int8(source)) if err != nil { return } for _, v := range list { items = append(items, v.ToChartClassifyItems()) } return } func (m ChartPublicClassify) GetChartClassifyCount(classifyName string, parentId int, source int) (count int, err error) { sql := `SELECT COUNT(1) AS count FROM chart_public_classify WHERE parent_id=? AND chart_public_classify_name=? AND source = ? ` err = global.DmSQL["data"].Raw(sql, parentId, classifyName, source).Scan(&count).Error return } // UpdateChartClassifyNameAndNamePath // @Description: 更新分类信息 // @author: Roc // @receiver m // @datetime 2024-12-05 09:27:33 // @param cols []string // @param oldClassifyNamePath string // @param newClassifyNamePath string // @return err error func (m *ChartPublicClassify) UpdateChartClassifyNameAndNamePath(cols []string, oldClassifyNamePath, newClassifyNamePath string) (err error) { tx := global.DmSQL["data"].Begin() defer func() { if err != nil { _ = tx.Rollback() return } _ = tx.Commit() }() // 变更分类信息 err = tx.Select(cols).Updates(m).Error if err != nil { return } // 更改子分类的完整的路径 if oldClassifyNamePath != `` && newClassifyNamePath != `` { sql := `UPDATE chart_public_classify SET chart_public_classify_name_path = REPLACE(chart_public_classify_name_path,?,?) WHERE chart_public_classify_id_path LIKE ?` err = tx.Exec(sql, oldClassifyNamePath, newClassifyNamePath, fmt.Sprint(m.ChartPublicClassifyIdPath+`,%`)).Error if err != nil { return } } return } // GetChartClassifyMaxSort 获取分类下最大的排序数 func (m ChartPublicClassify) GetChartClassifyMaxSort(parentId int, source int) (sort int, err error) { sql := `SELECT COALESCE(Max(sort), 0) AS sort FROM chart_public_classify WHERE parent_id=? AND source=? ` err = global.DmSQL["data"].Raw(sql, parentId, source).Scan(&sort).Error return } // GetChartInfoCountByClassifyIdList // @Description: 根据公共分类id集合获取名下指标数量 // @author: Roc // @receiver m // @datetime 2024-12-05 09:59:56 // @param classifyIdList []int // @return count int // @return err error func (m ChartPublicClassify) GetChartInfoCountByClassifyIdList(classifyIdList []int) (count int, err error) { if len(classifyIdList) <= 0 { return } sql := `SELECT COUNT(1) total FROM chart_info WHERE chart_public_classify_id IN (?) ` err = global.DmSQL["data"].Raw(sql, classifyIdList).Scan(&count).Error return } func (m *ChartPublicClassify) GetAllChildClassifyIdList() (items []int, err error) { sql := ` SELECT chart_public_classify_id FROM chart_public_classify WHERE chart_public_classify_id_path LIKE ? ORDER BY create_time DESC ` err = global.DmSQL["data"].Raw(sql, fmt.Sprint(m.ChartPublicClassifyIdPath+`,%`)).Scan(&items).Error return } // BatchesDel // @Description: 根据分类id批量删除 // @author: Roc // @receiver m // @datetime 2024-12-05 11:13:06 // @param classifyIdList []int // @return err error func (m *ChartPublicClassify) BatchesDel(classifyIdList []int) (err error) { sql := ` DELETE FROM chart_public_classify WHERE chart_public_classify_id IN (?) ` err = global.DmSQL["data"].Exec(sql, classifyIdList).Error return } func (m *ChartPublicClassify) GetChildClassifyListByParentId(classifyIdPath, orderStr string) (items []*ChartPublicClassify, err error) { o := global.DmSQL["data"] sql := ` SELECT * FROM chart_public_classify WHERE chart_public_classify_id_path like ? ` if orderStr != "" { sql += orderStr } else { sql += ` order by level asc, sort asc, chart_public_classify_id_path asc` } err = o.Raw(sql, classifyIdPath).Scan(&items).Error return } // GetAllChildClassifyByParentId // @Description: 根据上级id获取所有的下级id // @author: Roc // @datetime 2024-12-02 15:14:13 // @param parentId int // @return targetList []*data_manage.ChartPublicClassify // @return err error // @return errMsg string func (m *ChartPublicClassify) GetAllChildClassifyByParentId(parentId int) (targetList []*ChartPublicClassify, err error, errMsg string) { //判断是否是挂在顶级目录下 parentClassify, err := m.GetChartClassifyById(parentId) if err != nil { if utils.IsErrNoRow(err) { errMsg = "当前分类不存在" err = errors.New(errMsg) return } errMsg = "获取失败" err = errors.New("获取分类信息失败,Err:" + err.Error()) return } orderStr := ` order by level asc, sort asc, chart_public_classify_id_path asc` targetList, err = m.GetChildClassifyListByParentId(fmt.Sprint(parentClassify.ChartPublicClassifyIdPath, ",%"), orderStr) return } // GetChartPublicClassifyByParentIdAndName 根据父级ID和名称获取分类 func (m *ChartPublicClassify) GetChartPublicClassifyByParentIdAndName(source, parentId int, classifyName string, classifyId int) (item *ChartPublicClassify, err error) { o := global.DmSQL["data"] sql := `SELECT * FROM chart_public_classify WHERE source = ? AND parent_id = ? AND chart_public_classify_name = ? AND chart_public_classify_id <> ? LIMIT 1` err = o.Raw(sql, source, parentId, classifyName, classifyId).First(&item).Error return } // UpdateChartClassifySortByParentId 根据父类id更新排序 func (m *ChartPublicClassify) UpdateChartClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string, source int) (err error) { sql := ` update chart_public_classify set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? AND source = ? ` if classifyId > 0 { sql += ` or ( chart_public_classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)` } err = global.DmSQL["data"].Exec(sql, parentId, nowSort, source).Error return } // GetFirstChartClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据 func (m *ChartPublicClassify) GetFirstChartClassifyByParentId(parentId int) (item *ChartPublicClassify, err error) { o := global.DmSQL["data"] sql := ` SELECT * FROM chart_public_classify WHERE parent_id=? order by sort asc,chart_public_classify_id asc limit 1` err = o.Raw(sql, parentId).First(&item).Error return } func (m *ChartPublicClassify) GetChartClassifyByRootIdLevel(rootId int, source int, orderStr string) (items []*ChartPublicClassify, err error) { o := global.DmSQL["data"] sql := ` SELECT * FROM chart_public_classify WHERE root_id=? AND source = ? ` if orderStr != "" { sql += orderStr } else { sql += ` order by level desc, sort asc, chart_public_classify_id asc` } err = o.Raw(sql, rootId, source).Scan(&items).Error return } func (m *ChartPublicClassify) UpdateChartClassifyChildByParentClassifyId(classifyIds []int, rootId int, levelStep int) (err error) { var pars []interface{} pars = append(pars, rootId, levelStep) pars = append(pars, classifyIds) // 更新相关联的二级分类的parentId,和classify_name_second sql := `update chart_public_classify SET root_id = ?, level = level+? where chart_public_classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)` err = global.DmSQL["data"].Exec(sql, pars...).Error if err != nil { return } return } // UpdateChartClassifyNameAndNamePathByOldClassifyIdPath // @Description: 根据源分类id全路径更新分类名称和子分类的全路径 // @author: Roc // @receiver chartClassify // @datetime 2024-11-26 14:39:09 // @param cols []string // @param oldClassifyNamePath string // @param newClassifyNamePath string // @return err error func (m *ChartPublicClassify) UpdateChartClassifyNameAndNamePathByOldClassifyIdPath(oldClassifyIdPath, newClassifyIdPath, oldClassifyNamePath, newClassifyNamePath string) (err error) { // 更改子分类的完整的路径 if oldClassifyIdPath != `` && newClassifyNamePath != `` && newClassifyIdPath != `` { sql := `UPDATE chart_public_classify SET chart_public_classify_id_path = REPLACE(chart_public_classify_id_path,?,?) ,chart_public_classify_name_path = REPLACE(chart_public_classify_name_path,?,?) WHERE chart_public_classify_id_path LIKE ?` err = global.DmSQL["data"].Exec(sql, oldClassifyIdPath, newClassifyIdPath, oldClassifyNamePath, newClassifyNamePath, fmt.Sprint(oldClassifyIdPath+`,%`)).Error if err != nil { return } } return }