package dao

import "server/global"

type Supplier struct {
	global.GVA_MODEL
	Name        string `json:"name" form:"name" gorm:"comment:供应商名称"`
	City        string `json:"city" form:"city" gorm:"comment:所在城市"`
	Address     string `json:"address" form:"address" gorm:"comment:详细地址"`
	ContactName string `json:"contactName" form:"contactName" gorm:"comment:联系人"`
	Email       string `json:"email" form:"email" gorm:"comment:Email"`
	Phone       string `json:"phone" form:"phone" gorm:"comment:联系电话"`
	Remarks     string `json:"remarks" form:"remarks" gorm:"comment:备注"`
}

func (Supplier) TableName() string {
	return "supplier"
}

func QueryAllSupplier() ([]Supplier, error) {
	var suppliers []Supplier
	err := global.GVA_DB.Find(&suppliers).Error
	return suppliers, err
}

func QuerySupplierList(limit, offset int) (suppliers []Supplier, total int64, err error) {
	// 创建db
	db := global.GVA_DB.Model(&Supplier{})
	// 如果有条件搜索 下方会自动创建搜索语句

	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Order("id desc").Limit(limit).Offset(offset).Find(&suppliers).Error
	return suppliers, total, err
}

func (s Supplier) CreateSupplier() error {
	return global.GVA_DB.Create(&s).Error
}

func (s Supplier) UpdateSupplier() error {
	return global.GVA_DB.Model(&s).Select("*").Omit("created_at").Updates(&s).Error
}

func (s Supplier) DeleteSupplier() error {
	return global.GVA_DB.Unscoped().Delete(&s).Error
}
