package dao

import "server/global"

type Warehouse struct {
	global.GVA_MODEL
	Name     string `json:"name" form:"name" gorm:"comment:库房名称"`
	Location string `json:"location" form:"location" gorm:"comment:库房位置"`
	Head     string `json:"head" form:"head" gorm:"comment:负责人"`
	Remarks  string `json:"remarks" form:"remarks" gorm:"comment:备注"`
}

func (Warehouse) TableName() string {
	return "storehouse"
}

func QueryAllWarehouse() (warehouse []Warehouse, err error) {
	err = global.GVA_DB.Model(&Warehouse{}).Find(&warehouse).Error
	return warehouse, err
}

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

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

func (w Warehouse) CreateWarehouse() error {
	return global.GVA_DB.Create(&w).Error
}

func (w Warehouse) UpdateWarehouse() error {
	return global.GVA_DB.Where("id = ?", w.ID).Updates(&w).Error
}

func (w Warehouse) DeleteWarehouse() error {
	return global.GVA_DB.Unscoped().Delete(&w).Error
}
