diff --git a/app/admin/hdl/order/hdl_order_list.go b/app/admin/hdl/order/hdl_order_list.go index 0f3a2c2..4182b9b 100644 --- a/app/admin/hdl/order/hdl_order_list.go +++ b/app/admin/hdl/order/hdl_order_list.go @@ -8,6 +8,9 @@ import ( func OrderList(c *gin.Context) { order.OrderList(c) } +func OrderGoodsList(c *gin.Context) { + order.OrderGoodsList(c) +} func OrderExport(c *gin.Context) { order.OrderExport(c) @@ -16,7 +19,9 @@ func OrderExport(c *gin.Context) { func OrderTotal(c *gin.Context) { order.OrderTotal(c) } - +func OrderGoodsTotal(c *gin.Context) { + order.OrderGoodsTotal(c) +} func OrderCancel(c *gin.Context) { order.OrderCancel(c) } diff --git a/app/admin/svc/order/svc_order_list.go b/app/admin/svc/order/svc_order_list.go index ea17d54..cca69c8 100644 --- a/app/admin/svc/order/svc_order_list.go +++ b/app/admin/svc/order/svc_order_list.go @@ -12,8 +12,10 @@ import ( "fmt" "github.com/360EntSecGroup-Skylar/excelize" "github.com/gin-gonic/gin" + "github.com/syyongx/php2go" "github.com/tidwall/gjson" "strconv" + "strings" "time" "xorm.io/xorm" ) @@ -83,6 +85,74 @@ func OrderList(c *gin.Context) { e.OutSuc(c, res, nil) return } +func OrderGoodsList(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.GetOrderGoodsList(eg, args) + data := make([]map[string]interface{}, 0) + if list != nil { + stateArr := []string{"待制作", "制作中", "制作完成", "烘焙完成", "分拣完成", "已取消"} + enterpriseIds := make([]int, 0) + for _, v := range list { + enterpriseIds = append(enterpriseIds, utils.StrToInt(v["enterprise_id"])) + } + enterpriseMap := db.GetEnterpriseMore(eg, enterpriseIds) + for _, v := range list { + JudgePackageOrdOrdStateSecond(v["oid"], v["state"]) + enterpriseName := "" + insideEnterpriseName := "" + _, ok := enterpriseMap[utils.StrToInt(v["enterprise_id"])] + if ok { + enterpriseName = enterpriseMap[utils.StrToInt(v["enterprise_id"])].Name + insideEnterpriseName = enterpriseMap[utils.StrToInt(v["enterprise_id"])].InsideName + } + var tmp = map[string]interface{}{ + "buy_phone": v["buy_phone"], + "ord_no": v["ord_no"], + "goods_title": v["goods_title"], + "oid": v["oid"], + "price": v["price"], + "num": v["num"], + "state_str": stateArr[utils.StrToInt(v["state"])], + "state": v["state"], + "time": gjson.Get(v["buy_info"], "time").String(), + "date": gjson.Get(v["buy_info"], "date").String(), + "create_at": v["create_at"], + "enterprise_name": enterpriseName, + "inside_name": insideEnterpriseName, + "memo": v["deduct_memo"], + "make_date": utils.TimeParseDateStd1(utils.StrToInt(v["make_date"])), + } + skuData := make([]md.Sku, 0) + json.Unmarshal([]byte(v["sku"]), &skuData) + skuStr := "" + for _, v1 := range skuData { + if skuStr != "" { + skuStr += ";" + } + skuStr += v1.Value + } + tmp["sku_str"] = skuStr + data = append(data, tmp) + } + } + state := []map[string]string{ + {"name": "制作中", "value": "1"}, + {"name": "已完成", "value": "4"}, + {"name": "已取消", "value": "5"}, + } + res := map[string]interface{}{ + "total": total, + "list": data, + "state": state, + } + e.OutSuc(c, res, nil) + return +} func OrderExport(c *gin.Context) { var args map[string]string if err := c.ShouldBindJSON(&args); err != nil { @@ -272,6 +342,59 @@ func JudgePackageOrdOrdState(ord *model.Order) *model.Order { } return ord } +func JudgePackageOrdOrdStateSecond(oid, state string) string { + if state == "5" { + return state + } + var ordState, oldOrdState int + oldOrdState = utils.StrToInt(state) + ordState = oldOrdState + //全部订单 + countAll, err := db.Db.Where("oid =? and goods_type=?", oid, 0).Count(&model.OrderGoods{}) + if err != nil { + return state + } + //1、判断是否有 `制作中` 有一个就是制作中 + count1, err := db.Db.Where("oid =?", oid).And("state =? and goods_type=?", 1, 0).Count(&model.OrderGoods{}) + if err != nil { + return state + } + if count1 > 0 { + ordState = 1 + } + //3、判断是否有 `烘焙中` 要全部制作完成 + count2, err := db.Db.Where("oid =?", oid).And("state =? and goods_type=0", 2).Count(&model.OrderGoods{}) + if err != nil { + return state + } + if count2 > 0 && count2 == countAll { + ordState = 2 + } + //3、判断是否有 `分拣中` 要全部烘焙完成 + count3, err := db.Db.Where("oid =?", oid).And("state =? and goods_type=0", 3).Count(&model.OrderGoods{}) + if err != nil { + return state + } + if count3 > 0 && count3 == countAll { + ordState = 3 + } + //4、判断是否有 `已完成` 要全部已完成 + count4, err := db.Db.Where("oid =?", oid).And("state =? and goods_type=?", 4, 0).Count(&model.OrderGoods{}) + if err != nil { + return state + } + if count4 > 0 && count4 == countAll { + ordState = 4 + } + if ordState != oldOrdState { + state = utils.IntToStr(ordState) + _, err2 := db.Db.Where("oid=?", oid).Cols("state").Update(&model.Order{State: ordState}) + if err2 != nil { + return state + } + } + return state +} func OrderTotal(c *gin.Context) { var args map[string]string if err := c.ShouldBindJSON(&args); err != nil { @@ -296,6 +419,44 @@ func OrderTotal(c *gin.Context) { } where += " and oid in(" + oids + ")" } + if args["state"] != "" { + where += " and state =" + args["state"] + } + if args["start_at"] != "" { + where += " and create_at >='" + args["start_at"] + "'" + + } + if args["end_at"] != "" { + where += " and create_at <='" + args["end_at"] + "'" + } + if args["date"] != "" { + args["date"] = strings.ReplaceAll(args["date"], "-", "") + where += " and date ='" + args["date"] + "'" + + } + if args["make_date"] != "" { + args["make_date"] = strings.ReplaceAll(args["make_date"], "-", "") + where += " and make_date ='" + args["make_date"] + "'" + + } + if args["enterprise_name"] != "" { + var enterprise []model.Enterprise + eg.Where("name like ?", "%"+args["enterprise_name"]+"%").Find(&enterprise) + oids := []string{"-1"} + for _, v := range enterprise { + oids = append(oids, utils.IntToStr(v.Id)) + } + where += " and enterprise_id in(" + php2go.Implode(",", oids) + ")" + } + if args["inside_name"] != "" { + var enterprise []model.Enterprise + eg.Where("inside_name like ?", "%"+args["inside_name"]+"%").Find(&enterprise) + oids := []string{"-1"} + for _, v := range enterprise { + oids = append(oids, utils.IntToStr(v.Id)) + } + where += " and enterprise_id in(" + php2go.Implode(",", 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) @@ -310,7 +471,80 @@ func OrderTotal(c *gin.Context) { e.OutSuc(c, res, nil) return } +func OrderGoodsTotal(c *gin.Context) { + var param map[string]string + if err := c.ShouldBindJSON(¶m); err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err) + return + } + eg := db.Db + where := "goods_type=0" + if param["phone"] != "" { + where += " and o.buy_phone like '%" + param["phone"] + "%'" + } + if param["oid"] != "" { + where += " and o.oid like '%" + param["oid"] + "%'" + + } + if param["ord_no"] != "" { + where += " and o.ord_no like '%" + param["ord_no"] + "%'" + } + if param["goods_title"] != "" { + where += " and og.goods_title like '%" + param["goods_title"] + "%'" + } + if param["state"] != "" { + where += " and o.state =" + param["state"] + + } + if param["start_at"] != "" { + where += " and o.create_at >='" + param["start_at"] + "'" + + } + if param["end_at"] != "" { + where += " and o.create_at <='" + param["end_at"] + "'" + } + if param["date"] != "" { + param["date"] = strings.ReplaceAll(param["date"], "-", "") + where += " and o.date ='" + param["date"] + "'" + } + if param["make_date"] != "" { + param["make_date"] = strings.ReplaceAll(param["make_date"], "-", "") + where += " and o.make_date ='" + param["make_date"] + "'" + + } + if param["enterprise_name"] != "" { + var enterprise []model.Enterprise + eg.Where("name like ?", "%"+param["enterprise_name"]+"%").Find(&enterprise) + oids := []string{"-1"} + for _, v := range enterprise { + oids = append(oids, utils.IntToStr(v.Id)) + } + where += " and o.enterprise_id in(" + php2go.Implode(",", oids) + ")" + } + if param["inside_name"] != "" { + var enterprise []model.Enterprise + eg.Where("inside_name like ?", "%"+param["inside_name"]+"%").Find(&enterprise) + oids := []string{"-1"} + for _, v := range enterprise { + oids = append(oids, utils.IntToStr(v.Id)) + } + where += " and o.enterprise_id in(" + php2go.Implode(",", oids) + ")" + } + sql := "select SUM(IF(state!=5,num*price,0)) as amount,SUM(IF(state!=5,num,0)) as alls,SUM(IF(state=0,num,0)) as wait_do,SUM(IF(state=1,num,0)) as dos,SUM(IF(state=2,num,0)) as baking,SUM(IF(state=3,num,0)) as sorting,SUM(IF(state=4,num,0)) as success,SUM(IF(state=5,num,0)) as cancel from `order_goods` left join `order` o on o.oid=og.oid 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["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 { diff --git a/app/db/db_order_goods_list.go b/app/db/db_order_goods_list.go index ce84192..2bf7976 100644 --- a/app/db/db_order_goods_list.go +++ b/app/db/db_order_goods_list.go @@ -3,6 +3,9 @@ package db import ( "applet/app/db/model" "applet/app/utils" + "fmt" + "github.com/syyongx/php2go" + "strings" "xorm.io/xorm" ) @@ -84,3 +87,73 @@ func GetOrderGoodsByOid(eg *xorm.Engine, oid []int64) *[]model.OrderGoods { } return &order } + +func GetOrderGoodsList(eg *xorm.Engine, param map[string]string) ([]map[string]string, int64) { + where := "goods_type=0" + if param["phone"] != "" { + where += " and o.buy_phone like '%" + param["phone"] + "%'" + } + if param["oid"] != "" { + where += " and o.oid like '%" + param["oid"] + "%'" + + } + if param["ord_no"] != "" { + where += " and o.ord_no like '%" + param["ord_no"] + "%'" + } + if param["goods_title"] != "" { + where += " and og.goods_title like '%" + param["goods_title"] + "%'" + } + if param["state"] != "" { + where += " and o.state =" + param["state"] + + } + if param["start_at"] != "" { + where += " and o.create_at >='" + param["start_at"] + "'" + + } + if param["end_at"] != "" { + where += " and o.create_at <='" + param["end_at"] + "'" + } + if param["date"] != "" { + param["date"] = strings.ReplaceAll(param["date"], "-", "") + where += " and o.date ='" + param["date"] + "'" + + } + if param["make_date"] != "" { + param["make_date"] = strings.ReplaceAll(param["make_date"], "-", "") + where += " and o.make_date ='" + param["make_date"] + "'" + + } + if param["enterprise_name"] != "" { + var enterprise []model.Enterprise + eg.Where("name like ?", "%"+param["enterprise_name"]+"%").Find(&enterprise) + oids := []string{"-1"} + for _, v := range enterprise { + oids = append(oids, utils.IntToStr(v.Id)) + } + where += " and o.enterprise_id in(" + php2go.Implode(",", oids) + ")" + } + if param["inside_name"] != "" { + var enterprise []model.Enterprise + eg.Where("inside_name like ?", "%"+param["inside_name"]+"%").Find(&enterprise) + oids := []string{"-1"} + for _, v := range enterprise { + oids = append(oids, utils.IntToStr(v.Id)) + } + where += " and o.enterprise_id in(" + php2go.Implode(",", oids) + ")" + } + size := utils.StrToInt(param["limit"]) + start := (utils.StrToInt(param["page"]) - 1) * size + + sql := "SELECT %s FROM order_goods og left join `order` o on o.oid=og.oid where %s order by og.id desc %s" + sql1 := fmt.Sprintf(sql, "og.deduct_memo,og.oid,og.goods_id,og.sku_id,og.num,og.price,og.state,og.sku_code,og.goods_title,og.sku,og.success_num,og.memo,og.time,og.admin_id,og.enterprise_id,og.deduct_memo,og.ord_no,og.make_date,o.create_at,o.buy_info,o.buy_phone", where, "limit "+utils.IntToStr(start)+","+utils.IntToStr(size)) + sql2 := fmt.Sprintf(sql, "COUNT(*) as count", where, "") + nativeString, _ := QueryNativeString(eg, sql2) + count := 0 + for _, v := range nativeString { + count = utils.StrToInt(v["count"]) + } + nativeString1, _ := QueryNativeString(eg, sql1) + + return nativeString1, int64(count) +} diff --git a/app/router/admin_router.go b/app/router/admin_router.go index 27747a1..61c535e 100644 --- a/app/router/admin_router.go +++ b/app/router/admin_router.go @@ -137,6 +137,8 @@ func rGoodsPay(r *gin.RouterGroup) { } func rOrder(r *gin.RouterGroup) { r.POST("/list", orderHdl.OrderList) //用户订单 + r.POST("/goods/list", orderHdl.OrderGoodsList) //用户订单 + r.POST("/goods/total", orderHdl.OrderGoodsTotal) //用户订单统计 r.POST("/total", orderHdl.OrderTotal) //用户订单统计 r.POST("/cancel", orderHdl.OrderCancel) //用户订单取消 r.POST("/detail", orderHdl.OrderDetail) //用户订单详情