pet-house/business/api/goods.go

162 lines
5.5 KiB
Go

package api
import (
"encoding/json"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"go.uber.org/zap"
"io"
"pet-house.com/business/models"
"pet-house.com/core/server/database"
"pet-house.com/core/server/web/web_iris"
"pet-house.com/core/server/zap_server"
"strconv"
)
type GoodsListRequest struct {
Pid int64
}
type GoodsDetail struct {
Id int64 `json:"id"` //商品ID
Name string `json:"name"` //商品名称
GoodsType int `json:"goodsType"` //商品类型 1基础服务 2附加产品 3附加服务
Time string `json:"time"` //时间
Price int `json:"price"` //价格
ParentId int64 `json:"parentId"` //商品父ID
BuyMany int `json:"buyMany"` //同类型是否可购买多个 0否 1是
GoodsDetail string `json:"goodsDetail"` //商品详情 图片URL
GoodsIcon string `json:"goodsIcon"` //iconUrl
GoodsSubType int `json:"goodsSubType"` //商品子类型
GoodsSubList []GoodsDetail `json:"goodsSubList,omitempty"`
}
type GoodsListResponse struct {
GoodsList []GoodsDetail `json:"goodsList"`
}
// 商品列表
func (p DefParty) goodsList() web_iris.Party {
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
index.Post(GoodsBase+"/goodsList", func(ctx *context.Context) {
headerBaseInfo := GetHeaderBaseInfo(ctx)
body, _ := io.ReadAll(ctx.Request().Body)
var goodsListRequest GoodsListRequest
json.Unmarshal(body, &goodsListRequest)
userPetInfo := GetUserPet(headerBaseInfo.Uid, goodsListRequest.Pid)
var userInfo *models.User
database.Instance().Model(&models.User{}).Where("id = ?", headerBaseInfo.Uid).Find(&userInfo)
if userPetInfo.PetInfo.Id == 0 && userInfo.Role == 0 {
PetNotExistError.Fail(ctx, goodsListRequest)
return
}
if userInfo.Role == 1 {
userPetInfo = GetPet(goodsListRequest.Pid)
}
//先取默认的商品
key := "0" + strconv.Itoa(userPetInfo.PetBaseInfo.PetType) + "0" + "0"
goodsList := GetPetGoodsList(key)
key1 := strconv.Itoa(userPetInfo.PetBaseInfo.Id) + strconv.Itoa(userPetInfo.PetBaseInfo.PetType) + strconv.Itoa(userPetInfo.PetInfo.Weight) + "0"
goodsList = append(goodsList, GetPetGoodsList(key1)...)
key2 := strconv.Itoa(userPetInfo.PetBaseInfo.Id) + strconv.Itoa(userPetInfo.PetBaseInfo.PetType) + strconv.Itoa(userPetInfo.PetInfo.Weight) + strconv.Itoa(userPetInfo.PetBaseInfo.Hair)
goodsList = append(goodsList, GetPetGoodsList(key2)...)
key3 := "0" + strconv.Itoa(userPetInfo.PetBaseInfo.PetType) + strconv.Itoa(userPetInfo.PetInfo.Weight) + strconv.Itoa(userPetInfo.PetBaseInfo.Hair)
goodsList = append(goodsList, GetPetGoodsList(key3)...)
key4 := "0" + strconv.Itoa(userPetInfo.PetBaseInfo.PetType) + strconv.Itoa(userPetInfo.PetInfo.Weight) + "0"
goodsList = append(goodsList, GetPetGoodsList(key4)...)
var goodsT1 []GoodsDetail
var goodsT2 []GoodsDetail
var goodsT3 []GoodsDetail
var goodsT4 []GoodsDetail
for _, value := range goodsList {
if value.GoodsType == 4 {
goodsT4 = append(goodsT4, value)
}
if value.GoodsType == 3 {
goodsT3 = append(goodsT3, value)
}
if value.GoodsType == 2 {
goodsT2 = append(goodsT2, value)
}
if value.GoodsType == 1 {
goodsT1 = append(goodsT1, value)
}
}
goodsT1Main := GoodsDetail{
Name: "洗护",
GoodsSubList: goodsT1,
GoodsType: 1,
}
goodsT2Main := GoodsDetail{
Name: "美容",
GoodsSubList: goodsT2,
GoodsType: 2,
}
goodsT3Main := GoodsDetail{
Name: "附加服务",
GoodsSubList: goodsT3,
GoodsType: 3,
}
goodsT4Main := GoodsDetail{
Name: "附加产品",
GoodsSubList: goodsT4,
GoodsType: 4,
}
var goodsTs []GoodsDetail
goodsTs = append(goodsTs, goodsT1Main, goodsT2Main, goodsT3Main, goodsT4Main)
zap_server.ZAPLOG.Info("key", zap.Any("key1", key), zap.Any("key2", key1), zap.Any("key3", key2), zap.Any("key4", key3), zap.Any("key5", key4))
Success(ctx, goodsListRequest, GoodsListResponse{goodsTs})
})
}}
}
func GetPetGoodsList(key string) []GoodsDetail {
var goodsList []GoodsDetail
baseGoodsList := PetGoodsMap[key]
for _, value := range baseGoodsList {
goods := GoodsMap[value.GoodsId]
if goods.Id == 0 {
continue
}
goodsDetail := GoodsDetail{
Id: goods.Id,
Name: goods.Name,
GoodsType: goods.GoodsType,
Time: goods.Time,
Price: goods.Price,
BuyMany: goods.BuyMany,
GoodsDetail: goods.GoodsDetail,
GoodsIcon: goods.GoodsIcon,
GoodsSubType: goods.GoodsSubType,
GoodsSubList: nil,
}
for _, goodsSub := range GoodsMap {
if goodsSub.ParentId == goodsDetail.Id {
goodsDetail.GoodsSubList = append(goodsDetail.GoodsSubList, GoodsDetail{
Id: goodsSub.Id,
Name: goodsSub.Name,
GoodsType: goodsSub.GoodsType,
Time: goodsSub.Time,
Price: goodsSub.Price,
BuyMany: goodsSub.BuyMany,
GoodsDetail: goodsSub.GoodsDetail,
GoodsIcon: goodsSub.GoodsIcon,
GoodsSubType: goodsSub.GoodsSubType,
})
}
}
goodsList = append(goodsList, goodsDetail)
}
return goodsList
}
// 商品详情
func (p DefParty) goodsDetail() web_iris.Party {
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
index.Post(GoodsBase+"/goodsDetail", func(ctx *context.Context) {
//暂留
Success(ctx, nil, nil)
})
}}
}