diff --git a/business/api/auth.go b/business/api/auth.go index bf6b055..79cb05f 100644 --- a/business/api/auth.go +++ b/business/api/auth.go @@ -159,7 +159,7 @@ type GetUserInfoResponse struct { Uid int64 `json:"uid"` NickName string `json:"nickName"` HeadImgUrl string `json:"headImgUrl"` - Discount float32 `json:"discount"` + Discount float64 `json:"discount"` Amount int `json:"amount"` Role int `json:"role"` UserPets []UserPetInfo `json:"userPets"` @@ -182,7 +182,7 @@ func (p DefParty) getUserInfo() web_iris.Party { NickName: userInfo.NickName, HeadImgUrl: userInfo.HeadImgUrl, Amount: userInfo.Amount, - Discount: float32(userInfo.Discount) / 100, + Discount: float64(userInfo.Discount) / 100, Role: userInfo.Role, UserPets: GetUserPets(userInfo.Id), } diff --git a/business/api/common.go b/business/api/common.go index 7d510e6..e092270 100644 --- a/business/api/common.go +++ b/business/api/common.go @@ -54,6 +54,8 @@ var ( OrderError = Error{Code: 212, Msg: "当前订单无法修改"} CarNotExistError = Error{Code: 213, Msg: "当前用户未关联车辆"} OrderExistError = Error{Code: 214, Msg: "订单不存在"} + PetInfoNotDelError = Error{Code: 215, Msg: "当前宠物存在待服务订单,无法删除"} + PetInfoNotUpdateError = Error{Code: 216, Msg: "当前宠物存在待服务订单,无法修改"} ) func Success(ctx *context.Context, request any, data any) { diff --git a/business/api/control.go b/business/api/control.go index 17ea4af..de6a5ac 100644 --- a/business/api/control.go +++ b/business/api/control.go @@ -30,6 +30,7 @@ func (p DefParty) RegisterList() []web_iris.Party { p.orderDetail(), p.orderGoodsUpdate(), p.orderPay(), + p.orderCancel(), //宠物 p.petList(), p.petInfo(), diff --git a/business/api/goods.go b/business/api/goods.go index 1a8c811..5c48213 100644 --- a/business/api/goods.go +++ b/business/api/goods.go @@ -6,6 +6,8 @@ import ( "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" @@ -41,7 +43,9 @@ func (p DefParty) goodsList() web_iris.Party { var goodsListRequest GoodsListRequest json.Unmarshal(body, &goodsListRequest) userPetInfo := GetUserPet(headerBaseInfo.Uid, goodsListRequest.Pid) - if userPetInfo.PetInfo.Id == 0 { + 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 } diff --git a/business/api/order.go b/business/api/order.go index 714cf08..5ef0719 100644 --- a/business/api/order.go +++ b/business/api/order.go @@ -8,6 +8,7 @@ import ( "gorm.io/gorm" "gorm.io/gorm/clause" "io" + "math" "pet-house.com/business/models" "pet-house.com/business/utils" "pet-house.com/core/server/database" @@ -71,7 +72,7 @@ func (p DefParty) orderCreate() web_iris.Party { } haveReserve := checkOrderServiceTime(orderCreateRequest.ServiceTime, projectionServiceTime) if haveReserve { - OrderCreateError.DefFail(ctx, orderCreateRequest, "当前时间点不可预约") + OrderCreateError.DefFail(ctx, orderCreateRequest, "当前订单预估服务时长预计:"+strconv.Itoa(projectionServiceTime)+"分钟,服务时间过长,请更换时间段预约") return } for _, value := range orderCreateRequest.PetGoodsInfos { @@ -100,6 +101,7 @@ func (p DefParty) orderCreate() web_iris.Party { subOrderId := NextId.Generate().String() var totalAmount int32 = 0 var projectionServiceTime = 0 + var goodsName string for _, value := range value.GoodsIds { goods := GoodsMap[value] totalAmount = totalAmount + goods.Price @@ -111,18 +113,22 @@ func (p DefParty) orderCreate() web_iris.Party { if goods.GoodsType == 1 || goods.GoodsType == 2 { mainGoods++ } + goodsName = goods.Name + "、" + goodsName } + userPetInfo := GetUserPet(headerBaseInfo.Uid, value.PetId) + var petInfo = "宠物名称:" + userPetInfo.PetInfo.NickName + "
品种:" + userPetInfo.PetBaseInfo.Assortment + "
服务项目:" + goodsName projectionServiceTimeAll += projectionServiceTime orderSub := models.OrderSub{ - OrderId: subOrderId, - MainOrderId: orderMain.OrderId, - OrderStatus: 1, - PetId: value.PetId, - PayType: 1, - Discount: 100, - TotalAmount: totalAmount, - PayAmount: totalAmount, - PayTime: time.Now(), + OrderId: subOrderId, + MainOrderId: orderMain.OrderId, + OrderStatus: 1, + PetId: value.PetId, + PayType: 1, + Discount: 100, + TotalAmount: totalAmount, + PayAmount: totalAmount, + PetInfo: petInfo, + //PayTime: time.Now(), ProjectionServiceTime: projectionServiceTime, } orderTotalAmount = orderTotalAmount + int(totalAmount) @@ -136,11 +142,13 @@ func (p DefParty) orderCreate() web_iris.Party { var userInfo *models.User database.Instance().Model(&models.User{}).Where("id = ?", headerBaseInfo.Uid).Find(&userInfo) if userInfo.Discount > 0 { - orderTotalAmount = orderTotalAmount * (userInfo.Discount / 100) + discount := float64(userInfo.Discount) + orderTotalAmount = int(math.Round(float64(orderTotalAmount) * (discount / 100.0))) } tx := database.Instance().Begin() var db4 *gorm.DB if userInfo.Amount >= orderTotalAmount { + var currAmount = userInfo.Amount orderMain.PayStatus = 1 orderMain.PayTotalAmount = orderTotalAmount userInfo.Amount = userInfo.Amount - orderTotalAmount @@ -148,14 +156,16 @@ func (p DefParty) orderCreate() web_iris.Party { "Amount": userInfo.Amount, } db4 = tx.Model(&userInfo).Updates(&updateValues) + orderMain.PayDiscount = userInfo.Discount + zap_server.ZAPLOG.Info("会员金额扣除", zap.Any("用户ID", userInfo.Id), zap.Any("当前余额", currAmount), zap.Any("扣除余额", orderTotalAmount), zap.Any("剩余余额", userInfo.Amount), zap.Any("折扣", userInfo.Discount)) } db := tx.Model(&models.OrderDetail{}).CreateInBatches(&orderSubDetailList, len(orderSubDetailList)) db1 := tx.Model(&models.OrderSub{}).CreateInBatches(&orderSubList, len(orderSubList)) db2 := tx.Model(&models.OrderMain{}).Create(&orderMain) - if db4.Error != nil || db.Error != nil || db1.Error != nil || db2.Error != nil { + if (db4 != nil && db4.Error != nil) || db.Error != nil || db1.Error != nil || db2.Error != nil { tx.Callback() orderLock.Unlock() - zap_server.ZAPLOG.Error("订单插入失败", zap.Any("db", db.Error), zap.Any("db1", db1.Error), zap.Any("db2", db2.Error)) + zap_server.ZAPLOG.Error("订单插入失败", zap.Any("db", db.Error), zap.Any("db1", db1.Error), zap.Any("db2", db2.Error), zap.Any("db4", db4.Error)) OrderCreateError.Fail(ctx, orderCreateRequest) return } @@ -362,13 +372,14 @@ func (p DefParty) orderCancel() web_iris.Party { orderMain.PayTotalAmount = 0 orderMain.PayStatus = 0 } - orderMain.OrderStatus = 5 + orderMain.OrderStatus = 4 updateValues := map[string]interface{}{ "OrderStatus": orderMain.OrderStatus, "PayStatus": orderMain.PayStatus, "PayTotalAmount": orderMain.PayTotalAmount, } database.Instance().Model(&orderMain).Updates(&updateValues) + Success(ctx, orderCancelRequest, OrderCancelResponse{GetOrderDetail(orderMain.OrderId)}) }) }} } diff --git a/business/api/pet.go b/business/api/pet.go index 649bb2b..eea4e7b 100644 --- a/business/api/pet.go +++ b/business/api/pet.go @@ -34,7 +34,7 @@ type UserPetInfo struct { func GetUserPets(uId int64) []UserPetInfo { var userPetList []models.Pet - database.Instance().Model(&models.Pet{}).Where("uid = ?", uId).Find(&userPetList) + database.Instance().Model(&models.Pet{}).Where("uid = ? and status = 1", uId).Find(&userPetList) var userPets []UserPetInfo if len(userPetList) > 0 { for _, pet := range userPetList { @@ -46,7 +46,7 @@ func GetUserPets(uId int64) []UserPetInfo { func GetUserPet(uId int64, pId int64) UserPetInfo { var userPet models.Pet - database.Instance().Model(&models.Pet{}).Where("uid = ? and id = ?", uId, pId).Find(&userPet) + database.Instance().Model(&models.Pet{}).Where("uid = ? and id = ? and status = 1", uId, pId).Find(&userPet) return UserPetInfo{userPet, PetBaseInfoMap[userPet.PetId]} } @@ -139,10 +139,20 @@ func (p DefParty) petAddOrEdit() web_iris.Party { pet.Id = petAddOrEditRequest.Id var userPetInfo models.Pet database.Instance().Model(&models.Pet{}).Where("id = ? and uid = ?", pet.Id, headerBaseInfo.Uid).Find(&userPetInfo) - if userPetInfo.Id == 0 { + 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 + } + } updateValues := map[string]interface{}{ "NickName": pet.NickName, "HeadImgType": pet.HeadImgType, @@ -201,7 +211,16 @@ func (p DefParty) delPet() web_iris.Party { PetNotExistError.Fail(ctx, delPetRequest) return } - database.Instance().Where("id = ?", userPetInfo.PetId).Delete(&models.Pet{}) + 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": 0, + } + database.Instance().Model(&userPetInfo).Updates(&updateValues) Success(ctx, nil, nil) }) }} diff --git a/business/api/service.go b/business/api/service.go index 6ae5d13..7a90cfa 100644 --- a/business/api/service.go +++ b/business/api/service.go @@ -100,6 +100,7 @@ func (p DefParty) serviceAddOrEdit() web_iris.Party { } }*/ //userServiceAddr.AddrArea = recogResponse.County + if serviceAddOrEditRequest.Id == 0 { database.Instance().Model(&models.UserServiceAddr{}).Create(&userServiceAddr) } else { @@ -121,6 +122,12 @@ func (p DefParty) serviceAddOrEdit() web_iris.Party { } database.Instance().Model(&userServiceAddr).Updates(&updateValues) } + var userInfo *models.User + database.Instance().Model(&models.User{}).Where("id = ?", headerBaseInfo.Uid).Find(&userInfo) + updateValues := map[string]interface{}{ + "Mobile": userInfo.Mobile, + } + database.Instance().Model(&userInfo).Updates(&updateValues) var userServiceAddrList []*models.UserServiceAddr database.Instance().Model(&models.UserServiceAddr{}).Where("uid = ?", headerBaseInfo.Uid).Find(&userServiceAddrList) for _, value := range userServiceAddrList { diff --git a/business/models/dataModel.go b/business/models/dataModel.go index 57ad395..9fda698 100644 --- a/business/models/dataModel.go +++ b/business/models/dataModel.go @@ -39,6 +39,7 @@ type Pet struct { LastServiceProj string `gorm:"default:'-'" json:"lastServiceProj"` //上次服务项目 LastServiceDate string `gorm:"default:'-'" json:"lastServiceDate"` //上次服务时间 CreateTime time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP;not null" json:"createTime"` //创建时间 + Status int `gorm:"default:1" json:"status"` //信息状态 } // PetBaseInfo 宠物基础信息 @@ -53,12 +54,12 @@ type PetBaseInfo struct { // ServiceAddr 服务地址 type ServiceAddr struct { - Id int64 `gorm:"primaryKey;autoIncrement" json:"id"` //ID - Longitude string `json:"longitude"` //经度 - Latitude string `json:"latitude"` //纬度 - Addr string `gorm:"not null" json:"addr"` //详细地址 - DistantGapMeters float64 `gorm:"not null" json:"distantGapMeters"` //服务最远距离 单位/米 - ServiceArea string `json:"serviceArea"` //服务区域 + Id int64 `gorm:"primaryKey;autoIncrement" json:"id"` //ID + Longitude string `json:"longitude"` //经度 + Latitude string `json:"latitude"` //纬度 + Addr string `gorm:"not null" json:"addr"` //详细地址 + DistantGapMeters int64 `gorm:"not null" json:"distantGapMeters"` //服务最远距离 单位/米 + ServiceArea string `json:"serviceArea"` //服务区域 } // UserServiceAddr 用户服务地址 @@ -130,6 +131,7 @@ type OrderMain struct { PayStatus int `gorm:"default:0" json:"payStatus"` //支付状态 0未支付 1已支付 DispatchStatus int `gorm:"default:0" json:"dispatchStatus"` //派单状态 0未派单 1已派单 PayTotalAmount int `json:"payTotalAmount"` //支付总金额 + PayDiscount int `gorm:"default:0" json:"payDiscount"` //支付折扣 } // OrderSub 子订单 @@ -147,6 +149,7 @@ type OrderSub struct { PayAmount int32 `json:"payAmount"` //实际支付金额 PayTime time.Time `gorm:"type:timestamp;" json:"payTime"` //支付时间 PayRemark string `json:"payRemark"` //支付备注 + PetInfo string `json:"petInfo"` //宠物快照信息 CreateTime time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP" json:"createTime"` //创建时间 UpdateTime time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP" json:"-"` //更新时间 } diff --git a/business/utils/commonUtil.go b/business/utils/commonUtil.go index 01f6af8..dce9470 100644 --- a/business/utils/commonUtil.go +++ b/business/utils/commonUtil.go @@ -22,7 +22,7 @@ type TimeObject struct { func GetStrDays(day int, minute int, reserveMap map[string]models.ReserveTimeFilter, orderTimeMap map[string]string, carNum int, orderNumMap map[string]int, carServiceNum int, times []string) map[string][]TimeObject { // 获取当前时间 - currentTime := time.Now().AddDate(0, 0, 1) + currentTime := time.Now() // 计算半小时后的时间 halfHourLater := currentTime.Add(time.Duration(minute) * time.Minute) @@ -56,7 +56,8 @@ func GetStrDays(day int, minute int, reserveMap map[string]models.ReserveTimeFil orderNum, existOrderNum := orderNumMap[nextHour.Format("2006-01-02 15:04")] if (nextHour.Hour() >= workStart && nextHour.Hour() < workEnd) && (!existsDay && !existsHour) && !existOrderTime { key := nextHour.Format("2006-01-02") - if _, ok := serviceDaysMap[key]; len(serviceDaysMap) > 0 && !ok { + key1 := nextHour.Format("2006-01-02 15:04") + if _, ok := serviceDaysMap[key1]; len(serviceDaysMap) > 0 && !ok { //不存在服务配置中返回false dayHoursMap[key] = append(dayHoursMap[key], TimeObject{ Time: nextHour.Format("15:04"), diff --git a/business/utils/commonUtil_test.go b/business/utils/commonUtil_test.go index 28cedf7..7376d52 100644 --- a/business/utils/commonUtil_test.go +++ b/business/utils/commonUtil_test.go @@ -2,6 +2,7 @@ package utils import ( "fmt" + "math" "pet-house.com/business/models" "testing" ) @@ -17,6 +18,15 @@ func TestGetStrDays(t *testing.T) { Type: 2, Content: "12", } - days := GetStrDays(7, 30, ReserveMap, nil, 1, nil, 2) - fmt.Println(days) + //days := GetStrDays(7, 30, ReserveMap, nil, 1, nil, 2) + //fmt.Println(days) +} + +func TestCalc(t *testing.T) { + i := math.Round(386 * (float64(85) / 100)) + fmt.Println(i) +} +func TestCalc1(t *testing.T) { + i := float64(85) / 100 + fmt.Println(i) }