package models import ( "eta_gn/eta_report/global" "eta_gn/eta_report/utils" "time" ) const ( // 事件类型-事件库 KnowledgeResourceTypeEvent = iota // 事件-政策库 KnowledgeResourceTypePolicy // 事件-报告库 KnowledgeResourceTypeReport // 事件类型-知识库 KnowledgeResourceTypeKnow // 事件-观点库 KnowledgeResourceTypeOpinion ) const ( // 未发布 KnowledgeResourceStateUnpublished = iota // 已发布 KnowledgeResourceStatePublished // 待审批 KnowledgeResourceStatePending // 已驳回 KnowledgeResourceStateRejected // 已通过 KnowledgeResourceStateApproved ) const ( // 内部 KnowledgeOutSourceInternal = iota // 智力共享 KnowledgeOutSourceIntelligence // 数据节点 KnowledgeOutSourceDataNode ) type KnowledgeResourceFile struct { KnowledgeResourceFileId int `gorm:"column:knowledge_resource_file_id;primaryKey;autoIncrement"` KnowledgeResourceId int `gorm:"column:knowledge_resource_id"` FileUrl string `gorm:"column:file_url"` } func (k KnowledgeResourceFile) TableName() string { return "knowledge_resource_file" } type KnowledgeResource struct { KnowledgeResourceId int `gorm:"column:knowledge_resource_id;;primaryKey;autoIncrement"` ResourceType int `gorm:"column:resource_type;"` ClassifyId int `gorm:"column:classify_id"` Title string `gorm:"column:title;"` CreateTime time.Time `gorm:"column:create_time" description:"创建时间"` ModifyTime time.Time `gorm:"column:modify_time;autoUpdateTime" description:"修改时间"` State int `gorm:"column:state" description:"0:未发布;1:已发布;2:待审批;3:已驳回;4:已通过"` Content string `gorm:"column:content"` ResourceCode string `gorm:"column:resource_code"` AdminId int `gorm:"column:admin_id" description:"创建者账号"` AdminRealName string `gorm:"column:admin_real_name" description:"创建者姓名"` SourceFrom string `gorm:"column:source_from"` TagId int `gorm:"column:tag_id;default:0;NOT NULL"` StartTime *time.Time `gorm:"column:start_time"` EndTime *time.Time `gorm:"column:end_time"` IsFile int `gorm:"column:is_file;default:0;NOT NULL"` FileUrl string `gorm:"column:file_url"` OutId int `gorm:"column:out_id" description:"外部系统ID"` IsDelete int `gorm:"column:is_delete;default:0;NOT NULL"` OutSource int `gorm:"column:out_source" description:"外部来源:0:内部 1:智力共享 2:数据节点"` } func (k KnowledgeResource) TableName() string { return "knowledge_resource" } func (k *KnowledgeResource) Create(fileUrl []string) (err error) { tx := global.DEFAULT_DmSQL.Begin() defer func() { if err != nil { tx.Rollback() } else { tx.Commit() } }() err = tx.Create(k).Error if len(fileUrl) > 0 { addFile := make([]*KnowledgeResourceFile, 0, len(fileUrl)) for _, url := range fileUrl { file := new(KnowledgeResourceFile) file.KnowledgeResourceId = k.KnowledgeResourceId file.FileUrl = url addFile = append(addFile, file) } err = tx.CreateInBatches(addFile, utils.MultiAddNum).Error } return } func (k *KnowledgeResource) Update(cols []string) error { return global.DEFAULT_DmSQL.Model(k).Select(cols).Updates(k).Error } // GetKnowledgeResourceByOutId 根据外部系统ID获取知识资源 func (k *KnowledgeResource) GetKnowledgeResourceByOutId(outId int) (item *KnowledgeResource, err error) { sql := "SELECT * FROM knowledge_resource WHERE out_id = ?" err = global.DEFAULT_DmSQL.Raw(sql, outId).First(&item).Error return } type KnowledgeClassify struct { ClassifyId int `gorm:"primaryKey;column:classify_id"` ClassifyName string `gorm:"column:classify_name;default:'';type:varchar(125);not null"` // 注意:varchar的默认长度可能需要根据实际情况调整 Sort int `gorm:"column:sort;default:0;type:tinyint"` ParentId int `gorm:"column:parent_id;default:0;type:int"` CreateTime time.Time `gorm:"column:create_time;default:CURRENT_TIMESTAMP"` ModifyTime time.Time `gorm:"column:modify_time;default:CURRENT_TIMESTAMP"` Enabled int `gorm:"column:enabled;default:1;type:tinyint"` Level int `gorm:"column:level;default:0;type:bigint"` ResourceType int `gorm:"column:resource_type;default:0;not null;type:tinyint"` } func (k KnowledgeClassify) TableName() string { return "knowledge_classify" } func (k *KnowledgeClassify) Create() error { return global.DEFAULT_DmSQL.Create(k).Error } func (k *KnowledgeClassify) GetClassifyByNameTypeAndParentId(classifyName string, resourceType, parentId int) (items *KnowledgeClassify, err error) { sql := "SELECT * FROM knowledge_classify WHERE classify_name = ? AND resource_type = ? AND parent_id = ?" err = global.DEFAULT_DmSQL.Raw(sql, classifyName, resourceType, parentId).First(&items).Error return } func (k *KnowledgeClassify) GetClassifysByNameAndType(classifyName string, resourceType int) (item *KnowledgeClassify, err error) { sql := "SELECT * FROM knowledge_classify WHERE classify_name = ? AND resource_type = ?" err = global.DEFAULT_DmSQL.Raw(sql, classifyName, resourceType).Find(&item).Error return } func (k *KnowledgeClassify) GetChildClassifyIdByNamePath(resourceType int, classifyNameFirst string, classifyNameSecond string, classifyNameThird string) (id int, err error) { sql := `SELECT c3.classify_id AS id FROM knowledge_classify c1 JOIN knowledge_classify c2 ON c2.parent_id = c1.classify_id AND c2.classify_name = ? AND c2.level = 2 JOIN knowledge_classify c3 ON c3.parent_id = c2.classify_id AND c3.classify_name = ? AND c3.level = 3 WHERE c1.classify_name = ? AND c1.level = 1 AND c1.resource_type = ?` err = global.DEFAULT_DmSQL.Raw(sql, classifyNameSecond, classifyNameThird, classifyNameFirst, resourceType).Scan(&id).Error return }