195 lines
4.9 KiB
Go
195 lines
4.9 KiB
Go
package service
|
||
|
||
import (
|
||
"errors"
|
||
"hepa-calc-admin-api/api/dao"
|
||
"hepa-calc-admin-api/api/dto"
|
||
"hepa-calc-admin-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.GetOrderSingleOrderList(maps, "created_at desc", 3)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// 参与过
|
||
if len(orderSingles) > 0 {
|
||
for i, single := range orderSingles {
|
||
questions[i] = single.Question
|
||
}
|
||
}
|
||
}
|
||
|
||
// 未参与过/未指定用户
|
||
if len(questions) == 0 {
|
||
maps := make(map[string]interface{})
|
||
maps["question_status"] = 1
|
||
maps["is_hide"] = 0
|
||
questions, err = questionDao.GetQuestionListRand(maps, 3)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
}
|
||
|
||
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{}
|
||
orderSingle, _ := orderSingleDao.GetUserFirstTimeBuyOrderSingle(userId, questionId)
|
||
if orderSingle == nil {
|
||
return false
|
||
}
|
||
|
||
return true
|
||
}
|
||
|
||
// GetUserFirstTimeBuyPrice 获取用户首次购买价格
|
||
func (r *QuestionService) GetUserFirstTimeBuyPrice(userId, questionId int64) (f *float64, err error) {
|
||
// 检测用户是否购买过该问题
|
||
isFirstBuy := r.CheckUserBuyQuestion(userId, questionId)
|
||
if isFirstBuy == 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
|
||
}
|
||
|
||
// 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 *time.Time) (p *float64) {
|
||
// 优惠价格
|
||
if discountPrice != nil {
|
||
// 检测是否超出优惠时间
|
||
now := time.Now()
|
||
if discountEndTime.Before(now) {
|
||
p = nil
|
||
} else {
|
||
p = discountPrice
|
||
}
|
||
}
|
||
|
||
return p
|
||
}
|
||
|
||
// GetQuestionBaseClass 获取问题关联分类
|
||
func (r *QuestionService) GetQuestionBaseClass(questionId int64) (g []*dto.BaseClassDto, err error) {
|
||
questionClassDao := dao.QuestionClassDao{}
|
||
questionClass, _ := questionClassDao.GetQuestionClassListByQuestionId(questionId)
|
||
if len(questionClass) > 0 {
|
||
baseClassDao := dao.BaseClassDao{}
|
||
for _, class := range questionClass {
|
||
baseClass, err := baseClassDao.GetBaseClassById(class.ClassId)
|
||
if err != nil {
|
||
return nil, errors.New("题目异常")
|
||
}
|
||
|
||
baseClassDto := dto.GetBaseClassDto(baseClass)
|
||
|
||
g = append(g, baseClassDto)
|
||
}
|
||
}
|
||
|
||
return g, nil
|
||
}
|