249 lines
6.1 KiB
Go
249 lines
6.1 KiB
Go
package service
|
||
|
||
import (
|
||
"errors"
|
||
"gorm.io/gorm"
|
||
"hepa-calc-api/api/dao"
|
||
"hepa-calc-api/api/model"
|
||
"time"
|
||
)
|
||
|
||
type QuestionService struct {
|
||
}
|
||
|
||
// GetHotList 获取算一算热榜-人气数最高的9个
|
||
func (r *QuestionService) GetHotList() (m []*model.Question, err error) {
|
||
questionDao := dao.QuestionDao{}
|
||
|
||
maps := make(map[string]interface{})
|
||
maps["question_status"] = 1
|
||
maps["is_hide"] = 0
|
||
questions, err := questionDao.GetQuestionOrderLimitList(maps, "click_count desc", 9)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return questions, nil
|
||
}
|
||
|
||
// GetRecommendList 获取为你推荐-后台指定的推广
|
||
func (r *QuestionService) GetRecommendList() (m []*model.Question, err error) {
|
||
questionDao := dao.QuestionDao{}
|
||
|
||
maps := make(map[string]interface{})
|
||
maps["question_status"] = 1
|
||
maps["is_hide"] = 0
|
||
maps["is_recommend"] = 1
|
||
questions, err := questionDao.GetQuestionList(maps)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return questions, nil
|
||
}
|
||
|
||
// GetGuessUserLIkeList 获取猜你喜欢-暂用公众参与过的最新算一算,至多显示3个。若未参与,则指定或者随机显示3个
|
||
func (r *QuestionService) GetGuessUserLIkeList(userId int64) (m []*model.Question, err error) {
|
||
orderSingleDao := dao.OrderSingleDao{}
|
||
questionDao := dao.QuestionDao{}
|
||
|
||
var questions []*model.Question
|
||
|
||
if userId != 0 {
|
||
maps := make(map[string]interface{})
|
||
maps["user_id"] = userId
|
||
orderSingles, err := orderSingleDao.GetOrderSingleOrderDistinctList(maps, "created_at desc", 3)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// 参与过
|
||
if len(orderSingles) > 0 {
|
||
for _, single := range orderSingles {
|
||
questions = append(questions, single.Question)
|
||
}
|
||
}
|
||
}
|
||
|
||
// 未参与过/未指定用户
|
||
if len(questions) < 3 {
|
||
limit := 3 - len(questions)
|
||
|
||
maps := make(map[string]interface{})
|
||
maps["question_status"] = 1
|
||
maps["is_hide"] = 0
|
||
results, err := questionDao.GetQuestionListRand(maps, limit)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
for _, result := range results {
|
||
questions = append(questions, result)
|
||
}
|
||
}
|
||
|
||
return questions, nil
|
||
}
|
||
|
||
// CheckUserCollectionQuestion 检测问题是否被用户收藏
|
||
func (r *QuestionService) CheckUserCollectionQuestion(userId, questionId int64) (bool, error) {
|
||
userCollectionDao := dao.UserCollectionDao{}
|
||
|
||
maps := make(map[string]interface{})
|
||
maps["user_id"] = userId
|
||
maps["question_id"] = questionId
|
||
userCollection, err := userCollectionDao.GetUserCollection(maps)
|
||
if userCollection == nil {
|
||
return false, err
|
||
}
|
||
|
||
return true, nil
|
||
}
|
||
|
||
// CheckUserBuyQuestion 检测用户是否购买过该问题
|
||
func (r *QuestionService) CheckUserBuyQuestion(userId, questionId int64) bool {
|
||
orderSingleDao := dao.OrderSingleDao{}
|
||
|
||
maps := make(map[string]interface{})
|
||
maps["user_id"] = userId
|
||
maps["question_id"] = questionId
|
||
maps["order_status"] = 3
|
||
orderSingle, _ := orderSingleDao.GetOrderSingle(maps)
|
||
if orderSingle == nil {
|
||
return false
|
||
}
|
||
|
||
return true
|
||
}
|
||
|
||
// GetUserFirstTimeBuyPrice 获取用户首单价格
|
||
func (r *QuestionService) GetUserFirstTimeBuyPrice(userId int64) (f *float64, err error) {
|
||
// 检测用户是否购买过单项产品
|
||
userService := &UserService{}
|
||
isBuy := userService.CheckUserBuySingle(userId)
|
||
if isBuy == false {
|
||
// 未购买过
|
||
systemSingleDao := dao.SystemSingleDao{}
|
||
|
||
maps := make(map[string]interface{})
|
||
systemSingle, err := systemSingleDao.GetSystemSingle(maps)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return systemSingle.FirstTimePrice, nil
|
||
}
|
||
|
||
return nil, nil
|
||
}
|
||
|
||
// GetQuestionBuyCount 获取问题被购买数量
|
||
func (r *QuestionService) GetQuestionBuyCount(userId, questionId int64) (c int, err error) {
|
||
// 未购买过
|
||
orderSingleDao := dao.OrderSingleDao{}
|
||
|
||
maps := make(map[string]interface{})
|
||
maps["user_id"] = userId
|
||
maps["question_id"] = questionId
|
||
maps["order_status"] = 2
|
||
maps["refund_status"] = 0
|
||
buyCount, err := orderSingleDao.GetOrderSingleCount(maps)
|
||
if err != nil {
|
||
return 0, err
|
||
}
|
||
|
||
return int(buyCount), nil
|
||
}
|
||
|
||
// GetUserBuyPrice 获取问题最终价格
|
||
func (r *QuestionService) GetUserBuyPrice(userId int64, question *model.Question) (p float64, err error) {
|
||
// 检测用户是否购买过单项产品
|
||
var firstTimePrice *float64
|
||
userService := &UserService{}
|
||
isBuy := userService.CheckUserBuySingle(userId)
|
||
if isBuy == false {
|
||
// 未购买过
|
||
systemSingleDao := dao.SystemSingleDao{}
|
||
|
||
maps := make(map[string]interface{})
|
||
systemSingle, err := systemSingleDao.GetSystemSingle(maps)
|
||
if err != nil {
|
||
return 0, err
|
||
}
|
||
|
||
if systemSingle.FirstTimePrice != nil {
|
||
// 首次购买价格
|
||
firstTimePrice = systemSingle.FirstTimePrice
|
||
}
|
||
}
|
||
|
||
if firstTimePrice != nil {
|
||
// 首次购买价格
|
||
p = *firstTimePrice
|
||
} else {
|
||
// 优惠价格
|
||
price := r.HandleQuestionDiscountPrice(question.DiscountPrice, question.DiscountEndTime)
|
||
if price == nil {
|
||
// 正常价格
|
||
p = question.Price
|
||
} else {
|
||
p = *price
|
||
}
|
||
}
|
||
|
||
return p, nil
|
||
}
|
||
|
||
// CheckQuestion 检测题目
|
||
func (r *QuestionService) CheckQuestion(m *model.Question) (bool, error) {
|
||
if m.QuestionStatus != 1 {
|
||
return false, errors.New("题目异常")
|
||
}
|
||
|
||
if m.IsHide != 0 {
|
||
return false, errors.New("题目异常")
|
||
}
|
||
|
||
return true, nil
|
||
}
|
||
|
||
// HandleQuestionDiscountPrice 处理问题优惠价格
|
||
func (r *QuestionService) HandleQuestionDiscountPrice(discountPrice *float64, discountEndTime *model.LocalTime) (p *float64) {
|
||
// 优惠价格
|
||
if discountPrice != nil {
|
||
discountEndTime := time.Time(*discountEndTime)
|
||
|
||
// 检测是否超出优惠时间
|
||
now := time.Now()
|
||
if discountEndTime.Before(now) {
|
||
p = nil
|
||
} else {
|
||
p = discountPrice
|
||
}
|
||
}
|
||
|
||
return p
|
||
}
|
||
|
||
// AddQuestionSubmitCount 增加单项提交次数
|
||
func (r *QuestionService) AddQuestionSubmitCount(tx *gorm.DB, questionId int64) (bool, error) {
|
||
questionDao := dao.QuestionDao{}
|
||
err := questionDao.Inc(tx, questionId, "submit_count", 1)
|
||
if err != nil {
|
||
return false, err
|
||
}
|
||
|
||
return true, nil
|
||
}
|
||
|
||
// AddQuestionPayCount 增加单项支付次数
|
||
func (r *QuestionService) AddQuestionPayCount(tx *gorm.DB, userId int64) (bool, error) {
|
||
questionDao := dao.QuestionDao{}
|
||
err := questionDao.Inc(tx, userId, "pay_count", 1)
|
||
if err != nil {
|
||
return false, err
|
||
}
|
||
|
||
return true, nil
|
||
}
|