123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- package knowledge
- import (
- "eta_gn/eta_api/global"
- "eta_gn/eta_api/utils"
- "fmt"
- "time"
- )
- type KnowledgeClassify struct {
- ClassifyId int `gorm:"primaryKey;column:classify_id"`
- ClassifyName string `gorm:"column:classify_name;default:'';type:varchar(125);not null"` // 注意:varchar的默认长度可能需要根据实际情况调整
- Sort int `gorm:"column:sort;default:0;type:tinyint"`
- ParentId int `gorm:"column:parent_id;default:0;type:int"`
- CreateTime time.Time `gorm:"column:create_time;default:CURRENT_TIMESTAMP"`
- ModifyTime time.Time `gorm:"column:modify_time;default:CURRENT_TIMESTAMP"`
- Enabled int `gorm:"column:enabled;default:1;type:tinyint"`
- Level int `gorm:"column:level;default:0;type:bigint"`
- ResourceType int `gorm:"column:resource_type;default:0;not null;type:tinyint"`
- }
- func (k *KnowledgeClassify) TableName() string {
- return "knowledge_classify"
- }
- func (k *KnowledgeClassify) GetPrimaryKey() string {
- return "classify_id"
- }
- func (k *KnowledgeClassify) GetClassifyByName(classifyName string, parentId, resourceType int) (item *KnowledgeClassify, err error) {
- sql := fmt.Sprintf(`SELECT * FROM %s WHERE classify_name=? AND parent_id=? AND resource_type=?`, k.TableName())
- err = global.DmSQL["rddp"].Raw(sql, classifyName, parentId, resourceType).First(&item).Error
- return
- }
- func (k *KnowledgeClassify) GetClassifyById(classifyId int) (item *KnowledgeClassify, err error) {
- sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s=? `, k.TableName(), k.GetPrimaryKey())
- err = global.DmSQL["rddp"].Raw(sql, classifyId).First(&item).Error
- return
- }
- // 添加分类
- func (k *KnowledgeClassify) Add(item *KnowledgeClassify) (err error) {
- err = global.DmSQL["rddp"].Create(item).Error
- return
- }
- func (k *KnowledgeClassify) GetResourceCountByClassifyId(classifyId int) (count int, err error) {
- sql := `SELECT COUNT(1) as num FROM knowledge_resource WHERE classify_id=? `
- err = global.DmSQL["rddp"].Raw(sql, classifyId).First(&count).Error
- return
- }
- // GetClassifySubCountByClassifyId 查询当前分类下的子分类是否关联事件
- func (k *KnowledgeClassify) GetClassifySubCountByClassifyId(classifyId int) (count int, err error) {
- sql := `SELECT COUNT(1) as num FROM knowledge_classify AS a
- INNER JOIN knowledge_resource AS b ON a.classify_id=b.classify_id
- WHERE a.parent_id=? `
- err = global.DmSQL["rddp"].Raw(sql, classifyId).First(&count).Error
- return
- }
- func (k *KnowledgeClassify) GetClassifySubCountByParentId(classifyId int) (count int, err error) {
- sqlCount := `
- SELECT COUNT(1) as num FROM %s AS a
- WHERE a.parent_id=? `
- sqlCount = fmt.Sprintf(sqlCount, k.TableName())
- err = global.DmSQL["rddp"].Raw(sqlCount, classifyId).Scan(&count).Error
- return
- }
- func (k *KnowledgeClassify) GetChildClassifyIdByParentId(classifyId int) (ids []int, err error) {
- sqlCount := `
- SELECT classify_id FROM %s AS a
- WHERE a.parent_id=? `
- sqlCount = fmt.Sprintf(sqlCount, k.TableName())
- err = global.DmSQL["rddp"].Raw(sqlCount, classifyId).Scan(&ids).Error
- return
- }
- func (k *KnowledgeClassify) GetChildClassifyIdByParentIds(classifyIds []int) (ids []int, err error) {
- sqlCount := `
- SELECT classify_id FROM %s AS a
- WHERE a.parent_id in (?) `
- sqlCount = fmt.Sprintf(sqlCount, k.TableName())
- err = global.DmSQL["rddp"].Raw(sqlCount, classifyIds).Scan(&ids).Error
- return
- }
- // 删除分类
- func (k *KnowledgeClassify) DeleteByIds(classifyIds []int) (err error) {
- sql := `DELETE FROM %s WHERE classify_id in (?) `
- sql = fmt.Sprintf(sql, k.TableName())
- err = global.DmSQL["rddp"].Exec(sql, classifyIds).Error
- if err != nil {
- return
- }
- return
- }
- type KnowledgeClassifyItem struct {
- KnowledgeClassify
- Child []*KnowledgeClassifyItem `gorm:"-"`
- }
- type KnowledgeClassifyListResp struct {
- List []*KnowledgeClassifyItem
- }
- type CheckDeleteClassifyResp struct {
- Code int `description:"编码:0:检测成功,可进行删除,1:分类不存在,2:该分类有关联报告,不允许删除,3:二级分类有关联报告,不允许删除,4:该分类下有关联分类,是否确认全部删除"`
- Msg string `description:"描述信息"`
- }
- type ClassifyAddReq struct {
- ClassifyName string `description:"分类名称"`
- ParentId int `description:"父级分类id,没有父级分类传0"`
- ResourceType int `description:"分类类型:0事件库,1政策库,2观点库,3知识库"`
- }
- type DeleteClassifyReq struct {
- ClassifyId int `description:"分类ID"`
- ResourceType int `description:"分类类型:0事件库,1政策库,2观点库,3知识库"`
- }
- type EditClassifyReq struct {
- ClassifyId int `description:"分类ID"`
- ClassifyAddReq
- }
- type FindByIdClassifyReq struct {
- ClassifyId int `description:"分类ID"`
- }
- // EditClassifyPermissionReq 编辑分类权限请求
- type EditClassifyPermissionReq struct {
- ClassifyId int `description:"分类ID"`
- ChartPermissionIdList []int `description:"权限id数组"`
- }
- // GetAllClassify 获取所有分类
- func (k *KnowledgeClassify) GetAllClassify(resourceType int) (list []*KnowledgeClassify, err error) {
- sql := ` SELECT * FROM %s where resource_type=?`
- sql = fmt.Sprintf(sql, k.TableName())
- err = global.DmSQL["rddp"].Raw(sql, resourceType).Find(&list).Error
- return
- }
- // UpdateClassify 更新分类
- func (k *KnowledgeClassify) Update(cols []string) (err error) {
- err = global.DmSQL["rddp"].Select(cols).Updates(k).Error
- return
- }
- // SimpleClassifyList 简版分类列表
- type SimpleClassifyList struct {
- Id int `description:"分类ID"`
- ClassifyName string `description:"分类名称"`
- ParentId int `description:"父级ID"`
- Sort int `description:"排序"`
- Child []*SimpleClassifyList
- }
- // UpdateClassifySortByParentId 根据父类id更新排序
- func (k *KnowledgeClassify) UpdateClassifySortByParentId(parentId, permissionId, nowSort int, updateSort string, resourceType int) (err error) {
- sql := ` update %s set sort = ` + updateSort + ` WHERE parent_id=? AND resource_type=? AND sort > ? `
- if permissionId > 0 {
- sql += ` or ( classify_id > ` + fmt.Sprint(permissionId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
- }
- sql = fmt.Sprintf(sql, k.TableName())
- err = global.DmSQL["rddp"].Exec(sql, parentId, resourceType, nowSort).Error
- return
- }
- // GetMaxSortByParentId 获取最大的排序值
- func (k *KnowledgeClassify) GetMaxSortByParentId(parentId int) (maxSort int, err error) {
- sql := `SELECT COALESCE(MAX(sort),0) AS sort FROM %s WHERE parent_id = ? `
- sql = fmt.Sprintf(sql, k.TableName())
- err = global.DmSQL["rddp"].Raw(sql, parentId).Scan(&maxSort).Error
- return
- }
- // GetMaxSort 获取最大的排序值
- func (k *KnowledgeClassify) GetMaxSort() (maxSort int, err error) {
- sql := `SELECT COALESCE(MAX(sort),0) AS sort FROM %s`
- sql = fmt.Sprintf(sql, k.TableName())
- err = global.DmSQL["rddp"].Raw(sql).Scan(&maxSort).Error
- return
- }
- // GetFirstClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
- func (k *KnowledgeClassify) GetFirstClassifyByParentId(parentId int) (item *KnowledgeClassify, err error) {
- sql := `SELECT * FROM %s WHERE parent_id = ? order by sort asc, classify_id asc limit 1`
- sql = fmt.Sprintf(sql, k.TableName())
- err = global.DmSQL["rddp"].Raw(sql, parentId).First(&item).Error
- return
- }
- type ClassifyMoveReq struct {
- ClassifyId int `description:"分类ID"`
- PrevClassifyId int `description:"上一个兄弟节点分类id"`
- NextClassifyId int `description:"下一个兄弟节点分类id"`
- }
- type ClassifySetEnabledReq struct {
- ClassifyId int `description:"分类ID"`
- Enabled int `description:"是否可用,1可用,0禁用"`
- }
- // GetCountClassifyChildByParentId
- // @Description: 获取父级分类下子分类数量
- // @author: Roc
- // @datetime 2024-06-17 10:58:46
- // @param parentId int
- // @return total int
- // @return err error
- func (k *KnowledgeClassify) GetCountClassifyChildByParentId(parentId int) (total int, err error) {
- sql := fmt.Sprintf(`SELECT count(1) AS total FROM %s WHERE parent_id = ? `, k.TableName())
- err = global.DmSQL["rddp"].Raw(sql, parentId).Scan(&total).Error
- return
- }
- // GetClassifyListByKeyword
- // @Description: 获取分类列表
- // @author: Roc
- // @datetime 2024-06-19 09:49:33
- // @param keyWord string
- // @param enabled int
- // @return items []*KnowledgeClassifyList
- // @return err error
- func (k *KnowledgeClassify) GetClassifyListByKeyword(keyWord string, resourceType int) (items []*KnowledgeClassifyItem, err error) {
- sql := ``
- pars := make([]interface{}, 0)
- sql = `SELECT * FROM %s WHERE resource_type=? `
- pars = append(pars, resourceType)
- if keyWord != `` {
- sql += ` AND classify_name LIKE ? `
- pars = utils.GetLikeKeywordPars(pars, keyWord, 1)
- }
- sql += ` ORDER BY sort ASC, create_time ASC`
- sql = fmt.Sprintf(sql, k.TableName())
- err = global.DmSQL["rddp"].Raw(sql, pars...).Find(&items).Error
- return
- }
- // GetClassifyListByParentIdList
- // @Description: 获取分类列表
- // @author: Roc
- // @datetime 2024-06-19 09:49:33
- // @param keyWord string
- // @param enabled int
- // @return items []*KnowledgeClassifyList
- // @return err error
- func (k *KnowledgeClassify) GetClassifyListByParentIdList(parentClassifyIdList []int) (items []*KnowledgeClassifyItem, err error) {
- num := len(parentClassifyIdList)
- if num <= 0 {
- return
- }
- sql := `SELECT * FROM %s WHERE classify_id in (?) ORDER BY sort ASC, create_time ASC`
- sql = fmt.Sprintf(sql, k.TableName())
- err = global.DmSQL["rddp"].Raw(sql, parentClassifyIdList).Find(&items).Error
- return
- }
- type KnowledgeFullClassify struct {
- KnowledgeClassify
- ParentName string `gorm:"column:parent_name"`
- RootName string `gorm:"column:root_name"`
- }
- func GetFullClassifyListByIdList(classifyIdList []int, resourceType int) (items []*KnowledgeFullClassify, err error) {
- num := len(classifyIdList)
- if num <= 0 {
- return
- }
- sql := `SELECT kc1.*, kc2.classify_name AS parent_name, kc3.classify_name AS root_name
- FROM knowledge_classify kc1
- LEFT JOIN knowledge_classify kc2 ON kc1.parent_id = kc2.classify_id
- LEFT JOIN knowledge_classify kc3 ON kc2.parent_id = kc3.classify_id WHERE kc1.classify_id IN (?) and kc1.resource_type=?`
- err = global.DmSQL["rddp"].Raw(sql, classifyIdList, resourceType).Find(&items).Error
- return
- }
- func GetFullClassifyListByName(classifyNames []string, resourceType int) (items []*KnowledgeFullClassify, err error) {
- num := len(classifyNames)
- if num <= 0 {
- return
- }
- sql := `SELECT kc1.*, kc2.classify_name AS parent_name, kc3.classify_name AS root_name
- FROM knowledge_classify kc1
- LEFT JOIN knowledge_classify kc2 ON kc1.parent_id = kc2.classify_id
- LEFT JOIN knowledge_classify kc3 ON kc2.parent_id = kc3.classify_id WHERE kc1.classify_name in (?) and kc1.resource_type=? `
- err = global.DmSQL["rddp"].Raw(sql, classifyNames, resourceType).Find(&items).Error
- return
- }
|