service-queenter-upload.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package api
  2. import (
  3. "fmt"
  4. "mats/db/model"
  5. "mats/db/repo"
  6. "github.com/gin-gonic/gin"
  7. "go.mongodb.org/mongo-driver/bson/primitive"
  8. "infish.cn/comm"
  9. )
  10. func RegQueenterUploadApi(router *GinRouter) {
  11. router.GETJWT("/queenter/upload/category", getUploadCategory)
  12. router.POSTJWT("/queenter/upload/matgroup", uploadMatGroup)
  13. }
  14. func getUploadCategory(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  15. body := &struct {
  16. Uuid string //设备uuid
  17. }{}
  18. c.ShouldBindJSON(body)
  19. if len(body.Uuid) < 1 {
  20. return nil, fmt.Errorf("uuid不能为空")
  21. }
  22. //获取设备绑定的数据库
  23. userId, _ := primitive.ObjectIDFromHex(apictx.User.Parent)
  24. list := []*model.DeviceQueenter{}
  25. err := repo.RepoSeachDocs(apictx.CreateRepoCtx(), &repo.DocsSearchOptions{
  26. CollectName: repo.CollectionQueenters,
  27. Query: map[string]interface{}{"userId": userId},
  28. }, &list)
  29. if err != nil {
  30. return nil, err
  31. }
  32. if len(list) < 1 {
  33. return nil, fmt.Errorf("后台没有创建设备无法获取分类")
  34. }
  35. var queenter *model.DeviceQueenter = nil
  36. var defaultQueenter *model.DeviceQueenter = nil
  37. for _, v := range list {
  38. if v.IsDefaut != nil && *v.IsDefaut && defaultQueenter == nil {
  39. defaultQueenter = v
  40. }
  41. if body.Uuid == v.Uuid {
  42. queenter = v
  43. break
  44. }
  45. }
  46. if queenter == nil && defaultQueenter == nil {
  47. return nil, fmt.Errorf("没有设置默认设备")
  48. }
  49. if queenter == nil {
  50. queenter = defaultQueenter
  51. }
  52. if len(queenter.BindOutId) < 1 {
  53. return nil, fmt.Errorf("当前设备没有绑定输出内容")
  54. }
  55. //获取输出配置
  56. output := &model.DeviceOutput{}
  57. err = repo.RepoSeachDoc2(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  58. CollectName: repo.CollectionQueenterOutputs,
  59. Query: repo.Map{"_id": queenter.BindOutId},
  60. }, output)
  61. if err != nil {
  62. return nil, err
  63. }
  64. if output.Type == "queenmat" { //面料库
  65. hub := &model.HubMat{}
  66. err = repo.RepoSeachDoc2(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  67. CollectName: repo.CollectionHubMats,
  68. Query: repo.Map{"_id": output.QueenMatId},
  69. }, hub)
  70. if err != nil {
  71. return nil, fmt.Errorf("获取面料库失败")
  72. }
  73. scope := "queenmat"
  74. userCaties := &comm.DbCategory{}
  75. _, err = repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  76. CollectName: repo.CollectionCategories,
  77. Query: repo.Map{"userId": userId, "scope": scope},
  78. }, userCaties)
  79. if err != nil {
  80. return nil, err
  81. }
  82. userAssetCaties := &comm.DbAssetUserCategory{}
  83. _, err = repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  84. CollectName: repo.CollectionDbAssetCategory,
  85. Query: repo.Map{"userId": userId, "scope": scope},
  86. }, userAssetCaties)
  87. if err != nil {
  88. return nil, err
  89. }
  90. cates := &comm.DbCategory{}
  91. for _, v := range userAssetCaties.CategoryConfs {
  92. if v.DbAssetId.Hex() == hub.DefineId {
  93. for _, id := range v.CategoryIds {
  94. for _, c := range userCaties.Categories {
  95. if c.Id == id {
  96. cates.Categories = append(cates.Categories, c)
  97. break
  98. }
  99. }
  100. }
  101. }
  102. }
  103. return cates, nil
  104. }
  105. return nil, nil
  106. }
  107. // 上传面料组
  108. func uploadMatGroup(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  109. body := &struct {
  110. Uuid string //设备uuid
  111. }{}
  112. c.ShouldBindJSON(body)
  113. //获取设备绑定的数据库
  114. return nil, nil
  115. }