|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- 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()
- sysCfgDb := db.SysCfgDb{}
- sysCfgDb.Set()
- beforeHourShowOrder, _ := sysCfgDb.SysCfgGetOne("before_hour_show_order")
- if utils.StrToInt(beforeHourShowOrder.Val) > time.Now().Hour() {
- e.OutErr(c, 400, e.NewErr(400, "请"+beforeHourShowOrder.Val+"点后再下单"))
- return
- }
- 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,
- MakeDate: utils.StrToInt(strings.ReplaceAll(req.MakeDate, "-", "")),
- }
- 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,
- MakeDate: utils.StrToInt(strings.ReplaceAll(req.MakeDate, "-", "")),
- }
- 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.GetOrderGoodsMakeStockByDate(sess, tmp.GoodsId, tmp.SkuId, tmp.MakeDate)
- 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
- }
|