knowledge_classify.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. func (k *KnowledgeClassify) Add(item *KnowledgeClassify) (err error) {
  36. err = global.DmSQL["rddp"].Create(item).Error
  37. return
  38. }
  39. func (k *KnowledgeClassify) GetResourceCountByClassifyId(classifyId int) (count int, err error) {
  40. sql := `SELECT COUNT(1) as num FROM knowledge_resource WHERE classify_id=? `
  41. err = global.DmSQL["rddp"].Raw(sql, classifyId).First(&count).Error
  42. return
  43. }
  44. func (k *KnowledgeClassify) GetClassifySubCountByClassifyId(classifyId int) (count int, err error) {
  45. sql := `SELECT COUNT(1) as num FROM knowledge_classify AS a
  46. INNER JOIN knowledge_resource AS b ON a.classify_id=b.classify_id
  47. WHERE a.parent_id=? `
  48. err = global.DmSQL["rddp"].Raw(sql, classifyId).First(&count).Error
  49. return
  50. }
  51. func (k *KnowledgeClassify) GetClassifySubCountByParentId(classifyId int) (count int, err error) {
  52. sqlCount := `
  53. SELECT COUNT(1) as num FROM %s AS a
  54. WHERE a.parent_id=? `
  55. sqlCount = fmt.Sprintf(sqlCount, k.TableName())
  56. err = global.DmSQL["rddp"].Raw(sqlCount, classifyId).Scan(&count).Error
  57. return
  58. }
  59. func (k *KnowledgeClassify) GetChildClassifyIdByParentId(classifyId int) (ids []int, err error) {
  60. sqlCount := `
  61. SELECT classify_id FROM %s AS a
  62. WHERE a.parent_id=? `
  63. sqlCount = fmt.Sprintf(sqlCount, k.TableName())
  64. err = global.DmSQL["rddp"].Raw(sqlCount, classifyId).Scan(&ids).Error
  65. return
  66. }
  67. func (k *KnowledgeClassify) GetChildClassifyIdByParentIds(classifyIds []int) (ids []int, err error) {
  68. sqlCount := `
  69. SELECT classify_id FROM %s AS a
  70. WHERE a.parent_id in (?) `
  71. sqlCount = fmt.Sprintf(sqlCount, k.TableName())
  72. err = global.DmSQL["rddp"].Raw(sqlCount, classifyIds).Scan(&ids).Error
  73. return
  74. }
  75. func (k *KnowledgeClassify) DeleteByIds(classifyIds []int) (err error) {
  76. sql := `DELETE FROM %s WHERE classify_id in (?) `
  77. sql = fmt.Sprintf(sql, k.TableName())
  78. err = global.DmSQL["rddp"].Exec(sql, classifyIds).Error
  79. if err != nil {
  80. return
  81. }
  82. return
  83. }
  84. type KnowledgeClassifyItem struct {
  85. KnowledgeClassify
  86. Child []*KnowledgeClassifyItem `gorm:"-"`
  87. }
  88. type KnowledgeClassifyListResp struct {
  89. List []*KnowledgeClassifyItem
  90. }
  91. type CheckDeleteClassifyResp struct {
  92. Code int `description:"编码:0:检测成功,可进行删除,1:分类不存在,2:该分类有关联报告,不允许删除,3:二级分类有关联报告,不允许删除,4:该分类下有关联分类,是否确认全部删除"`
  93. Msg string `description:"描述信息"`
  94. }
  95. type ClassifyAddReq struct {
  96. ClassifyName string `description:"分类名称"`
  97. ParentId int `description:"父级分类id,没有父级分类传0"`
  98. ResourceType int `description:"分类类型:0事件库,1政策库,2观点库,3知识库"`
  99. }
  100. type DeleteClassifyReq struct {
  101. ClassifyId int `description:"分类ID"`
  102. ResourceType int `description:"分类类型:0事件库,1政策库,2观点库,3知识库"`
  103. }
  104. type EditClassifyReq struct {
  105. ClassifyId int `description:"分类ID"`
  106. ClassifyAddReq
  107. }
  108. type FindByIdClassifyReq struct {
  109. ClassifyId int `description:"分类ID"`
  110. }
  111. type EditClassifyPermissionReq struct {
  112. ClassifyId int `description:"分类ID"`
  113. ChartPermissionIdList []int `description:"权限id数组"`
  114. }
  115. func (k *KnowledgeClassify) GetAllClassify(resourceType int) (list []*KnowledgeClassify, err error) {
  116. sql := ` SELECT * FROM %s where resource_type=?`
  117. sql = fmt.Sprintf(sql, k.TableName())
  118. err = global.DmSQL["rddp"].Raw(sql, resourceType).Find(&list).Error
  119. return
  120. }
  121. func (k *KnowledgeClassify) Update(cols []string) (err error) {
  122. err = global.DmSQL["rddp"].Select(cols).Updates(k).Error
  123. return
  124. }
  125. type SimpleClassifyList struct {
  126. Id int `description:"分类ID"`
  127. ClassifyName string `description:"分类名称"`
  128. ParentId int `description:"父级ID"`
  129. Sort int `description:"排序"`
  130. Child []*SimpleClassifyList
  131. }
  132. func (k *KnowledgeClassify) UpdateClassifySortByParentId(parentId, permissionId, nowSort int, updateSort string, resourceType int) (err error) {
  133. sql := ` update %s set sort = ` + updateSort + ` WHERE parent_id=? AND resource_type=? AND sort > ? `
  134. if permissionId > 0 {
  135. sql += ` or ( classify_id > ` + fmt.Sprint(permissionId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  136. }
  137. sql = fmt.Sprintf(sql, k.TableName())
  138. err = global.DmSQL["rddp"].Exec(sql, parentId, resourceType, nowSort).Error
  139. return
  140. }
  141. func (k *KnowledgeClassify) GetMaxSortByParentId(parentId int) (maxSort int, err error) {
  142. sql := `SELECT COALESCE(MAX(sort),0) AS sort FROM %s WHERE parent_id = ? `
  143. sql = fmt.Sprintf(sql, k.TableName())
  144. err = global.DmSQL["rddp"].Raw(sql, parentId).Scan(&maxSort).Error
  145. return
  146. }
  147. func (k *KnowledgeClassify) GetMaxSort() (maxSort int, err error) {
  148. sql := `SELECT COALESCE(MAX(sort),0) AS sort FROM %s`
  149. sql = fmt.Sprintf(sql, k.TableName())
  150. err = global.DmSQL["rddp"].Raw(sql).Scan(&maxSort).Error
  151. return
  152. }
  153. func (k *KnowledgeClassify) GetFirstClassifyByParentId(parentId int) (item *KnowledgeClassify, err error) {
  154. sql := `SELECT * FROM %s WHERE parent_id = ? order by sort asc, classify_id asc limit 1`
  155. sql = fmt.Sprintf(sql, k.TableName())
  156. err = global.DmSQL["rddp"].Raw(sql, parentId).First(&item).Error
  157. return
  158. }
  159. type ClassifyMoveReq struct {
  160. ClassifyId int `description:"分类ID"`
  161. PrevClassifyId int `description:"上一个兄弟节点分类id"`
  162. NextClassifyId int `description:"下一个兄弟节点分类id"`
  163. }
  164. type ClassifySetEnabledReq struct {
  165. ClassifyId int `description:"分类ID"`
  166. Enabled int `description:"是否可用,1可用,0禁用"`
  167. }
  168. func (k *KnowledgeClassify) GetCountClassifyChildByParentId(parentId int) (total int, err error) {
  169. sql := fmt.Sprintf(`SELECT count(1) AS total FROM %s WHERE parent_id = ? `, k.TableName())
  170. err = global.DmSQL["rddp"].Raw(sql, parentId).Scan(&total).Error
  171. return
  172. }
  173. func (k *KnowledgeClassify) GetClassifyListByKeyword(keyWord string, resourceType int) (items []*KnowledgeClassifyItem, err error) {
  174. sql := ``
  175. pars := make([]interface{}, 0)
  176. sql = `SELECT * FROM %s WHERE resource_type=? `
  177. pars = append(pars, resourceType)
  178. if keyWord != `` {
  179. sql += ` AND classify_name LIKE ? `
  180. pars = utils.GetLikeKeywordPars(pars, keyWord, 1)
  181. }
  182. sql += ` ORDER BY sort ASC, create_time ASC`
  183. sql = fmt.Sprintf(sql, k.TableName())
  184. err = global.DmSQL["rddp"].Raw(sql, pars...).Find(&items).Error
  185. return
  186. }
  187. func (k *KnowledgeClassify) GetClassifyListByParentIdList(parentClassifyIdList []int) (items []*KnowledgeClassifyItem, err error) {
  188. num := len(parentClassifyIdList)
  189. if num <= 0 {
  190. return
  191. }
  192. sql := `SELECT * FROM %s WHERE classify_id in (?) ORDER BY sort ASC, create_time ASC`
  193. sql = fmt.Sprintf(sql, k.TableName())
  194. err = global.DmSQL["rddp"].Raw(sql, parentClassifyIdList).Find(&items).Error
  195. return
  196. }
  197. type KnowledgeFullClassify struct {
  198. KnowledgeClassify
  199. ParentName string `gorm:"column:parent_name"`
  200. RootName string `gorm:"column:root_name"`
  201. }
  202. func GetFullClassifyListByIdList(classifyIdList []int, resourceType int) (items []*KnowledgeFullClassify, err error) {
  203. num := len(classifyIdList)
  204. if num <= 0 {
  205. return
  206. }
  207. sql := `SELECT kc1.*, kc2.classify_name AS parent_name, kc3.classify_name AS root_name
  208. FROM knowledge_classify kc1
  209. LEFT JOIN knowledge_classify kc2 ON kc1.parent_id = kc2.classify_id
  210. LEFT JOIN knowledge_classify kc3 ON kc2.parent_id = kc3.classify_id WHERE kc1.classify_id IN (?) and kc1.resource_type=?`
  211. err = global.DmSQL["rddp"].Raw(sql, classifyIdList, resourceType).Find(&items).Error
  212. return
  213. }
  214. func GetFullClassifyListByName(classifyNames []string, resourceType int) (items []*KnowledgeFullClassify, err error) {
  215. num := len(classifyNames)
  216. if num <= 0 {
  217. return
  218. }
  219. sql := `SELECT kc1.*, kc2.classify_name AS parent_name, kc3.classify_name AS root_name
  220. FROM knowledge_classify kc1
  221. LEFT JOIN knowledge_classify kc2 ON kc1.parent_id = kc2.classify_id
  222. LEFT JOIN knowledge_classify kc3 ON kc2.parent_id = kc3.classify_id WHERE kc1.classify_name in (?) and kc1.resource_type=? `
  223. err = global.DmSQL["rddp"].Raw(sql, classifyNames, resourceType).Find(&items).Error
  224. return
  225. }