From a19e0f8dac0c23cd0070305f1f2bee95f575a2ab Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Wed, 27 Mar 2024 17:48:05 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=95=86=E5=93=81=E4=B8=8B=E5=8D=95=202.?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E8=AE=A2=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/hdl/order/hdl_order_list.go | 35 ++ app/admin/hdl/order/hdl_order_pay.go | 13 + app/admin/md/good_list_resp.go | 23 +- app/admin/md/goods_pay.go | 21 ++ app/admin/md/md_goods_list.go | 6 + app/admin/svc/goods/svc_goods_list.go | 19 + app/admin/svc/order/svc_order_list.go | 484 ++++++++++++++++++++++++++ app/admin/svc/order/svc_order_pay.go | 118 +++++++ app/db/db_goods_list.go | 26 +- app/db/db_order_list.go | 78 +++++ app/db/db_sku.go | 22 ++ app/db/model/order.go | 17 + app/db/model/order_goods.go | 14 + app/router/admin_router.go | 20 +- go.mod | 11 +- 15 files changed, 885 insertions(+), 22 deletions(-) create mode 100644 app/admin/hdl/order/hdl_order_list.go create mode 100644 app/admin/hdl/order/hdl_order_pay.go create mode 100644 app/admin/md/goods_pay.go create mode 100644 app/admin/svc/order/svc_order_list.go create mode 100644 app/admin/svc/order/svc_order_pay.go create mode 100644 app/db/db_order_list.go create mode 100644 app/db/model/order.go create mode 100644 app/db/model/order_goods.go diff --git a/app/admin/hdl/order/hdl_order_list.go b/app/admin/hdl/order/hdl_order_list.go new file mode 100644 index 0000000..4040f94 --- /dev/null +++ b/app/admin/hdl/order/hdl_order_list.go @@ -0,0 +1,35 @@ +package order + +import ( + "applet/app/admin/svc/order" + "github.com/gin-gonic/gin" +) + +func OrderList(c *gin.Context) { + order.OrderList(c) +} + +func OrderTotal(c *gin.Context) { + order.OrderTotal(c) +} + +func OrderCancel(c *gin.Context) { + order.OrderCancel(c) +} + +func OrderDetail(c *gin.Context) { + order.OrderDetail(c) +} + +func OrderOutput(c *gin.Context) { + order.OrderOutput(c) +} +func OrderEdit(c *gin.Context) { + order.OrderEdit(c) +} +func OrderEditNum(c *gin.Context) { + order.OrderEditNum(c) +} +func OrderDel(c *gin.Context) { + order.OrderDel(c) +} diff --git a/app/admin/hdl/order/hdl_order_pay.go b/app/admin/hdl/order/hdl_order_pay.go new file mode 100644 index 0000000..586effa --- /dev/null +++ b/app/admin/hdl/order/hdl_order_pay.go @@ -0,0 +1,13 @@ +package order + +import ( + "applet/app/admin/svc/order" + "github.com/gin-gonic/gin" +) + +func GoodsPayCalcAmount(c *gin.Context) { + order.GoodsPayCalcAmount(c) +} +func GoodsPayCreate(c *gin.Context) { + order.GoodsPayCreate(c) +} diff --git a/app/admin/md/good_list_resp.go b/app/admin/md/good_list_resp.go index d1b31c8..e694073 100644 --- a/app/admin/md/good_list_resp.go +++ b/app/admin/md/good_list_resp.go @@ -3,15 +3,16 @@ package md import "applet/app/db/model" type MallGoodListResp struct { - GoodsId string `json:"id"` // 商品id - Price string `json:"price"` - CreateAt string `json:"create_at"` - UpdateAt string `json:"update_at"` - SkuList []model.Sku `json:"skuList"` - Title string `json:"title"` - Image string `json:"image"` - SaleStateText string `json:"state_text"` - SaleState int `json:"sale_state"` - CategoryId string `json:"category_id"` - Sales int `json:"sales"` + GoodsId string `json:"id"` // 商品id + Price string `json:"price"` + CreateAt string `json:"create_at"` + UpdateAt string `json:"update_at"` + SkuList []model.Sku `json:"skuList"` + Title string `json:"title"` + Image string `json:"image"` + SaleStateText string `json:"state_text"` + SaleState int `json:"sale_state"` + CategoryId string `json:"category_id"` + Sales int `json:"sales"` + Spe []map[string]interface{} `json:"spe"` } diff --git a/app/admin/md/goods_pay.go b/app/admin/md/goods_pay.go new file mode 100644 index 0000000..d5a9147 --- /dev/null +++ b/app/admin/md/goods_pay.go @@ -0,0 +1,21 @@ +package md + +type GoodsPayParam struct { + GoodsInfo []GoodsInfo `json:"goods_info"` + EnterpriseId string `json:"enterprise_id"` + BuyInfo BuyInfo `json:"buy_info"` +} +type BuyInfo struct { + Name string `json:"name"` + Phone string `json:"phone"` + Date string `json:"date"` + Time string `json:"time"` + Address string `json:"address"` +} +type GoodsInfo struct { + GoodsId string `json:"goods_id"` + SkuId string `json:"sku_id"` + SkuCode string `json:"sku_code"` + Num string `json:"num"` + GoodsTitle string `json:"goods_title"` +} diff --git a/app/admin/md/md_goods_list.go b/app/admin/md/md_goods_list.go index 3530abb..2946854 100644 --- a/app/admin/md/md_goods_list.go +++ b/app/admin/md/md_goods_list.go @@ -22,6 +22,8 @@ type MallGoodsListReq struct { PageSize string `json:"pageSize"` OrderBy string `json:"order_by"` OrderBySate string `json:"order_by_sate"` + StartPrice string `json:"start_price"` + EndPrice string `json:"end_price"` } type GoodsRangeData struct { @@ -50,3 +52,7 @@ type GoodListResp struct { Image string `json:"image"` State string `json:"state"` } +type Sku struct { + Name string `json:"name"` + Value string `json:"value"` +} diff --git a/app/admin/svc/goods/svc_goods_list.go b/app/admin/svc/goods/svc_goods_list.go index c8394fe..d47561f 100644 --- a/app/admin/svc/goods/svc_goods_list.go +++ b/app/admin/svc/goods/svc_goods_list.go @@ -65,6 +65,25 @@ func GetMallGoodsList(c *gin.Context, req *md.MallGoodsListReq) (interface{}, in } // 获取价格、库存、重量范围、利润空间 goods.Price = item["price"] + // spe + var spe []map[string]interface{} + var speTmp []map[string]interface{} + utils.Unserialize([]byte(item["spe"]), &spe) + for _, itemSpe := range spe { + tmp := make(map[string]interface{}) + tmp["type"] = "text" + tmp["name"] = itemSpe["name"] + subTmp := make([]map[string]interface{}, 0) + for i, v := range itemSpe["values"].([]interface{}) { + subTmp = append(subTmp, map[string]interface{}{ + "key": i, + "name": v, + }) + } + tmp["values"] = subTmp + speTmp = append(speTmp, tmp) + } + goods.Spe = speTmp goodsListResp = append(goodsListResp, goods) } diff --git a/app/admin/svc/order/svc_order_list.go b/app/admin/svc/order/svc_order_list.go new file mode 100644 index 0000000..b542c32 --- /dev/null +++ b/app/admin/svc/order/svc_order_list.go @@ -0,0 +1,484 @@ +package order + +import ( + "applet/app/admin/md" + "applet/app/db" + "applet/app/db/model" + "applet/app/e" + "applet/app/svc" + "applet/app/utils" + "encoding/json" + "fmt" + "github.com/gin-gonic/gin" + "github.com/tidwall/gjson" +) + +func OrderList(c *gin.Context) { + var args map[string]string + if err := c.ShouldBindJSON(&args); err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err) + return + } + eg := db.Db + list, total := db.GetOrderList(eg, args) + data := make([]map[string]string, 0) + if list != nil { + stateArr := []string{"待制作", "制作中", "烘焙中", "分拣中", "已完成", "已取消"} + enterpriseIds := make([]int, 0) + for _, v := range *list { + enterpriseIds = append(enterpriseIds, v.EnterpriseId) + } + enterpriseMap := make(map[int]model.Enterprise) + if len(enterpriseIds) > 0 { + enterpriseData := make([]model.Enterprise, 0) + eg.In("id", enterpriseIds).Find(&enterpriseData) + for _, v := range enterpriseData { + enterpriseMap[v.Id] = v + } + } + for _, v := range *list { + v1 := JudgePackageOrdOrdState(&v) + isCanCancel := "1" + if v1.State > 0 { + isCanCancel = "0" + } + enterpriseName := "" + _, ok := enterpriseMap[v.EnterpriseId] + if ok { + enterpriseName = enterpriseMap[v.EnterpriseId].Name + } + var tmp = map[string]string{ + "buy_phone": v1.BuyPhone, + "oid": utils.Int64ToStr(v1.Oid), + "id": utils.IntToStr(v1.Id), + "amount": v1.Amount, + "state_str": stateArr[v1.State], + "state": utils.IntToStr(v1.State), + "time": gjson.Get(v1.BuyInfo, "time").String(), + "date": gjson.Get(v1.BuyInfo, "date").String(), + "create_at": v1.CreateAt.Format("2006-01-02 15:04:05"), + "is_can_cancel": isCanCancel, + "enterprise_name": enterpriseName, + } + data = append(data, tmp) + } + } + state := []map[string]string{ + {"name": "待制作", "value": "0"}, + {"name": "制作中", "value": "1"}, + {"name": "烘焙中", "value": "2"}, + {"name": "分拣中", "value": "3"}, + {"name": "已完成", "value": "4"}, + {"name": "已取消", "value": "5"}, + } + res := map[string]interface{}{ + "total": total, + "list": data, + "state": state, + } + e.OutSuc(c, res, nil) + return +} + +// JudgePackageOrdOrdState 判断订单状态 +func JudgePackageOrdOrdState(ord *model.Order) *model.Order { + if ord.State == 5 { + return ord + } + var ordState, oldOrdState int + oldOrdState = ord.State + ordState = oldOrdState + //全部订单 + countAll, err := db.Db.Where("oid =?", ord.Oid).Count(&model.OrderGoods{}) + if err != nil { + return ord + } + //1、判断是否有 `制作中` 有一个就是制作中 + count1, err := db.Db.Where("oid =?", ord.Oid).And("state =?", 1).Count(&model.OrderGoods{}) + if err != nil { + return ord + } + if count1 > 0 { + ordState = 1 + } + //3、判断是否有 `烘焙中` 要全部制作完成 + count2, err := db.Db.Where("oid =?", ord.Oid).And("state =?", 2).Count(&model.OrderGoods{}) + if err != nil { + return ord + } + if count2 > 0 && count2 == countAll { + ordState = 2 + } + //3、判断是否有 `分拣中` 要全部烘焙完成 + count3, err := db.Db.Where("oid =?", ord.Oid).And("state =?", 3).Count(&model.OrderGoods{}) + if err != nil { + return ord + } + if count3 > 0 && count3 == countAll { + ordState = 3 + } + //4、判断是否有 `已完成` 要全部已完成 + count4, err := db.Db.Where("oid =?", ord.Oid).And("state =?", 4).Count(&model.OrderGoods{}) + if err != nil { + return ord + } + if count4 > 0 && count4 == countAll { + ordState = 4 + } + if ordState != oldOrdState { + ord.State = ordState + _, err2 := db.Db.Where("oid=?", ord.Oid).Cols("state").Update(&ord) + if err2 != nil { + return ord + } + } + return ord +} +func OrderTotal(c *gin.Context) { + var args map[string]string + if err := c.ShouldBindJSON(&args); err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err) + return + } + eg := db.Db + where := "1=1" + + if args["phone"] != "" { + where += " and buy_phone like '%" + args["phone"] + "%'" + } + if args["oid"] != "" { + where += " and oid like '%" + args["oid"] + "%'" + } + if args["goods_title"] != "" { + var orderGoods []model.OrderGoods + eg.Where("goods_title like ?", "%"+args["goods_title"]+"%").Find(&orderGoods) + oids := "-1" + for _, v := range orderGoods { + oids += "," + utils.Int64ToStr(v.Oid) + } + where += " and oid in(" + oids + ")" + } + sql := "select SUM(IF(state!=5,amount,0)) as amount,SUM(IF(state!=5,1,0)) as alls,SUM(IF(state=0,1,0)) as wait_do,SUM(IF(state=1,1,0)) as dos,SUM(IF(state=2,1,0)) as baking,SUM(IF(state=3,1,0)) as sorting,SUM(IF(state=4,1,0)) as success,SUM(IF(state=5,1,0)) as cancel from `order` where %s" + sql = fmt.Sprintf(sql, where) + nativeString, _ := db.QueryNativeString(eg, sql) + var res = make([]string, 0) + for _, v := range nativeString { + res = append(res, "订单总金额(元):"+v["amount"]) + res = append(res, "总订单数量:"+v["alls"]) + res = append(res, "待制作订单数:"+v["wait_do"]) + res = append(res, "待烘焙订单数:"+v["dos"]) + res = append(res, "已完成订单数:"+v["success"]) + res = append(res, "已取消订单数:"+v["cancel"]) + } + e.OutSuc(c, res, nil) + return +} + +func OrderCancel(c *gin.Context) { + var args map[string]string + if err := c.ShouldBindJSON(&args); err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err) + return + } + eg := db.Db + order := db.GetOrderById(eg, args["oid"]) + if order == nil { + e.OutErr(c, 400, e.NewErr(400, "订单不存在")) + return + } + order = JudgePackageOrdOrdState(order) + if order.State == 5 { + e.OutErr(c, 400, e.NewErr(400, "订单已取消")) + return + } + if order.State > 0 { + e.OutErr(c, 400, e.NewErr(400, "订单制作中,不能取消")) + return + } + order.State = 5 + eg.Where("oid=?", order.Oid).Cols("state").Update(order) + eg.Where("oid=?", order.Oid).Cols("state").Update(&model.OrderGoods{State: 5}) + e.OutSuc(c, "success", nil) + return +} + +func OrderDetail(c *gin.Context) { + var args map[string]string + if err := c.ShouldBindJSON(&args); err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err) + return + } + eg := db.Db + order := db.GetOrderById(eg, args["oid"]) + if order == nil { + e.OutErr(c, 400, e.NewErr(400, "订单不存在")) + return + } + order = JudgePackageOrdOrdState(order) + var enterpriseData model.Enterprise + eg.Where("id=?", order.EnterpriseId).Get(&enterpriseData) + stateArr := []string{"待制作", "制作中", "烘焙中", "分拣中", "已完成", "已取消"} + enterpriseName := enterpriseData.Name + + //订单商品 + var orderGoods []model.OrderGoods + eg.Where("oid=?", args["oid"]).Find(&orderGoods) + skuIds := make([]int64, 0) + gids := make([]int64, 0) + + for _, v := range orderGoods { + skuIds = append(skuIds, v.SkuId) + gids = append(gids, v.GoodsId) + } + //查商品和sku + goodsMap := db.GetGoodsMore(eg, gids) + goodsInfo := make([]map[string]interface{}, 0) + scheme, host, subDomain, moreSubDomain := svc.ImageBucketNew(c) + var tmpMap interface{} + var skus []model.Sku + eg.Table("sku").In("goods_id", gids).Find(&skus) + for _, v := range orderGoods { + skuList := make([]model.Sku, 0) + for _, skuItem := range skus { + if skuItem.GoodsId == v.GoodsId { + skuList = append(skuList, skuItem) + } + } + tmp := map[string]interface{}{ + "goods_img": "", + "id": utils.IntToStr(v.Id), + "sku_id": utils.Int64ToStr(v.SkuId), + "goods_id": utils.Int64ToStr(v.GoodsId), + "num": utils.IntToStr(v.Num), + "amount": utils.Float64ToStr(utils.StrToFloat64(v.Price) * float64(v.Num)), + "sku_str": "", + "spe": "", + "sku_list": skuList, + "goods_title": v.GoodsTitle, + } + + skuData := make([]md.Sku, 0) + json.Unmarshal([]byte(v.Sku), &skuData) + skuStr := "" + for _, v1 := range skuData { + if skuStr != "" { + skuStr += ";" + } + skuStr += v1.Name + ":" + v1.Value + } + tmp["sku_str"] = skuStr + + // spe + var spe []map[string]interface{} + var speTmp []map[string]interface{} + utils.Unserialize([]byte(goodsMap[v.GoodsId].Spe), &spe) + for _, itemSpe := range spe { + tmp1 := make(map[string]interface{}) + tmp1["type"] = "text" + tmp1["name"] = itemSpe["name"] + subTmp := make([]map[string]interface{}, 0) + for i, v2 := range itemSpe["values"].([]interface{}) { + subTmp = append(subTmp, map[string]interface{}{ + "key": i, + "name": v2, + }) + } + tmp1["values"] = subTmp + speTmp = append(speTmp, tmp1) + } + tmp["spe"] = speTmp + // 商品主图 + if goodsMap[v.GoodsId].ImageList != "" { + utils.Unserialize([]byte(goodsMap[v.GoodsId].ImageList), &tmpMap) + var imageListUrlTmp []string + for _, v := range tmpMap.([]interface{}) { + imageListUrlTmp = append(imageListUrlTmp, svc.ImageFormatWithBucketNew(scheme, host, subDomain, moreSubDomain, v.(string))) + } + if imageListUrlTmp != nil { + tmp["goods_img"] = imageListUrlTmp[0] + } + } + goodsInfo = append(goodsInfo, tmp) + } + + var res = map[string]interface{}{ + "buy_phone": order.BuyPhone, + "oid": utils.Int64ToStr(order.Oid), + "id": utils.IntToStr(order.Id), + "amount": order.Amount, + "state_str": stateArr[order.State], + "state": utils.IntToStr(order.State), + "date": gjson.Get(order.BuyInfo, "date").String(), + "buy_name": gjson.Get(order.BuyInfo, "name").String(), + "address": gjson.Get(order.BuyInfo, "address").String(), + "time": gjson.Get(order.BuyInfo, "time").String(), + "create_at": order.CreateAt.Format("2006-01-02 15:04:05"), + "enterprise_name": enterpriseName, + "goods_info": goodsInfo, + } + e.OutSuc(c, res, nil) + return +} + +func OrderOutput(c *gin.Context) { +} +func OrderEdit(c *gin.Context) { + var args map[string]string + if err := c.ShouldBindJSON(&args); err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err) + return + } + eg := db.Db + order := db.GetOrderById(eg, args["oid"]) + if order == nil { + e.OutErr(c, 400, e.NewErr(400, "订单不存在")) + return + } + var tmp md.BuyInfo + json.Unmarshal([]byte(order.BuyInfo), &tmp) + tmp.Time = args["time"] + tmp.Date = args["date"] + tmp.Phone = args["phone"] + tmp.Name = args["name"] + tmp.Address = args["address"] + order.BuyPhone = args["phone"] + order.BuyInfo = utils.SerializeStr(tmp) + eg.Where("id=?", order.Id).Cols("buy_phone,buy_info").Update(order) + e.OutSuc(c, "success", nil) + return +} +func OrderDel(c *gin.Context) { + var args map[string]string + if err := c.ShouldBindJSON(&args); err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err) + return + } + eg := db.Db + order := db.GetOrderById(eg, args["oid"]) + if order == nil { + e.OutErr(c, 400, e.NewErr(400, "订单不存在")) + return + } + sess := eg.NewSession() + defer sess.Close() + sess.Begin() + orderGoodsSku := db.GetOrderGoodsById(eg, args["id"]) + i, err2 := eg.Where("id=?", args["id"]).Delete(&model.OrderGoods{}) + if i == 0 || err2 != nil { + sess.Rollback() + e.OutErr(c, 400, e.NewErr(400, "删除失败")) + return + } + //销量扣除 + sql := `UPDATE goods SET sale=sale-%d WHERE id=%d` + sql = fmt.Sprintf(sql, orderGoodsSku.Num, orderGoodsSku.GoodsId) + db.QueryNativeStringSess(sess, sql) + order.Amount = utils.Float64ToStr(utils.StrToFloat64(order.Amount) - float64(orderGoodsSku.Num)*utils.StrToFloat64(orderGoodsSku.Price)) + _, err := sess.Where("id=?", order.Id).Cols("amount").Update(order) + if err != nil { + sess.Rollback() + e.OutErr(c, 400, e.NewErr(400, "修改失败")) + return + } + sess.Commit() + e.OutSuc(c, "success", nil) + return +} +func OrderEditNum(c *gin.Context) { + var args map[string]string + if err := c.ShouldBindJSON(&args); err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err) + return + } + eg := db.Db + order := db.GetOrderById(eg, args["oid"]) + if order == nil { + e.OutErr(c, 400, e.NewErr(400, "订单不存在")) + return + } + order = JudgePackageOrdOrdState(order) + if order.State == 5 { + e.OutErr(c, 400, e.NewErr(400, "订单已取消,不能修改")) + return + } + if order.State > 0 { + e.OutErr(c, 400, e.NewErr(400, "订单制作中,不能修改")) + return + } + sess := eg.NewSession() + defer sess.Close() + sess.Begin() + + orderGoods := db.GetOrderGoodsByIdSess(sess, args["id"]) + isHasSku := 0 + if orderGoods.SkuId != utils.StrToInt64(args["sku_id"]) { + orderGoodsSku := db.GetOrderGoodsBySkuIdSess(sess, args["oid"], args["sku_id"]) + if orderGoodsSku != nil { + isHasSku = 1 + //先删掉修改的 ,然后改另外的sku + i, err := sess.Where("id=?", args["id"]).Delete(&model.OrderGoods{}) + if i == 0 || err != nil { + sess.Rollback() + e.OutErr(c, 400, e.NewErr(400, "修改失败")) + return + } + //销量扣除 + sql := `UPDATE goods SET sale=sale-%d WHERE id=%d` + sql = fmt.Sprintf(sql, orderGoods.Num, orderGoods.GoodsId) + db.QueryNativeStringSess(sess, sql) + //销量增加 + sqlSecond := `UPDATE goods SET sale=sale+%d WHERE id=%d` + sqlSecond = fmt.Sprintf(sqlSecond, args["num"], orderGoodsSku.GoodsId) + db.QueryNativeStringSess(sess, sqlSecond) + //重新计算价格 + orderGoodsSku.Num += utils.StrToInt(args["num"]) + order.Amount = utils.Float64ToStr(utils.StrToFloat64(order.Amount) + utils.StrToFloat64(args["num"])*utils.StrToFloat64(orderGoodsSku.Price)) + _, err = sess.Where("id=?", orderGoodsSku.Id).Cols("num").Update(orderGoodsSku) + if err != nil { + sess.Rollback() + e.OutErr(c, 400, e.NewErr(400, "修改失败")) + return + } + } else { + orderGoods.SkuId = utils.StrToInt64(args["sku_id"]) + _, sku, _ := db.GetMallSkuBySkuId(eg, args["sku_id"], utils.Int64ToStr(orderGoods.GoodsId)) + if sku != nil { + orderGoods.SkuCode = sku.SkuCode + orderGoods.Sku = sku.Sku + } + } + } + if isHasSku == 0 { + num := utils.StrToInt(args["num"]) - orderGoods.Num + if num < 0 { //少要了 就总金额 减少 + num = orderGoods.Num - utils.StrToInt(args["num"]) + order.Amount = utils.Float64ToStr(utils.StrToFloat64(order.Amount) - float64(num)*utils.StrToFloat64(orderGoods.Price)) + //销量扣除 + sqlSecond := `UPDATE goods SET sale=sale-%d WHERE id=%d` + sqlSecond = fmt.Sprintf(sqlSecond, num, orderGoods.GoodsId) + db.QueryNativeStringSess(sess, sqlSecond) + } else { + order.Amount = utils.Float64ToStr(utils.StrToFloat64(order.Amount) + float64(num)*utils.StrToFloat64(orderGoods.Price)) + //销量增加 + sqlSecond := `UPDATE goods SET sale=sale+%d WHERE id=%d` + sqlSecond = fmt.Sprintf(sqlSecond, num, orderGoods.GoodsId) + db.QueryNativeStringSess(sess, sqlSecond) + } + orderGoods.Num = utils.StrToInt(args["num"]) + _, err := sess.Where("id=?", orderGoods.Id).Cols("num,sku_code,sku_id,sku").Update(orderGoods) + if err != nil { + sess.Rollback() + e.OutErr(c, 400, e.NewErr(400, "修改失败")) + return + } + } + _, err := sess.Where("id=?", order.Id).Cols("amount").Update(order) + if err != nil { + sess.Rollback() + e.OutErr(c, 400, e.NewErr(400, "修改失败")) + return + } + sess.Commit() + e.OutSuc(c, "success", nil) + return +} diff --git a/app/admin/svc/order/svc_order_pay.go b/app/admin/svc/order/svc_order_pay.go new file mode 100644 index 0000000..726bbb9 --- /dev/null +++ b/app/admin/svc/order/svc_order_pay.go @@ -0,0 +1,118 @@ +package order + +import ( + "applet/app/admin/md" + "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], + } + 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) + for _, v := range req.GoodsInfo { + skuIds = append(skuIds, utils.StrToInt64(v.SkuId)) + } + if len(skuIds) == 0 { + return 0, skuPrice + } + engine := db.Db + 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 + } + skuPrice[v.SkuId] = sku.Price + amount += utils.StrToFloat64(sku.Price) * utils.StrToFloat64(v.Num) + } + return amount, skuPrice + +} diff --git a/app/db/db_goods_list.go b/app/db/db_goods_list.go index 357cb2b..0ab6c7e 100644 --- a/app/db/db_goods_list.go +++ b/app/db/db_goods_list.go @@ -128,9 +128,15 @@ func GetMallGoodsListLeftOnMallSku(engine *xorm.Engine, req *md.MallGoodsListReq } if req.SaleState == "1" { // 销售中 - whereCondition = whereCondition + " AND sale_state = 1" + whereCondition = whereCondition + " AND mg.sale_state = 1" } else if req.SaleState == "2" { // 已下架 - whereCondition = whereCondition + " AND sale_state = 2" + whereCondition = whereCondition + " AND mg.sale_state = 2" + } + if utils.StrToFloat64(req.StartPrice) > 0 { // 价格区间 + whereCondition = whereCondition + " AND mg.price >=" + req.StartPrice + } + if utils.StrToFloat64(req.EndPrice) > 0 { // 价格区间 + whereCondition = whereCondition + " AND mg.price <=" + req.EndPrice } if req.OrderBySate == "" { @@ -146,6 +152,8 @@ func GetMallGoodsListLeftOnMallSku(engine *xorm.Engine, req *md.MallGoodsListReq orderBy = "mg.profit_rate DESC" case "sale_count_desc": orderBy = "mg.sale_count DESC" + case "price_desc": + orderBy = "mg.price DESC" } } if req.OrderBySate == "asc" { @@ -156,6 +164,8 @@ func GetMallGoodsListLeftOnMallSku(engine *xorm.Engine, req *md.MallGoodsListReq orderBy = "mg.profit_rate ASC" case "sale_count_desc": orderBy = "mg.sale_count ASC" + case "price_desc": + orderBy = "mg.price ASC" } } @@ -275,3 +285,15 @@ func GetMallGoodsById(Db *xorm.Engine, goodsId string) (mm *model.Goods, err err } return &m, nil } +func GetGoodsMore(engine *xorm.Engine, gids []int64) map[int64]model.Goods { + skuMap := make(map[int64]model.Goods) + var skus []model.Goods + err := engine.In("goods_id", gids).Find(&skus) + if err != nil { + return skuMap + } + for _, v := range skus { + skuMap[v.GoodsId] = v + } + return skuMap +} diff --git a/app/db/db_order_list.go b/app/db/db_order_list.go new file mode 100644 index 0000000..d35c0f2 --- /dev/null +++ b/app/db/db_order_list.go @@ -0,0 +1,78 @@ +package db + +import ( + "applet/app/db/model" + "applet/app/utils" + "xorm.io/xorm" +) + +func GetOrderById(eg *xorm.Engine, id string) *model.Order { + var order model.Order + get, err := eg.Where("oid=?", id).Get(&order) + if get == false || err != nil { + return nil + } + return &order +} +func GetOrderGoodsById(eg *xorm.Engine, id string) *model.OrderGoods { + var order model.OrderGoods + get, err := eg.Where("id=?", id).Get(&order) + if get == false || err != nil { + return nil + } + return &order +} +func GetOrderGoodsBySkuId(eg *xorm.Engine, oid, skuId string) *model.OrderGoods { + var order model.OrderGoods + get, err := eg.Where("oid=? and sku_id=?", oid, skuId).Get(&order) + if get == false || err != nil { + return nil + } + return &order +} + +func GetOrderGoodsByIdSess(sess *xorm.Session, id string) *model.OrderGoods { + var order model.OrderGoods + get, err := sess.Where("id=?", id).Get(&order) + if get == false || err != nil { + return nil + } + return &order +} +func GetOrderGoodsBySkuIdSess(sess *xorm.Session, oid, skuId string) *model.OrderGoods { + var order model.OrderGoods + get, err := sess.Where("oid=? and sku_id=?", oid, skuId).Get(&order) + if get == false || err != nil { + return nil + } + return &order +} +func GetOrderList(eg *xorm.Engine, param map[string]string) (*[]model.Order, int64) { + var order []model.Order + sess := eg.Where("1=1") + if param["phone"] != "" { + sess.And("buy_phone like ?", "%"+param["phone"]+"%") + } + if param["oid"] != "" { + sess.And("oid like ?", "%"+param["oid"]+"%") + } + if param["state"] != "" { + sess.And("state =", param["state"]) + } + if param["goods_title"] != "" { + var orderGoods []model.OrderGoods + eg.Where("goods_title like ?", "%"+param["goods_title"]+"%").Find(&orderGoods) + oids := []int64{-1} + for _, v := range orderGoods { + oids = append(oids, v.Oid) + } + sess.In("oid", oids) + } + size := utils.StrToInt(param["limit"]) + start := (utils.StrToInt(param["page"]) - 1) * size + count, err := sess.Limit(size, start).OrderBy("id desc").FindAndCount(&order) + if err != nil { + return nil, count + } + return &order, count +} diff --git a/app/db/db_sku.go b/app/db/db_sku.go index 3cbf791..5ae1d23 100644 --- a/app/db/db_sku.go +++ b/app/db/db_sku.go @@ -20,3 +20,25 @@ func GetMallSkuBySkuCode(engine *xorm.Engine, skuCode, goodsId string) (isHas bo } return isHas, &m, nil } + +func GetMallSkuBySkuId(engine *xorm.Engine, skuId, goodsId string) (isHas bool, mm *model.Sku, err error) { + isHas = false + var m model.Sku + isHas, err = engine.Where("sku_id=?", skuId).And("goods_id!=?", goodsId).Get(&m) + if err != nil { + return isHas, &m, err + } + return isHas, &m, nil +} +func GetSkuMore(engine *xorm.Engine, skuIds []int64) map[int64]model.Sku { + skuMap := make(map[int64]model.Sku) + var skus []model.Sku + err := engine.Table("sku").In("sku_id", skuIds).Find(&skus) + if err != nil { + return skuMap + } + for _, v := range skus { + skuMap[v.SkuId] = v + } + return skuMap +} diff --git a/app/db/model/order.go b/app/db/model/order.go new file mode 100644 index 0000000..371b90c --- /dev/null +++ b/app/db/model/order.go @@ -0,0 +1,17 @@ +package model + +import ( + "time" +) + +type Order struct { + Id int `json:"id" xorm:"not null pk autoincr INT(11)"` + Oid int64 `json:"oid" xorm:"comment('订单号') BIGINT(20)"` + State int `json:"state" xorm:"default 0 comment('状态 0待制作 1制作中 2烘焙中 3分拣中 4已完成 5已取消') INT(11)"` + CreateAt time.Time `json:"create_at" xorm:"comment('下单时间') DATETIME"` + CancelAt time.Time `json:"cancel_at" xorm:"comment('取消时间') DATETIME"` + EnterpriseId int `json:"enterprise_id" xorm:"default 0 comment('校企id') INT(11)"` + BuyInfo string `json:"buy_info" xorm:"comment('购买人信息 json') VARCHAR(2000)"` + Amount string `json:"amount" xorm:"default 0.00 comment('付款金额') DECIMAL(20,2)"` + BuyPhone string `json:"buy_phone" xorm:"comment('购买人手机 ') VARCHAR(20)"` +} diff --git a/app/db/model/order_goods.go b/app/db/model/order_goods.go new file mode 100644 index 0000000..27595bc --- /dev/null +++ b/app/db/model/order_goods.go @@ -0,0 +1,14 @@ +package model + +type OrderGoods struct { + Id int `json:"id" xorm:"not null pk autoincr INT(11)"` + Oid int64 `json:"oid" xorm:"default 0 comment('订单号') BIGINT(20)"` + GoodsId int64 `json:"goods_id" xorm:"default 0 comment('商品id') INT(11)"` + SkuId int64 `json:"sku_id" xorm:"default 0 comment('sku') INT(255)"` + Num int `json:"num" xorm:"default 0 comment('购买数量') INT(11)"` + Price string `json:"price" xorm:"default 0.00 comment('单价') DECIMAL(20,2)"` + SkuCode string `json:"sku_code" xorm:"default 0.00 comment('') VARCHAR(255)"` + Sku string `json:"sku_code" xorm:"default 0.00 comment('') VARCHAR(255)"` + GoodsTitle string `json:"goods_title" xorm:"default 0.00 comment('') VARCHAR(255)"` + State int `json:"state" xorm:"default 0 comment('状态 0待制作 1制作中 2烘焙中 3分拣中 4已完成 ') INT(11)"` +} diff --git a/app/router/admin_router.go b/app/router/admin_router.go index f217172..e591e86 100644 --- a/app/router/admin_router.go +++ b/app/router/admin_router.go @@ -4,6 +4,7 @@ import ( "applet/app/admin/hdl" categoryHdl "applet/app/admin/hdl/category" goodsHdl "applet/app/admin/hdl/goods" + orderHdl "applet/app/admin/hdl/order" "applet/app/admin/mw" "applet/app/cfg" "github.com/gin-gonic/gin" @@ -121,7 +122,22 @@ func rRole(r *gin.RouterGroup) { r.GET("/adminInfo", hdl.AdminInfo) //获取管理员信息 r.POST("/bindAdminRole", hdl.BindAdminRole) //绑定角色 } - +func rGoodsPay(r *gin.RouterGroup) { + r.GET("/enterpriseList", hdl.EnterpriseList) //校企列表 + r.GET("/goodsList", goodsHdl.GetMallGoodsList) //商品列表 + r.POST("/calcAmount", orderHdl.GoodsPayCalcAmount) //商品下单计算价格 + r.POST("/create", orderHdl.GoodsPayCreate) //商品下单 +} +func rOrder(r *gin.RouterGroup) { + r.POST("/list", orderHdl.OrderList) //用户订单 + r.POST("/total", orderHdl.OrderTotal) //用户订单统计 + r.POST("/cancel", orderHdl.OrderCancel) //用户订单取消 + r.POST("/detail", orderHdl.OrderDetail) //用户订单详情 + r.POST("/output", orderHdl.OrderOutput) //用户订单导出 + r.POST("/del", orderHdl.OrderDel) //用户订单商品删除 + r.POST("/edit", orderHdl.OrderEdit) //用户订单修改购买人信息 + r.POST("/editNum", orderHdl.OrderEditNum) //用户订单修改订单数 +} func AdminRoute(r *gin.RouterGroup) { r.GET("/demo", hdl.Demo) @@ -146,4 +162,6 @@ func AdminRoute(r *gin.RouterGroup) { rRole(r.Group("/role")) //权限管理 rSetCenter(r.Group("/setCenter")) //设置中心 rUser(r.Group("/user")) //用户管理 + rGoodsPay(r.Group("/goodsPay")) //商品下单 + rOrder(r.Group("/order")) //用户订单 } diff --git a/go.mod b/go.mod index 2757707..b3f6443 100644 --- a/go.mod +++ b/go.mod @@ -3,15 +3,12 @@ module applet go 1.15 require ( - github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5 github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 github.com/antchfx/htmlquery v1.3.0 // indirect github.com/antchfx/xmlquery v1.3.18 // indirect github.com/boombuler/barcode v1.0.1 - github.com/dchest/uniuri v0.0.0-20200228104902-7aecb25e1fe5 github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/forgoer/openssl v0.0.0-20201023062029-c3112b0c8700 - github.com/gin-contrib/sessions v0.0.3 github.com/gin-gonic/gin v1.6.3 github.com/go-openapi/spec v0.20.3 // indirect github.com/go-openapi/swag v0.19.15 // indirect @@ -26,23 +23,21 @@ require ( github.com/golang/snappy v0.0.3 // indirect github.com/gomodule/redigo v2.0.0+incompatible github.com/gookit/color v1.3.8 // indirect - github.com/gorilla/sessions v1.2.1 // indirect - github.com/jinzhu/copier v0.4.0 // indirect + github.com/jinzhu/copier v0.4.0 github.com/json-iterator/go v1.1.10 // indirect github.com/kennygrant/sanitize v1.2.4 // indirect github.com/leodido/go-urn v1.2.1 // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/makiuchi-d/gozxing v0.0.0-20210324052758-57132e828831 - github.com/mcuadros/go-defaults v1.2.0 // indirect + github.com/mcuadros/go-defaults v1.2.0 github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.1 // indirect github.com/onsi/ginkgo v1.15.0 // indirect github.com/onsi/gomega v1.10.5 // indirect - github.com/pkg/errors v0.9.1 // indirect + github.com/pkg/errors v0.9.1 github.com/qiniu/api.v7/v7 v7.8.2 github.com/robfig/cron/v3 v3.0.1 github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d // indirect - github.com/smartystreets/goconvey v1.6.4 // indirect github.com/sony/sonyflake v1.0.0 github.com/stretchr/testify v1.7.0 // indirect github.com/swaggo/swag v1.7.0