hepa-calc-api/api/controller/SystemMember.go

86 lines
1.9 KiB
Go

package controller
import (
"github.com/gin-gonic/gin"
"hepa-calc-api/api/dao"
"hepa-calc-api/api/dto"
"hepa-calc-api/api/responses"
"hepa-calc-api/api/service"
"math"
)
type SystemMember struct{}
// GetSystemMember 获取会员配置数据
func (b *SystemMember) GetSystemMember(c *gin.Context) {
userId := c.GetInt64("UserId")
systemMemberDao := dao.SystemMemberDao{}
maps := make(map[string]interface{})
systemMembers, err := systemMemberDao.GetSystemMemberList(maps)
if err != nil {
responses.FailWithMessage(err.Error(), c)
return
}
// 检测用户是否购买过会员
userService := service.UserService{}
isBuy := userService.CheckUserBuyMember(userId)
if isBuy == true {
for _, member := range systemMembers {
member.FirstTimePrice = nil
}
}
// 处理返回值
g := dto.GetSystemMemberListDto(systemMembers)
responses.OkWithData(g, c)
}
// GetSystemMemberLeast 获取会员配置数据-最少立减金额
func (b *SystemMember) GetSystemMemberLeast(c *gin.Context) {
var reductionAmount *float64
// 检测用户是否购买过会员
userId := c.GetInt64("UserId")
if userId != 0 {
userService := service.UserService{}
isBuy := userService.CheckUserBuyMember(userId)
if isBuy == true {
responses.OkWithData(reductionAmount, c)
return
}
}
systemMemberDao := dao.SystemMemberDao{}
maps := make(map[string]interface{})
systemMembers, err := systemMemberDao.GetSystemMemberList(maps)
if err != nil {
responses.FailWithMessage(err.Error(), c)
return
}
for _, member := range systemMembers {
if member.FirstTimePrice == nil {
continue
}
amount := member.Price - *member.FirstTimePrice
amount = math.Round(amount*100) / 100
if reductionAmount == nil {
reductionAmount = &amount
continue
}
if amount < *reductionAmount {
reductionAmount = &amount
}
}
responses.OkWithData(reductionAmount, c)
}