@@ -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 {