knowledge_resource.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. type KnowledgeResourceFile struct {
  32. KnowledgeResourceFileId int `gorm:"column:knowledge_resource_file_id;primaryKey;autoIncrement"`
  33. KnowledgeResourceId int `gorm:"column:knowledge_resource_id"`
  34. FileUrl string `gorm:"column:file_url"`
  35. }
  36. func (k KnowledgeResourceFile) TableName() string {
  37. return "knowledge_resource_file"
  38. }
  39. type KnowledgeResource struct {
  40. KnowledgeResourceId int `gorm:"column:knowledge_resource_id;;primaryKey;autoIncrement"`
  41. ResourceType int `gorm:"column:resource_type;"`
  42. ClassifyId int `gorm:"column:classify_id"`
  43. Title string `gorm:"column:title;"`
  44. CreateTime time.Time `gorm:"column:create_time" description:"创建时间"`
  45. ModifyTime time.Time `gorm:"column:modify_time;autoUpdateTime" description:"修改时间"`
  46. State int `gorm:"column:state" description:"0:未发布;1:已发布;2:待审批;3:已驳回;4:已通过"`
  47. Content string `gorm:"column:content"`
  48. ResourceCode string `gorm:"column:resource_code"`
  49. AdminId int `gorm:"column:admin_id" description:"创建者账号"`
  50. AdminRealName string `gorm:"column:admin_real_name" description:"创建者姓名"`
  51. SourceFrom string `gorm:"column:source_from"`
  52. TagId int `gorm:"column:tag_id;default:0;NOT NULL"`
  53. StartTime *time.Time `gorm:"column:start_time"`
  54. EndTime *time.Time `gorm:"column:end_time"`
  55. IsFile int `gorm:"column:is_file;default:0;NOT NULL"`
  56. FileUrl string `gorm:"column:file_url"`
  57. OutId int `gorm:"column:out_id" description:"外部系统ID"`
  58. IsDelete int `gorm:"column:is_delete;default:0;NOT NULL"`
  59. }
  60. func (k KnowledgeResource) TableName() string {
  61. return "knowledge_resource"
  62. }
  63. func (k *KnowledgeResource) Create(fileUrl []string) (err error) {
  64. tx := global.DEFAULT_DmSQL.Begin()
  65. defer func() {
  66. if err != nil {
  67. tx.Rollback()
  68. } else {
  69. tx.Commit()
  70. }
  71. }()
  72. err = tx.Create(k).Error
  73. if len(fileUrl) > 0 {
  74. addFile := make([]*KnowledgeResourceFile, 0, len(fileUrl))
  75. for _, url := range fileUrl {
  76. file := new(KnowledgeResourceFile)
  77. file.KnowledgeResourceId = k.KnowledgeResourceId
  78. file.FileUrl = url
  79. addFile = append(addFile, file)
  80. }
  81. err = tx.CreateInBatches(addFile, utils.MultiAddNum).Error
  82. }
  83. return
  84. }
  85. func (k *KnowledgeResource) Update(cols []string) error {
  86. return global.DEFAULT_DmSQL.Model(k).Select(cols).Updates(k).Error
  87. }
  88. // GetKnowledgeResourceByOutId 根据外部系统ID获取知识资源
  89. func (k *KnowledgeResource) GetKnowledgeResourceByOutId(outId int) (item *KnowledgeResource, err error) {
  90. sql := "SELECT * FROM knowledge_resource WHERE out_id = ?"
  91. err = global.DEFAULT_DmSQL.Raw(sql, outId).First(&item).Error
  92. return
  93. }
  94. type KnowledgeClassify struct {
  95. ClassifyId int `gorm:"primaryKey;column:classify_id"`
  96. ClassifyName string `gorm:"column:classify_name;default:'';type:varchar(125);not null"` // 注意:varchar的默认长度可能需要根据实际情况调整
  97. Sort int `gorm:"column:sort;default:0;type:tinyint"`
  98. ParentId int `gorm:"column:parent_id;default:0;type:int"`
  99. CreateTime time.Time `gorm:"column:create_time;default:CURRENT_TIMESTAMP"`
  100. ModifyTime time.Time `gorm:"column:modify_time;default:CURRENT_TIMESTAMP"`
  101. Enabled int `gorm:"column:enabled;default:1;type:tinyint"`
  102. Level int `gorm:"column:level;default:0;type:bigint"`
  103. ResourceType int `gorm:"column:resource_type;default:0;not null;type:tinyint"`
  104. }
  105. func (k KnowledgeClassify) TableName() string {
  106. return "knowledge_classify"
  107. }
  108. func (k *KnowledgeClassify) Create() error {
  109. return global.DEFAULT_DmSQL.Create(k).Error
  110. }
  111. func (k *KnowledgeClassify) GetClassifyByNameTypeAndParentId(classifyName string, resourceType, parentId int) (items *KnowledgeClassify, err error) {
  112. sql := "SELECT * FROM knowledge_classify WHERE classify_name = ? AND resource_type = ? AND parent_id = ?"
  113. err = global.DEFAULT_DmSQL.Raw(sql, classifyName, resourceType, parentId).First(&items).Error
  114. return
  115. }
  116. func (k *KnowledgeClassify) GetClassifysByNameAndType(classifyName string, resourceType int) (item *KnowledgeClassify, err error) {
  117. sql := "SELECT * FROM knowledge_classify WHERE classify_name = ? AND resource_type = ?"
  118. err = global.DEFAULT_DmSQL.Raw(sql, classifyName, resourceType).Find(&item).Error
  119. return
  120. }
  121. func (k *KnowledgeClassify) GetChildClassifyIdByNamePath(classifyNameFirst string, classifyNameSecond string, classifyNameThird string) (id int, err error) {
  122. sql := `SELECT
  123. c3.classify_id AS id
  124. FROM knowledge_classify c1
  125. JOIN knowledge_classify c2 ON c2.parent_id = c1.classify_id
  126. AND c2.classify_name = ?
  127. AND c2.level = 2
  128. JOIN knowledge_classify c3 ON c3.parent_id = c2.classify_id
  129. AND c3.classify_name = ?
  130. AND c3.level = 3
  131. WHERE c1.classify_name = ?
  132. AND c1.level = 1 `
  133. err = global.DEFAULT_DmSQL.Raw(sql, classifyNameSecond, classifyNameThird, classifyNameFirst).Scan(&id).Error
  134. return
  135. }