附近小店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

182 lines
4.7 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. "applet/app/e"
  6. "applet/app/md"
  7. "applet/app/svc"
  8. "applet/app/utils"
  9. "encoding/json"
  10. "github.com/gin-gonic/gin"
  11. "strings"
  12. )
  13. func AgentGoods(c *gin.Context) {
  14. var arg map[string]string
  15. if err := c.ShouldBindJSON(&arg); err != nil {
  16. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  17. return
  18. }
  19. user := svc.GetUser(c)
  20. store := db.GetStoreIdEg(svc.MasterDb(c), utils.IntToStr(user.Info.Uid))
  21. arg["uid"] = utils.IntToStr(store.ParentUid)
  22. arg["store_type"] = "2"
  23. arg["is_up"] = "1"
  24. cate, total := db.GetStoreGoods(svc.MasterDb(c), arg)
  25. list := make([]map[string]string, 0)
  26. cateData := db.GetGoodsCateAllArg(svc.MasterDb(c), arg)
  27. cateList := make([]map[string]string, 0)
  28. cateMap := make(map[int]string)
  29. if cateData != nil {
  30. for _, v := range *cateData {
  31. tmp := map[string]string{
  32. "name": v.Title, "value": utils.IntToStr(v.Id),
  33. }
  34. cateList = append(cateList, tmp)
  35. cateMap[v.Id] = v.Title
  36. }
  37. }
  38. if cate != nil {
  39. stateMap := []string{"上架", "下架"}
  40. scheme, host := svc.ImageBucket(c)
  41. for _, v := range *cate {
  42. tmp := map[string]string{
  43. "id": utils.IntToStr(v.Id),
  44. "title": v.Title,
  45. "price": v.Price,
  46. "info": v.Info,
  47. "img_url": svc.ImageFormatWithBucket(scheme, host, v.Img),
  48. "create_at": v.CreateAt.Format("2006-01-02 15:04:05"),
  49. "cate": cateMap[v.Cid],
  50. "state": stateMap[v.State],
  51. }
  52. list = append(list, tmp)
  53. }
  54. }
  55. res := map[string]interface{}{
  56. "list": list, "total": total, "cate_list": cateList,
  57. }
  58. e.OutSuc(c, res, nil)
  59. return
  60. }
  61. func AgentGoodsCopy(c *gin.Context) {
  62. var arg map[string]string
  63. if err := c.ShouldBindJSON(&arg); err != nil {
  64. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  65. return
  66. }
  67. user := svc.GetUser(c)
  68. sess := svc.MasterDb(c).NewSession()
  69. defer sess.Close()
  70. sess.Begin()
  71. var goods = make([]model.CommunityTeamGoods, 0)
  72. sess.In("id", strings.Split(arg["ids"], ",")).Find(&goods)
  73. for _, v := range goods {
  74. id := v.Id
  75. v.Id = 0
  76. v.Stock = 0
  77. v.Uid = user.Info.Uid
  78. v.StoreType = 1
  79. v.Cid = utils.StrToInt(arg["cid"])
  80. _, err := sess.Insert(&v)
  81. if err != nil {
  82. sess.Rollback()
  83. e.OutErr(c, 400, e.NewErr(400, "添加失败"))
  84. return
  85. }
  86. var goodsSku = make([]model.CommunityTeamSku, 0)
  87. sess.Where("goods_id=?", id).Get(&goodsSku)
  88. for k1 := range goodsSku {
  89. goodsSku[k1].SkuId = 0
  90. goodsSku[k1].GoodsId = v.Id
  91. goodsSku[k1].Stock = 0
  92. }
  93. _, err = sess.Insert(&goodsSku)
  94. if err != nil {
  95. sess.Rollback()
  96. e.OutErr(c, 400, e.NewErr(400, "添加失败"))
  97. return
  98. }
  99. }
  100. sess.Commit()
  101. e.OutSuc(c, "success", nil)
  102. return
  103. }
  104. func AgentGoodsDetail(c *gin.Context) {
  105. var arg map[string]string
  106. if err := c.ShouldBindJSON(&arg); err != nil {
  107. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  108. return
  109. }
  110. goods := db.GetGoodsById(svc.MasterDb(c), arg["id"])
  111. if goods == nil {
  112. e.OutErr(c, 400, e.NewErr(400, "商品不存在"))
  113. return
  114. }
  115. cateData := db.GetGoodsCateAll(svc.MasterDb(c))
  116. cateList := make([]map[string]string, 0)
  117. cateMap := make(map[int]string)
  118. if cateData != nil {
  119. for _, v := range *cateData {
  120. tmp := map[string]string{
  121. "name": v.Title, "value": utils.IntToStr(v.Id),
  122. }
  123. cateList = append(cateList, tmp)
  124. cateMap[v.Id] = v.Title
  125. }
  126. }
  127. stateMap := []string{"上架", "下架"}
  128. scheme, host := svc.ImageBucket(c)
  129. res := md.BaseGoods{
  130. Id: utils.IntToStr(goods.Id),
  131. Title: goods.Title,
  132. Cid: utils.IntToStr(goods.Cid),
  133. CategoryName: cateMap[goods.Cid],
  134. IsSingleSku: utils.IntToStr(goods.IsSingleSku),
  135. Price: goods.Price,
  136. Stock: utils.IntToStr(goods.Stock),
  137. Img: goods.Img,
  138. ImgUrl: svc.ImageFormatWithBucket(scheme, host, goods.Img),
  139. Spe: nil,
  140. Sort: utils.IntToStr(goods.Sort),
  141. Info: goods.Info,
  142. State: utils.IntToStr(goods.State),
  143. StateStr: stateMap[goods.State],
  144. }
  145. GoodsSpe := make([]md.GoodsSpe, 0)
  146. json.Unmarshal([]byte(goods.Spe), &GoodsSpe)
  147. for k, v := range GoodsSpe {
  148. if len(v.Values) == 0 {
  149. GoodsSpe[k].Values = make([]string, 0)
  150. }
  151. }
  152. res.Spe = GoodsSpe
  153. sku := db.GetGoodsSkuById(svc.MasterDb(c), goods.Id)
  154. skuList := make([]md.MallGoodsSku, 0)
  155. if sku != nil {
  156. for _, v := range *sku {
  157. skuMap := make([]map[string]string, 0)
  158. json.Unmarshal([]byte(v.Sku), &skuMap)
  159. tmp := md.MallGoodsSku{
  160. SkuID: utils.Int64ToStr(v.SkuId),
  161. GoodsID: utils.IntToStr(v.GoodsId),
  162. Price: v.Price,
  163. Stock: utils.IntToStr(v.Stock),
  164. Indexes: v.Indexes,
  165. Sku: skuMap,
  166. }
  167. skuList = append(skuList, tmp)
  168. }
  169. }
  170. if goods.IsSingleSku == 1 {
  171. skuList = nil
  172. }
  173. re := map[string]interface{}{
  174. "base": res,
  175. "sku_list": skuList,
  176. }
  177. e.OutSuc(c, re, nil)
  178. return
  179. }