|
@@ -4,6 +4,7 @@ import (
|
|
|
"3dshow/db/model"
|
|
|
"3dshow/db/repo"
|
|
|
"errors"
|
|
|
+ "fmt"
|
|
|
"time"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
@@ -11,6 +12,8 @@ import (
|
|
|
)
|
|
|
|
|
|
func ShopCart(r *GinRouter) {
|
|
|
+ r.POSTJWT("/shopCart/deleteBatch", ShopCartDeleteBatch)
|
|
|
+ r.GETJWT("/shopCart/groupList", shopCartList)
|
|
|
|
|
|
CreateCRUD(r, "/shopCart", &CRUDOption{
|
|
|
Collection: repo.CollectionShopCart,
|
|
@@ -47,26 +50,77 @@ func ShopCart(r *GinRouter) {
|
|
|
ety := entity.(*model.ShopCart)
|
|
|
ety.UpdateTime = time.Now()
|
|
|
},
|
|
|
- SearchPostProcess: func(page *repo.PageResult, _ *gin.Context, apictx *ApiSession, _ map[string]interface{}) (interface{}, error) {
|
|
|
- // 查询组装数据
|
|
|
- if len(page.List) > 0 {
|
|
|
- for _, v := range page.List {
|
|
|
- // 查询产品信息
|
|
|
- supplyId := v["supplyId"].(primitive.ObjectID)
|
|
|
- supply := model.Supply{}
|
|
|
- // 供应商信息
|
|
|
- repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
- CollectName: repo.CollectionSupply,
|
|
|
- Query: repo.Map{"_id": supplyId},
|
|
|
- Project: []string{"name", "avatar", "contact"},
|
|
|
- }, &supply)
|
|
|
- v["supplyName"] = supply.Name
|
|
|
- v["supplyAvatar"] = supply.Avatar
|
|
|
- v["supplyContact"] = supply.Contact
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+type ShopCartDeleteBatchReq struct {
|
|
|
+ Ids []string `json:"ids"`
|
|
|
+}
|
|
|
+
|
|
|
+func ShopCartDeleteBatch(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
+ var form ShopCartDeleteBatchReq
|
|
|
+ err := c.ShouldBindJSON(&form)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(form)
|
|
|
+ return nil, errors.New("参数错误")
|
|
|
+ }
|
|
|
+ if len(form.Ids) > 0 {
|
|
|
+ // 批量删除
|
|
|
+ for _, v := range form.Ids {
|
|
|
+ // id 错误
|
|
|
+ _, err := primitive.ObjectIDFromHex(v)
|
|
|
+ if err != nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ // 删除
|
|
|
+ repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionShopCart, v)
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return true, nil
|
|
|
+}
|
|
|
+
|
|
|
+func shopCartList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
+ var shopCarts []*model.ShopCart
|
|
|
+ userId, _ := primitive.ObjectIDFromHex(apictx.User.ID)
|
|
|
+ option := &repo.PageSearchOptions{
|
|
|
+ CollectName: repo.CollectionShopCart,
|
|
|
+ Query: repo.Map{"userId": userId},
|
|
|
+ }
|
|
|
+
|
|
|
+ repo.RepoDocsSearch(apictx.CreateRepoCtx(), option, &shopCarts)
|
|
|
+
|
|
|
+ supplyArr := map[primitive.ObjectID]struct{}{}
|
|
|
+ supplyList := make([]map[string]interface{}, 0)
|
|
|
+
|
|
|
+ if len(shopCarts) > 0 {
|
|
|
+ for _, v := range shopCarts {
|
|
|
+ supplyArr[v.SupplyId] = struct{}{}
|
|
|
+ }
|
|
|
+ // supplyId分组列表
|
|
|
+ for k, _ := range supplyArr {
|
|
|
+ // 查询产品信息
|
|
|
+ supply := model.Supply{}
|
|
|
+ // 供应商信息
|
|
|
+ repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
+ CollectName: repo.CollectionSupply,
|
|
|
+ Query: repo.Map{"_id": k},
|
|
|
+ Project: []string{"name", "avatar", "contact"},
|
|
|
+ }, &supply)
|
|
|
+ products := make([]*model.ShopCart, 0)
|
|
|
+ for _, v := range shopCarts {
|
|
|
+ if k == v.SupplyId {
|
|
|
+ products = append(products, v)
|
|
|
|
|
|
}
|
|
|
}
|
|
|
- return page, nil
|
|
|
- },
|
|
|
- })
|
|
|
+ supplyList = append(supplyList, map[string]interface{}{
|
|
|
+ "supply": supply,
|
|
|
+ "products": products,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return supplyList, nil
|
|
|
}
|