fix
wangpengfei
2023-08-18 0409a5d0235f4a87ecf08b12baa9149279a90447
fix
3个文件已修改
63 ■■■■ 已修改文件
model/product.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/quotation.go 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/quotation.go 52 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/product.go
@@ -12,7 +12,7 @@
        Name       string          `json:"name" gorm:"column:name;type:varchar(255);comment:产品名称"`
        Price      decimal.Decimal `json:"price" gorm:"column:price;type:decimal(10,2);comment:产品价格"`
        Number     string          `json:"number" gorm:"column:number;type:varchar(255);comment:产品编号"`
        Amount     decimal.Decimal `json:"amount" gorm:"column:amount;type:int;comment:产品数量"`
        Amount     decimal.Decimal `json:"amount" gorm:"column:amount;type:decimal(12,2);comment:产品数量"`
        Total      decimal.Decimal `json:"total" gorm:"column:total;type:decimal(10,2);comment:产品总价"`
        Desc       string          `json:"desc" gorm:"column:desc;type:varchar(255);comment:产品描述"`
        gorm.Model `json:"-"`
model/quotation.go
@@ -43,9 +43,12 @@
    return "quotation"
}
func NewQuotationSearch() *QuotationSearch {
func NewQuotationSearch(db *gorm.DB) *QuotationSearch {
    if db == nil {
        db = mysqlx.GetDB()
    }
    return &QuotationSearch{
        Orm: mysqlx.GetDB(),
        Orm: db,
    }
}
@@ -104,7 +107,7 @@
func (slf *QuotationSearch) Find() (*Quotation, error) {
    var db = slf.build()
    var record Quotation
    err := db.Preload("Client").Preload("Contact").Preload("SaleChance").First(&record).Error
    err := db.Preload("Products").Preload("Client").Preload("Contact").Preload("SaleChance").First(&record).Error
    return &record, err
}
service/quotation.go
@@ -3,12 +3,13 @@
import (
    "aps_crm/model"
    "aps_crm/pkg/ecode"
    "aps_crm/pkg/mysqlx"
)
type QuotationService struct{}
func (QuotationService) AddQuotation(quotation *model.Quotation) int {
    err := model.NewQuotationSearch().Create(quotation)
    err := model.NewQuotationSearch(nil).Create(quotation)
    if err != nil {
        return ecode.QuotationExist
    }
@@ -18,14 +19,51 @@
func (QuotationService) UpdateQuotation(quotation *model.Quotation) int {
    // check quotation exist
    _, err := model.NewQuotationSearch().SetId(quotation.Id).Find()
    tmp, err := model.NewQuotationSearch(nil).SetId(quotation.Id).Find()
    if err != nil {
        return ecode.QuotationNotExist
    }
    err = model.NewQuotationSearch().SetId(quotation.Id).Update(quotation)
    if err != nil {
        return ecode.QuotationSetErr
    if len(quotation.Products) == 0 {
        err = model.NewQuotationSearch(nil).SetId(quotation.Id).Update(quotation)
        if err != nil {
            return ecode.QuotationSetErr
        }
    } else {
        tx := mysqlx.GetDB().Begin()
        err = tx.Model(tmp).Association("Products").Clear()
        if err != nil {
            tx.Rollback()
            return ecode.QuotationSetErr
        }
        err = model.NewQuotationSearch(tx).SetId(quotation.Id).Update(quotation)
        if err != nil {
            tx.Rollback()
            return ecode.QuotationSetErr
        }
        for _, product := range quotation.Products {
            if product.Id == 0 {
                err = model.NewProductSearch(tx).Create(&product)
                if err != nil {
                    tx.Rollback()
                    return ecode.QuotationSetErr
                }
            } else {
                err = model.NewProductSearch(tx).SetId(product.Id).Update(&product)
                if err != nil {
                    tx.Rollback()
                    return ecode.QuotationSetErr
                }
            }
            err = tx.Model(&tmp).Association("Products").Append(&product)
            if err != nil {
                tx.Rollback()
                return ecode.QuotationSetErr
            }
        }
        tx.Commit()
    }
    return ecode.OK
@@ -33,7 +71,7 @@
func (QuotationService) GetQuotationList(page, pageSize int, data map[string]interface{}) ([]*model.Quotation, int64, int) {
    // get contact list
    contacts, total, err := model.NewQuotationSearch().SetPage(page, pageSize).SetSearchMap(data).FindAll()
    contacts, total, err := model.NewQuotationSearch(nil).SetPage(page, pageSize).SetSearchMap(data).FindAll()
    if err != nil {
        return nil, 0, ecode.QuotationListErr
    }
@@ -42,7 +80,7 @@
func (QuotationService) DeleteQuotation(ids []int) int {
    // delete client
    err := model.NewQuotationSearch().SetIds(ids).Delete()
    err := model.NewQuotationSearch(nil).SetIds(ids).Delete()
    if err != nil {
        return ecode.QuotationDeleteErr
    }