317 lines
12 KiB
Go
317 lines
12 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/business/utils"
|
|
"pet-house.com/core/server/database"
|
|
"pet-house.com/core/server/web/web_iris"
|
|
"pet-house.com/core/server/zap_server"
|
|
"strconv"
|
|
)
|
|
|
|
var CatWeightMap = map[int]string{
|
|
//1: "0-1.5kg",
|
|
2: "0-3kg",
|
|
3: "3-5kg",
|
|
4: "5-8kg",
|
|
6: "8kg以上",
|
|
}
|
|
var DogWeightMap = map[int]string{
|
|
1: "0-5kg",
|
|
2: "3-6kg",
|
|
3: "5-9kg",
|
|
4: "9-13kg",
|
|
5: "13-18kg",
|
|
6: "18-22kg",
|
|
7: "22-30kg",
|
|
8: "0-3kg",
|
|
}
|
|
|
|
type UserPetInfo struct {
|
|
PetInfo models.Pet `json:"petInfo"`
|
|
PetBaseInfo models.PetBaseInfo `json:"petBaseInfo"`
|
|
}
|
|
|
|
func GetUserPets(uId int64) []UserPetInfo {
|
|
var userPetList []models.Pet
|
|
database.Instance().Model(&models.Pet{}).Where("uid = ? and status = 1", uId).Find(&userPetList)
|
|
var userPets []UserPetInfo
|
|
if len(userPetList) > 0 {
|
|
for _, pet := range userPetList {
|
|
userPets = append(userPets, UserPetInfo{pet, PetBaseInfoMap[pet.PetId]})
|
|
}
|
|
}
|
|
return userPets
|
|
}
|
|
|
|
func GetUserPet(uId int64, pId int64) UserPetInfo {
|
|
var userPet models.Pet
|
|
database.Instance().Model(&models.Pet{}).Where("uid = ? and id = ?", uId, pId).Find(&userPet)
|
|
return UserPetInfo{userPet, PetBaseInfoMap[userPet.PetId]}
|
|
}
|
|
|
|
func GetPet(pId int64) UserPetInfo {
|
|
var userPet models.Pet
|
|
database.Instance().Model(&models.Pet{}).Where("id = ?", pId).Find(&userPet)
|
|
return UserPetInfo{userPet, PetBaseInfoMap[userPet.PetId]}
|
|
}
|
|
|
|
type PetListResponse struct {
|
|
UserPets []UserPetInfo `json:"userPets"`
|
|
}
|
|
|
|
// 宠物列表
|
|
func (p DefParty) petList() web_iris.Party {
|
|
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
|
|
index.Post(PetBase+"/petList", func(ctx *context.Context) {
|
|
headerBaseInfo := GetHeaderBaseInfo(ctx)
|
|
userPets := GetUserPets(headerBaseInfo.Uid)
|
|
Success(ctx, headerBaseInfo, PetListResponse{
|
|
userPets,
|
|
})
|
|
})
|
|
}}
|
|
}
|
|
|
|
type PetInfoRequest struct {
|
|
Id int64 `json:"id"`
|
|
}
|
|
|
|
type PetInfoResponse struct {
|
|
UserPet UserPetInfo `json:"userPet"`
|
|
}
|
|
|
|
func (p DefParty) petInfo() web_iris.Party {
|
|
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
|
|
index.Post(PetBase+"/petInfo", func(ctx *context.Context) {
|
|
headerBaseInfo := GetHeaderBaseInfo(ctx)
|
|
body, _ := io.ReadAll(ctx.Request().Body)
|
|
var petInfoRequest PetInfoRequest
|
|
json.Unmarshal(body, &petInfoRequest)
|
|
userPet := GetUserPet(headerBaseInfo.Uid, petInfoRequest.Id)
|
|
Success(ctx, petInfoRequest, PetInfoResponse{userPet})
|
|
})
|
|
}}
|
|
}
|
|
|
|
type PetAddOrEditRequest struct {
|
|
Id int64 //Id 不为0表示修改
|
|
NickName string //宠物昵称
|
|
HeadImgType int //头像类型 0远程头像 1本地头像
|
|
HeadImgUrl string //宠物头像
|
|
Desc string //宠物描述
|
|
Weight int //宠物体重
|
|
Precaution string //注意事项
|
|
Gender int //性别 0男 1女
|
|
Birthday string //生日
|
|
PetId int //宠物类型
|
|
Eunuch int //是否绝育 0否 1是 2未知
|
|
Vaccine int //是否接种疫苗 0否 1是
|
|
}
|
|
|
|
type PetAddOrEditResponse struct {
|
|
UserPets []UserPetInfo `json:"userPets"`
|
|
}
|
|
|
|
// 宠物添加或编辑
|
|
func (p DefParty) petAddOrEdit() web_iris.Party {
|
|
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
|
|
index.Post(PetBase+"/petAddOrEdit", func(ctx *context.Context) {
|
|
headerBaseInfo := GetHeaderBaseInfo(ctx)
|
|
body, _ := io.ReadAll(ctx.Request().Body)
|
|
var petAddOrEditRequest PetAddOrEditRequest
|
|
json.Unmarshal(body, &petAddOrEditRequest)
|
|
if PetBaseInfoMap[petAddOrEditRequest.PetId].Id == 0 {
|
|
PetBaseNotExistError.Fail(ctx, petAddOrEditRequest)
|
|
return
|
|
}
|
|
pet := models.Pet{
|
|
Uid: headerBaseInfo.Uid,
|
|
NickName: petAddOrEditRequest.NickName,
|
|
HeadImgType: petAddOrEditRequest.HeadImgType,
|
|
HeadImgUrl: petAddOrEditRequest.HeadImgUrl,
|
|
Desc: petAddOrEditRequest.Desc,
|
|
Weight: petAddOrEditRequest.Weight,
|
|
Precaution: petAddOrEditRequest.Precaution,
|
|
Gender: petAddOrEditRequest.Gender,
|
|
Birthday: petAddOrEditRequest.Birthday,
|
|
PetId: petAddOrEditRequest.PetId,
|
|
Eunuch: petAddOrEditRequest.Eunuch,
|
|
Vaccine: petAddOrEditRequest.Vaccine,
|
|
Status: 1,
|
|
}
|
|
if petAddOrEditRequest.Id == 0 {
|
|
database.Instance().Model(&models.Pet{}).Create(&pet)
|
|
} else {
|
|
pet.Id = petAddOrEditRequest.Id
|
|
var userPetInfo models.Pet
|
|
database.Instance().Model(&models.Pet{}).Where("id = ?", pet.Id).Find(&userPetInfo)
|
|
var userInfo *models.User
|
|
database.Instance().Model(&models.User{}).Where("id = ?", headerBaseInfo.Uid).Find(&userInfo)
|
|
if userPetInfo.Id == 0 && userInfo.Role == 0 {
|
|
PetNotExistError.Fail(ctx, petAddOrEditRequest)
|
|
return
|
|
}
|
|
if userInfo.Role == 0 {
|
|
var count int64
|
|
database.Instance().Model(&models.OrderSub{}).Where("pet_id = ? and (order_status = 1 or order_status = 2)", userPetInfo.Id).Count(&count)
|
|
if count > 0 {
|
|
PetInfoNotUpdateError.Fail(ctx, petAddOrEditRequest)
|
|
return
|
|
}
|
|
} else {
|
|
if userPetInfo.Weight != pet.Weight {
|
|
//护理人员修改体重,需要更新对应的服务项目
|
|
var orderMainList []models.OrderMain
|
|
database.Instance().Model(&models.OrderMain{}).Where("uid = ? and order_status in (1,2)", userPetInfo.Uid).Find(&orderMainList)
|
|
for _, orderMain := range orderMainList {
|
|
if orderMain.PayStatus == 1 {
|
|
continue
|
|
}
|
|
var orderSubList []models.OrderSub
|
|
database.Instance().Model(&models.OrderSub{}).Where("main_order_id = ? ", orderMain.OrderId).Find(&orderSubList)
|
|
for orderSubIndex, orderSub := range orderSubList {
|
|
var orderDetailList []models.OrderDetail
|
|
database.Instance().Model(&models.OrderDetail{}).Where("sub_order_id = ? ", orderSub.OrderId).Find(&orderDetailList)
|
|
for orderDetailIndex, orderDetail := range orderDetailList {
|
|
var newGoodsDetail GoodsDetail
|
|
orderGoods := GoodsMap[orderDetail.GoodsId]
|
|
var goodsList []GoodsDetail
|
|
petBaseInfo := PetBaseInfoMap[userPetInfo.PetId]
|
|
key1 := strconv.Itoa(petBaseInfo.Id) + strconv.Itoa(petBaseInfo.PetType) + strconv.Itoa(pet.Weight) + "0"
|
|
goodsList = append(goodsList, GetPetGoodsList(key1)...)
|
|
key2 := strconv.Itoa(petBaseInfo.Id) + strconv.Itoa(petBaseInfo.PetType) + strconv.Itoa(pet.Weight) + strconv.Itoa(petBaseInfo.Hair)
|
|
goodsList = append(goodsList, GetPetGoodsList(key2)...)
|
|
key3 := "0" + strconv.Itoa(petBaseInfo.PetType) + strconv.Itoa(pet.Weight) + strconv.Itoa(petBaseInfo.Hair)
|
|
goodsList = append(goodsList, GetPetGoodsList(key3)...)
|
|
key4 := "0" + strconv.Itoa(petBaseInfo.PetType) + strconv.Itoa(pet.Weight) + "0"
|
|
goodsList = append(goodsList, GetPetGoodsList(key4)...)
|
|
for _, goodsDetail := range goodsList {
|
|
//取同名称 同类型 同子类型的商品
|
|
if goodsDetail.Name == orderGoods.Name && goodsDetail.GoodsType == orderGoods.GoodsType && goodsDetail.GoodsSubType == orderGoods.GoodsSubType {
|
|
zap_server.ZAPLOG.Info("更新体重命中的商品", zap.Any("原价", orderGoods.Price), zap.Any("Name", goodsDetail.Name), zap.Any("Price", goodsDetail.Price), zap.Any("GoodsType", goodsDetail.GoodsType), zap.Any("GoodsSubType", goodsDetail.GoodsSubType))
|
|
newGoodsDetail = goodsDetail
|
|
break
|
|
}
|
|
}
|
|
if newGoodsDetail.Id > 0 {
|
|
zap_server.ZAPLOG.Info("订单更新前", zap.Any("orderDetailId", orderDetail.SubOrderId), zap.Any("GoodsId", orderDetail.GoodsId), zap.Any("Amount", orderDetail.Amount), zap.Any("DiscountAmount", orderDetail.DiscountAmount))
|
|
//更新订单商品
|
|
orderDetail.GoodsId = newGoodsDetail.Id
|
|
orderDetail.Amount = newGoodsDetail.Price
|
|
orderDetail.DiscountAmount = int(utils.RoundToOneDecimalPlace(float64(orderDetail.Amount)*(float64(orderDetail.Discount)/100.0)) * 10)
|
|
zap_server.ZAPLOG.Info("商品订单更新后", zap.Any("orderDetailId", orderDetail.SubOrderId), zap.Any("GoodsId", orderDetail.GoodsId), zap.Any("Amount", orderDetail.Amount), zap.Any("DiscountAmount", orderDetail.DiscountAmount))
|
|
database.Instance().Model(&orderDetail).Updates(&orderDetail)
|
|
orderDetailList[orderDetailIndex] = orderDetail
|
|
}
|
|
}
|
|
var totalAmount = 0
|
|
var goodsOriginAmount = 0
|
|
var goodsDiscountAmount = 0
|
|
for _, orderDetail := range orderDetailList {
|
|
if orderDetail.DiscountAmount > 0 {
|
|
goodsDiscountAmount = goodsDiscountAmount + orderDetail.DiscountAmount
|
|
} else {
|
|
goodsOriginAmount = goodsOriginAmount + orderDetail.Amount
|
|
}
|
|
totalAmount = totalAmount + orderDetail.Amount
|
|
}
|
|
zap_server.ZAPLOG.Info("子订单更新前", zap.Any("orderSubId", orderSub.OrderId), zap.Any("goodsOriginAmount", orderSub.GoodsOriginAmount), zap.Any("goodsDiscountAmount", orderSub.GoodsDiscountAmount), zap.Any("totalAmount", orderSub.TotalAmount), zap.Any("PayAmount", orderSub.PayAmount))
|
|
orderSub.GoodsOriginAmount = goodsOriginAmount
|
|
orderSub.GoodsDiscountAmount = goodsDiscountAmount
|
|
orderSub.TotalAmount = totalAmount
|
|
orderSub.PayAmount = goodsDiscountAmount + (goodsOriginAmount * 10)
|
|
zap_server.ZAPLOG.Info("子订单更新后", zap.Any("orderSubId", orderSub.OrderId), zap.Any("goodsOriginAmount", orderSub.GoodsOriginAmount), zap.Any("goodsDiscountAmount", orderSub.GoodsDiscountAmount), zap.Any("totalAmount", orderSub.TotalAmount), zap.Any("PayAmount", orderSub.PayAmount))
|
|
database.Instance().Model(&orderSub).Updates(&orderSub)
|
|
orderSubList[orderSubIndex] = orderSub
|
|
}
|
|
var totalAmount = 0
|
|
for _, orderSub := range orderSubList {
|
|
totalAmount = totalAmount + orderSub.TotalAmount
|
|
}
|
|
zap_server.ZAPLOG.Info("主订单更新前", zap.Any("orderMainId", orderMain.OrderId), zap.Any("totalAmount", orderMain.TotalAmount))
|
|
orderMain.TotalAmount = totalAmount
|
|
zap_server.ZAPLOG.Info("主订单更新后", zap.Any("orderMainId", orderMain.OrderId), zap.Any("totalAmount", orderMain.TotalAmount))
|
|
database.Instance().Model(&orderMain).Updates(&orderMain)
|
|
}
|
|
}
|
|
|
|
}
|
|
updateValues := map[string]interface{}{
|
|
"NickName": pet.NickName,
|
|
"HeadImgType": pet.HeadImgType,
|
|
"HeadImgUrl": pet.HeadImgUrl,
|
|
"Desc": pet.Desc,
|
|
"Weight": pet.Weight,
|
|
"Precaution": pet.Precaution,
|
|
"Gender": pet.Gender,
|
|
"Birthday": pet.Birthday,
|
|
"PetId": pet.PetId,
|
|
"Eunuch": pet.Eunuch,
|
|
"Vaccine": pet.Vaccine,
|
|
}
|
|
database.Instance().Model(&pet).Updates(&updateValues)
|
|
}
|
|
userPets := GetUserPets(headerBaseInfo.Uid)
|
|
Success(ctx, petAddOrEditRequest, PetAddOrEditResponse{userPets})
|
|
})
|
|
}}
|
|
}
|
|
|
|
type PetTypeListResponse struct {
|
|
PetBaseInfoList []models.PetBaseInfo `json:"petBaseInfoList"`
|
|
CatWeights map[int]string `json:"catWeights"`
|
|
DogWeights map[int]string `json:"dogWeights"`
|
|
}
|
|
|
|
// 宠物基础信息列表
|
|
func (p DefParty) petTypeList() web_iris.Party {
|
|
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
|
|
index.Post(PetBase+"/petTypeList", func(ctx *context.Context) {
|
|
var petBaseInfoList []models.PetBaseInfo
|
|
for _, petBaseInfo := range PetBaseInfoMap {
|
|
petBaseInfoList = append(petBaseInfoList, petBaseInfo)
|
|
}
|
|
|
|
Success(ctx, nil, PetTypeListResponse{petBaseInfoList, CatWeightMap, DogWeightMap})
|
|
})
|
|
}}
|
|
}
|
|
|
|
type DelPetRequest struct {
|
|
Id int64
|
|
}
|
|
|
|
func (p DefParty) delPet() web_iris.Party {
|
|
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
|
|
index.Post(PetBase+"/delPet", func(ctx *context.Context) {
|
|
headerBaseInfo := GetHeaderBaseInfo(ctx)
|
|
body, _ := io.ReadAll(ctx.Request().Body)
|
|
var delPetRequest DelPetRequest
|
|
json.Unmarshal(body, &delPetRequest)
|
|
var userPetInfo models.Pet
|
|
database.Instance().Model(&models.Pet{}).Where("id = ? and uid = ?", delPetRequest.Id, headerBaseInfo.Uid).Find(&userPetInfo)
|
|
if userPetInfo.Id == 0 {
|
|
PetNotExistError.Fail(ctx, delPetRequest)
|
|
return
|
|
}
|
|
var count int64
|
|
database.Instance().Model(&models.OrderSub{}).Where("pet_id = ? and (order_status = 1 or order_status = 2)", userPetInfo.Id).Count(&count)
|
|
if count > 0 {
|
|
PetInfoNotDelError.Fail(ctx, delPetRequest)
|
|
return
|
|
}
|
|
updateValues := map[string]interface{}{
|
|
"Status": 3,
|
|
}
|
|
database.Instance().Model(&userPetInfo).Updates(&updateValues)
|
|
Success(ctx, delPetRequest, nil)
|
|
})
|
|
}}
|
|
}
|