面包店
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

335 行
9.0 KiB

  1. package svc
  2. import (
  3. "applet/app/admin/md"
  4. "applet/app/db"
  5. "applet/app/db/model"
  6. "applet/app/e"
  7. "applet/app/svc"
  8. "applet/app/utils"
  9. "applet/app/utils/logx"
  10. "errors"
  11. "fmt"
  12. "github.com/gin-gonic/gin"
  13. "reflect"
  14. "strings"
  15. )
  16. func SchemeBindEnterprise(req md.SchemeBindEnterpriseReq) (err error) {
  17. session := db.Db.NewSession()
  18. defer session.Close()
  19. session.Begin()
  20. //1、查询 `scheme`
  21. schemeDb := db.SchemeDb{}
  22. schemeDb.Set()
  23. scheme, err := schemeDb.GetScheme(req.Id)
  24. if err != nil {
  25. return
  26. }
  27. if scheme == nil {
  28. return errors.New("未查询到相应记录")
  29. }
  30. //1、删除 `scheme_with_enterprise`
  31. schemeEnterpriseDb := db.SchemeEnterpriseDb{}
  32. schemeEnterpriseDb.Set()
  33. _, err = schemeEnterpriseDb.SchemeEnterpriseDeleteBySession(session, req.Id)
  34. if err != nil {
  35. _ = session.Rollback()
  36. return
  37. }
  38. //2、新增 `scheme_with_enterprise``
  39. var mm []*model.SchemeWithEnterprise
  40. for _, v := range req.EnterpriseIds {
  41. mm = append(mm, &model.SchemeWithEnterprise{
  42. SchemeId: scheme.Id,
  43. EnterpriseId: v,
  44. })
  45. }
  46. _, err = schemeEnterpriseDb.BatchAddSchemeEnterpriseBySession(session, mm)
  47. if err != nil {
  48. _ = session.Rollback()
  49. return
  50. }
  51. return session.Commit()
  52. }
  53. func DeleteScheme(id int) (err error) {
  54. session := db.Db.NewSession()
  55. defer session.Close()
  56. session.Begin()
  57. //1、删除 `scheme`
  58. schemeDb := db.SchemeDb{}
  59. schemeDb.Set()
  60. _, err = schemeDb.SchemeDeleteBySession(session, id)
  61. if err != nil {
  62. _ = session.Rollback()
  63. return
  64. }
  65. //2、删除 `scheme_with_enterprise`
  66. schemeEnterpriseDb := db.SchemeEnterpriseDb{}
  67. schemeEnterpriseDb.Set()
  68. _, err = schemeEnterpriseDb.SchemeEnterpriseDeleteBySession(session, id)
  69. if err != nil {
  70. _ = session.Rollback()
  71. return
  72. }
  73. return session.Commit()
  74. }
  75. func MallGoodsDetailByScheme(c *gin.Context, id int64, schemeId int) (map[string]interface{}, error) {
  76. schemeDb := db.SchemeDb{}
  77. schemeDb.Set()
  78. schemeInfo, err := schemeDb.GetScheme(schemeId)
  79. if err != nil {
  80. return nil, err
  81. }
  82. if schemeInfo == nil {
  83. return nil, errors.New("未查询到相应方案记录")
  84. }
  85. resp := make(map[string]interface{})
  86. var skuList []*model.Sku
  87. var goodsModel *model.Goods
  88. // 新增商品的时候默认数据
  89. if id == 0 {
  90. resp["base_goods"] = nil
  91. resp["sku"] = nil
  92. return resp, nil
  93. }
  94. goodsModelInterface, has, err := db.GetComm(db.Db, &model.Goods{GoodsId: int64(id)})
  95. if !has {
  96. return nil, errors.New("当前商品不存在")
  97. }
  98. if err != nil {
  99. _ = logx.Error(err)
  100. return nil, e.NewErr(e.ERR_DB_ORM, fmt.Sprintf("查询商品失败:%#v", err))
  101. }
  102. goodsModel = goodsModelInterface.(*model.Goods)
  103. err = db.Db.Where("goods_id=?", id).Find(&skuList)
  104. if err != nil || len(skuList) == 0 {
  105. _ = logx.Error(err)
  106. return nil, e.NewErr(e.ERR_DB_ORM, fmt.Sprintf("查询商品规格失败:%#v\n", err))
  107. }
  108. var goodsMap map[string]interface{}
  109. utils.Unserialize([]byte(utils.SerializeStr(goodsModel)), &goodsMap)
  110. goodsMap["goods_id"] = utils.AnyToString(goodsModel.GoodsId)
  111. // 分类名称
  112. if goodsModel != nil && goodsModel.CategoryId != 0 {
  113. categoryInterface, has, err := db.GetComm(db.Db, &model.GoodsCategory{Id: goodsModel.CategoryId})
  114. if err != nil {
  115. return nil, errors.New("查询商品类目失败")
  116. }
  117. categoryModel := categoryInterface.(*model.GoodsCategory)
  118. if !has {
  119. goodsMap["category_name"] = ""
  120. } else {
  121. goodsMap["category_name"] = categoryModel.Name
  122. }
  123. }
  124. // **处理各复合结构**
  125. var tmpMap interface{}
  126. scheme, host, subDomain, moreSubDomain := svc.ImageBucketNew(c)
  127. // 商品主图
  128. if goodsMap["image_list"] != nil && goodsMap["image_list"] != "null" {
  129. utils.Unserialize([]byte(goodsMap["image_list"].(string)), &tmpMap)
  130. goodsMap["image_list"] = tmpMap
  131. var imageListUrlTmp []string
  132. for _, v := range goodsMap["image_list"].([]interface{}) {
  133. imageListUrlTmp = append(imageListUrlTmp, svc.ImageFormatWithBucketNew(scheme, host, subDomain, moreSubDomain, v.(string)))
  134. }
  135. goodsMap["image_list_url"] = imageListUrlTmp
  136. } else {
  137. goodsMap["image_list"] = []struct{}{}
  138. goodsMap["image_list_url"] = []struct{}{}
  139. }
  140. // 详情图
  141. if goodsMap["detail"] != nil && goodsMap["detail"] != "null" {
  142. var tmpDetailImageList []interface{}
  143. utils.Unserialize([]byte(goodsMap["detail"].(string)), &tmpDetailImageList)
  144. goodsMap["detail"] = tmpDetailImageList
  145. var detailImageUrlTmp []string
  146. for _, v := range goodsMap["detail"].([]interface{}) {
  147. detailImageUrlTmp = append(detailImageUrlTmp, svc.ImageFormatWithBucketNew(scheme, host, subDomain, moreSubDomain, v.(string)))
  148. }
  149. goodsMap["detail_url"] = detailImageUrlTmp
  150. }
  151. if goodsMap["detail"] == nil || reflect.ValueOf(goodsMap["detail"]).IsNil() {
  152. goodsMap["detail"] = []interface{}{}
  153. goodsMap["detail_url"] = []interface{}{}
  154. }
  155. // 规格属性
  156. speJson, ok := goodsMap["spe"].(string)
  157. if ok {
  158. if goodsMap["spe"] != nil && speJson != "" && speJson != "null" {
  159. utils.Unserialize([]byte(speJson), &tmpMap)
  160. goodsMap["spe"] = tmpMap
  161. } else {
  162. utils.Unserialize([]byte("[{\"name\": \"默认\",\"values\": [\"默认\"]}]"), &tmpMap)
  163. goodsMap["spe"] = tmpMap
  164. }
  165. } else {
  166. utils.Unserialize([]byte("[{\"name\": \"默认\",\"values\": [\"默认\"]}]"), &tmpMap)
  167. goodsMap["spe"] = tmpMap
  168. }
  169. if goodsMap["spe_images"] != nil && goodsMap["spe_images"] != "null" && goodsMap["spe_images"] != "[]" && goodsMap["spe_images"] != "" {
  170. var speImageListUrlTmp []string
  171. utils.Unserialize([]byte(goodsMap["spe_images"].(string)), &tmpMap)
  172. goodsMap["spe_images"] = tmpMap
  173. for _, v := range goodsMap["spe_images"].([]interface{}) {
  174. speImageListUrlTmp = append(speImageListUrlTmp, svc.ImageFormatWithBucketNew(scheme, host, subDomain, moreSubDomain, v.(string)))
  175. }
  176. goodsMap["spe_images_url"] = speImageListUrlTmp
  177. } else {
  178. goodsMap["spe_images"] = []struct{}{}
  179. goodsMap["spe_images_url"] = []struct{}{}
  180. }
  181. // sku处理
  182. var skuIds []string
  183. for _, v := range skuList {
  184. skuIds = append(skuIds, utils.Int64ToStr(v.SkuId))
  185. }
  186. var schemeWithSkusMap = map[int64]string{}
  187. if len(skuIds) > 0 {
  188. var schemeWithSkus []*model.SchemeWithSku
  189. err = db.Db.Where("scheme_id = ?", schemeId).And(fmt.Sprintf("sku_id In (%s)", strings.Join(skuIds, ","))).Find(&schemeWithSkus)
  190. if err != nil {
  191. return nil, err
  192. }
  193. for _, v := range schemeWithSkus {
  194. schemeWithSkusMap[v.SkuId] = v.Price
  195. }
  196. }
  197. skuListMap := make([]map[string]interface{}, 0, len(skuList))
  198. for _, item := range skuList {
  199. tmp := make(map[string]interface{})
  200. tmp["goods_id"] = item.GoodsId
  201. tmp["sku_id"] = item.SkuId
  202. tmp["sku_code"] = item.SkuCode
  203. tmp["original_price"] = item.Price
  204. tmp["indexes"] = item.Indexes
  205. utils.Unserialize([]byte(item.Sku), &tmpMap)
  206. tmp["sku"] = tmpMap
  207. tmp["scheme_price"] = schemeWithSkusMap[item.SkuId]
  208. if item.Indexes == "" || item.Indexes == " " {
  209. tmp["indexes"] = "0"
  210. utils.Unserialize([]byte("[{\"name\": \"默认\",\"values\": [\"默认\"]}]"), &tmpMap)
  211. tmp["sku"] = tmpMap
  212. }
  213. skuListMap = append(skuListMap, tmp)
  214. }
  215. // 清理不用的字段
  216. if _, ok := goodsMap["create_time"]; ok {
  217. delete(goodsMap, "create_at")
  218. }
  219. if _, ok := goodsMap["update_time"]; ok {
  220. delete(goodsMap, "update_at")
  221. }
  222. if _, ok := goodsMap["delete_at"]; ok {
  223. delete(goodsMap, "delete_at")
  224. }
  225. goodsMap["sale_state_zh"] = ""
  226. if _, ok := goodsMap["sale_state"]; ok {
  227. switch goodsMap["sale_state"] {
  228. case 1.00:
  229. goodsMap["sale_state_zh"] = "销售中"
  230. break
  231. case 2.00:
  232. goodsMap["sale_state_zh"] = "下架"
  233. break
  234. }
  235. }
  236. goodsMap["create_at"] = goodsModel.CreateAt
  237. goodsMap["update_at"] = goodsModel.UpdateAt
  238. resp["base_goods"] = goodsMap
  239. resp["sku"] = skuListMap
  240. return resp, nil
  241. }
  242. func SaveSchemeGoods(req md.SaveSchemeGoodsReq) (err error) {
  243. //1、查询方案、商品记录是否存在
  244. schemeDb := db.SchemeDb{}
  245. schemeDb.Set()
  246. schemeInfo, err := schemeDb.GetScheme(req.SchemeId)
  247. if err != nil {
  248. return err
  249. }
  250. if schemeInfo == nil {
  251. return errors.New("未查询到相应方案记录")
  252. }
  253. goods := new(model.Goods)
  254. has, err := schemeDb.Db.Where("goods_id =?", req.GoodsId).Get(goods)
  255. if err != nil {
  256. return
  257. }
  258. if !has {
  259. return errors.New("未查询到相应商品记录")
  260. }
  261. //2、处理 scheme_with_goods 数据
  262. schemeWithGoodsDb := db.SchemeWithGoodsDb{}
  263. schemeWithGoodsDb.Set()
  264. schemeWithGoods, err := schemeWithGoodsDb.GetSchemeWithGoods(req.SchemeId, req.GoodsId)
  265. if err != nil {
  266. return err
  267. }
  268. if schemeWithGoods == nil {
  269. _, err = schemeWithGoodsDb.SchemeWithGoodsInsert(&model.SchemeWithGoods{
  270. SchemeId: req.SchemeId,
  271. GoodsId: req.GoodsId,
  272. })
  273. if err != nil {
  274. return err
  275. }
  276. }
  277. //3、处理 scheme_with_sku 数据
  278. schemeWithSkuDb := db.SchemeWithSkuDb{}
  279. schemeWithSkuDb.Set()
  280. //3.1 删除之前所有记录
  281. _, err = schemeWithSkuDb.SchemeDeleteByGoods(req.SchemeId, req.GoodsId)
  282. if err != nil {
  283. return err
  284. }
  285. //3.2 新增记录
  286. var mm []*model.SchemeWithSku
  287. for _, v := range req.SkuList {
  288. mm = append(mm, &model.SchemeWithSku{
  289. SchemeId: req.SchemeId,
  290. GoodsId: req.GoodsId,
  291. SkuId: v.SkuId,
  292. Price: v.Price,
  293. })
  294. }
  295. _, err = schemeWithSkuDb.BatchAddSchemeWithSku(mm)
  296. if err != nil {
  297. return err
  298. }
  299. return nil
  300. }