package data_manage import ( "errors" "eta_gn/eta_api/global" "eta_gn/eta_api/utils" "fmt" "time" ) // EdbPublicClassify // @Description: 公共分类 type EdbPublicClassify struct { EdbPublicClassifyId int `gorm:"primaryKey" ` ClassifyType uint8 `description:"分类类型,0:普通指标分类,1:预测指标分类,2:指标加工的分类(普通指标的计算指标)"` EdbPublicClassifyName 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:"最后修改人姓名"` EdbPublicClassifyNamePath string `description:"分类名称的完整路径,格式为:父级分类名称|当前分类名称"` EdbPublicClassifyIdPath string `description:"分类的完整路径,格式为:父级ID,当前ID"` } func (m EdbPublicClassify) ToEdbClassifyItems() *EdbClassifyItems { return &EdbClassifyItems{ ClassifyId: m.EdbPublicClassifyId, //EdbInfoId: 0, ClassifyType: m.ClassifyType, ClassifyName: m.EdbPublicClassifyName, ClassifyNameEn: m.EdbPublicClassifyName, ParentId: m.ParentId, RootId: m.RootId, Level: m.Level, Sort: m.Sort, UniqueCode: m.UniqueCode, //Source: 0, //SourceName: "", SysUserId: m.SysUserId, SysUserRealName: m.SysUserRealName, //StartDate: "", //EdbCode: "", //EdbType: 0, //Children: nil, //Button: EdbClassifyItemsButton{}, //IsJoinPermission: 0, //HaveOperaAuth: false, ClassifyIdPath: m.EdbPublicClassifyIdPath, } } // Add // @Description: 添加分类 // @author: Roc // @receiver m // @datetime 2024-12-04 17:00:18 // @return err error func (m *EdbPublicClassify) 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 *EdbPublicClassify) Update(cols []string) (err error) { err = global.DmSQL["data"].Select(cols).Updates(m).Error return } // GetEdbClassifyById // @Description: 根据id获取分类 // @author: Roc // @receiver m // @datetime 2024-12-04 16:56:15 // @param classifyId int // @return item *EdbPublicClassify // @return err error func (m EdbPublicClassify) GetEdbClassifyById(classifyId int) (item *EdbPublicClassify, err error) { sql := `SELECT * FROM edb_public_classify WHERE edb_public_classify_id=? ` err = global.DmSQL["data"].Raw(sql, classifyId).First(&item).Error return } // GetEdbClassifyByIdList // @Description: 根据id列表获取分类列表 // @author: Roc // @receiver m // @datetime 2024-12-04 16:56:15 // @param classifyIdList []int // @return items []*EdbPublicClassify // @return err error func (m EdbPublicClassify) GetEdbClassifyByIdList(classifyIdList []int) (items []*EdbPublicClassify, err error) { if len(classifyIdList) <= 0 { return } sql := `SELECT * FROM edb_public_classify WHERE edb_public_classify_id in (?) ` err = global.DmSQL["data"].Raw(sql, classifyIdList).Find(&items).Error return } // GetEdbClassifyListByParentId // @Description: 根据父级id获取下级分类列表 // @author: Roc // @receiver m // @datetime 2024-12-03 13:57:48 // @param parentId int // @param classifyType int8 // @return items []*EdbPublicClassify // @return err error func (m EdbPublicClassify) GetEdbClassifyListByParentId(parentId int, classifyType int8) (items []*EdbPublicClassify, err error) { o := global.DmSQL["data"] sql := `SELECT * FROM edb_public_classify WHERE parent_id=? AND classify_type = ? order by sort asc,edb_public_classify_id asc ` err = o.Raw(sql, parentId, classifyType).Find(&items).Error return } // GetEdbClassifyItemsByParentId // @Description: 根据父级id获取下级分类列表(结构型的) // @author: Roc // @receiver m // @datetime 2024-12-03 13:56:18 // @param parentId int // @param classifyType int8 // @return items []*EdbClassifyItems // @return err error func (m EdbPublicClassify) GetEdbClassifyItemsByParentId(parentId int, classifyType int8) (items []*EdbClassifyItems, err error) { list, err := m.GetEdbClassifyListByParentId(parentId, classifyType) if err != nil { return } for _, v := range list { items = append(items, v.ToEdbClassifyItems()) } return } // GetEdbClassifyListByParentId // @Description: 根据父级id获取下级分类列表 // @author: Roc // @receiver m // @datetime 2024-12-03 13:57:48 // @param parentId int // @param classifyType int8 // @return items []*EdbPublicClassify // @return err error func (m EdbPublicClassify) GetEdbClassifyListByType(classifyType int8) (items []*EdbPublicClassify, err error) { o := global.DmSQL["data"] sql := `SELECT * FROM edb_public_classify WHERE classify_type = ? order by sort asc,edb_public_classify_id asc ` err = o.Raw(sql, classifyType).Find(&items).Error return } // GetAllEdbClassifyByType 根据类型获取分类列表 func (m EdbPublicClassify) GetAllEdbClassifyByType(classifyType int8) (items []*EdbClassifyItems, err error) { list, err := m.GetEdbClassifyListByType(int8(classifyType)) if err != nil { return } for _, v := range list { items = append(items, v.ToEdbClassifyItems()) } return } func (m EdbPublicClassify) GetEdbClassifyCount(classifyName string, parentId int, classifyType uint8) (count int, err error) { sql := `SELECT COUNT(1) AS count FROM edb_public_classify WHERE parent_id=? AND edb_public_classify_name=? AND classify_type = ? ` err = global.DmSQL["data"].Raw(sql, parentId, classifyName, classifyType).Scan(&count).Error return } // UpdateEdbClassifyNameAndNamePath // @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 *EdbPublicClassify) UpdateEdbClassifyNameAndNamePath(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 edb_public_classify SET edb_public_classify_name_path = REPLACE(edb_public_classify_name_path,?,?) WHERE edb_public_classify_id_path LIKE ?` err = tx.Exec(sql, oldClassifyNamePath, newClassifyNamePath, fmt.Sprint(m.EdbPublicClassifyIdPath+`,%`)).Error if err != nil { return } } return } // GetEdbClassifyMaxSort 获取分类下最大的排序数 func (m EdbPublicClassify) GetEdbClassifyMaxSort(parentId int, classifyType uint8) (sort int, err error) { sql := `SELECT COALESCE(Max(sort), 0) AS sort FROM edb_public_classify WHERE parent_id=? AND classify_type=? ` err = global.DmSQL["data"].Raw(sql, parentId, classifyType).Scan(&sort).Error return } // GetEdbInfoCountByClassifyIdList // @Description: 根据公共分类id集合获取名下指标数量 // @author: Roc // @receiver m // @datetime 2024-12-05 09:59:56 // @param classifyIdList []int // @return count int // @return err error func (m EdbPublicClassify) GetEdbInfoCountByClassifyIdList(classifyIdList []int) (count int, err error) { if len(classifyIdList) <= 0 { return } sql := `SELECT COUNT(1) total FROM edb_info WHERE edb_public_classify_id IN (?) ` err = global.DmSQL["data"].Raw(sql, classifyIdList).Scan(&count).Error return } func (m *EdbPublicClassify) GetAllChildClassifyIdList() (items []int, err error) { sql := ` SELECT edb_public_classify_id FROM edb_public_classify WHERE edb_public_classify_id_path LIKE ? ORDER BY create_time DESC ` err = global.DmSQL["data"].Raw(sql, fmt.Sprint(m.EdbPublicClassifyIdPath+`,%`)).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 *EdbPublicClassify) BatchesDel(classifyIdList []int) (err error) { sql := ` DELETE FROM edb_public_classify WHERE edb_public_classify_id IN (?) ` err = global.DmSQL["data"].Exec(sql, classifyIdList).Error return } func (m *EdbPublicClassify) GetChildEdbClassifyListByParentId(classifyIdPath, orderStr string) (items []*EdbPublicClassify, err error) { o := global.DmSQL["data"] sql := ` SELECT * FROM edb_public_classify WHERE edb_public_classify_id_path like ? ` if orderStr != "" { sql += orderStr } else { sql += ` order by level asc, sort asc, edb_public_classify_id 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.EdbClassifyItems // @return err error // @return errMsg string func (m *EdbPublicClassify) GetAllChildClassifyByParentId(parentId int) (targetList []*EdbPublicClassify, err error, errMsg string) { //判断是否是挂在顶级目录下 parentClassify, err := m.GetEdbClassifyById(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, edb_public_classify_id asc` targetList, err = m.GetChildEdbClassifyListByParentId(fmt.Sprint(parentClassify.EdbPublicClassifyIdPath, ",%"), orderStr) return } // GetEdbPublicClassifyByParentIdAndName 根据父级ID和名称获取分类 func (m *EdbPublicClassify) GetEdbPublicClassifyByParentIdAndName(parentId int, classifyName string, classifyId int, classifyType uint8) (item *EdbPublicClassify, err error) { sql := `SELECT * FROM edb_public_classify WHERE parent_id = ? AND edb_public_classify_name = ? AND edb_public_classify_id <> ? and classify_type=? LIMIT 1` err = global.DmSQL["data"].Raw(sql, parentId, classifyName, classifyId, classifyType).First(&item).Error return } // UpdateEdbClassifySortByParentId 根据父类id更新排序 func (m *EdbPublicClassify) UpdateEdbClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string, classifyType uint8) (err error) { sql := ` update edb_public_classify set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? AND classify_type = ? ` if classifyId > 0 { sql += ` or ( edb_public_classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)` } err = global.DmSQL["data"].Exec(sql, parentId, nowSort, classifyType).Error return } // GetFirstEdbClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据 func (m *EdbPublicClassify) GetFirstEdbClassifyByParentId(parentId int) (item *EdbPublicClassify, err error) { o := global.DmSQL["data"] sql := ` SELECT * FROM edb_public_classify WHERE parent_id=? order by sort asc,edb_public_classify_id asc limit 1` err = o.Raw(sql, parentId).First(&item).Error return } func (m *EdbPublicClassify) GetEdbClassifyByRootIdLevel(rootId int, classifyType uint8, orderStr string) (items []*EdbPublicClassify, err error) { o := global.DmSQL["data"] sql := ` SELECT * FROM edb_public_classify WHERE root_id=? AND classify_type = ? ` if orderStr != "" { sql += orderStr } else { sql += ` order by level desc, sort asc, edb_public_classify_id asc` } err = o.Raw(sql, rootId, classifyType).Scan(&items).Error return } func (m *EdbPublicClassify) UpdateEdbClassifyChildByParentClassifyId(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 edb_public_classify SET root_id = ?, level = level+? where edb_public_classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)` err = global.DmSQL["data"].Exec(sql, pars...).Error if err != nil { return } return } // UpdateEdbClassifyNameAndNamePathByOldClassifyIdPath // @Description: 根据源分类id全路径更新分类名称和子分类的全路径 // @author: Roc // @receiver edbClassify // @datetime 2024-11-26 14:39:09 // @param cols []string // @param oldClassifyNamePath string // @param newClassifyNamePath string // @return err error func (m *EdbPublicClassify) UpdateEdbClassifyNameAndNamePathByOldClassifyIdPath(oldClassifyIdPath, newClassifyIdPath, oldClassifyNamePath, newClassifyNamePath string) (err error) { // 更改子分类的完整的路径 if oldClassifyIdPath != `` && newClassifyNamePath != `` && newClassifyIdPath != `` { sql := `UPDATE edb_public_classify SET edb_public_classify_id_path = REPLACE(edb_public_classify_id_path,?,?) ,edb_public_classify_name_path = REPLACE(edb_public_classify_name_path,?,?) WHERE edb_public_classify_id_path LIKE ?` err = global.DmSQL["data"].Exec(sql, oldClassifyIdPath, newClassifyIdPath, oldClassifyNamePath, newClassifyNamePath, fmt.Sprint(oldClassifyIdPath+`,%`)).Error if err != nil { return } } return }