package svc import ( md2 "applet/app/admin/md" "applet/app/bigData/md" "applet/app/db" "applet/app/db/model" "applet/app/utils" "encoding/json" "fmt" "github.com/gin-gonic/gin" "strings" "time" ) func LeftDataForMake() (resp []md.LeftDataForMakeResp, err error) { //now := time.Now() //startDate := now.Format("2006-01-02") + " 00:00:00" //endDate := now.AddDate(0, 0, 1).Format("2006-01-02") + " 00:00:00" //engine := db.Db //var enterprise []model.Enterprise //err = engine.Where("1=1").Find(&enterprise) //if err != nil { // return //} //var enterpriseMap = map[int]model.Enterprise{} //for _, v := range enterprise { // enterpriseMap[v.Id] = v //} // ////1、查找当天的 order 数据(大于 待制作) //var orders []model.Order //err = engine.Where("create_at >= ?", startDate).And("create_at < ?", endDate).And("state > ? and state != 5", 0).Find(&orders) //if err != nil { // return //} //var oIds []int64 //for _, v := range orders { // oIds = append(oIds, v.Oid) //} // ////2、查找对应的订单产品记录 //var orderGoods []model.OrderGoods //err = engine.In("oid", oIds).Find(&orderGoods) //if err != nil { // return //} //var orderGoodsMap = map[int64][]model.OrderGoods{} //var records []int //for _, v := range orderGoods { // if orderGoodsMap[v.Oid] == nil { // orderGoodsMap[v.Oid] = []model.OrderGoods{} // } // orderGoodsMap[v.Oid] = append(orderGoodsMap[v.Oid], v) // records = append(records, v.Id) //} // ////3、用户订单商品操作流程 //var orderGoodsDoingFlows []model.OrderGoodsDoingFlow //err = engine.Where("state = 2").In("record_id", records).Find(&orderGoodsDoingFlows) //if err != nil { // return //} //var orderGoodsDoingFlowsMap = map[int]model.OrderGoodsDoingFlow{} //for _, v := range orderGoodsDoingFlows { // orderGoodsDoingFlowsMap[v.RecordId] = v //} // ////4、整合数据 //orderStateArr := []string{"待制作", "制作中", "烘焙完成", "分拣完成", "已取消"} //for _, v := range orders { // var list []md.LeftDataForMakeList // if len(orderGoodsMap[v.Oid]) > 0 { // orderGoodsStateArr := []string{"待制作", "制作中"} // for _, vv := range orderGoodsMap[v.Oid] { // skuData := make([]md2.Sku, 0) // json.Unmarshal([]byte(vv.Sku), &skuData) // skuStr := "" // for _, v1 := range skuData { // if skuStr != "" { // skuStr += ";" // } // skuStr += v1.Value // } // var stateZh = "已完成" // if vv.State <= 1 { // stateZh = orderGoodsStateArr[vv.State] // } // list = append(list, md.LeftDataForMakeList{ // GoodsName: vv.GoodsTitle, // Sku: skuStr, // Num: vv.Num, // SuccessNum: orderGoodsDoingFlowsMap[vv.Id].SuccessNum, // StateZh: stateZh, // State: vv.State, // }) // } // // resp = append(resp, md.LeftDataForMakeResp{ // EnterpriseName: enterpriseMap[v.EnterpriseId].Name, // EnterpriseId: v.EnterpriseId, // StateZh: orderStateArr[v.State], // State: v.State, // List: list, // }) // } //} return } func LeftDataForBaking() (resp []md.LeftDataForMakeResp, err error) { //now := time.Now() //startDate := now.Format("2006-01-02") + " 00:00:00" //endDate := now.AddDate(0, 0, 1).Format("2006-01-02") + " 00:00:00" //engine := db.Db //var enterprise []model.Enterprise //err = engine.Where("1=1").Find(&enterprise) //if err != nil { // return //} //var enterpriseMap = map[int]model.Enterprise{} //for _, v := range enterprise { // enterpriseMap[v.Id] = v //} // ////1、查找当天的 order 数据(大于 待制作) //var orders []model.Order //err = engine.Where("create_at >= ?", startDate).And("create_at < ?", endDate).And("state > ? and state != 5", 1).Find(&orders) //if err != nil { // return //} //var oIds []int64 //for _, v := range orders { // oIds = append(oIds, v.Oid) //} // ////2、查找对应的订单产品记录 //var orderGoods []model.OrderGoods //err = engine.In("oid", oIds).Find(&orderGoods) //if err != nil { // return //} //var orderGoodsMap = map[int64][]model.OrderGoods{} //var records []int //for _, v := range orderGoods { // if orderGoodsMap[v.Oid] == nil { // orderGoodsMap[v.Oid] = []model.OrderGoods{} // } // orderGoodsMap[v.Oid] = append(orderGoodsMap[v.Oid], v) // records = append(records, v.Id) //} // ////3、用户订单商品操作流程 //var orderGoodsDoingFlows []model.OrderGoodsDoingFlow //err = engine.Where("state = 2").In("record_id", records).Find(&orderGoodsDoingFlows) //if err != nil { // return //} //var orderGoodsDoingFlowsMap = map[int]model.OrderGoodsDoingFlow{} //for _, v := range orderGoodsDoingFlows { // orderGoodsDoingFlowsMap[v.RecordId] = v //} // ////4、整合数据 //orderStateArr := []string{"待制作", "制作中", "制作完成", "烘焙完成", "分拣完成", "已取消"} //for _, v := range orders { // var list []md.LeftDataForMakeList // if len(orderGoodsMap[v.Oid]) > 0 { // for _, vv := range orderGoodsMap[v.Oid] { // skuData := make([]md2.Sku, 0) // json.Unmarshal([]byte(vv.Sku), &skuData) // skuStr := "" // for _, v1 := range skuData { // if skuStr != "" { // skuStr += ";" // } // skuStr += v1.Value // } // var stateZh = "已完成" // if vv.State == 2 { // stateZh = "烘焙中" // } // // list = append(list, md.LeftDataForMakeList{ // GoodsName: vv.GoodsTitle, // Sku: skuStr, // Num: vv.Num, // SuccessNum: orderGoodsDoingFlowsMap[vv.Id].SuccessNum, // StateZh: stateZh, // State: vv.State, // }) // } // // resp = append(resp, md.LeftDataForMakeResp{ // EnterpriseName: enterpriseMap[v.EnterpriseId].Name, // EnterpriseId: v.EnterpriseId, // StateZh: orderStateArr[v.State], // State: v.State, // List: list, // }) // } //} return } func LeftDataForSorting() (resp []md.LeftDataForMakeResp, err error) { engine := db.Db var enterprise []model.Enterprise err = engine.Where("1=1").Find(&enterprise) if err != nil { return } var enterpriseMap = map[int]model.Enterprise{} for _, v := range enterprise { enterpriseMap[v.Id] = v } //1、查找不等于4 5 sysCfgDb := db.SysCfgDb{} sysCfgDb.Set() now := CommDate() var orders []model.Order err = engine.Where(" state != 4 and state != 5 and make_date=?", now).Find(&orders) if err != nil { return } var oIds []int64 for _, v := range orders { v1 := v if v1.State == 4 || v1.State == 5 { continue } oIds = append(oIds, v.Oid) } //2、查找对应的订单产品记录 var orderGoods []model.OrderGoods err = engine.In("oid", oIds).Find(&orderGoods) if err != nil { return } var orderGoodsMap = map[int64][]model.OrderGoods{} var records []int for _, v := range orderGoods { if orderGoodsMap[v.Oid] == nil { orderGoodsMap[v.Oid] = []model.OrderGoods{} } orderGoodsMap[v.Oid] = append(orderGoodsMap[v.Oid], v) records = append(records, v.Id) } //4、整合数据 orderStateArr := []string{"待分拣", "待分拣", "待分拣", "待分拣", "已完成", "已取消"} for _, v := range orders { var list []md.LeftDataForMakeList if len(orderGoodsMap[v.Oid]) > 0 { orderGoodsStateArr := []string{"待分拣", "待分拣", "待分拣", "待分拣", "已完成"} for _, vv := range orderGoodsMap[v.Oid] { skuData := make([]md2.Sku, 0) json.Unmarshal([]byte(vv.Sku), &skuData) skuStr := "" for _, v1 := range skuData { if skuStr != "" { skuStr += ";" } skuStr += v1.Value } var stateZh = orderGoodsStateArr[vv.State] list = append(list, md.LeftDataForMakeList{ GoodsName: vv.GoodsTitle, Sku: skuStr, Num: vv.Num, SuccessNum: vv.SuccessNum, StateZh: stateZh, State: vv.State, }) } resp = append(resp, md.LeftDataForMakeResp{ EnterpriseName: enterpriseMap[v.EnterpriseId].Name, EnterpriseId: v.EnterpriseId, StateZh: orderStateArr[v.State], State: v.State, List: list, }) } } return } func RightDataForMake(req md.RightDataForMakeReq) (resp []md.RightDataForMakeResp, total int64, err error) { now := time.Now() startDate := now.Format("2006-01-02") + " 00:00:00" endDate := now.AddDate(0, 0, 1).Format("2006-01-02") + " 00:00:00" engine := db.Db var enterprise []model.Enterprise err = engine.Where("1=1").Find(&enterprise) if err != nil { return } var enterpriseMap = map[int]model.Enterprise{} for _, v := range enterprise { enterpriseMap[v.Id] = v } //1、查找当天的 order 数据(大于 待制作) var orders []model.Order total, err = engine.Where("create_at >= ?", startDate).And("create_at < ?", endDate).And("state > ? and state != 5", 0). Limit(req.Limit, (req.Page-1)*req.Limit).FindAndCount(&orders) if err != nil { return } var oIds []int64 for _, v := range orders { oIds = append(oIds, v.Oid) } //2、整合数据 orderStateArr := []string{"待制作", "制作中", "制作完成", "烘焙完成", "分拣完成", "已取消"} for _, v := range orders { resp = append(resp, md.RightDataForMakeResp{ EnterpriseName: enterpriseMap[v.EnterpriseId].Name, EnterpriseId: v.EnterpriseId, StateZh: orderStateArr[v.State], State: v.State, }) } return } func RightDataForBaking(req md.RightDataForMakeReq) (resp []md.RightDataForMakeResp, total int64, err error) { now := time.Now() startDate := now.Format("2006-01-02") + " 00:00:00" endDate := now.AddDate(0, 0, 1).Format("2006-01-02") + " 00:00:00" engine := db.Db var enterprise []model.Enterprise err = engine.Where("1=1").Find(&enterprise) if err != nil { return } var enterpriseMap = map[int]model.Enterprise{} for _, v := range enterprise { enterpriseMap[v.Id] = v } //1、查找当天的 order 数据(大于 待制作) var orders []model.Order total, err = engine.Where("create_at >= ?", startDate).And("create_at < ?", endDate).And("state > ? and state != 5", 1). Limit(req.Limit, (req.Page-1)*req.Limit).FindAndCount(&orders) if err != nil { return } var oIds []int64 for _, v := range orders { oIds = append(oIds, v.Oid) } //2、整合数据 orderStateArr := []string{"待制作", "制作中", "制作完成", "烘焙完成", "分拣完成", "已取消"} for _, v := range orders { resp = append(resp, md.RightDataForMakeResp{ EnterpriseName: enterpriseMap[v.EnterpriseId].Name, EnterpriseId: v.EnterpriseId, StateZh: orderStateArr[v.State], State: v.State, }) } return } func RightDataForSorting(req md.RightDataForMakeReq) (resp []md.RightDataForMakeResp, total int64, err error) { now := time.Now() startDate := now.Format("2006-01-02") + " 00:00:00" endDate := now.AddDate(0, 0, 1).Format("2006-01-02") + " 00:00:00" engine := db.Db var enterprise []model.Enterprise err = engine.Where("1=1").Find(&enterprise) if err != nil { return } var enterpriseMap = map[int]model.Enterprise{} for _, v := range enterprise { enterpriseMap[v.Id] = v } //1、查找当天的 order 数据(大于 待制作) var orders []model.Order total, err = engine.Where("create_at >= ?", startDate).And("create_at < ?", endDate).And("state > ? and state != 5", 2). Limit(req.Limit, (req.Page-1)*req.Limit).FindAndCount(&orders) if err != nil { return } var oIds []int64 for _, v := range orders { oIds = append(oIds, v.Oid) } //2、整合数据 orderStateArr := []string{"待制作", "制作中", "制作完成", "烘焙完成", "分拣完成", "已取消"} for _, v := range orders { resp = append(resp, md.RightDataForMakeResp{ EnterpriseName: enterpriseMap[v.EnterpriseId].Name, EnterpriseId: v.EnterpriseId, StateZh: orderStateArr[v.State], State: v.State, }) } return } func NewSortingCate(c *gin.Context) []map[string]string { engine := db.Db res := make([]map[string]string, 0) var args = make(map[string]string) c.ShouldBindJSON(&args) var enterprise []model.Enterprise sess := engine.Where("1=1") if args["name"] != "" { sess.And("name like ? or inside_name like ?", "%"+args["name"]+"%", "%"+args["name"]+"%") } err := sess.Find(&enterprise) if err != nil { return res } var enterpriseMap = map[int]model.Enterprise{} enterpriseId := []int{-1} for _, v := range enterprise { enterpriseMap[v.Id] = v enterpriseId = append(enterpriseId, v.Id) } //1、查找不等于4 5 now := CommDate() var orders []model.Order sess1 := engine.Where(" state != 4 and state != 5 and make_date=?", now) if args["name"] != "" { sess1.In("enterprise_id", enterpriseId) } err = sess1.Find(&orders) if err != nil { return res } ids := make([]string, 0) orderStateArr := []string{"待分拣", "待分拣", "待分拣", "待分拣", "已完成", "已取消"} for _, v := range orders { if utils.InArr(utils.IntToStr(v.EnterpriseId), ids) { continue } tmp := map[string]string{ "id": utils.IntToStr(v.EnterpriseId), "name": enterpriseMap[v.EnterpriseId].Name, "state": utils.IntToStr(v.State), "state_str": orderStateArr[v.State], } ids = append(ids, utils.IntToStr(v.EnterpriseId)) res = append(res, tmp) } return res } func NewSortingData(c *gin.Context) []map[string]string { res := make([]map[string]string, 0) var args map[string]string if err := c.ShouldBindJSON(&args); err != nil { return res } engine := db.Db var enterprise []model.Enterprise err := engine.Where("1=1").Find(&enterprise) if err != nil { return res } var enterpriseMap = map[int]model.Enterprise{} for _, v := range enterprise { enterpriseMap[v.Id] = v } //1、查找不等于4 5 now := CommDate() var orders []model.Order err = engine.Where(" state != 4 and state != 5 and make_date=? and enterprise_id=?", now, args["id"]).Find(&orders) if err != nil { return res } var oIds []string for _, v := range orders { v1 := v if v1.State == 4 || v1.State == 5 { continue } oIds = append(oIds, utils.Int64ToStr(v.Oid)) } sql := `SELECT SUM(num) as num,SUM(success_num) as success_num,goods_title,sku FROM order_goods WHERE %s oid in (%s) and make_date=%s and goods_type=0 GROUP BY enterprise_id,goods_id,sku_id` str := "" if args["name"] != "" { str = " goods_title like %" + args["name"] + "% and " } sql = fmt.Sprintf(sql, str, strings.Join(oIds, ","), now) nativeString, err := db.QueryNativeString(engine, sql) for _, v := range nativeString { skuData := make([]md2.Sku, 0) json.Unmarshal([]byte(v["sku"]), &skuData) skuStr := "" for _, v1 := range skuData { if skuStr != "" { skuStr += ";" } skuStr += v1.Value } tmp := map[string]string{ "goods_name": v["goods_title"], "sku": skuStr, "num": v["num"], "success_num": v["success_num"], "state": "1", "state_str": "待分拣", } if utils.StrToInt(v["num"]) <= utils.StrToInt(v["success_num"]) { tmp["state"] = "4" tmp["state_str"] = "分拣完成" } res = append(res, tmp) } return res } func NewSortingGoodsCate(c *gin.Context) []map[string]string { res := make([]map[string]string, 0) engine := db.Db //1、查找不等于4 5 now := CommDate() var orders []model.Order var args = make(map[string]string) c.ShouldBindJSON(&args) err := engine.Where(" state != 4 and state != 5 and make_date=? ", now).Find(&orders) if err != nil { return res } var oIds []string for _, v := range orders { v1 := v if v1.State == 4 || v1.State == 5 { continue } oIds = append(oIds, utils.Int64ToStr(v.Oid)) } sql := `SELECT SUM(num) as num,SUM(success_num) as success_num,goods_title,sku,goods_id,sku_id FROM order_goods WHERE %s oid in (%s) and goods_type=0 and make_date=%s GROUP BY goods_id,sku_id` str := "" if args["name"] != "" { str = " goods_title like %" + args["name"] + "% and " } sql = fmt.Sprintf(sql, str, strings.Join(oIds, ","), now) nativeString, err := db.QueryNativeString(engine, sql) for _, v := range nativeString { skuData := make([]md2.Sku, 0) json.Unmarshal([]byte(v["sku"]), &skuData) skuStr := "" for _, v1 := range skuData { if skuStr != "" { skuStr += ";" } skuStr += v1.Value } tmp := map[string]string{ "goods_id": v["goods_id"], "sku_id": v["sku_id"], "goods_name": v["goods_title"], "sku": skuStr, "num": v["num"], "success_num": v["success_num"], "state": "1", "state_str": "待分拣", } if utils.StrToInt(v["num"]) <= utils.StrToInt(v["success_num"]) { tmp["state"] = "4" tmp["state_str"] = "分拣完成" } res = append(res, tmp) } return res } func NewSortingGoodsData(c *gin.Context, arg map[string]string) []map[string]string { engine := db.Db res := make([]map[string]string, 0) var enterprise []model.Enterprise err := engine.Where("1=1").Find(&enterprise) if err != nil { return res } var enterpriseMap = map[int]model.Enterprise{} for _, v := range enterprise { enterpriseMap[v.Id] = v } //1、查找不等于4 5 now := CommDate() var args = make(map[string]string) c.ShouldBindJSON(&args) var orders []model.Order err = engine.Where(" state != 4 and state != 5 and make_date=? ", now).Find(&orders) if err != nil { return res } var oIds []string for _, v := range orders { v1 := v if v1.State == 4 || v1.State == 5 { continue } oIds = append(oIds, utils.Int64ToStr(v.Oid)) } sql := `SELECT SUM(num) as num,SUM(success_num) as success_num,goods_title,sku,goods_id,sku_id,enterprise_id FROM order_goods WHERE %s oid in (%s) and goods_id=%s and goods_type=0 and sku_id=%s and make_date=%s GROUP BY enterprise_id ORDER BY id asc` str := "" if args["name"] != "" { str = " goods_title like %" + args["name"] + "% and " } sql = fmt.Sprintf(sql, str, strings.Join(oIds, ","), arg["goods_id"], arg["sku_id"], now) nativeString, err := db.QueryNativeString(engine, sql) for _, v := range nativeString { skuData := make([]md2.Sku, 0) json.Unmarshal([]byte(v["sku"]), &skuData) skuStr := "" for _, v1 := range skuData { if skuStr != "" { skuStr += ";" } skuStr += v1.Value } tmp := map[string]string{ "enterprise_name": enterpriseMap[utils.StrToInt(v["enterprise_id"])].Name, "num": v["num"], "success_num": v["success_num"], "state": "1", "state_str": "待分拣", } if utils.StrToInt(v["num"]) <= utils.StrToInt(v["success_num"]) { tmp["state"] = "4" tmp["state_str"] = "分拣完成" } res = append(res, tmp) } return res }