Administrator 2 anni fa
parent
commit
2c3cc74352

+ 8 - 0
configs/config.go

@@ -64,10 +64,18 @@ func ConfigInit() {
 	fmt.Println(fmt.Sprintf("%s:%s@(%s:%s)/%s?charset=utf8", config.Get("mysql.user"), config.Get("mysql.password"), config.Get("mysql.host"), config.Get("mysql.port"), config.Get("mysql.database")))
 	Engine, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@(%s:%s)/%s?charset=utf8", config.Get("mysql.user"), config.Get("mysql.password"), config.Get("mysql.host"), config.Get("mysql.port"), config.Get("mysql.database")))
 	Engine.Ping() //连接测试
+	//TODO  显示sql
+	Engine.ShowSQL(true)
+	Engine.Logger().ShowSQL(true)
 	RedisDb = redis.NewClient(&redis.Options{
 		Addr:     fmt.Sprintf("%s:%s", config.Get("redis.host"), config.Get("redis.port")), // Redis地址
 		Password: "",                                                                       // Redis密码,如果没有则为空字符串
 		DB:       0,                                                                        // 使用默认DB
 	})
+	//开启debug模式
+
 	Config = config
 }
+func GoGormConfigInit() {
+
+}

+ 140 - 0
data/dao/GoodsDetailDao.go

@@ -0,0 +1,140 @@
+package dao
+
+import (
+	"demo/configs"
+	"demo/data/domain"
+	"demo/data/domain/vo"
+	"fmt"
+)
+
+// SelectGoodsDetailById 获取相同的商品详情
+func SelectGoodsDetailById(skuId int) (vo.DetailVo, error) {
+	detailVo := vo.DetailVo{}
+	//================查询sku========================
+	var sku domain.GoodsSku
+	//查询 sku
+	rows, err := configs.Engine.Table("goods_sku").
+		Where("goods_id = (select a.goods_id as goods_id from goods_sku as a where a.id = ? )", skuId).
+		Desc("create_time").Rows(&sku)
+	if err != nil {
+		println("查询sku失败", err)
+		return vo.DetailVo{}, err
+	}
+	var skus []domain.GoodsSku
+	var commodityAreaIds = make([]int, 0)
+	commodityAreaIdsString := ""
+A:
+	for rows.Next() {
+		rows.Scan(&sku)
+		skus = append(skus, sku)
+		//去重
+		for i := range commodityAreaIds {
+			if sku.CommodityAreaId == commodityAreaIds[i] {
+				continue A
+			}
+		}
+		commodityAreaIds = append(commodityAreaIds, sku.CommodityAreaId)
+		commodityAreaIdsString = fmt.Sprint(commodityAreaIdsString, ",", sku.CommodityAreaId)
+	}
+	commodityAreaIdsString = commodityAreaIdsString[1:]
+	fmt.Println("=============================>", commodityAreaIdsString)
+	//================查询goods========================
+	var goods domain.Goods
+	_, err = configs.Engine.Table("goods").Where("id = ?", sku.GoodsId).Get(&goods)
+	if err != nil {
+		println("查询goods失败", err)
+		return vo.DetailVo{}, err
+	}
+	//=================查询goods_tag========================
+	var goodsTag domain.GoodsTag
+	rows, err = configs.Engine.Table("goods_tag").
+		Where(" FIND_IN_SET(goods_tag.id,?)", goods.TagIds).
+		Rows(&goodsTag)
+	if err != nil {
+		println("查询goods_tag失败", err)
+		return vo.DetailVo{}, err
+	}
+	var goodsTags []domain.GoodsTag
+	for rows.Next() {
+		rows.Scan(&goodsTag)
+		goodsTags = append(goodsTags, goodsTag)
+	}
+	//=================查询goods_type========================
+	var goodsType domain.GoodsType
+	_, err = configs.Engine.Table("goods_type").Where("id = ?", goods.TypeId).Get(&goodsType)
+	if err != nil {
+		println("查询goods_type失败", err)
+		return vo.DetailVo{}, err
+	}
+	//=================查询goods_CommodityArea========================
+	var goodsCommodityArea domain.GoodsCommodityArea
+	rows, err = configs.Engine.Table("goods_commodity_area").
+		Where(" FIND_IN_SET(goods_commodity_area.id,?) and goods_id = ?", commodityAreaIdsString, goods.Id).
+		Rows(&goodsCommodityArea)
+	if err != nil {
+		println("查询goods_CommodityArea失败", err)
+		return vo.DetailVo{}, err
+	}
+	var goodsCommodityAreas []domain.GoodsCommodityArea
+
+	var introductionIds = make([]int, 0)
+	var introductionIdString = ""
+B:
+	for rows.Next() {
+		rows.Scan(&goodsCommodityArea)
+		goodsCommodityAreas = append(goodsCommodityAreas, goodsCommodityArea)
+		for i := range introductionIds {
+			if goodsCommodityArea.DetailIntroductionId == introductionIds[i] {
+				continue B
+			}
+		}
+		introductionIds = append(introductionIds, goodsCommodityArea.DetailIntroductionId)
+		introductionIdString = fmt.Sprint(introductionIdString, ",", goodsCommodityArea.DetailIntroductionId)
+	}
+	introductionIdString = introductionIdString[1:]
+	fmt.Println("=============================>", introductionIdString)
+	//=================查询goods_introduction========================
+	var goodsIntroduction domain.GoodsIntroduction
+	rows, err = configs.Engine.Table("goods_introduction").
+		Where("FIND_IN_SET(id,?)", introductionIdString).
+		Rows(&goodsIntroduction)
+	if err != nil {
+		println("查询goods_introduction失败", err)
+		return vo.DetailVo{}, err
+	}
+
+	var goodsIntroductions []domain.GoodsIntroduction
+	for rows.Next() {
+		rows.Scan(&goodsIntroduction)
+		goodsIntroductions = append(goodsIntroductions, goodsIntroduction)
+	}
+
+	detailVo.DetailGoodsVo = vo.DetailGoodsVo{
+		Tags:        goodsTags,
+		GoodsId:     goods.Id,
+		Name:        goods.GoodsName,
+		SalesVolume: goods.SalesVolume,
+		Type:        goodsType,
+	}
+
+	detailVo.DetailSkuVo = vo.DetailSkuVo{
+		SkuList:  skus,
+		NowSkuId: skuId,
+	}
+
+	detailVo.DetailCommodityAreaVo = vo.DetailCommodityAreaVo{
+		CommodityAreaList:  goodsCommodityAreas,
+		NowCommodityAreaId: sku.CommodityAreaId,
+	}
+
+	//=================相关推荐========================
+	//复制sku 8个数据
+	if len(skus) >= 8 {
+		detailVo.RecommendedGoods = skus[0:8]
+	} else {
+		detailVo.RecommendedGoods = skus
+	}
+	detailVo.Introduction = goodsIntroductions
+
+	return detailVo, nil
+}

+ 5 - 0
data/dao/GoodsIndexDao.go

@@ -0,0 +1,5 @@
+package dao
+
+func GetIndexPage(skuId int) {
+
+}

+ 15 - 0
data/dao/GoodsSkuDao.go

@@ -0,0 +1,15 @@
+package dao
+
+import (
+	"demo/configs"
+	"demo/data/domain"
+)
+
+func SelectGoodsSkuById(skuId int) {
+	var goodsSku domain.GoodsSku
+	_, err := configs.Engine.Table("goods_sku").Where("id = ?", skuId).Limit(1).Get(&goodsSku)
+	if err != nil {
+		return
+	}
+
+}

+ 2 - 0
data/domain/Goods.go

@@ -1,9 +1,11 @@
 package domain
 
+// Goods 商品
 type Goods struct {
 	Id             int    `xorm:"'id'" json:"id"`
 	TypeId         int    `xorm:"'type_id'" json:"type_id"`
 	GoodsName      string `xorm:"'goods_name'" json:"goods_name"`
 	IntroductionId int    `xorm:"'introduction_id'" json:"introduction_id"`
+	SalesVolume    int    `xorm:"'sales_volume'" json:"sales_volume"`
 	TagIds         string `xorm:"'tag_ids'" json:"tag_ids"`
 }

+ 6 - 2
data/domain/GoodsCommodityArea.go

@@ -1,6 +1,10 @@
 package domain
 
+// GoodsCommodityArea 商品属性
 type GoodsCommodityArea struct {
-	Id                int    `xorm:"'id'" json:"id"`
-	CommodityAreaName string `xorm:"'commodity_area_name'" json:"commodity_area_name"`
+	Id                   int    `xorm:"'id'" json:"id"`
+	GoodsId              int    `xorm:"'goods_id'" json:"goods_id"`
+	CommodityAreaName    string `xorm:"'commodity_area_name'" json:"commodity_area_name"`
+	DetailImage          string `xorm:"'detail_image'" json:"detail_image"`
+	DetailIntroductionId int    `xorm:"'detail_introduction_id'" json:"detail_introduction_id"`
 }

+ 1 - 0
data/domain/GoodsIntroduction.go

@@ -2,6 +2,7 @@ package domain
 
 import "time"
 
+// GoodsIntroduction 商品文章
 type GoodsIntroduction struct {
 	Id               int       `xorm:"'id'" json:"id"`
 	GoodsArticleName string    `xorm:"'goods_article_name'" json:"goods_article_name"`

+ 11 - 0
data/domain/GoodsSku.go

@@ -1,11 +1,22 @@
 package domain
 
+import "fmt"
+
+// GoodsSku 商品规格
 type GoodsSku struct {
 	Id               int     `xorm:"'id'" json:"id"`
+	SkuImage         string  `xorm:"'sku_image'" json:"sku_image"`
 	SkuName          string  `xorm:"'sku_name'" json:"sku_name"`
 	Price            float64 `xorm:"'price'" json:"price"`
 	HistoricalPrices float64 `xorm:"'historical_prices'" json:"historical_prices"`
 	InventoryNumber  int     `xorm:"'inventory_number'" json:"inventory_number"`
 	CommodityAreaId  int     `xorm:"'commodity_area_id'" json:"commodity_area_id"`
 	GoodsId          int     `xorm:"'goods_id'" json:"goods_id"`
+	CreateBy         string  `xorm:"'create_by'" json:"create_by"`
+	CreateTime       string  `xorm:"'create_time'" json:"create_time"`
+}
+
+func (receiver GoodsSku) String() string {
+	return fmt.Sprintf("GoodsSku{Id:%d, SkuImage:%s, SkuName:%s, Price:%f, HistoricalPrices:%f, InventoryNumber:%d, CommodityAreaId:%d, GoodsId:%d}",
+		receiver.Id, receiver.SkuImage, receiver.SkuName, receiver.Price, receiver.HistoricalPrices, receiver.InventoryNumber, receiver.CommodityAreaId, receiver.GoodsId)
 }

+ 1 - 0
data/domain/GoodsTag.go

@@ -1,5 +1,6 @@
 package domain
 
+// GoodsTag 商品标签
 type GoodsTag struct {
 	Id      int    `xorm:"'id'" json:"id"`
 	Name    string `xorm:"'name'" json:"name"`

+ 9 - 0
data/domain/GoodsTopic.go

@@ -0,0 +1,9 @@
+package domain
+
+type GoodsTopic struct {
+	Id             int    `xorm:"'id'" json:"id"`
+	TopicName      string `xorm:"'topic_name'" json:"topic_name"`
+	TopicPageImage string `xorm:"'topic_page_image'" json:"topic_page_image"`
+	TopicDesc      string `xorm:"'topic_desc'" json:"topic_desc"`
+	GoodsIds       string `xorm:"'goods_ids'" json:"goods_ids"`
+}

+ 1 - 0
data/domain/GoodsType.go

@@ -2,6 +2,7 @@ package domain
 
 import "time"
 
+// GoodsType 商品类型
 type GoodsType struct {
 	Id         int       `xorm:"'id'" json:"id"`
 	Sort       int       `xorm:"'sort'" json:"sort"`

+ 31 - 0
data/domain/vo/DetailVo.go

@@ -0,0 +1,31 @@
+package vo
+
+import (
+	"demo/data/domain"
+)
+
+type DetailVo struct {
+	DetailGoodsVo         DetailGoodsVo              `json:"goods"`
+	DetailSkuVo           DetailSkuVo                `json:"sku"`
+	DetailCommodityAreaVo DetailCommodityAreaVo      `json:"commodityArea"`
+	Introduction          []domain.GoodsIntroduction `json:"introduction"`     //商品描述
+	RecommendedGoods      []domain.GoodsSku          `json:"recommendedGoods"` //推荐商品
+}
+
+type DetailGoodsVo struct {
+	GoodsId     int               `xorm:"'id'" json:"id"`
+	Name        string            `xorm:"'name'" json:"name"`
+	SalesVolume int               `xorm:"'sales_volume'" json:"sales_volume"`
+	Type        domain.GoodsType  `xorm:"'type'" json:"type"`
+	Tags        []domain.GoodsTag `xorm:"'tags'" json:"tags"`
+}
+
+type DetailSkuVo struct {
+	NowSkuId int               `json:"sku"`     //当前sku
+	SkuList  []domain.GoodsSku `json:"skuList"` //相同区的
+}
+
+type DetailCommodityAreaVo struct {
+	NowCommodityAreaId int                         `json:"commodityArea"`     //当前地区
+	CommodityAreaList  []domain.GoodsCommodityArea `json:"commodityAreaList"` //地区列表
+}

+ 28 - 0
data/domain/vo/IndexData.go

@@ -0,0 +1,28 @@
+package vo
+
+import "demo/data/domain"
+
+type IndexDataVo struct {
+	//广告位标签栏
+	AdviceTopics []domain.GoodsTopic
+	//广告位左侧商品列表
+	AdviceGoodsList struct {
+		Name    string `json:"name"`
+		GoodsId int    `json:"goodsId"`
+	}
+	//广告为商品图片展示位
+	AdviceGoodsShowList struct {
+		Name    string `json:"name"`
+		GoodsId int    `json:"goodsId"`
+		Image   string `json:"image"`
+	}
+	//优惠包列表
+	GiftPackage struct {
+		Name          string  `json:"name"`
+		Text          string  `json:"text"`
+		Price         float32 `json:"price"`
+		OriginalPrice float32 `json:"originalPrice"`
+	}
+	// 主题商品页面展示,及部分商品显示
+	GoodsList []domain.Goods
+}

+ 6 - 0
go.sum

@@ -64,6 +64,10 @@ github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
 github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
 github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
 github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
+github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
+github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
+github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
+github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
 github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
 github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
 github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
@@ -227,6 +231,8 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+gorm.io/gorm v1.25.9 h1:wct0gxZIELDk8+ZqF/MVnHLkA1rvYlBWUMv2EdsK1g8=
+gorm.io/gorm v1.25.9/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
 lukechampine.com/uint128 v1.2.0 h1:mBi/5l91vocEN8otkC5bDLhi2KdCticRiwbdB0O+rjI=
 lukechampine.com/uint128 v1.2.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=
 modernc.org/cc/v3 v3.40.0 h1:P3g79IUS/93SYhtoeaHW+kRCIrYaxJ27MFPv+7kaTOw=

+ 3 - 0
main.go

@@ -2,6 +2,7 @@ package main
 
 import (
 	"demo/configs"
+	"demo/router"
 	"demo/share"
 	"demo/user"
 	"github.com/gin-gonic/gin"
@@ -14,6 +15,8 @@ func main() {
 	apiGroup := Router.Group("/api")
 
 	user.UserRouth(apiGroup)
+	router.DetailRouth(apiGroup)
+
 	TestRouth(apiGroup)
 	apiGroup.GET("/ping", share.JwtMiddleware(), func(context *gin.Context) {
 		context.String(200, "pong")

+ 28 - 0
router/DetailRouter.go

@@ -0,0 +1,28 @@
+package router
+
+import (
+	"demo/data/dao"
+	"github.com/gin-gonic/gin"
+	"strconv"
+)
+
+func DetailRouth(engine *gin.RouterGroup) {
+	user := engine.Group("/detail")
+	{
+		user.GET("/:id", GetDetailData)
+	}
+}
+
+func GetDetailData(c *gin.Context) {
+	id, err2 := strconv.Atoi(c.Param("id"))
+	if err2 != nil {
+		c.JSON(200, CreateResultError(400, "param 参数错误"))
+		return
+	}
+	vo, err := dao.SelectGoodsDetailById(id)
+	if err != nil {
+		c.JSON(200, CreateResultError(400, "查询失败"))
+		return
+	}
+	c.JSON(200, CreateResultData(vo))
+}

+ 24 - 0
router/ResultData.go

@@ -0,0 +1,24 @@
+package router
+
+import "github.com/gin-gonic/gin"
+
+func CreateResult() gin.H {
+	return gin.H{
+		"code": 200,
+		"msg":  "success",
+	}
+}
+func CreateResultData(Data any) gin.H {
+	return gin.H{
+		"code": 200,
+		"msg":  "success",
+		"data": Data,
+	}
+}
+
+func CreateResultError(errCode int, errMsg string) gin.H {
+	return gin.H{
+		"code": errCode,
+		"msg":  errMsg,
+	}
+}

+ 1 - 1
test/test.http

@@ -1,4 +1,4 @@
-GET http://localhost:80/api/item
+GET http://localhost:8080/api/detail/test
 Accept: application/json
 
 ###

+ 1 - 0
user/user.go

@@ -25,6 +25,7 @@ func UserRouth(engine *gin.RouterGroup) {
 		user.GET("/captcha", VerificationCode)
 	}
 }
+
 func VerificationCode(c *gin.Context) {
 	// 配置
 	driver := base64Captcha.NewDriverDigit(80, 240, 4, 0.5, 80)