New file |
| | |
| | | package purchase |
| | | |
| | | import ( |
| | | "github.com/spf13/cast" |
| | | "gorm.io/gorm" |
| | | "srm/global" |
| | | "srm/model/common/request" |
| | | "srm/model/purchase" |
| | | purchaserequest "srm/model/purchase/request" |
| | | ) |
| | | |
| | | type PurchaseService struct{} |
| | | |
| | | func NewPurchaseService() *PurchaseService { |
| | | return &PurchaseService{} |
| | | } |
| | | |
| | | //@function: CreatePurchase |
| | | //@description: 创建采购单 |
| | | //@param: params *purchaserequest.AddPurchase |
| | | //@return: err error |
| | | |
| | | func (exa *PurchaseService) CreatePurchase(params purchaserequest.AddPurchase) (err error) { |
| | | err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { |
| | | err = global.GVA_DB.Create(¶ms.Purchase).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | for _, product := range params.ProductList { |
| | | product.PurchaseId = cast.ToInt(params.Purchase.ID) |
| | | } |
| | | return global.GVA_DB.Create(params.ProductList).Error |
| | | }) |
| | | |
| | | return err |
| | | } |
| | | |
| | | //@function: DeletePurchase |
| | | //@description: 删除采购单 |
| | | //@param: id uint |
| | | //@return: err error |
| | | |
| | | func (exa *PurchaseService) DeletePurchase(id uint) (err error) { |
| | | err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { |
| | | err = global.GVA_DB.Where("id = ?", id).Delete(&purchase.Purchase{}).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | return global.GVA_DB.Where("purchase_id = ?", id).Delete(&purchase.PurchaseProducts{}).Error |
| | | }) |
| | | return err |
| | | } |
| | | |
| | | //@function: UpdatePurchase |
| | | //@description: 更新采购单 |
| | | //@param: params *purchaserequest.AddPurchase |
| | | //@return: err error |
| | | |
| | | func (exa *PurchaseService) UpdatePurchase(params *purchaserequest.AddPurchase) (err error) { |
| | | err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { |
| | | err = global.GVA_DB.Updates(params.Purchase).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | err = global.GVA_DB.Where("purchase_id = ?", params.Purchase.ID).Delete(&purchase.PurchaseProducts{}).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | for _, product := range params.ProductList { |
| | | product.ID = 0 |
| | | product.PurchaseId = cast.ToInt(params.Purchase.ID) |
| | | } |
| | | return global.GVA_DB.Create(params.ProductList).Error |
| | | }) |
| | | return err |
| | | } |
| | | |
| | | //@function: GetPurchase |
| | | //@description: 获取采购单信息 |
| | | //@param: id uint |
| | | //@return: purchase model.Purchase, err error |
| | | |
| | | func (exa *PurchaseService) GetPurchase(id uint) (purchase purchase.Purchase, err error) { |
| | | err = global.GVA_DB.Where("id = ?", id).First(&purchase).Error |
| | | return |
| | | } |
| | | |
| | | //@function: GetPurchaseList |
| | | //@description: 分页获取采购单列表 |
| | | //@param: info request.PageInfo |
| | | //@return: list interface{}, total int64, err error |
| | | |
| | | func (exa *PurchaseService) GetPurchaseList(info request.PageInfo) (list interface{}, total int64, err error) { |
| | | limit := info.PageSize |
| | | offset := info.PageSize * (info.Page - 1) |
| | | db := global.GVA_DB.Model(&purchase.Purchase{}) |
| | | if info.Keyword != "" { |
| | | db.Distinct("purchases.id").Joins("left join purchase_products on purchase_products.purchase_id = purchases.id"). |
| | | Joins("left join Product on Product.Id = purchase_products.product_id"). |
| | | Joins("left join supplier on supplier.Id = purchases.supplier_id"). |
| | | Where("purchases.name like ?", "%"+info.Keyword+"%"). |
| | | Or("Product.name like ?", "%"+info.Keyword+"%"). |
| | | Or("supplier.name like ?", "%"+info.Keyword+"%") |
| | | } |
| | | var purchaseList []purchase.Purchase |
| | | err = db.Count(&total).Error |
| | | if err != nil { |
| | | return purchaseList, total, err |
| | | } |
| | | err = db.Limit(limit).Offset(offset).Find(&purchaseList).Error |
| | | return purchaseList, total, err |
| | | } |
| | | |
| | | //@function: GetPurchaseProductList |
| | | //@description: 分页获取采购单产品列表 |
| | | //@param: purchaseId int |
| | | //@return: list interface{}, err error |
| | | |
| | | func (exa *PurchaseService) GetPurchaseProductList(purchaseId uint) (list []*purchase.PurchaseProducts, err error) { |
| | | db := global.GVA_DB.Model(&purchase.PurchaseProducts{}) |
| | | list = make([]*purchase.PurchaseProducts, 0) |
| | | err = db.Where("purchase_id = ?", purchaseId).Find(&list).Error |
| | | return list, err |
| | | } |
| | | |
| | | //@function: Submit |
| | | //@description: 提交采购单 |
| | | //@param: id uint |
| | | //@return: err error |
| | | |
| | | func (exa *PurchaseService) Submit(id uint) (err error) { |
| | | err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { |
| | | err = global.GVA_DB.Where("id = ?", id).Model(&purchase.Purchase{}).Updates(map[string]interface{}{"status": 1}).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | return global.GVA_DB.Where("purchase_id = ?", id).Delete(&purchase.PurchaseProducts{}).Error |
| | | }) |
| | | return err |
| | | } |