package order import ( "applet/app/admin/md" "applet/app/admin/svc/goods" "applet/app/db" "applet/app/db/model" "applet/app/e" "applet/app/utils" "fmt" "github.com/gin-gonic/gin" "time" ) func GoodsPayCalcAmount(c *gin.Context) { var req md.GoodsPayParam if err := c.ShouldBindJSON(&req); err != nil { e.OutErr(c, e.ERR_INVALID_ARGS, err) return } amount, _ := commCalc(c, req) res := map[string]string{ "amount": utils.Float64ToStr(amount), } e.OutSuc(c, res, nil) return } func GoodsPayCreate(c *gin.Context) { var req md.GoodsPayParam if err := c.ShouldBindJSON(&req); err != nil { e.OutErr(c, e.ERR_INVALID_ARGS, err) return } amount, skuPrice := commCalc(c, req) if amount <= 0 { e.OutErr(c, 400, e.NewErr(400, "价格计算错误,下单失败")) return } sess := db.Db.NewSession() defer sess.Close() sess.Begin() sess.Commit() var order = model.Order{ Oid: utils.StrToInt64(utils.OrderUUID(utils.StrToInt(req.EnterpriseId))), CreateAt: time.Now(), EnterpriseId: utils.StrToInt(req.EnterpriseId), BuyInfo: utils.SerializeStr(req.BuyInfo), BuyPhone: req.BuyInfo.Phone, Amount: utils.Float64ToStr(amount), } one, err := sess.InsertOne(&order) if one == 0 || err != nil { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "下单失败")) return } var orderGoods = make([]model.OrderGoods, 0) goodsTotal := make(map[int64]int) skuIds := make([]int64, 0) gids := make([]int64, 0) for _, v := range req.GoodsInfo { skuIds = append(skuIds, utils.StrToInt64(v.SkuId)) gids = append(gids, utils.StrToInt64(v.GoodsId)) } skuMap := db.GetSkuMore(db.Db, skuIds) goodsMap := db.GetGoodsMore(db.Db, gids) for _, v := range req.GoodsInfo { tmp := model.OrderGoods{ Oid: order.Oid, GoodsId: utils.StrToInt64(v.GoodsId), GoodsTitle: goodsMap[utils.StrToInt64(v.GoodsId)].Title, SkuId: utils.StrToInt64(v.SkuId), SkuCode: skuMap[utils.StrToInt64(v.SkuId)].SkuCode, Sku: skuMap[utils.StrToInt64(v.SkuId)].Sku, Num: utils.StrToInt(v.Num), Price: skuPrice[v.SkuId], EnterpriseId: utils.StrToInt(req.EnterpriseId), } goodsTotal[tmp.GoodsId] += tmp.Num orderGoods = append(orderGoods, tmp) } for k, v := range goodsTotal { sql := `UPDATE goods SET sale=sale-%d WHERE goods_id=%d` sql = fmt.Sprintf(sql, v, k) db.QueryNativeStringSess(sess, sql) } one, err = sess.Insert(&orderGoods) if one == 0 || err != nil { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "下单失败")) return } e.OutSuc(c, "success", nil) return } func commCalc(c *gin.Context, req md.GoodsPayParam) (float64, map[string]string) { skuPrice := make(map[string]string, 0) skuIds := make([]int64, 0) gids := make([]string, 0) for _, v := range req.GoodsInfo { skuIds = append(skuIds, utils.StrToInt64(v.SkuId)) gids = append(gids, v.GoodsId) } if len(skuIds) == 0 { return 0, skuPrice } engine := db.Db skuSchemeMap := goods.GetSkuScheme(engine, req.EnterpriseId, gids) skuMap := db.GetSkuMore(engine, skuIds) //计算价格 TODO 对应方案的价格怎么算 var amount float64 = 0 for _, v := range req.GoodsInfo { sku, ok := skuMap[utils.StrToInt64(v.SkuId)] if ok == false { return 0, skuPrice } //方案的价格 skuScheme, ok := skuSchemeMap[sku.SkuId] if ok { sku.Price = skuScheme.Price } skuPrice[v.SkuId] = sku.Price amount += utils.StrToFloat64(sku.Price) * utils.StrToFloat64(v.Num) } return amount, skuPrice }