knowledge_resource.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package models
  2. import (
  3. "eta_gn/eta_report/global"
  4. "eta_gn/eta_report/utils"
  5. "time"
  6. )
  7. const (
  8. // 事件类型-事件库
  9. KnowledgeResourceTypeEvent = iota
  10. // 事件-政策库
  11. KnowledgeResourceTypePolicy
  12. // 事件-报告库
  13. KnowledgeResourceTypeReport
  14. // 事件类型-知识库
  15. KnowledgeResourceTypeKnow
  16. // 事件-观点库
  17. KnowledgeResourceTypeOpinion
  18. )
  19. const (
  20. // 未发布
  21. KnowledgeResourceStateUnpublished = iota
  22. // 已发布
  23. KnowledgeResourceStatePublished
  24. // 待审批
  25. KnowledgeResourceStatePending
  26. // 已驳回
  27. KnowledgeResourceStateRejected
  28. // 已通过
  29. KnowledgeResourceStateApproved
  30. )
  31. const (
  32. // 内部
  33. KnowledgeOutSourceInternal = iota
  34. // 智力共享
  35. KnowledgeOutSourceIntelligence
  36. // 数据节点
  37. KnowledgeOutSourceDataNode
  38. )
  39. type KnowledgeResourceFile struct {
  40. KnowledgeResourceFileId int `gorm:"column:knowledge_resource_file_id;primaryKey;autoIncrement"`
  41. KnowledgeResourceId int `gorm:"column:knowledge_resource_id"`
  42. FileUrl string `gorm:"column:file_url"`
  43. }
  44. func (k KnowledgeResourceFile) TableName() string {
  45. return "knowledge_resource_file"
  46. }
  47. type KnowledgeResource struct {
  48. KnowledgeResourceId int `gorm:"column:knowledge_resource_id;;primaryKey;autoIncrement"`
  49. ResourceType int `gorm:"column:resource_type;"`
  50. ClassifyId int `gorm:"column:classify_id"`
  51. Title string `gorm:"column:title;"`
  52. CreateTime time.Time `gorm:"column:create_time" description:"创建时间"`
  53. ModifyTime time.Time `gorm:"column:modify_time;autoUpdateTime" description:"修改时间"`
  54. State int `gorm:"column:state" description:"0:未发布;1:已发布;2:待审批;3:已驳回;4:已通过"`
  55. Content string `gorm:"column:content"`
  56. ResourceCode string `gorm:"column:resource_code"`
  57. AdminId int `gorm:"column:admin_id" description:"创建者账号"`
  58. AdminRealName string `gorm:"column:admin_real_name" description:"创建者姓名"`
  59. SourceFrom string `gorm:"column:source_from"`
  60. TagId int `gorm:"column:tag_id;default:0;NOT NULL"`
  61. StartTime *time.Time `gorm:"column:start_time"`
  62. EndTime *time.Time `gorm:"column:end_time"`
  63. IsFile int `gorm:"column:is_file;default:0;NOT NULL"`
  64. FileUrl string `gorm:"column:file_url"`
  65. OutId int `gorm:"column:out_id" description:"外部系统ID"`
  66. IsDelete int `gorm:"column:is_delete;default:0;NOT NULL"`
  67. OutSource int `gorm:"column:out_source" description:"外部来源:0:内部 1:智力共享 2:数据节点"`
  68. }
  69. func (k KnowledgeResource) TableName() string {
  70. return "knowledge_resource"
  71. }
  72. func (k *KnowledgeResource) Create(fileUrl []string) (err error) {
  73. tx := global.DEFAULT_DmSQL.Begin()
  74. defer func() {
  75. if err != nil {
  76. tx.Rollback()
  77. } else {
  78. tx.Commit()
  79. }
  80. }()
  81. err = tx.Create(k).Error
  82. if len(fileUrl) > 0 {
  83. addFile := make([]*KnowledgeResourceFile, 0, len(fileUrl))
  84. for _, url := range fileUrl {
  85. file := new(KnowledgeResourceFile)
  86. file.KnowledgeResourceId = k.KnowledgeResourceId
  87. file.FileUrl = url
  88. addFile = append(addFile, file)
  89. }
  90. err = tx.CreateInBatches(addFile, utils.MultiAddNum).Error
  91. }
  92. return
  93. }
  94. func (k *KnowledgeResource) Update(cols []string) error {
  95. return global.DEFAULT_DmSQL.Model(k).Select(cols).Updates(k).Error
  96. }
  97. // GetKnowledgeResourceByOutId 根据外部系统ID获取知识资源
  98. func (k *KnowledgeResource) GetKnowledgeResourceByOutId(outId int) (item *KnowledgeResource, err error) {
  99. sql := "SELECT * FROM knowledge_resource WHERE out_id = ?"
  100. err = global.DEFAULT_DmSQL.Raw(sql, outId).First(&item).Error
  101. return
  102. }
  103. type KnowledgeClassify struct {
  104. ClassifyId int `gorm:"primaryKey;column:classify_id"`
  105. ClassifyName string `gorm:"column:classify_name;default:'';type:varchar(125);not null"` // 注意:varchar的默认长度可能需要根据实际情况调整
  106. Sort int `gorm:"column:sort;default:0;type:tinyint"`
  107. ParentId int `gorm:"column:parent_id;default:0;type:int"`
  108. CreateTime time.Time `gorm:"column:create_time;default:CURRENT_TIMESTAMP"`
  109. ModifyTime time.Time `gorm:"column:modify_time;default:CURRENT_TIMESTAMP"`
  110. Enabled int `gorm:"column:enabled;default:1;type:tinyint"`
  111. Level int `gorm:"column:level;default:0;type:bigint"`
  112. ResourceType int `gorm:"column:resource_type;default:0;not null;type:tinyint"`
  113. }
  114. func (k KnowledgeClassify) TableName() string {
  115. return "knowledge_classify"
  116. }
  117. func (k *KnowledgeClassify) Create() error {
  118. return global.DEFAULT_DmSQL.Create(k).Error
  119. }
  120. func (k *KnowledgeClassify) GetClassifyByNameTypeAndParentId(classifyName string, resourceType, parentId int) (items *KnowledgeClassify, err error) {
  121. sql := "SELECT * FROM knowledge_classify WHERE classify_name = ? AND resource_type = ? AND parent_id = ?"
  122. err = global.DEFAULT_DmSQL.Raw(sql, classifyName, resourceType, parentId).First(&items).Error
  123. return
  124. }
  125. func (k *KnowledgeClassify) GetClassifysByNameAndType(classifyName string, resourceType int) (item *KnowledgeClassify, err error) {
  126. sql := "SELECT * FROM knowledge_classify WHERE classify_name = ? AND resource_type = ?"
  127. err = global.DEFAULT_DmSQL.Raw(sql, classifyName, resourceType).Find(&item).Error
  128. return
  129. }
  130. func (k *KnowledgeClassify) GetChildClassifyIdByNamePath(resourceType int, classifyNameFirst string, classifyNameSecond string, classifyNameThird string) (id int, err error) {
  131. sql := `SELECT
  132. c3.classify_id AS id
  133. FROM knowledge_classify c1
  134. JOIN knowledge_classify c2 ON c2.parent_id = c1.classify_id
  135. AND c2.classify_name = ?
  136. AND c2.level = 2
  137. JOIN knowledge_classify c3 ON c3.parent_id = c2.classify_id
  138. AND c3.classify_name = ?
  139. AND c3.level = 3
  140. WHERE c1.classify_name = ?
  141. AND c1.level = 1 AND c1.resource_type = ?`
  142. err = global.DEFAULT_DmSQL.Raw(sql, classifyNameSecond, classifyNameThird, classifyNameFirst, resourceType).Scan(&id).Error
  143. return
  144. }