| | |
| | | return |
| | | } |
| | | //params.ID = utils.GetUUID() |
| | | err := models.NewMaterialSearch().Create(¶ms) |
| | | |
| | | err := models.WithTransaction(func(tx *gorm.DB) error { |
| | | if err := models.NewMaterialSearch().SetOrm(tx).Create(¶ms); err != nil { |
| | | return err |
| | | } |
| | | 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().SetOrm(tx).CreateBatch(materialAttachmentList); err != nil { |
| | | return err |
| | | } |
| | | } |
| | | avs := make([]*models.AttributeValue, 0) |
| | | for _, v := range params.Attributes { |
| | | av := models.AttributeValue{ |
| | | Model: gorm.Model{}, |
| | | EntityID: params.ID, |
| | | AttributeID: v.ID, |
| | | Value: v.Value, |
| | | } |
| | | avs = append(avs, &av) |
| | | |
| | | } |
| | | if err := models.NewAttributeValueSearch().SetOrm(tx).CreateBatch(avs); err != nil { |
| | | return err |
| | | } |
| | | return nil |
| | | }) |
| | | if err != nil { |
| | | 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, "保存成功") |
| | |
| | | util.ResponseFormat(c, code.RequestParamError, "产品信息更新失败") |
| | | return |
| | | } |
| | | |
| | | //更新在库的产品类型 |
| | | if params.CategoryId > 0 { |
| | | find, _ := models.NewLocationProductAmountSearch().SetProductId(params.ID).Find() |
| | | if len(find) > 0 { |
| | | var ids []int |
| | | for _, f := range find { |
| | | if f.ProductCategoryID != params.CategoryId { |
| | | ids = append(ids, f.Id) |
| | | err = models.WithTransaction(func(tx *gorm.DB) error { |
| | | //更新在库的产品类型 |
| | | if params.CategoryId > 0 { |
| | | find, _ := models.NewLocationProductAmountSearch().SetProductId(params.ID).Find() |
| | | if len(find) > 0 { |
| | | var ids []int |
| | | for _, f := range find { |
| | | if f.ProductCategoryID != params.CategoryId { |
| | | ids = append(ids, f.Id) |
| | | } |
| | | } |
| | | m := make(map[string]interface{}) |
| | | m["productCategoryId"] = params.CategoryId |
| | | _ = models.NewLocationProductAmountSearch().SetOrm(tx).SetIds(ids).UpdateByMap(m) |
| | | } |
| | | m := make(map[string]interface{}) |
| | | m["productCategoryId"] = params.CategoryId |
| | | _ = models.NewLocationProductAmountSearch().SetIds(ids).UpdateByMap(m) |
| | | } |
| | | } |
| | | materialAttachmentList := []*models.MaterialAttachment{} |
| | | for _, v := range params.AttachmentIDs { |
| | | ma := &models.MaterialAttachment{MaterialID: params.ID, AttachmentID: v} |
| | | materialAttachmentList = append(materialAttachmentList, ma) |
| | | } |
| | | if err := models.NewMaterialAttachmentSearch().SetOrm(tx).SetMaterialID(params.ID).Delete(); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "产品附件清除失败") |
| | | return err |
| | | } |
| | | if len(materialAttachmentList) > 0 { |
| | | if err := models.NewMaterialAttachmentSearch().SetOrm(tx).CreateBatch(materialAttachmentList); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "产品信息更新失败") |
| | | return err |
| | | } |
| | | } |
| | | |
| | | 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, "产品附件清除失败") |
| | | attributeValueSearch := models.NewAttributeValueSearch() |
| | | //删除从新插入 |
| | | if err := attributeValueSearch.SetOrm(tx).SetEntityID(params.ID).Delete(); err != nil { |
| | | return err |
| | | } |
| | | avs := make([]*models.AttributeValue, 0) |
| | | for _, v := range params.Attributes { |
| | | av := models.AttributeValue{ |
| | | Model: gorm.Model{}, |
| | | EntityID: params.ID, |
| | | AttributeID: v.ID, |
| | | Value: v.Value, |
| | | } |
| | | avs = append(avs, &av) |
| | | |
| | | } |
| | | if err := models.NewAttributeValueSearch().SetOrm(tx).CreateBatch(avs); err != nil { |
| | | return err |
| | | } |
| | | return nil |
| | | }) |
| | | if 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, "更新成功") |
| | | } |
| | | |