/*
 * @description: 安全巡检数据库操作相关
 * @Author: LanJianRong
 * @Date: 2020-11-20
 * @FilePath: \construction_management\dao\safe_dao.go
 */

package dao

import (
	"github.com/go-xorm/xorm"
	"go.mod/models"
)

// 数据库操作引擎
type SafeFileDao struct {
	engine *xorm.Engine
}

// 获得一个DAO对象
func NewSafeFileDao(engine *xorm.Engine) *SafeFileDao {
	return &SafeFileDao{
		engine: engine,
	}
}

// 通过safe_id获得数据
func (d *SafeFileDao) GetListBySid(id int) []models.CmSafeFile {
	dataList := make([]models.CmSafeFile, 0)
	err := d.engine.
		Asc("create_time").
		Sql("select f.`create_time`, f.`file_name`, f.`file_path`, a.`name`from `cm_safe_file` as f, `cm_project_acount` as a where f.`uid` = a.`id` and f.`safe_id` = ?", id).
		Find(&dataList)
	if err != nil {
		return dataList
	}
	return dataList
}

// 获取总数
func (d *SafeFileDao) GetCount(id int) int64 {
	file := models.CmSafeFile{}
	total, err := d.engine.
		Where("safe_id=?", id).
		Count(file)
	if err != nil {
		return 0
	}
	return total
}

// 批量插入数据
func (d *SafeFileDao) InsertByList(data []models.CmSafeFile) error {
	_, err := d.engine.Insert(data)
	return err
}
