jiangshuai
2023-11-09 f50a5ab504b4b02fa73fd8fd9d789c68c968efd3
附件信息,产品带附件
1个文件已添加
3个文件已修改
320 ■■■■■ 已修改文件
controllers/product_controller.go 33 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/attachment.go 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/material.go 18 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/material_attachment.go 261 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
controllers/product_controller.go
@@ -52,6 +52,19 @@
        util.ResponseFormat(c, code.RequestParamError, "产品信息保存失败")
        return
    }
    materialAttachmentList := []*models.MaterialAttachment{}
    for _, v := range params.AttachmentIDs {
        ma := &models.MaterialAttachment{MaterialID: params.ID, AttachmentID: v}
        materialAttachmentList = append(materialAttachmentList, ma)
    }
    if len(materialAttachmentList) > 0 {
        if err := models.NewMaterialAttachmentSearch().CreateBatch(materialAttachmentList); err != nil {
            util.ResponseFormat(c, code.SaveFail, "附件保存失败")
            return
        }
    }
    util.ResponseFormat(c, code.Success, "保存成功")
}
@@ -72,7 +85,7 @@
    if params.PageInfo.Check() {
        search.SetPage(params.Page, params.PageSize)
    }
    products, total, err := search.SetKeyword(params.KeyWord).SetCategoryId(params.CategoryId).SetOrder("created_at desc").Find()
    products, total, err := search.SetPreload(true).SetKeyword(params.KeyWord).SetCategoryId(params.CategoryId).SetOrder("created_at desc").Find()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查找失败")
        return
@@ -110,7 +123,7 @@
        util.ResponseFormat(c, code.RequestParamError, "无效id")
        return
    }
    material, err := models.NewMaterialSearch().SetID(id).First()
    material, err := models.NewMaterialSearch().SetID(id).SetPreload(true).First()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查找失败")
        return
@@ -148,6 +161,22 @@
        util.ResponseFormat(c, code.RequestParamError, "产品信息更新失败")
        return
    }
    materialAttachmentList := []*models.MaterialAttachment{}
    for _, v := range params.AttachmentIDs {
        ma := &models.MaterialAttachment{MaterialID: params.ID, AttachmentID: v}
        materialAttachmentList = append(materialAttachmentList, ma)
    }
    if err := models.NewMaterialAttachmentSearch().SetMaterialID(params.ID).Delete(); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "产品附件清除失败")
        return
    }
    if len(materialAttachmentList) > 0 {
        if err := models.NewMaterialAttachmentSearch().CreateBatch(materialAttachmentList); err != nil {
            util.ResponseFormat(c, code.RequestParamError, "产品信息更新失败")
            return
        }
    }
    util.ResponseFormat(c, code.Success, "更新成功")
}
models/attachment.go
@@ -9,8 +9,8 @@
type (
    Attachment struct {
        WmsModel
        Id       int               `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
        BaseModelInt
        Id       uint              `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
        FileName string            `json:"fileName" gorm:"type:varchar(127);comment:文件名"`
        FileUrl  string            `json:"FileUrl" gorm:"type:varchar(255);comment:文件地址"`
        Ext      string            `json:"ext" gorm:"type:varchar(15);comment:文件后缀名"`
@@ -29,7 +29,7 @@
)
func (slf *Attachment) TableName() string {
    return "wms_attachment"
    return "attachment"
}
func NewAttachmentSearch() *AttachmentSearch {
@@ -51,7 +51,7 @@
    return slf
}
func (slf *AttachmentSearch) SetID(ID int) *AttachmentSearch {
func (slf *AttachmentSearch) SetID(ID uint) *AttachmentSearch {
    slf.Id = ID
    return slf
}
models/material.go
@@ -67,9 +67,11 @@
        //HSCode                  string                     `gorm:"type:varchar(255);comment:HS编码" json:"HSCode"`                    //HS编码
        //OriginCountryId         int                        `gorm:"type:int(11);comment:原产地id" json:"originCountryId"`               //原产地id
        //OriginCountryName       string                     `gorm:"type:varchar(255);comment:原产地名称" json:"originCountryName"`        //原产地名称
        InStorageExplain        string `gorm:"type:varchar(512);comment:入库说明" json:"inStorageExplain"`          //入库说明
        OutStorageExplain       string `gorm:"type:varchar(512);comment:出库说明" json:"outStorageExplain"`         //出库说明
        InternalTransferExplain string `gorm:"type:varchar(512);comment:内部调拨说明" json:"internalTransferExplain"` //内部调拨说明
        InStorageExplain        string        `gorm:"type:varchar(512);comment:入库说明" json:"inStorageExplain"`          //入库说明
        OutStorageExplain       string        `gorm:"type:varchar(512);comment:出库说明" json:"outStorageExplain"`         //出库说明
        InternalTransferExplain string        `gorm:"type:varchar(512);comment:内部调拨说明" json:"internalTransferExplain"` //内部调拨说明
        AttachmentList          []*Attachment `json:"attachmentList" gorm:"many2many:material_attachment"`
        AttachmentIDs           []uint        `json:"attachmentIDs" gorm:"-"`
    }
    MaterialSearch struct {
@@ -83,6 +85,7 @@
        Ids         []string
        Orm         *gorm.DB
        CategoryIds []int
        Preload     bool
    }
    IdAndName struct {
@@ -174,6 +177,11 @@
//    return slf
//}
func (slf *MaterialSearch) SetPreload(preload bool) *MaterialSearch {
    slf.Preload = preload
    return slf
}
func (slf *MaterialSearch) build() *gorm.DB {
    var db = slf.Orm.Table(slf.TableName())
@@ -240,6 +248,10 @@
        db = db.Where("category_id in ?", slf.CategoryIds)
    }
    if slf.Preload {
        db = db.Preload("AttachmentList")
    }
    return db
}
models/material_attachment.go
New file
@@ -0,0 +1,261 @@
package models
import (
    "fmt"
    "gorm.io/gorm"
    "wms/pkg/mysqlx"
)
type (
    MaterialAttachment struct {
        BaseModelInt
        AttachmentID uint   `json:"attachmentID" gorm:"comment:附件表外键"`
        MaterialID   string `json:"materialID" gorm:"type:varchar(191);comment:物料表外检"`
    }
    MaterialAttachmentSearch struct {
        MaterialAttachment
        Order    string
        PageNum  int
        PageSize int
        Keyword  string
        Orm      *gorm.DB
        Preload  bool
    }
)
func (slf *MaterialAttachment) TableName() string {
    return "material_attachment"
}
func NewMaterialAttachmentSearch() *MaterialAttachmentSearch {
    return &MaterialAttachmentSearch{Orm: mysqlx.GetDB()}
}
func (slf *MaterialAttachmentSearch) SetOrm(tx *gorm.DB) *MaterialAttachmentSearch {
    slf.Orm = tx
    return slf
}
func (slf *MaterialAttachmentSearch) SetPage(page, size int) *MaterialAttachmentSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
func (slf *MaterialAttachmentSearch) SetOrder(order string) *MaterialAttachmentSearch {
    slf.Order = order
    return slf
}
func (slf *MaterialAttachmentSearch) SetID(ID uint) *MaterialAttachmentSearch {
    slf.ID = ID
    return slf
}
func (slf *MaterialAttachmentSearch) SetKeyword(keyword string) *MaterialAttachmentSearch {
    slf.Keyword = keyword
    return slf
}
func (slf *MaterialAttachmentSearch) SetPreload(preload bool) *MaterialAttachmentSearch {
    slf.Preload = preload
    return slf
}
func (slf *MaterialAttachmentSearch) SetMaterialID(materialID string) *MaterialAttachmentSearch {
    slf.MaterialID = materialID
    return slf
}
func (slf *MaterialAttachmentSearch) build() *gorm.DB {
    var db = slf.Orm.Model(&MaterialAttachment{})
    if slf.ID != 0 {
        db = db.Where("id = ?", slf.ID)
    }
    if slf.Order != "" {
        db = db.Order(slf.Order)
    }
    if slf.Keyword != "" {
        db = db.Where("name like ? ", fmt.Sprintf("%%%v%%", slf.Keyword))
    }
    if slf.MaterialID != "" {
        db = db.Where("material_id=?", slf.MaterialID)
    }
    //if slf.Preload {
    //    db = db.Model(&Attachment{}).Preload("Details").Preload("Details.Product").Preload("FromLocation").Preload("ToLocation")
    //}
    return db
}
// Create 单条插入
func (slf *MaterialAttachmentSearch) Create(record *MaterialAttachment) error {
    var db = slf.build()
    if err := db.Create(record).Error; err != nil {
        return err
    }
    return nil
}
// CreateBatch 批量插入
func (slf *MaterialAttachmentSearch) CreateBatch(records []*MaterialAttachment) error {
    var db = slf.build()
    if err := db.Model(&MaterialAttachment{}).Create(&records).Error; err != nil {
        return fmt.Errorf("create batch err: %v, records: %+v", err, records)
    }
    return nil
}
func (slf *MaterialAttachmentSearch) Save(record *MaterialAttachment) error {
    var db = slf.build()
    if err := db.Omit("CreatedAt").Save(record).Error; err != nil {
        return fmt.Errorf("save err: %v, record: %+v", err, record)
    }
    return nil
}
func (slf *MaterialAttachmentSearch) Update(record *MaterialAttachment) error {
    var db = slf.build()
    if err := db.Omit("CreatedAt").Updates(record).Error; err != nil {
        return fmt.Errorf("save err: %v, record: %+v", err, record)
    }
    return nil
}
func (slf *MaterialAttachmentSearch) UpdateByMap(upMap map[string]interface{}) error {
    var (
        db = slf.build()
    )
    if err := db.Updates(upMap).Error; err != nil {
        return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap)
    }
    return nil
}
func (slf *MaterialAttachmentSearch) UpdateByQuery(query string, args []interface{}, upMap map[string]interface{}) error {
    var (
        db = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
    if err := db.Updates(upMap).Error; err != nil {
        return fmt.Errorf("update by query err: %v, query: %s, args: %+v, upMap: %+v", err, query, args, upMap)
    }
    return nil
}
func (slf *MaterialAttachmentSearch) Delete() error {
    var db = slf.build()
    return db.Delete(&MaterialAttachment{}).Error
}
func (slf *MaterialAttachmentSearch) First() (*MaterialAttachment, error) {
    var (
        record = new(MaterialAttachment)
        db     = slf.build()
    )
    if err := db.First(record).Error; err != nil {
        return record, err
    }
    return record, nil
}
func (slf *MaterialAttachmentSearch) Find() ([]*MaterialAttachment, int64, error) {
    var (
        records = make([]*MaterialAttachment, 0)
        total   int64
        db      = slf.build()
    )
    if err := db.Count(&total).Error; err != nil {
        return records, total, fmt.Errorf("find count err: %v", err)
    }
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Order("created_at desc").Find(&records).Error; err != nil {
        return records, total, fmt.Errorf("find records err: %v", err)
    }
    return records, total, nil
}
func (slf *MaterialAttachmentSearch) FindNotTotal() ([]*MaterialAttachment, error) {
    var (
        records = make([]*MaterialAttachment, 0)
        db      = slf.build()
    )
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Preload("FromLocation").Preload("ToLocation").Find(&records).Error; err != nil {
        return records, fmt.Errorf("find records err: %v", err)
    }
    return records, nil
}
// FindByQuery 指定条件查询.
func (slf *MaterialAttachmentSearch) FindByQuery(query string, args []interface{}) ([]*MaterialAttachment, int64, error) {
    var (
        records = make([]*MaterialAttachment, 0)
        total   int64
        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
    if err := db.Count(&total).Error; err != nil {
        return records, total, fmt.Errorf("find by query count err: %v", err)
    }
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, total, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args)
    }
    return records, total, nil
}
// FindByQueryNotTotal 指定条件查询&不查询总条数.
func (slf *MaterialAttachmentSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*MaterialAttachment, error) {
    var (
        records = make([]*MaterialAttachment, 0)
        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args)
    }
    return records, nil
}
func (slf *MaterialAttachmentSearch) CreateBatchWithResp(records []*MaterialAttachment) ([]*MaterialAttachment, error) {
    var db = slf.build()
    if err := db.Create(&records).Error; err != nil {
        return nil, fmt.Errorf("create batch err: %v, records: %+v", err, records)
    }
    return records, nil
}