package models

import (
	"fmt"
	"time"
)

type OSS struct {
	ID             int       `json:"id"`
	Name           string    `json:"name"`
	PublicAddress  string    `json:"public_address"`
	PrivateAddress string    `json:"private_address"`
	VendorId       int       `json:"vendor_id"`
	Location       string    `json:"location"`
	RwPermit       string    `json:"rw_permit"`
	Owner          string    `json:"owner"`
	Remark         string    `json:"remark"`
	CreateTime     time.Time `json:"create_time"`
}

func CreateS3Bucket(oss interface{}) (err error) {
	err = db.Create(oss).Error
	return
}

func GetBucketList(key string, vendorId int, pageNum int, pageSize int) (oss []OSS) {
	selectSql := fmt.Sprintf(`SELECT * FROM oss WHERE id > 0 `)
	if vendorId != 0 {
		selectSql += fmt.Sprintf(`AND vendor_id = '%d' `, vendorId)
	}
	if key != "" {
		selectSql += fmt.Sprintf(`AND (name LIKE '%%%s%%') `, key)
	}

	db.Raw(selectSql).Offset(pageNum).Limit(pageSize).Scan(&oss)
	return
}

func GetBucketTotal(key string, vendorId int) (count int) {
	selectSql := fmt.Sprintf(`SELECT COUNT(*) FROM oss WHERE id > 0 `)
	if key != "" {
		selectSql += fmt.Sprintf(`AND (name LIKE '%%%s%%') `, key)
	}
	if vendorId != 0 {
		selectSql += fmt.Sprintf(`AND vendor_id = '%d' `, vendorId)
	}
	db.Raw(selectSql).Count(&count)
	return
}
