|
- package svc
-
- import (
- "applet/app/admin/md"
- "applet/app/db"
- "applet/app/db/model"
- "applet/app/e"
- "applet/app/svc"
- "applet/app/utils"
- "applet/app/utils/logx"
- "errors"
- "fmt"
- "github.com/gin-gonic/gin"
- "reflect"
- "strings"
- )
-
- func SchemeBindEnterprise(req md.SchemeBindEnterpriseReq) (err error) {
- session := db.Db.NewSession()
- defer session.Close()
- session.Begin()
- //1、查询 `scheme`
- schemeDb := db.SchemeDb{}
- schemeDb.Set()
- scheme, err := schemeDb.GetScheme(req.Id)
- if err != nil {
- return
- }
- if scheme == nil {
- return errors.New("未查询到相应记录")
- }
-
- //1、删除 `scheme_with_enterprise`
- schemeEnterpriseDb := db.SchemeEnterpriseDb{}
- schemeEnterpriseDb.Set()
- _, err = schemeEnterpriseDb.SchemeEnterpriseDeleteBySession(session, req.Id)
- if err != nil {
- _ = session.Rollback()
- return
- }
-
- //2、新增 `scheme_with_enterprise``
- var mm []*model.SchemeWithEnterprise
- for _, v := range req.EnterpriseIds {
- mm = append(mm, &model.SchemeWithEnterprise{
- SchemeId: scheme.Id,
- EnterpriseId: v,
- })
- }
- _, err = schemeEnterpriseDb.BatchAddSchemeEnterpriseBySession(session, mm)
- if err != nil {
- _ = session.Rollback()
- return
- }
-
- return session.Commit()
- }
-
- func DeleteScheme(id int) (err error) {
- session := db.Db.NewSession()
- defer session.Close()
- session.Begin()
-
- //1、删除 `scheme`
- schemeDb := db.SchemeDb{}
- schemeDb.Set()
- _, err = schemeDb.SchemeDeleteBySession(session, id)
- if err != nil {
- _ = session.Rollback()
- return
- }
-
- //2、删除 `scheme_with_enterprise`
- schemeEnterpriseDb := db.SchemeEnterpriseDb{}
- schemeEnterpriseDb.Set()
- _, err = schemeEnterpriseDb.SchemeEnterpriseDeleteBySession(session, id)
- if err != nil {
- _ = session.Rollback()
- return
- }
-
- return session.Commit()
- }
-
- func MallGoodsDetailByScheme(c *gin.Context, id int64, schemeId int) (map[string]interface{}, error) {
- schemeDb := db.SchemeDb{}
- schemeDb.Set()
- schemeInfo, err := schemeDb.GetScheme(schemeId)
- if err != nil {
- return nil, err
- }
- if schemeInfo == nil {
- return nil, errors.New("未查询到相应方案记录")
- }
-
- resp := make(map[string]interface{})
- var skuList []*model.Sku
- var goodsModel *model.Goods
-
- // 新增商品的时候默认数据
- if id == 0 {
- resp["base_goods"] = nil
- resp["sku"] = nil
- return resp, nil
- }
-
- goodsModelInterface, has, err := db.GetComm(db.Db, &model.Goods{GoodsId: int64(id)})
- if !has {
- return nil, errors.New("当前商品不存在")
- }
- if err != nil {
- _ = logx.Error(err)
- return nil, e.NewErr(e.ERR_DB_ORM, fmt.Sprintf("查询商品失败:%#v", err))
- }
- goodsModel = goodsModelInterface.(*model.Goods)
-
- err = db.Db.Where("goods_id=?", id).Find(&skuList)
- if err != nil || len(skuList) == 0 {
- _ = logx.Error(err)
- return nil, e.NewErr(e.ERR_DB_ORM, fmt.Sprintf("查询商品规格失败:%#v\n", err))
- }
-
- var goodsMap map[string]interface{}
- utils.Unserialize([]byte(utils.SerializeStr(goodsModel)), &goodsMap)
- goodsMap["goods_id"] = utils.AnyToString(goodsModel.GoodsId)
- // 分类名称
- if goodsModel != nil && goodsModel.CategoryId != 0 {
- categoryInterface, has, err := db.GetComm(db.Db, &model.GoodsCategory{Id: goodsModel.CategoryId})
- if err != nil {
- return nil, errors.New("查询商品类目失败")
- }
- categoryModel := categoryInterface.(*model.GoodsCategory)
- if !has {
- goodsMap["category_name"] = ""
- } else {
- goodsMap["category_name"] = categoryModel.Name
- }
- }
-
- // **处理各复合结构**
- var tmpMap interface{}
-
- scheme, host, subDomain, moreSubDomain := svc.ImageBucketNew(c)
-
- // 商品主图
- if goodsMap["image_list"] != nil && goodsMap["image_list"] != "null" {
- utils.Unserialize([]byte(goodsMap["image_list"].(string)), &tmpMap)
- goodsMap["image_list"] = tmpMap
- var imageListUrlTmp []string
- for _, v := range goodsMap["image_list"].([]interface{}) {
- imageListUrlTmp = append(imageListUrlTmp, svc.ImageFormatWithBucketNew(scheme, host, subDomain, moreSubDomain, v.(string)))
- }
- goodsMap["image_list_url"] = imageListUrlTmp
- } else {
- goodsMap["image_list"] = []struct{}{}
- goodsMap["image_list_url"] = []struct{}{}
- }
-
- // 详情图
- if goodsMap["detail"] != nil && goodsMap["detail"] != "null" {
- var tmpDetailImageList []interface{}
- utils.Unserialize([]byte(goodsMap["detail"].(string)), &tmpDetailImageList)
- goodsMap["detail"] = tmpDetailImageList
- var detailImageUrlTmp []string
- for _, v := range goodsMap["detail"].([]interface{}) {
- detailImageUrlTmp = append(detailImageUrlTmp, svc.ImageFormatWithBucketNew(scheme, host, subDomain, moreSubDomain, v.(string)))
- }
- goodsMap["detail_url"] = detailImageUrlTmp
- }
-
- if goodsMap["detail"] == nil || reflect.ValueOf(goodsMap["detail"]).IsNil() {
- goodsMap["detail"] = []interface{}{}
- goodsMap["detail_url"] = []interface{}{}
- }
-
- // 规格属性
- speJson, ok := goodsMap["spe"].(string)
- if ok {
- if goodsMap["spe"] != nil && speJson != "" && speJson != "null" {
- utils.Unserialize([]byte(speJson), &tmpMap)
- goodsMap["spe"] = tmpMap
- } else {
- utils.Unserialize([]byte("[{\"name\": \"默认\",\"values\": [\"默认\"]}]"), &tmpMap)
- goodsMap["spe"] = tmpMap
- }
- } else {
- utils.Unserialize([]byte("[{\"name\": \"默认\",\"values\": [\"默认\"]}]"), &tmpMap)
- goodsMap["spe"] = tmpMap
- }
- if goodsMap["spe_images"] != nil && goodsMap["spe_images"] != "null" && goodsMap["spe_images"] != "[]" && goodsMap["spe_images"] != "" {
- var speImageListUrlTmp []string
- utils.Unserialize([]byte(goodsMap["spe_images"].(string)), &tmpMap)
- goodsMap["spe_images"] = tmpMap
- for _, v := range goodsMap["spe_images"].([]interface{}) {
- speImageListUrlTmp = append(speImageListUrlTmp, svc.ImageFormatWithBucketNew(scheme, host, subDomain, moreSubDomain, v.(string)))
- }
- goodsMap["spe_images_url"] = speImageListUrlTmp
- } else {
- goodsMap["spe_images"] = []struct{}{}
- goodsMap["spe_images_url"] = []struct{}{}
- }
-
- // sku处理
- var skuIds []string
- for _, v := range skuList {
- skuIds = append(skuIds, utils.Int64ToStr(v.SkuId))
- }
-
- var schemeWithSkusMap = map[int64]string{}
- if len(skuIds) > 0 {
- var schemeWithSkus []*model.SchemeWithSku
- err = db.Db.Where("scheme_id = ?", schemeId).And(fmt.Sprintf("sku_id In (%s)", strings.Join(skuIds, ","))).Find(&schemeWithSkus)
- if err != nil {
- return nil, err
- }
- for _, v := range schemeWithSkus {
- schemeWithSkusMap[v.SkuId] = v.Price
- }
- }
-
- skuListMap := make([]map[string]interface{}, 0, len(skuList))
- for _, item := range skuList {
- tmp := make(map[string]interface{})
- tmp["goods_id"] = item.GoodsId
- tmp["sku_id"] = item.SkuId
- tmp["sku_code"] = item.SkuCode
- tmp["original_price"] = item.Price
- tmp["indexes"] = item.Indexes
- utils.Unserialize([]byte(item.Sku), &tmpMap)
- tmp["sku"] = tmpMap
- tmp["scheme_price"] = schemeWithSkusMap[item.SkuId]
- if item.Indexes == "" || item.Indexes == " " {
- tmp["indexes"] = "0"
- utils.Unserialize([]byte("[{\"name\": \"默认\",\"values\": [\"默认\"]}]"), &tmpMap)
- tmp["sku"] = tmpMap
- }
- skuListMap = append(skuListMap, tmp)
- }
-
- // 清理不用的字段
- if _, ok := goodsMap["create_time"]; ok {
- delete(goodsMap, "create_at")
- }
- if _, ok := goodsMap["update_time"]; ok {
- delete(goodsMap, "update_at")
- }
- if _, ok := goodsMap["delete_at"]; ok {
- delete(goodsMap, "delete_at")
- }
-
- goodsMap["sale_state_zh"] = ""
- if _, ok := goodsMap["sale_state"]; ok {
- switch goodsMap["sale_state"] {
- case 1.00:
- goodsMap["sale_state_zh"] = "销售中"
- break
- case 2.00:
- goodsMap["sale_state_zh"] = "下架"
- break
- }
- }
-
- goodsMap["create_at"] = goodsModel.CreateAt
- goodsMap["update_at"] = goodsModel.UpdateAt
-
- resp["base_goods"] = goodsMap
- resp["sku"] = skuListMap
-
- return resp, nil
-
- }
-
- func SaveSchemeGoods(req md.SaveSchemeGoodsReq) (err error) {
- //1、查询方案、商品记录是否存在
- schemeDb := db.SchemeDb{}
- schemeDb.Set()
- schemeInfo, err := schemeDb.GetScheme(req.SchemeId)
- if err != nil {
- return err
- }
- if schemeInfo == nil {
- return errors.New("未查询到相应方案记录")
- }
- goods := new(model.Goods)
- has, err := schemeDb.Db.Where("goods_id =?", req.GoodsId).Get(goods)
- if err != nil {
- return
- }
- if !has {
- return errors.New("未查询到相应商品记录")
- }
-
- //2、处理 scheme_with_goods 数据
- schemeWithGoodsDb := db.SchemeWithGoodsDb{}
- schemeWithGoodsDb.Set()
- schemeWithGoods, err := schemeWithGoodsDb.GetSchemeWithGoods(req.SchemeId, req.GoodsId)
- if err != nil {
- return err
- }
- if schemeWithGoods == nil {
- _, err = schemeWithGoodsDb.SchemeWithGoodsInsert(&model.SchemeWithGoods{
- SchemeId: req.SchemeId,
- GoodsId: req.GoodsId,
- })
- if err != nil {
- return err
- }
- }
-
- //3、处理 scheme_with_sku 数据
- schemeWithSkuDb := db.SchemeWithSkuDb{}
- schemeWithSkuDb.Set()
- //3.1 删除之前所有记录
- _, err = schemeWithSkuDb.SchemeDeleteByGoods(req.SchemeId, req.GoodsId)
- if err != nil {
- return err
- }
-
- //3.2 新增记录
- var mm []*model.SchemeWithSku
- for _, v := range req.SkuList {
- mm = append(mm, &model.SchemeWithSku{
- SchemeId: req.SchemeId,
- GoodsId: req.GoodsId,
- SkuId: v.SkuId,
- Price: v.Price,
- })
- }
- _, err = schemeWithSkuDb.BatchAddSchemeWithSku(mm)
- if err != nil {
- return err
- }
- return nil
- }
|