@@ -0,0 +1,236 @@
|
||||
package discount_area_ser
|
||||
|
||||
import (
|
||||
"91porn-server/common"
|
||||
"91porn-server/common/cachev2"
|
||||
"91porn-server/common/constant/redisconst"
|
||||
sli "91porn-server/common/slice"
|
||||
"91porn-server/common/stderr"
|
||||
"91porn-server/models/v/discount_area_mod"
|
||||
"91porn-server/models/v/discount_area_video_mod"
|
||||
"91porn-server/models/v/vidmod"
|
||||
"github.com/shopspring/decimal"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"time"
|
||||
)
|
||||
|
||||
func AddDiscountArea(req *discount_area_mod.WebDiscountAreaAddReq) (err error) {
|
||||
now := time.Now()
|
||||
model := &discount_area_mod.DiscountArea{
|
||||
Title: req.Title,
|
||||
Desc: req.Desc,
|
||||
Discount: int(req.Discount),
|
||||
Status: int(req.Status),
|
||||
SortCode: int(req.SortCode),
|
||||
RecommendNum: int(req.RecommendNum),
|
||||
UpdatedAt: now,
|
||||
CreatedAt: now,
|
||||
}
|
||||
//最多10个
|
||||
if model.RecommendNum > 10 {
|
||||
model.RecommendNum = 10
|
||||
}
|
||||
err = discount_area_mod.AddDiscountArea(model)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// 删除缓存
|
||||
cachev2.Classes().AutoListKey(redisconst.DiscountArea).AutoClear(true, nil)
|
||||
return
|
||||
}
|
||||
|
||||
func UpdateDiscountArea(req *discount_area_mod.WebDiscountAreaUpdateReq) (err error) {
|
||||
var maxRecomment uint = 10
|
||||
if req.RecommendNum != nil && *req.RecommendNum > 10 {
|
||||
req.RecommendNum = &maxRecomment
|
||||
}
|
||||
objId, err := primitive.ObjectIDFromHex(req.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
update, err := common.ToBsonM(req.WebDiscountAreaUpdateData)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
update["updateAt"] = time.Now()
|
||||
err = discount_area_mod.UpdateDiscountArea(objId, update)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// 删除缓存
|
||||
cachev2.Classes().AutoListKey(redisconst.DiscountArea).AutoClear(true, nil)
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteDiscountArea 删除折扣专区
|
||||
func DeleteDiscountArea(id string) (err error) {
|
||||
objId, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
update := bson.M{
|
||||
"status": 2,
|
||||
"updateAt": time.Now(),
|
||||
}
|
||||
err = discount_area_mod.UpdateDiscountArea(objId, update)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// 删除缓存
|
||||
cachev2.Classes().AutoListKey(redisconst.DiscountArea).AutoClear(true, nil)
|
||||
return
|
||||
}
|
||||
|
||||
// GetDiscountAreaVids 获取专区下的视频
|
||||
func GetDiscountAreaVids(req *discount_area_mod.WebDiscountAreaVidsReq) (list []discount_area_mod.WebDiscountAreaVideo, total int64, err error) {
|
||||
filter := bson.M{}
|
||||
if req.DiscountAreaId != "" {
|
||||
discountAreaId, _ := primitive.ObjectIDFromHex(req.DiscountAreaId)
|
||||
filter["discountAreaId"] = discountAreaId
|
||||
}
|
||||
if req.Vid != "" {
|
||||
videoID, _ := primitive.ObjectIDFromHex(req.Vid)
|
||||
filter["videoID"] = videoID
|
||||
}
|
||||
opts := options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}).SetSkip(int64(req.Skip())).SetLimit(int64(req.Limit()))
|
||||
discountAreaVideoList, total, err := discount_area_video_mod.GetListByCond(filter, opts)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
vids := []primitive.ObjectID{}
|
||||
discountAreaIds := []primitive.ObjectID{}
|
||||
// 获取视频信息
|
||||
for _, v := range discountAreaVideoList {
|
||||
vids = append(vids, v.VideoID)
|
||||
discountAreaIds = append(discountAreaIds, v.DiscountAreaId)
|
||||
}
|
||||
|
||||
videoList, err := vidmod.GetVideoListByIDsNoStatus(sli.RemoveRepObjectID(vids))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
videoMap := make(map[primitive.ObjectID]vidmod.VideoModel)
|
||||
for _, video := range videoList {
|
||||
videoMap[video.ID] = *video
|
||||
}
|
||||
discountAreaList, err := discount_area_mod.GetNoStatusDiscountAreaByIds(sli.RemoveRepObjectID(discountAreaIds))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
discountAreaMap := make(map[primitive.ObjectID]discount_area_mod.DiscountArea)
|
||||
for _, discountArea := range discountAreaList {
|
||||
discountAreaMap[discountArea.ID] = discountArea
|
||||
}
|
||||
|
||||
for _, v := range discountAreaVideoList {
|
||||
discountAreaVideo := discount_area_mod.WebDiscountAreaVideo{
|
||||
Id: v.ID,
|
||||
VideoID: v.VideoID,
|
||||
DiscountAreaId: v.DiscountAreaId,
|
||||
CreatedAt: v.CreatedAt,
|
||||
UpdatedAt: v.UpdatedAt,
|
||||
}
|
||||
if video, ok := videoMap[v.VideoID]; ok {
|
||||
discountAreaVideo.Title = video.Title
|
||||
discountAreaVideo.Cover = video.Cover
|
||||
discountAreaVideo.PublisherID = video.PublisherID
|
||||
discountAreaVideo.OriginalPrice = video.Coins
|
||||
}
|
||||
if discountArea, ok := discountAreaMap[v.DiscountAreaId]; ok {
|
||||
discountAreaVideo.DiscountAreaTitle = discountArea.Title
|
||||
discountAreaVideo.DiscountedPrice = decimal.NewFromInt(int64(discountArea.Discount)).Shift(-2).
|
||||
Mul(decimal.NewFromInt(discountAreaVideo.OriginalPrice)).Round(0).IntPart()
|
||||
}
|
||||
list = append(list, discountAreaVideo)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// DiscountAreaAddVid 折扣区新增视频
|
||||
func DiscountAreaAddVid(req *discount_area_mod.WebDiscountAreaAddVidReq) (code stderr.Code) {
|
||||
if req.DiscountAreaId.IsZero() || len(req.VIds) == 0 {
|
||||
return stderr.ErrParamError
|
||||
}
|
||||
// 判断是否是视频
|
||||
videoList, err := vidmod.GetVideoListByIDs(req.VIds)
|
||||
if err != nil {
|
||||
return stderr.ErrDbQueryError
|
||||
}
|
||||
vids := []primitive.ObjectID{}
|
||||
for _, v := range videoList {
|
||||
// 不是视频,直接返回错误
|
||||
if v.NewsType != vidmod.SP {
|
||||
return stderr.NotAVideo
|
||||
}
|
||||
vids = append(vids, v.ID)
|
||||
}
|
||||
if len(vids) == 0 {
|
||||
return
|
||||
}
|
||||
// 查询是否已经在其他折扣区,是的话则直接报错
|
||||
list, err := discount_area_video_mod.GetDiscountAreaVideoByVids(vids)
|
||||
if err != nil {
|
||||
return stderr.ErrDbQueryError
|
||||
}
|
||||
if len(list) > 0 {
|
||||
// 不能一个视频同时在两个折扣区
|
||||
return stderr.VideoInDiscountArea
|
||||
}
|
||||
now := time.Now()
|
||||
batchAddVids := []discount_area_video_mod.DiscountAreaVideo{}
|
||||
for _, vid := range vids {
|
||||
batchAddVids = append(batchAddVids, discount_area_video_mod.DiscountAreaVideo{
|
||||
DiscountAreaId: req.DiscountAreaId,
|
||||
VideoID: vid,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
})
|
||||
}
|
||||
// 写入折扣区
|
||||
err = discount_area_video_mod.BatchAdd(batchAddVids)
|
||||
if err != nil {
|
||||
return stderr.ErrDbInsertManyError
|
||||
}
|
||||
update := bson.M{}
|
||||
update["discountAreaId"] = req.DiscountAreaId
|
||||
update["updatedAt"] = time.Now()
|
||||
_, err = vidmod.UpdateManyVideo(vids, update)
|
||||
if err != nil {
|
||||
return stderr.ErrDbUpdateError
|
||||
}
|
||||
|
||||
return stderr.Success
|
||||
}
|
||||
|
||||
// DiscountAreaDeleteVid 移除折扣区视频
|
||||
func DiscountAreaDeleteVid(ids []primitive.ObjectID) (code stderr.Code) {
|
||||
if len(ids) == 0 {
|
||||
return stderr.ErrParamError
|
||||
}
|
||||
|
||||
list, err := discount_area_video_mod.GetDiscountAreaVideoByIds(ids)
|
||||
if err != nil {
|
||||
return stderr.ErrDbQueryError
|
||||
}
|
||||
vids := []primitive.ObjectID{}
|
||||
for _, v := range list {
|
||||
vids = append(vids, v.VideoID)
|
||||
}
|
||||
// 删除视频
|
||||
err = discount_area_video_mod.DeleteDiscountAreaVideoByIds(ids)
|
||||
if err != nil {
|
||||
return stderr.ErrDbDeleteError
|
||||
}
|
||||
// 移除
|
||||
update := bson.M{}
|
||||
update["discountAreaId"] = nil
|
||||
update["updatedAt"] = time.Now()
|
||||
_, err = vidmod.UpdateManyVideo(vids, update)
|
||||
if err != nil {
|
||||
return stderr.ErrDbUpdateError
|
||||
}
|
||||
return stderr.Success
|
||||
}
|
||||
Reference in New Issue
Block a user