// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================

package dao

import (
	"context"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gtime"
	"hotgo/internal/consts"
	"hotgo/internal/dao/internal"
	"hotgo/internal/model/input/sysin"
	"hotgo/internal/service"
	"hotgo/utility/format"
)

// internalSysAttachmentDao is internal type for wrapping internal DAO implements.
type internalSysAttachmentDao = *internal.SysAttachmentDao

// sysAttachmentDao is the data access object for table hg_sys_attachment.
// You can define custom methods on it to extend its functionality as you wish.
type sysAttachmentDao struct {
	internalSysAttachmentDao
}

var (
	// SysAttachment is globally public accessible object for table hg_sys_attachment operations.
	SysAttachment = sysAttachmentDao{
		internal.NewSysAttachmentDao(),
	}
)

func (dao *sysAttachmentDao) GetMd5File(ctx context.Context, md5 string) (data *sysin.AttachmentListModel, err error) {
	if err = dao.Ctx(ctx).
		Where("md5", md5).
		Scan(&data); err != nil {
		err = gerror.Wrap(err, consts.ErrorORM)
		return nil, err
	}

	if data == nil {
		return nil, nil
	}

	conf, err := service.SysConfig().GetUpload(ctx)
	if err != nil {
		return nil, nil
	}

	data.SizeFormat = format.FileSize(data.Size)
	data.FileUrl = service.CommonUpload().LastUrl(ctx, conf, data.FileUrl, data.Drive)

	// 只有在上传时才会检查md5值，如果文件存在则更新最后上传时间，保证上传列表更新显示在最前面
	if data.Id > 0 {
		_, _ = dao.Ctx(ctx).Where("id", data.Id).Data(g.Map{
			"status":     consts.StatusEnabled,
			"updated_at": gtime.Now(),
		}).Update()
	}

	return data, nil
}
