1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
| package biz
import ( "context" "encoding/json" "errors" "github.com/go-kratos/kratos/v2/log" "goods/internal/domain" )
type GoodsRepo interface { CreateGoods(ctx context.Context, goods *domain.Goods) (*domain.Goods, error) }
type GoodsUsecase struct { repo GoodsRepo tr Transaction skuRepo GoodsSkuRepo categoryRepo CategoryRepo brandRepo BrandRepo typeRepo GoodsTypeRepo specificationRepo SpecificationRepo goodsAttrRepo GoodsAttrRepo inventoryRepo InventoryRepo log *log.Helper }
func NewGoodsUsecase(repo GoodsRepo, skuRepo GoodsSkuRepo, tx Transaction, gRepo GoodsTypeRepo, cRepo CategoryRepo, bRepo BrandRepo, sRepo SpecificationRepo, aRepo GoodsAttrRepo, iRepo InventoryRepo, logger log.Logger) *GoodsUsecase {
return &GoodsUsecase{ repo: repo, skuRepo: skuRepo, tr: tx, typeRepo: gRepo, categoryRepo: cRepo, brandRepo: bRepo, specificationRepo: sRepo, goodsAttrRepo: aRepo, inventoryRepo: iRepo, log: log.NewHelper(logger), } }
func (g GoodsUsecase) CreateGoods(ctx context.Context, r *domain.Goods) (*domain.GoodsInfoResponse, error) { var ( err error goods *domain.Goods ) _, err = g.brandRepo.IsBrandByID(ctx, r.BrandsID) if err != nil { return nil, errors.New("品牌不存在") }
_, err = g.categoryRepo.GetCategoryByID(ctx, r.CategoryID) if err != nil { return nil, errors.New("分类不存在") } _, err = g.typeRepo.IsExistsByID(ctx, r.TypeID) if err != nil { return nil, errors.New("商品类型不存在") } for _, sku := range r.Sku { var sIDs []*int64 for _, info := range sku.Specification { sIDs = append(sIDs, &info.SpecificationID) }
specList, err := g.specificationRepo.ListByIds(ctx, sIDs...) if err != nil { return nil, err } for _, sId := range sIDs { info := specList.FindById(*sId) if info == nil { return nil, errors.New("商品规格不存在") } } var attrIDs []int64 for _, attr := range sku.GroupAttr { for _, id := range attr.Attr { attrIDs = append(attrIDs, id.AttrID) } } attrList, err := g.goodsAttrRepo.ListByIds(ctx, attrIDs...) if err != nil { return nil, err }
for _, attr := range sku.GroupAttr { for _, id := range attr.Attr { attrIDs = append(attrIDs, id.AttrID) true := attrList.IsNotExist(attr.GroupId, id.AttrID) if true { return nil, errors.New("商品属性不存在") } } } }
err = g.tr.ExecTx(ctx, func(ctx context.Context) error { goods, err = g.repo.CreateGoods(ctx, &domain.Goods{ CategoryID: r.CategoryID, BrandsID: r.BrandsID, TypeID: r.TypeID, Name: r.Name, NameAlias: r.NameAlias, GoodsSn: r.GoodsSn, GoodsTags: r.GoodsTags, MarketPrice: r.MarketPrice, GoodsBrief: r.GoodsBrief, GoodsFrontImage: r.GoodsFrontImage, GoodsImages: r.GoodsImages, OnSale: r.OnSale, IsNew: r.IsNew, IsHot: r.IsHot, ShipFree: r.ShipFree, ShipID: r.ShipID, }) if err != nil { return err } for _, v := range r.Sku { res := &domain.GoodsSku{ GoodsID: goods.ID, GoodsSn: goods.GoodsSn, GoodsName: goods.Name, SkuName: v.SkuName, SkuCode: v.SkuCode, BarCode: v.BarCode, Price: v.Price, PromotionPrice: v.PromotionPrice, Points: v.Points, RemarksInfo: v.RemarksInfo, Pic: v.Pic, Inventory: v.Inventory, OnSale: v.OnSale, }
goodsAttr, err := json.Marshal(v.GroupAttr) if err != nil { return err } res.AttrInfo = string(goodsAttr)
skuInfo, err := g.skuRepo.Create(ctx, res) if err != nil { return err }
_, err = g.inventoryRepo.Create(ctx, &domain.Inventory{ SkuID: skuInfo.ID, Inventory: skuInfo.Inventory, }) if err != nil { return err } var skuRelation []*domain.GoodsSpecificationSku for _, spec := range v.Specification { skuRelation = append(skuRelation, &domain.GoodsSpecificationSku{ SkuID: skuInfo.ID, SkuCode: skuInfo.SkuCode, SpecificationId: spec.SpecificationID, ValueId: spec.SpecificationValueID, }) }
err = g.skuRepo.CreateSkuRelation(ctx, skuRelation) if err != nil { return err }
} return nil })
if err != nil { return nil, err } return &domain.GoodsInfoResponse{GoodsID: goods.ID}, nil }
|