package logic import ( "context" "lone-services/pkg/modelbase" "lone-services/pkg/utils" "lone-services/rpc/order/pb" product "lone-services/rpc/product/pb" "lone-services/services/order/internal/dao" "lone-services/services/order/internal/model" "lone-services/services/order/internal/svc" "lone-services/services/order/validator" "strconv" "github.com/zeromicro/go-zero/core/logx" ) type WebCreateOneLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger BaseLogic } func NewWebCreateOneLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WebCreateOneLogic { return &WebCreateOneLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } func (l *WebCreateOneLogic) WebCreateOne(in *order.CreateOrderOneRequest) (*order.Response, error) { var v validator.OrderOneCreateValidator if fail := l.checkParams(in, &v); fail != nil { return fail, nil } adminInfo := utils.GetUserFromCtx(l.ctx) if adminInfo.ID < utils.NumberOne { return l.fail(utils.ErrorNoLoginInfo) } w := modelbase.Params{ Eq: map[string]string{ "id": strconv.FormatInt(in.AddressId, utils.NumberTen), "type": strconv.Itoa(int(in.Type)), "target_id": strconv.FormatInt(adminInfo.ID, utils.NumberTen), }, } var address dao.Address err := model.AddressModel{}.Init().GetOne(w, &address) if err != nil { return l.fail(utils.Fail) } if address.Id < utils.NumberOne { return l.out(utils.Fail, "地址不存在") } data := dao.OrderCreate{ Note: in.Note, ExpiredTime: utils.GetAfterMinutes(utils.NumberThirty), Mobile: address.Mobile, Address: address.Address, Addressee: address.Name, DistrictIds: address.DistrictIds, } ww := modelbase.Params{ Eq: map[string]string{ "type": strconv.Itoa(int(in.Type)), }, } var orderSn string switch in.Type { case dao.CartTypeSale: data.SaleId = int(adminInfo.ID) data.SaleName = adminInfo.Name data.CreateType = dao.OrderTypeSale data.Type = dao.OrderTypeSale ww.Eq["sale_id"] = strconv.FormatInt(adminInfo.ID, utils.NumberTen) orderSn = svc.GetOrderSn(strconv.Itoa(dao.OrderTypeSale), adminInfo.ID) case dao.CartTypeUser: data.UserId = int(adminInfo.ID) data.UserName = adminInfo.Name data.CreateType = dao.OrderTypePersonal data.Type = dao.OrderTypePersonal ww.Eq["user_id"] = strconv.FormatInt(adminInfo.ID, utils.NumberTen) orderSn = svc.GetOrderSn(strconv.Itoa(dao.OrderTypePersonal), adminInfo.ID) case dao.CartTypeStore: data.StoreId = int(adminInfo.ID) data.StoreName = adminInfo.Name data.SaleId = int(adminInfo.SaleId) data.GroupId = int(adminInfo.GroupId) data.SaleMobile = adminInfo.SaleMobile data.SaleProvince = int(adminInfo.SaleProvince) data.CreateType = dao.OrderTypeStore data.Type = dao.OrderTypeStore ww.Eq["store_id"] = strconv.FormatInt(adminInfo.ID, utils.NumberTen) orderSn = svc.GetOrderSn(strconv.Itoa(dao.OrderTypeStore), adminInfo.ID) } data.OrderSn = orderSn productInfo, pErr := svc.GetOneData(l.svcCtx.ProductSvcName, in.ProductId) if pErr != utils.Ok { return l.fail(utils.Fail) } if productInfo.Id != in.ProductId { return l.out(utils.Fail, "购买信息不存在") } if uint32(in.Num) > productInfo.Number { return l.out(utils.Fail, productInfo.Name+"购买数量超出库存,目前可以够买:"+strconv.Itoa(int(in.Num))) } orderProduct := dao.OrderProductCreate{ OrderSn: data.OrderSn, ProductId: int(in.ProductId), ProductName: productInfo.Name, Price: utils.RoundFloat(productInfo.Price), Number: int(in.Num), } data.ProductIds = strconv.FormatInt(in.ProductId, utils.NumberTen) data.TotalPrice = utils.RoundFloat(float64(in.Num) * productInfo.Price) mobileObj := model.OrderModel{}.Init() mobileObj.Begin() err = mobileObj.Create(&data) if err != nil { mobileObj.Rollback() return l.fail(utils.Fail) } err = model.OrderProductModel{}.Init().Create(&orderProduct) if err != nil { mobileObj.Rollback() return l.fail(utils.Fail) } cli, err := svc.GetRpcClient(l.svcCtx.ProductSvcName) if err != nil { mobileObj.Rollback() return l.fail(utils.Fail) } productClient := product.NewProductClient(cli.Conn()) _, err = productClient.Number(l.ctx, &product.NumberReq{Id: in.ProductId, Type: utils.NumberOne, Number: uint32(in.Num)}) if err != nil { mobileObj.Rollback() logx.Errorf("get products err: %v", err) return l.out(utils.Fail, productInfo.Name+"购买数量超出库存,目前可以够买:"+strconv.Itoa(int(in.Num))) } mobileObj.Commit() return l.ok(utils.NumberOne) }