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

svc_goods.go 10 KiB

3 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. "applet/app/utils/logx"
  10. "encoding/json"
  11. "github.com/gin-gonic/gin"
  12. "github.com/jinzhu/copier"
  13. "github.com/shopspring/decimal"
  14. "strings"
  15. "time"
  16. )
  17. func GoodsCate(c *gin.Context) {
  18. var arg map[string]string
  19. if err := c.ShouldBindJSON(&arg); err != nil {
  20. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  21. return
  22. }
  23. user := svc.GetUser(c)
  24. arg["uid"] = utils.IntToStr(user.Info.Uid)
  25. arg["store_type"] = "2"
  26. cate, total := db.GetGoodsCate(svc.MasterDb(c), arg)
  27. list := make([]map[string]string, 0)
  28. if cate != nil {
  29. for _, v := range *cate {
  30. tmp := map[string]string{
  31. "id": utils.IntToStr(v.Id),
  32. "sort": utils.IntToStr(v.Sort),
  33. "is_show": utils.IntToStr(v.IsShow),
  34. "title": v.Title,
  35. }
  36. list = append(list, tmp)
  37. }
  38. }
  39. res := map[string]interface{}{
  40. "list": list, "total": total,
  41. }
  42. e.OutSuc(c, res, nil)
  43. return
  44. }
  45. func GoodsCateSave(c *gin.Context) {
  46. var arg map[string]string
  47. if err := c.ShouldBindJSON(&arg); err != nil {
  48. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  49. return
  50. }
  51. user := svc.GetUser(c)
  52. var data = &model.CommunityTeamCate{}
  53. if utils.StrToInt(arg["id"]) > 0 {
  54. cate := db.GetGoodsCateById(svc.MasterDb(c), arg["id"])
  55. if cate == nil {
  56. e.OutErr(c, 400, e.NewErr(400, "分类不存在"))
  57. return
  58. }
  59. data = cate
  60. } else {
  61. data = &model.CommunityTeamCate{
  62. IsShow: utils.StrToInt(arg["is_show"]),
  63. }
  64. svc.MasterDb(c).Insert(data)
  65. }
  66. data.StoreType = 2
  67. data.Uid = user.Info.Uid
  68. data.Sort = utils.StrToInt(arg["sort"])
  69. data.IsShow = utils.StrToInt(arg["is_show"])
  70. data.Title = arg["title"]
  71. svc.MasterDb(c).Where("id=?", data.Id).Update(data)
  72. e.OutSuc(c, "success", nil)
  73. return
  74. }
  75. func GoodsCateShow(c *gin.Context) {
  76. var arg map[string]string
  77. if err := c.ShouldBindJSON(&arg); err != nil {
  78. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  79. return
  80. }
  81. cate := db.GetGoodsCateById(svc.MasterDb(c), arg["id"])
  82. if cate == nil {
  83. e.OutErr(c, 400, e.NewErr(400, "分类不存在"))
  84. return
  85. }
  86. cate.IsShow = utils.StrToInt(arg["is_show"])
  87. svc.MasterDb(c).Where("id=?", cate.Id).Cols("is_show").Update(cate)
  88. e.OutSuc(c, "success", nil)
  89. return
  90. }
  91. func GoodsCateDel(c *gin.Context) {
  92. var arg map[string]string
  93. if err := c.ShouldBindJSON(&arg); err != nil {
  94. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  95. return
  96. }
  97. svc.MasterDb(c).In("id", strings.Split(arg["ids"], ",")).Delete(&model.CommunityTeamCate{})
  98. e.OutSuc(c, "success", nil)
  99. return
  100. }
  101. func Goods(c *gin.Context) {
  102. var arg map[string]string
  103. if err := c.ShouldBindJSON(&arg); err != nil {
  104. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  105. return
  106. }
  107. user := svc.GetUser(c)
  108. arg["uid"] = utils.IntToStr(user.Info.Uid)
  109. arg["store_type"] = "2"
  110. cate, total := db.GetStoreGoods(svc.MasterDb(c), arg)
  111. list := make([]map[string]string, 0)
  112. cateData := db.GetGoodsCateAll(svc.MasterDb(c))
  113. cateList := make([]map[string]string, 0)
  114. cateMap := make(map[int]string)
  115. if cateData != nil {
  116. for _, v := range *cateData {
  117. tmp := map[string]string{
  118. "name": v.Title, "value": utils.IntToStr(v.Id),
  119. }
  120. cateList = append(cateList, tmp)
  121. cateMap[v.Id] = v.Title
  122. }
  123. }
  124. if cate != nil {
  125. stateMap := []string{"上架", "下架"}
  126. scheme, host := svc.ImageBucket(c)
  127. for _, v := range *cate {
  128. tmp := map[string]string{
  129. "id": utils.IntToStr(v.Id),
  130. "title": v.Title,
  131. "price": v.Price,
  132. "info": v.Info,
  133. "img_url": svc.ImageFormatWithBucket(scheme, host, v.Img),
  134. "create_at": v.CreateAt.Format("2006-01-02 15:04:05"),
  135. "cate": cateMap[v.Cid],
  136. "state": stateMap[v.State],
  137. }
  138. list = append(list, tmp)
  139. }
  140. }
  141. res := map[string]interface{}{
  142. "list": list, "total": total, "cate_list": cateList,
  143. }
  144. e.OutSuc(c, res, nil)
  145. return
  146. }
  147. func GoodsSave(c *gin.Context) {
  148. var args md.AddGoodsReq
  149. if err := c.ShouldBindJSON(&args); err != nil {
  150. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  151. return
  152. }
  153. saleCount := 0
  154. eg := svc.MasterDb(c)
  155. user := svc.GetUser(c)
  156. if utils.StrToInt(args.Base.Id) != 0 {
  157. oldGoods := db.GetGoodsById(eg, args.Base.Id)
  158. if oldGoods == nil {
  159. e.OutErr(c, 400, e.NewErr(400, "商品不存在"))
  160. return
  161. }
  162. saleCount = oldGoods.SaleCount
  163. }
  164. sess := eg.NewSession()
  165. defer func() {
  166. _ = sess.Close()
  167. }()
  168. err := sess.Begin()
  169. if err != nil {
  170. e.OutErr(c, 400, e.NewErr(400, err.Error()))
  171. return
  172. }
  173. // 商品基本信息保存
  174. var goods model.CommunityTeamGoods
  175. err = copier.Copy(&goods, args.Base)
  176. goods.StoreType = 2
  177. goods.Uid = user.Info.Uid
  178. goods.Id = utils.StrToInt(args.Base.Id)
  179. goods.Stock = utils.StrToInt(args.Base.Stock)
  180. goods.State = utils.StrToInt(args.Base.State)
  181. goods.Cid = utils.StrToInt(args.Base.Cid)
  182. goods.IsSingleSku = utils.StrToInt(args.Base.IsSingleSku)
  183. goods.Sort = utils.StrToInt(args.Base.Sort)
  184. if err != nil {
  185. _ = sess.Rollback()
  186. e.OutErr(c, 400, e.NewErr(400, err.Error()))
  187. return
  188. }
  189. goods.SaleCount = saleCount
  190. goods.Img = args.Base.Img
  191. goods.ImageList = utils.SerializeStr([]string{args.Base.Img})
  192. goods.Spe = utils.SerializeStr(args.Base.Spe)
  193. // 商品价格处理
  194. if goods.IsSingleSku == 1 { // 单规格 计算折扣价
  195. args.SkuList = []md.MallGoodsSku{
  196. {
  197. Price: args.Base.Price,
  198. Stock: args.Base.Stock,
  199. Indexes: "",
  200. Sku: []map[string]string{},
  201. },
  202. }
  203. } else { // 多规格
  204. var totalStock = 0
  205. minPriceD, err := decimal.NewFromString(args.SkuList[0].Price)
  206. if err != nil {
  207. _ = sess.Rollback()
  208. e.OutErr(c, 400, e.NewErr(400, "规格价格格式有误"))
  209. return
  210. }
  211. for _, item := range args.SkuList {
  212. tempPrice, err := decimal.NewFromString(item.Price)
  213. totalStock += utils.StrToInt(item.Stock)
  214. if err != nil {
  215. _ = sess.Rollback()
  216. logx.Warn("", err)
  217. e.OutErr(c, 400, e.NewErr(400, "规格价格格式有误"))
  218. return
  219. }
  220. if tempPrice.LessThan(minPriceD) {
  221. minPriceD = tempPrice
  222. }
  223. }
  224. goods.Price = minPriceD.String()
  225. goods.Stock = totalStock
  226. }
  227. goods.UpdateAt = time.Now()
  228. if utils.StrToInt(args.Base.Id) == 0 { // 新增goods
  229. goods.CreateAt = time.Now()
  230. _, err = sess.Insert(&goods)
  231. if err != nil {
  232. logx.Warn(err)
  233. _ = sess.Rollback()
  234. e.OutErr(c, 400, e.NewErr(400, "插入商品数据失败"))
  235. return
  236. }
  237. } else { // 更新数据
  238. oldGoods := db.GetGoodsByIdSess(sess, args.Base.Id)
  239. goods.CreateAt = oldGoods.CreateAt
  240. _, err = sess.Where(" id = ?", args.Base.Id).
  241. AllCols().Update(&goods)
  242. if err != nil {
  243. _ = sess.Rollback()
  244. logx.Warn(err)
  245. e.OutErr(c, 400, e.NewErr(400, "更新商品失败"))
  246. return
  247. }
  248. }
  249. skuModelList := make([]*model.CommunityTeamSku, 0, len(args.SkuList))
  250. for _, item := range args.SkuList {
  251. skuModelList = append(skuModelList, &model.CommunityTeamSku{
  252. GoodsId: goods.Id,
  253. Price: item.Price,
  254. Stock: utils.StrToInt(item.Stock),
  255. Indexes: GetIndexesStr(&args.Base, item),
  256. Sku: utils.SerializeStr(&item.Sku),
  257. Discount: "10",
  258. })
  259. }
  260. // 插入sku信息
  261. if len(skuModelList) != 0 {
  262. insert, err := sess.Insert(skuModelList)
  263. if err != nil || insert < 1 {
  264. _ = sess.Rollback()
  265. e.OutErr(c, 400, e.NewErr(400, "商品规格插入失败"))
  266. return
  267. }
  268. }
  269. sess.Commit()
  270. e.OutSuc(c, "success", nil)
  271. return
  272. }
  273. func GetIndexesStr(baseGoods *md.BaseGoods, goodsSku md.MallGoodsSku) string {
  274. spe := baseGoods.Spe
  275. sku := goodsSku.Sku
  276. result := ""
  277. for index, item := range sku {
  278. for _, itm := range spe {
  279. if item["name"] == itm.Name {
  280. for i, v := range spe[index].Values {
  281. if v == item["value"] {
  282. result = result + utils.IntToStr(i) + "-"
  283. }
  284. }
  285. }
  286. }
  287. }
  288. return strings.Trim(result, "-")
  289. }
  290. func GoodsDel(c *gin.Context) {
  291. var arg map[string]string
  292. if err := c.ShouldBindJSON(&arg); err != nil {
  293. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  294. return
  295. }
  296. svc.MasterDb(c).In("id", strings.Split(arg["ids"], ",")).Delete(&model.CommunityTeamGoods{})
  297. svc.MasterDb(c).In("goods_id", strings.Split(arg["ids"], ",")).Delete(&model.CommunityTeamSku{})
  298. e.OutSuc(c, "success", nil)
  299. return
  300. }
  301. func GoodsDetail(c *gin.Context) {
  302. var arg map[string]string
  303. if err := c.ShouldBindJSON(&arg); err != nil {
  304. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  305. return
  306. }
  307. goods := db.GetGoodsById(svc.MasterDb(c), arg["id"])
  308. if goods == nil {
  309. e.OutErr(c, 400, e.NewErr(400, "商品不存在"))
  310. return
  311. }
  312. cateData := db.GetGoodsCateAll(svc.MasterDb(c))
  313. cateList := make([]map[string]string, 0)
  314. cateMap := make(map[int]string)
  315. if cateData != nil {
  316. for _, v := range *cateData {
  317. tmp := map[string]string{
  318. "name": v.Title, "value": utils.IntToStr(v.Id),
  319. }
  320. cateList = append(cateList, tmp)
  321. cateMap[v.Id] = v.Title
  322. }
  323. }
  324. stateMap := []string{"上架", "下架"}
  325. scheme, host := svc.ImageBucket(c)
  326. res := md.BaseGoods{
  327. Id: utils.IntToStr(goods.Id),
  328. Title: goods.Title,
  329. Cid: utils.IntToStr(goods.Cid),
  330. CategoryName: cateMap[goods.Cid],
  331. IsSingleSku: utils.IntToStr(goods.IsSingleSku),
  332. Price: goods.Price,
  333. Stock: utils.IntToStr(goods.Stock),
  334. Img: goods.Img,
  335. ImgUrl: svc.ImageFormatWithBucket(scheme, host, goods.Img),
  336. Spe: nil,
  337. Sort: utils.IntToStr(goods.Sort),
  338. Info: goods.Info,
  339. State: utils.IntToStr(goods.State),
  340. StateStr: stateMap[goods.State],
  341. }
  342. GoodsSpe := make([]md.GoodsSpe, 0)
  343. json.Unmarshal([]byte(goods.Spe), &GoodsSpe)
  344. for k, v := range GoodsSpe {
  345. if len(v.Values) == 0 {
  346. GoodsSpe[k].Values = make([]string, 0)
  347. }
  348. }
  349. res.Spe = GoodsSpe
  350. sku := db.GetGoodsSkuById(svc.MasterDb(c), goods.Id)
  351. skuList := make([]md.MallGoodsSku, 0)
  352. if sku != nil {
  353. for _, v := range *sku {
  354. skuMap := make([]map[string]string, 0)
  355. json.Unmarshal([]byte(v.Sku), &skuMap)
  356. tmp := md.MallGoodsSku{
  357. SkuID: utils.Int64ToStr(v.SkuId),
  358. GoodsID: utils.IntToStr(v.GoodsId),
  359. Price: v.Price,
  360. Stock: utils.IntToStr(v.Stock),
  361. Indexes: v.Indexes,
  362. Sku: skuMap,
  363. }
  364. skuList = append(skuList, tmp)
  365. }
  366. }
  367. if goods.IsSingleSku == 1 {
  368. skuList = nil
  369. }
  370. re := map[string]interface{}{
  371. "base": res,
  372. "sku_list": skuList,
  373. }
  374. e.OutSuc(c, re, nil)
  375. return
  376. }