knowledge_classify.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package knowledge
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "time"
  7. )
  8. type KnowledgeClassify struct {
  9. ClassifyId int `gorm:"primaryKey;column:classify_id"`
  10. ClassifyName string `gorm:"column:classify_name;default:'';type:varchar(125);not null"` // 注意:varchar的默认长度可能需要根据实际情况调整
  11. Sort int `gorm:"column:sort;default:0;type:tinyint"`
  12. ParentId int `gorm:"column:parent_id;default:0;type:int"`
  13. CreateTime time.Time `gorm:"column:create_time;default:CURRENT_TIMESTAMP"`
  14. ModifyTime time.Time `gorm:"column:modify_time;default:CURRENT_TIMESTAMP"`
  15. Enabled int `gorm:"column:enabled;default:1;type:tinyint"`
  16. Level int `gorm:"column:level;default:0;type:bigint"`
  17. ResourceType int `gorm:"column:resource_type;default:0;not null;type:tinyint"`
  18. }
  19. func (k *KnowledgeClassify) TableName() string {
  20. return "knowledge_classify"
  21. }
  22. func (k *KnowledgeClassify) GetPrimaryKey() string {
  23. return "classify_id"
  24. }
  25. func (k *KnowledgeClassify) GetClassifyByName(classifyName string, parentId, resourceType int) (item *KnowledgeClassify, err error) {
  26. sql := fmt.Sprintf(`SELECT * FROM %s WHERE classify_name=? AND parent_id=? AND resource_type=?`, k.TableName())
  27. err = global.DmSQL["rddp"].Raw(sql, classifyName, parentId, resourceType).First(&item).Error
  28. return
  29. }
  30. func (k *KnowledgeClassify) GetClassifyById(classifyId int) (item *KnowledgeClassify, err error) {
  31. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s=? `, k.TableName(), k.GetPrimaryKey())
  32. err = global.DmSQL["rddp"].Raw(sql, classifyId).First(&item).Error
  33. return
  34. }
  35. // 添加分类
  36. func (k *KnowledgeClassify) Add(item *KnowledgeClassify) (err error) {
  37. err = global.DmSQL["rddp"].Create(item).Error
  38. return
  39. }
  40. func (k *KnowledgeClassify) GetResourceCountByClassifyId(classifyId int) (count int, err error) {
  41. sql := `SELECT COUNT(1) as num FROM knowledge_resource WHERE classify_id=? `
  42. err = global.DmSQL["rddp"].Raw(sql, classifyId).First(&count).Error
  43. return
  44. }
  45. // GetClassifySubCountByClassifyId 查询当前分类下的子分类是否关联事件
  46. func (k *KnowledgeClassify) GetClassifySubCountByClassifyId(classifyId int) (count int, err error) {
  47. sql := `SELECT COUNT(1) as num FROM knowledge_classify AS a
  48. INNER JOIN knowledge_resource AS b ON a.classify_id=b.classify_id
  49. WHERE a.parent_id=? `
  50. err = global.DmSQL["rddp"].Raw(sql, classifyId).First(&count).Error
  51. return
  52. }
  53. func (k *KnowledgeClassify) GetClassifySubCountByParentId(classifyId int) (count int, err error) {
  54. sqlCount := `
  55. SELECT COUNT(1) as num FROM %s AS a
  56. WHERE a.parent_id=? `
  57. sqlCount = fmt.Sprintf(sqlCount, k.TableName())
  58. err = global.DmSQL["rddp"].Raw(sqlCount, classifyId).Scan(&count).Error
  59. return
  60. }
  61. func (k *KnowledgeClassify) GetChildClassifyIdByParentId(classifyId int) (ids []int, err error) {
  62. sqlCount := `
  63. SELECT classify_id FROM %s AS a
  64. WHERE a.parent_id=? `
  65. sqlCount = fmt.Sprintf(sqlCount, k.TableName())
  66. err = global.DmSQL["rddp"].Raw(sqlCount, classifyId).Scan(&ids).Error
  67. return
  68. }
  69. func (k *KnowledgeClassify) GetChildClassifyIdByParentIds(classifyIds []int) (ids []int, err error) {
  70. sqlCount := `
  71. SELECT classify_id FROM %s AS a
  72. WHERE a.parent_id in (?) `
  73. sqlCount = fmt.Sprintf(sqlCount, k.TableName())
  74. err = global.DmSQL["rddp"].Raw(sqlCount, classifyIds).Scan(&ids).Error
  75. return
  76. }
  77. // 删除分类
  78. func (k *KnowledgeClassify) DeleteByIds(classifyIds []int) (err error) {
  79. sql := `DELETE FROM %s WHERE classify_id in (?) `
  80. sql = fmt.Sprintf(sql, k.TableName())
  81. err = global.DmSQL["rddp"].Exec(sql, classifyIds).Error
  82. if err != nil {
  83. return
  84. }
  85. return
  86. }
  87. type KnowledgeClassifyItem struct {
  88. KnowledgeClassify
  89. Child []*KnowledgeClassifyItem `gorm:"-"`
  90. }
  91. type KnowledgeClassifyListResp struct {
  92. List []*KnowledgeClassifyItem
  93. }
  94. type CheckDeleteClassifyResp struct {
  95. Code int `description:"编码:0:检测成功,可进行删除,1:分类不存在,2:该分类有关联报告,不允许删除,3:二级分类有关联报告,不允许删除,4:该分类下有关联分类,是否确认全部删除"`
  96. Msg string `description:"描述信息"`
  97. }
  98. type ClassifyAddReq struct {
  99. ClassifyName string `description:"分类名称"`
  100. ParentId int `description:"父级分类id,没有父级分类传0"`
  101. ResourceType int `description:"分类类型:0事件库,1政策库,2观点库,3知识库"`
  102. }
  103. type DeleteClassifyReq struct {
  104. ClassifyId int `description:"分类ID"`
  105. ResourceType int `description:"分类类型:0事件库,1政策库,2观点库,3知识库"`
  106. }
  107. type EditClassifyReq struct {
  108. ClassifyId int `description:"分类ID"`
  109. ClassifyAddReq
  110. }
  111. type FindByIdClassifyReq struct {
  112. ClassifyId int `description:"分类ID"`
  113. }
  114. // EditClassifyPermissionReq 编辑分类权限请求
  115. type EditClassifyPermissionReq struct {
  116. ClassifyId int `description:"分类ID"`
  117. ChartPermissionIdList []int `description:"权限id数组"`
  118. }
  119. // GetAllClassify 获取所有分类
  120. func (k *KnowledgeClassify) GetAllClassify(resourceType int) (list []*KnowledgeClassify, err error) {
  121. sql := ` SELECT * FROM %s where resource_type=?`
  122. sql = fmt.Sprintf(sql, k.TableName())
  123. err = global.DmSQL["rddp"].Raw(sql, resourceType).Find(&list).Error
  124. return
  125. }
  126. // UpdateClassify 更新分类
  127. func (k *KnowledgeClassify) Update(cols []string) (err error) {
  128. err = global.DmSQL["rddp"].Select(cols).Updates(k).Error
  129. return
  130. }
  131. // SimpleClassifyList 简版分类列表
  132. type SimpleClassifyList struct {
  133. Id int `description:"分类ID"`
  134. ClassifyName string `description:"分类名称"`
  135. ParentId int `description:"父级ID"`
  136. Sort int `description:"排序"`
  137. Child []*SimpleClassifyList
  138. }
  139. // UpdateClassifySortByParentId 根据父类id更新排序
  140. func (k *KnowledgeClassify) UpdateClassifySortByParentId(parentId, permissionId, nowSort int, updateSort string, resourceType int) (err error) {
  141. sql := ` update %s set sort = ` + updateSort + ` WHERE parent_id=? AND resource_type=? AND sort > ? `
  142. if permissionId > 0 {
  143. sql += ` or ( classify_id > ` + fmt.Sprint(permissionId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  144. }
  145. sql = fmt.Sprintf(sql, k.TableName())
  146. err = global.DmSQL["rddp"].Exec(sql, parentId, resourceType, nowSort).Error
  147. return
  148. }
  149. // GetMaxSortByParentId 获取最大的排序值
  150. func (k *KnowledgeClassify) GetMaxSortByParentId(parentId int) (maxSort int, err error) {
  151. sql := `SELECT COALESCE(MAX(sort),0) AS sort FROM %s WHERE parent_id = ? `
  152. sql = fmt.Sprintf(sql, k.TableName())
  153. err = global.DmSQL["rddp"].Raw(sql, parentId).Scan(&maxSort).Error
  154. return
  155. }
  156. // GetMaxSort 获取最大的排序值
  157. func (k *KnowledgeClassify) GetMaxSort() (maxSort int, err error) {
  158. sql := `SELECT COALESCE(MAX(sort),0) AS sort FROM %s`
  159. sql = fmt.Sprintf(sql, k.TableName())
  160. err = global.DmSQL["rddp"].Raw(sql).Scan(&maxSort).Error
  161. return
  162. }
  163. // GetFirstClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
  164. func (k *KnowledgeClassify) GetFirstClassifyByParentId(parentId int) (item *KnowledgeClassify, err error) {
  165. sql := `SELECT * FROM %s WHERE parent_id = ? order by sort asc, classify_id asc limit 1`
  166. sql = fmt.Sprintf(sql, k.TableName())
  167. err = global.DmSQL["rddp"].Raw(sql, parentId).First(&item).Error
  168. return
  169. }
  170. type ClassifyMoveReq struct {
  171. ClassifyId int `description:"分类ID"`
  172. PrevClassifyId int `description:"上一个兄弟节点分类id"`
  173. NextClassifyId int `description:"下一个兄弟节点分类id"`
  174. }
  175. type ClassifySetEnabledReq struct {
  176. ClassifyId int `description:"分类ID"`
  177. Enabled int `description:"是否可用,1可用,0禁用"`
  178. }
  179. // GetCountClassifyChildByParentId
  180. // @Description: 获取父级分类下子分类数量
  181. // @author: Roc
  182. // @datetime 2024-06-17 10:58:46
  183. // @param parentId int
  184. // @return total int
  185. // @return err error
  186. func (k *KnowledgeClassify) GetCountClassifyChildByParentId(parentId int) (total int, err error) {
  187. sql := fmt.Sprintf(`SELECT count(1) AS total FROM %s WHERE parent_id = ? `, k.TableName())
  188. err = global.DmSQL["rddp"].Raw(sql, parentId).Scan(&total).Error
  189. return
  190. }
  191. // GetClassifyListByKeyword
  192. // @Description: 获取分类列表
  193. // @author: Roc
  194. // @datetime 2024-06-19 09:49:33
  195. // @param keyWord string
  196. // @param enabled int
  197. // @return items []*KnowledgeClassifyList
  198. // @return err error
  199. func (k *KnowledgeClassify) GetClassifyListByKeyword(keyWord string, resourceType int) (items []*KnowledgeClassifyItem, err error) {
  200. sql := ``
  201. pars := make([]interface{}, 0)
  202. sql = `SELECT * FROM %s WHERE resource_type=? `
  203. pars = append(pars, resourceType)
  204. if keyWord != `` {
  205. sql += ` AND classify_name LIKE ? `
  206. pars = utils.GetLikeKeywordPars(pars, keyWord, 1)
  207. }
  208. sql += ` ORDER BY sort ASC, create_time ASC`
  209. sql = fmt.Sprintf(sql, k.TableName())
  210. err = global.DmSQL["rddp"].Raw(sql, pars...).Find(&items).Error
  211. return
  212. }
  213. // GetClassifyListByParentIdList
  214. // @Description: 获取分类列表
  215. // @author: Roc
  216. // @datetime 2024-06-19 09:49:33
  217. // @param keyWord string
  218. // @param enabled int
  219. // @return items []*KnowledgeClassifyList
  220. // @return err error
  221. func (k *KnowledgeClassify) GetClassifyListByParentIdList(parentClassifyIdList []int) (items []*KnowledgeClassifyItem, err error) {
  222. num := len(parentClassifyIdList)
  223. if num <= 0 {
  224. return
  225. }
  226. sql := `SELECT * FROM %s WHERE classify_id in (?) ORDER BY sort ASC, create_time ASC`
  227. sql = fmt.Sprintf(sql, k.TableName())
  228. err = global.DmSQL["rddp"].Raw(sql, parentClassifyIdList).Find(&items).Error
  229. return
  230. }
  231. type KnowledgeFullClassify struct {
  232. KnowledgeClassify
  233. ParentName string `gorm:"column:parent_name"`
  234. RootName string `gorm:"column:root_name"`
  235. }
  236. func GetFullClassifyListByIdList(classifyIdList []int, resourceType int) (items []*KnowledgeFullClassify, err error) {
  237. num := len(classifyIdList)
  238. if num <= 0 {
  239. return
  240. }
  241. sql := `SELECT kc1.*, kc2.classify_name AS parent_name, kc3.classify_name AS root_name
  242. FROM knowledge_classify kc1
  243. LEFT JOIN knowledge_classify kc2 ON kc1.parent_id = kc2.classify_id
  244. LEFT JOIN knowledge_classify kc3 ON kc2.parent_id = kc3.classify_id WHERE kc1.classify_id IN (?) and kc1.resource_type=?`
  245. err = global.DmSQL["rddp"].Raw(sql, classifyIdList, resourceType).Find(&items).Error
  246. return
  247. }
  248. func GetFullClassifyListByName(classifyNames []string, resourceType int) (items []*KnowledgeFullClassify, err error) {
  249. num := len(classifyNames)
  250. if num <= 0 {
  251. return
  252. }
  253. sql := `SELECT kc1.*, kc2.classify_name AS parent_name, kc3.classify_name AS root_name
  254. FROM knowledge_classify kc1
  255. LEFT JOIN knowledge_classify kc2 ON kc1.parent_id = kc2.classify_id
  256. LEFT JOIN knowledge_classify kc3 ON kc2.parent_id = kc3.classify_id WHERE kc1.classify_name in (?) and kc1.resource_type=? `
  257. err = global.DmSQL["rddp"].Raw(sql, classifyNames, resourceType).Find(&items).Error
  258. return
  259. }