面包店
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.

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