order.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package api
  2. import (
  3. "3dshow-supplier/db/model"
  4. "3dshow-supplier/db/repo"
  5. "errors"
  6. "fmt"
  7. "time"
  8. "github.com/gin-gonic/gin"
  9. "go.mongodb.org/mongo-driver/bson"
  10. "go.mongodb.org/mongo-driver/bson/primitive"
  11. )
  12. // 产品管理
  13. func Order(r *GinRouter) {
  14. r.GETJWT("/order/list", OrderList)
  15. r.POSTJWT("/order/delivery", OrderDelivery)
  16. r.GETJWT("/order/detail/:id", OrderDetail)
  17. //添加管理端接口
  18. CreateCRUD(r, "/admin/orders", &CRUDOption{
  19. Collection: repo.CollectionOrder,
  20. NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  21. entity := &model.Order{}
  22. c.ShouldBindJSON(entity)
  23. entity.CreateTime = time.Now()
  24. if entity.Status == 0 {
  25. entity.Status = -1
  26. }
  27. return entity, nil
  28. },
  29. EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
  30. return &model.Product{}
  31. },
  32. SearchSort: bson.M{"createTime": -1},
  33. SearchFilter: func(c *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
  34. if query["supplyId"] != nil {
  35. query["supplyId"], _ = primitive.ObjectIDFromHex(query["supplyId"].(string))
  36. }
  37. if query["userId"] != nil {
  38. query["userId"], _ = primitive.ObjectIDFromHex(query["userId"].(string))
  39. }
  40. return query
  41. },
  42. SearchPostProcess: func(page *repo.PageResult, c *gin.Context, apictx *ApiSession, query map[string]interface{}) (interface{}, error) {
  43. return page, nil
  44. },
  45. JWT: true,
  46. SearchProject: []string{"remark", "createTime", "status", "supplyId", "userId"},
  47. })
  48. }
  49. // 订单列表
  50. func OrderList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  51. page, size, query := UtilQueryPageSize(c)
  52. _userId := apictx.User.ID
  53. userId, _ := primitive.ObjectIDFromHex(_userId)
  54. // 供应商的订单列表
  55. query["supplyId"] = userId
  56. result, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  57. CollectName: repo.CollectionOrder,
  58. Page: page,
  59. Size: size,
  60. Query: query,
  61. Project: []string{"createTime", "userId", "supplyId", "products", "status"},
  62. Sort: repo.Map{"createTime": -1},
  63. })
  64. if len(result.List) > 0 {
  65. for _, v := range result.List {
  66. orderCount := 0
  67. notShipCount := 0
  68. // 客户信息
  69. customUserId := v["userId"].(primitive.ObjectID)
  70. customerUser := model.UserSmaple{}
  71. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  72. Db: "customer-user",
  73. CollectName: "users",
  74. Query: repo.Map{"_id": customUserId},
  75. }, &customerUser)
  76. v["customer"] = customerUser
  77. // 统计下单数和未发货数
  78. orderCount = len(v["products"].(primitive.A))
  79. if orderCount > 0 {
  80. for _, p := range v["products"].(primitive.A) {
  81. if p.(map[string]interface{})["status"].(int32) == -1 {
  82. notShipCount++
  83. }
  84. }
  85. }
  86. v["orderCount"] = orderCount
  87. v["notShipCount"] = notShipCount
  88. }
  89. }
  90. return result, err
  91. }
  92. // 发货
  93. type OrderDeliveryReq struct {
  94. Id primitive.ObjectID `json:"id"`
  95. UniqueId primitive.ObjectID `json:"uniqueId"`
  96. ExpressNo string `json:"expressNo"`
  97. }
  98. func OrderDelivery(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  99. var form OrderDeliveryReq
  100. err := c.ShouldBind(&form)
  101. if err != nil {
  102. return nil, errors.New("参数错误")
  103. }
  104. // 验证订单是不是自己的
  105. _userId := apictx.User.ID
  106. fmt.Println(_userId)
  107. fmt.Println(form.Id.Hex())
  108. fmt.Println(form.UniqueId.Hex())
  109. userId, _ := primitive.ObjectIDFromHex(_userId)
  110. order := model.Order{}
  111. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  112. CollectName: repo.CollectionOrder,
  113. Query: repo.Map{"_id": form.Id},
  114. }, &order)
  115. if !found || err != nil {
  116. return nil, errors.New("该订单未找到")
  117. }
  118. fmt.Println(order.SupplyId)
  119. if userId != order.SupplyId {
  120. return nil, errors.New("非法操作")
  121. }
  122. if len(order.Products) < 1 {
  123. return nil, errors.New("无效订单")
  124. }
  125. // 发货
  126. result, err := repo.RepoDocArrayOneUpdate(apictx.CreateRepoCtx(), &repo.ArrayOneUpdateOption{
  127. CollectName: repo.CollectionOrder,
  128. Id: form.Id.Hex(),
  129. Query: repo.Map{"products.id": form.UniqueId},
  130. Set: repo.Map{"products.$.status": 1, "products.$.expressNo": form.ExpressNo},
  131. })
  132. if err != nil {
  133. fmt.Println(err)
  134. return nil, errors.New("发货商品异常")
  135. }
  136. if result.ModifiedCount > 0 {
  137. // 查询更改后的订单
  138. order1 := model.Order{}
  139. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  140. CollectName: repo.CollectionOrder,
  141. Query: repo.Map{"_id": form.Id},
  142. }, &order1)
  143. // 查询其中产品 判断订单状态
  144. // 该订单产品有多少种状态 在此时订单只有-1,1两种状态
  145. statusMap := make(map[int]struct{})
  146. for _, v := range order1.Products {
  147. statusMap[v.Status] = struct{}{}
  148. }
  149. status := order.Status
  150. // 只有一种状态时为已发货
  151. if len(statusMap) == 1 {
  152. status = 1
  153. }
  154. // 有两种状态时为待发货
  155. if len(statusMap) == 2 {
  156. status = -1
  157. }
  158. // 发货成功该订单不可取消
  159. repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, form.Id.Hex(), &model.Order{Status: status, IsCancel: -1})
  160. }
  161. return result, nil
  162. }
  163. // 订单信息
  164. func OrderDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  165. _id := c.Param("id")
  166. if len(_id) < 1 {
  167. return nil, errors.New("id不能为空")
  168. }
  169. orderId, err := primitive.ObjectIDFromHex(_id)
  170. if err != nil {
  171. return nil, errors.New("非法id")
  172. }
  173. ok, result := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  174. CollectName: repo.CollectionOrder,
  175. Query: repo.Map{"_id": orderId},
  176. })
  177. if !ok {
  178. return nil, errors.New("数据未找到")
  179. }
  180. // 客户信息
  181. customer := model.UserSmaple{}
  182. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  183. Db: "customer-user",
  184. CollectName: "users",
  185. Query: repo.Map{"_id": result["userId"].(primitive.ObjectID)},
  186. }, &customer)
  187. result["customer"] = customer
  188. return result, nil
  189. }