附近小店
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.
 
 
 

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