package dao

import (
	"github.com/shopspring/decimal"
	"server/global"
)

type Purchase struct {
	global.GVA_MODEL
	ProjectCode string          `json:"projectCode" form:"projectCode" gorm:"comment:项目编码"`
	Name        string          `json:"name" form:"name" gorm:"comment:名称"`
	Quantity    int             `json:"quantity" form:"quantity" gorm:"comment:数量"`
	Company     string          `json:"company" form:"company" gorm:"comment:规格(单位)"`
	Origin      string          `json:"origin" form:"origin" gorm:"comment:产地"`
	Price       decimal.Decimal `json:"price" form:"price" gorm:"type:decimal(10,2);not null;"`
	Amount      decimal.Decimal `json:"amount" form:"amount" gorm:"type:decimal(10,2);not null;"`
	Method      string          `json:"method" form:"method" gorm:"comment:付款方式"`
}

func (Purchase) TableName() string {
	return "purchase"
}

// TODO:采购查询

// QueryPurchase 查询采购
func QueryPurchase(code string) (purchase []Purchase, err error) {
	err = global.GVA_DB.Model(&Purchase{}).Where("project_code = ?", code).Find(&purchase).Error
	return purchase, err
}

// TODO:采购新增

// CreatePurchase 新建采购
func (p Purchase) CreatePurchase() error {
	return global.GVA_DB.Create(&p).Error
}

// TODO:采购修改

// UpdatePurchase 修改采购
func (p Purchase) UpdatePurchase() error {
	return global.GVA_DB.Model(&p).Where("id = ?", p.ID).Updates(&p).Error
}

// TODO:采购删除

// DeletePurchase 删除采购
func (p Purchase) DeletePurchase() error {
	return global.GVA_DB.Unscoped().Delete(&p).Error
}
