diff --git a/app/bigData/hdl/hdl_data.go b/app/bigData/hdl/hdl_data.go new file mode 100644 index 0000000..ff56127 --- /dev/null +++ b/app/bigData/hdl/hdl_data.go @@ -0,0 +1,68 @@ +package hdl + +import ( + "applet/app/bigData/lib/validate" + "applet/app/bigData/md" + "applet/app/bigData/svc" + "applet/app/e" + "applet/app/enum" + "github.com/gin-gonic/gin" +) + +func LeftData(c *gin.Context) { + loginKind := svc.GetUser(c).LoginKind + var result interface{} + var err error + switch loginKind { + case enum.LoginKindForMake: + result, err = svc.LeftDataForMake() + break + case enum.LoginKindForBaking: + result, err = svc.LeftDataForBaking() + break + case enum.LoginKindForSorting: + result, err = svc.LeftDataForSorting() + break + } + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + e.OutSuc(c, result, nil) + return +} + +func RightData(c *gin.Context) { + var req md.RightDataForMakeReq + err := c.ShouldBindJSON(&req) + if err != nil { + err = validate.HandleValidateErr(err) + err1 := err.(e.E) + e.OutErr(c, err1.Code, err1.Error()) + return + } + loginKind := svc.GetUser(c).LoginKind + + var result interface{} + var total int64 + switch loginKind { + case enum.LoginKindForMake: + result, total, err = svc.RightDataForMake(req) + break + case enum.LoginKindForBaking: + result, total, err = svc.RightDataForMake(req) + break + case enum.LoginKindForSorting: + result, total, err = svc.RightDataForMake(req) + break + } + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + e.OutSuc(c, map[string]interface{}{ + "list": result, + "total": total, + }, nil) + return +} diff --git a/app/bigData/hdl/hdl_login.go b/app/bigData/hdl/hdl_login.go index 591eadf..07b804e 100644 --- a/app/bigData/hdl/hdl_login.go +++ b/app/bigData/hdl/hdl_login.go @@ -33,17 +33,19 @@ func Login(c *gin.Context) { } ip := utils.GetIP(c.Request) - key := fmt.Sprintf(md.UserJwtTokenKey, ip) + key := fmt.Sprintf(md.UserJwtTokenKey, ip, req.LoginKind) token, err := svc.HandleLoginToken(key, &md.User{ - Account: req.Account, - Password: req.Password, + Account: req.Account, + Password: req.Password, + LoginKind: req.LoginKind, }) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } e.OutSuc(c, md.LoginResponse{ - Token: token, + Token: token, + LoginKind: req.LoginKind, }, nil) return } diff --git a/app/bigData/lib/auth/auth.go b/app/bigData/lib/auth/auth.go index 903a68f..d211367 100644 --- a/app/bigData/lib/auth/auth.go +++ b/app/bigData/lib/auth/auth.go @@ -7,11 +7,12 @@ import ( ) // GenToken 生成JWT -func GenToken(account, password string) (string, error) { +func GenToken(account, password, loginKind string) (string, error) { // 创建一个我们自己的声明 c := JWTUser{ - Account: account, - Password: password, + Account: account, + Password: password, + LoginKind: loginKind, StandardClaims: jwt.StandardClaims{ ExpiresAt: time.Now().Add(TokenExpireDuration).Unix(), // 过期时间 Issuer: "bakery", // 签发人 diff --git a/app/bigData/lib/auth/base.go b/app/bigData/lib/auth/base.go index ff1c346..581950b 100644 --- a/app/bigData/lib/auth/base.go +++ b/app/bigData/lib/auth/base.go @@ -13,7 +13,8 @@ var Secret = []byte("bakery_big_data") // JWTUser 如果想要保存更多信息,都可以添加到这个结构体中 type JWTUser struct { - Account string `json:"account"` - Password string `json:"password"` + Account string `json:"account"` + Password string `json:"password"` + LoginKind string `json:"login_kind"` jwt.StandardClaims } diff --git a/app/bigData/md/md_app_redis_key.go b/app/bigData/md/md_app_redis_key.go index de9939a..293b8d4 100644 --- a/app/bigData/md/md_app_redis_key.go +++ b/app/bigData/md/md_app_redis_key.go @@ -2,6 +2,6 @@ package md // 缓存key统一管理 const ( - UserJwtTokenKey = "%s:bakery_big_data_user_jwt_token" // jwt, 占位符:ip, user:id + UserJwtTokenKey = "%s:bakery_big_data_user_jwt_token:%s" // jwt, 占位符:ip, LoginKind JwtTokenCacheTime = 3600 * 24 * 1 ) diff --git a/app/bigData/md/md_data.go b/app/bigData/md/md_data.go new file mode 100644 index 0000000..bf6aae6 --- /dev/null +++ b/app/bigData/md/md_data.go @@ -0,0 +1,30 @@ +package md + +type LeftDataForMakeResp struct { + EnterpriseName string `json:"enterprise_name"` + EnterpriseId int `json:"enterprise_id"` + StateZh string `json:"state_zh"` + State int `json:"state"` + List []LeftDataForMakeList `json:"list"` +} + +type LeftDataForMakeList struct { + GoodsName string `json:"goods_name"` + Sku string `json:"sku"` + Num int `json:"num"` + SuccessNum int `json:"success_num"` + StateZh string `json:"state_zh"` + State int `json:"state"` +} + +type RightDataForMakeReq struct { + Page int `json:"page"` + Limit int `json:"limit"` +} + +type RightDataForMakeResp struct { + EnterpriseName string `json:"enterprise_name"` + EnterpriseId int `json:"enterprise_id"` + StateZh string `json:"state_zh"` + State int `json:"state"` +} diff --git a/app/bigData/md/md_login.go b/app/bigData/md/md_login.go index bccdb11..d2c1dd4 100644 --- a/app/bigData/md/md_login.go +++ b/app/bigData/md/md_login.go @@ -1,15 +1,18 @@ package md type LoginReq struct { - Account string `json:"account" binding:"required" label:"账号"` - Password string `json:"password" binding:"required" label:"密码"` + Account string `json:"account" binding:"required" label:"账号"` + Password string `json:"password" binding:"required" label:"密码"` + LoginKind string `json:"login_kind" binding:"required" validate:"oneof=makeArea bakingArea sortingArea" label:"登录类型"` } type User struct { - Account string `json:"account" label:"账号"` - Password string `json:"password" label:"密码"` + Account string `json:"account" label:"账号"` + Password string `json:"password" label:"密码"` + LoginKind string `json:"login_kind" label:"登录类型"` } type LoginResponse struct { - Token string `json:"token"` + Token string `json:"token"` + LoginKind string `json:"login_kind" label:"登录类型"` } diff --git a/app/bigData/svc/svc_auth.go b/app/bigData/svc/svc_auth.go index 22e124c..5ac11ff 100644 --- a/app/bigData/svc/svc_auth.go +++ b/app/bigData/svc/svc_auth.go @@ -12,8 +12,9 @@ func GetUser(c *gin.Context) *md.User { user, _ := c.Get("user") if user == nil { return &md.User{ - Account: "", - Password: "", + Account: "", + Password: "", + LoginKind: "", } } return user.(*md.User) @@ -37,8 +38,9 @@ func CheckUser(c *gin.Context) (*md.User, error) { // 获取user user := &md.User{ - Account: mc.Account, - Password: mc.Password, + Account: mc.Account, + Password: mc.Password, + LoginKind: mc.LoginKind, } return user, nil } diff --git a/app/bigData/svc/svc_data.go b/app/bigData/svc/svc_data.go new file mode 100644 index 0000000..e11ecd1 --- /dev/null +++ b/app/bigData/svc/svc_data.go @@ -0,0 +1,412 @@ +package svc + +import ( + md2 "applet/app/admin/md" + "applet/app/bigData/md" + "applet/app/db" + "applet/app/db/model" + "encoding/json" + "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) { + 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", 2).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 = 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 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 +} diff --git a/app/bigData/svc/svc_login.go b/app/bigData/svc/svc_login.go index 0a79a34..25deafe 100644 --- a/app/bigData/svc/svc_login.go +++ b/app/bigData/svc/svc_login.go @@ -16,7 +16,7 @@ func HandleLoginToken(cacheKey string, user *md.User) (string, error) { // 没有获取到 if err != nil || token == "" { // 生成token - token, err = auth.GenToken(user.Account, user.Password) + token, err = auth.GenToken(user.Account, user.Password, user.LoginKind) if err != nil { return "", err } diff --git a/app/enum/enum_login.go b/app/enum/enum_login.go new file mode 100644 index 0000000..28a0267 --- /dev/null +++ b/app/enum/enum_login.go @@ -0,0 +1,22 @@ +package enum + +type LoginKind string + +const ( + LoginKindForMake = "makeArea" + LoginKindForBaking = "bakingArea" + LoginKindForSorting = "sortingArea" +) + +func (gt LoginKind) String() string { + switch gt { + case LoginKindForMake: + return "制作区" + case LoginKindForBaking: + return "烘焙区" + case LoginKindForSorting: + return "分拣区" + default: + return "未知" + } +} diff --git a/app/router/big_data_router.go b/app/router/big_data_router.go index 62d9cee..45d087d 100644 --- a/app/router/big_data_router.go +++ b/app/router/big_data_router.go @@ -2,12 +2,20 @@ package router import ( "applet/app/bigData/hdl" - "applet/app/ipad/mw" + "applet/app/bigData/mw" "github.com/gin-gonic/gin" ) func BigDataInit(r *gin.RouterGroup) { r.POST("/test", hdl.Demo) + r.POST("/login", hdl.Login) r.Use(mw.Auth) //检测登录状态 + r.GET("/userInfo", hdl.UserInfo) + rData(r.Group("data")) //数据接口 +} + +func rData(r *gin.RouterGroup) { + r.GET("/leftData", hdl.LeftData) //左侧数据 + r.POST("/rightData", hdl.RightData) //右侧数据 }