knowledge_resource.go 5.5 KB

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