|
- package order
-
- import (
- "applet/app/admin/md"
- "applet/app/admin/svc/goods"
- "applet/app/db"
- "applet/app/db/model"
- "applet/app/e"
- "applet/app/svc"
- "applet/app/utils"
- "errors"
- "fmt"
- "github.com/gin-gonic/gin"
- "strings"
- "time"
- "xorm.io/xorm"
- )
-
- 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()
- today := utils.GetTimeRange("today")
- count, _ := sess.Where("create_at>=?", time.Unix(today["start"], 0).Format("2006-01-02 15:04:05")).Count(&model.Order{})
- i := utils.IntToStr(int(count + 1))
- if utils.StrToInt(i) < 100 {
- i = "00" + i
- }
- 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),
- State: 1,
- Date: utils.StrToInt(strings.ReplaceAll(req.BuyInfo.Date, "-", "")),
- OrdNo: "AP" + time.Now().Format("20060102") + i,
- }
- 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),
- FirstNum: utils.StrToInt(v.Num),
- Price: skuPrice[v.SkuId],
- EnterpriseId: utils.StrToInt(req.EnterpriseId),
- State: 1,
- IsNew: 1,
- OrdNo: "AP" + time.Now().Format("20060102") + i,
- }
- goodsTotal[tmp.GoodsId] += tmp.Num
- orderGoods = append(orderGoods, tmp)
- err = Total(sess, tmp)
- if err != nil {
- sess.Rollback()
- e.OutErr(c, 400, e.NewErr(400, "下单失败"))
- return
- }
- }
- 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
- }
- sess.Commit()
- svc.SysCfgSet(c, "has_new_order", "1", "")
- 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
-
- }
-
- // 统计数量
- func Total(sess *xorm.Session, tmp model.OrderGoods) error {
- stock := db.GetOrderGoodsMakeStock(sess, tmp.GoodsId, tmp.SkuId)
- if stock == nil {
- return errors.New("下单失败")
- }
- stock.Sku = tmp.Sku
- stock.SkuCode = tmp.SkuCode
- stock.GoodsTitle = tmp.GoodsTitle
- stock.WaitMakeNum += tmp.Num
- update, err := sess.Where("id=?", stock.Id).Cols("sku,sku_code,goods_title,wait_make_num").Update(stock)
- if update == 0 || err != nil {
- return errors.New("下单失败")
- }
- return nil
- }
|