// @Author gmy 2024/9/19 15:13:00
package document_manage_model

import (
	"eta/eta_api/global"
	"eta/eta_api/utils"

	"gorm.io/gorm"
)

type OutsideReportAttachment struct {
	OutsideReportAttachmentId int    `orm:"column(outside_report_attachment_id);pk" gorm:"primaryKey" description:"外部报告附件ID"`
	OutsideReportId           int    `orm:"column(outside_report_id)" description:"报告id"`
	Title                     string `orm:"column(title)" description:"附件名称"`
	Url                       string `orm:"column(url)" description:"附件地址"`
	CreateTime                string `orm:"column(create_time)" description:"附件新增时间"`
	FileSize                  int64  `orm:"column(file_size)" description:"附件大小"`
}

func (o *OutsideReportAttachment) AfterFind(db *gorm.DB) (err error) {
	o.CreateTime = utils.GormDateStrToDateTimeStr(o.CreateTime)
	return
}

// SaveOutsideReportAttachment 保存附件
func SaveOutsideReportAttachment(attachment *OutsideReportAttachment) (int64, error) {
	o := global.DbMap[utils.DbNameReport]
	err := o.Create(attachment).Error
	if err != nil {
		return 0, err
	}
	id := int64(attachment.OutsideReportAttachmentId)
	return id, err
}

// GetOutsideReportAttachmentListByReportId 根据报告id获取附件列表
func GetOutsideReportAttachmentListByReportId(outsideReportId int) (attachmentList []*OutsideReportAttachment, err error) {
	o := global.DbMap[utils.DbNameReport]
	sql := "SELECT * FROM outside_report_attachment WHERE outside_report_id = ?"
	err = o.Raw(sql, outsideReportId).Find(&attachmentList).Error
	return
}

// DeleteReportAttachmentByReportId 根据报告id删除附件
func DeleteReportAttachmentByReportId(outsideReportId int) (err error) {
	o := global.DbMap[utils.DbNameReport]
	sql := "DELETE FROM outside_report_attachment WHERE outside_report_id = ?"
	err = o.Exec(sql, outsideReportId).Error
	return
}