附近小店
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

svc_agent_goods.go 4.6 KiB

2ヶ月前
2ヶ月前
2ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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"], ",")).Get(&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 = 0
  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].Stock = 0
  91. }
  92. _, err = sess.Insert(&goodsSku)
  93. if err != nil {
  94. sess.Rollback()
  95. e.OutErr(c, 400, e.NewErr(400, "添加失败"))
  96. return
  97. }
  98. }
  99. sess.Commit()
  100. e.OutSuc(c, "success", nil)
  101. return
  102. }
  103. func AgentGoodsDetail(c *gin.Context) {
  104. var arg map[string]string
  105. if err := c.ShouldBindJSON(&arg); err != nil {
  106. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  107. return
  108. }
  109. goods := db.GetGoodsById(svc.MasterDb(c), arg["id"])
  110. if goods == nil {
  111. e.OutErr(c, 400, e.NewErr(400, "商品不存在"))
  112. return
  113. }
  114. cateData := db.GetGoodsCateAll(svc.MasterDb(c))
  115. cateList := make([]map[string]string, 0)
  116. cateMap := make(map[int]string)
  117. if cateData != nil {
  118. for _, v := range *cateData {
  119. tmp := map[string]string{
  120. "name": v.Title, "value": utils.IntToStr(v.Id),
  121. }
  122. cateList = append(cateList, tmp)
  123. cateMap[v.Id] = v.Title
  124. }
  125. }
  126. stateMap := []string{"上架", "下架"}
  127. scheme, host := svc.ImageBucket(c)
  128. res := md.BaseGoods{
  129. Id: utils.IntToStr(goods.Id),
  130. Title: goods.Title,
  131. Cid: utils.IntToStr(goods.Cid),
  132. CategoryName: cateMap[goods.Cid],
  133. IsSingleSku: utils.IntToStr(goods.IsSingleSku),
  134. Price: goods.Price,
  135. Stock: utils.IntToStr(goods.Stock),
  136. Img: goods.Img,
  137. ImgUrl: svc.ImageFormatWithBucket(scheme, host, goods.Img),
  138. Spe: nil,
  139. Sort: utils.IntToStr(goods.Sort),
  140. Info: goods.Info,
  141. State: utils.IntToStr(goods.State),
  142. StateStr: stateMap[goods.State],
  143. }
  144. GoodsSpe := make([]md.GoodsSpe, 0)
  145. json.Unmarshal([]byte(goods.Spe), &GoodsSpe)
  146. for k, v := range GoodsSpe {
  147. if len(v.Values) == 0 {
  148. GoodsSpe[k].Values = make([]string, 0)
  149. }
  150. }
  151. res.Spe = GoodsSpe
  152. sku := db.GetGoodsSkuById(svc.MasterDb(c), goods.Id)
  153. skuList := make([]md.MallGoodsSku, 0)
  154. if sku != nil {
  155. for _, v := range *sku {
  156. skuMap := make([]map[string]string, 0)
  157. json.Unmarshal([]byte(v.Sku), &skuMap)
  158. tmp := md.MallGoodsSku{
  159. SkuID: utils.Int64ToStr(v.SkuId),
  160. GoodsID: utils.IntToStr(v.GoodsId),
  161. Price: v.Price,
  162. Stock: utils.IntToStr(v.Stock),
  163. Indexes: v.Indexes,
  164. Sku: skuMap,
  165. }
  166. skuList = append(skuList, tmp)
  167. }
  168. }
  169. if goods.IsSingleSku == 1 {
  170. skuList = nil
  171. }
  172. re := map[string]interface{}{
  173. "base": res,
  174. "sku_list": skuList,
  175. }
  176. e.OutSuc(c, re, nil)
  177. return
  178. }