You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.7 KiB
87 lines
2.7 KiB
package service
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"shop-api/global"
|
|
"shop-api/model"
|
|
"shop-api/model/request"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func ChangeCollectionGoods(uuid string, info request.SpuNo) (err error, id uint) {
|
|
var (
|
|
tmp model.CollectionGoods
|
|
collectionGoods model.CollectionGoods
|
|
)
|
|
err = global.MG_DB.Model(&model.TbGoods{}).Where("spu_no = ?", info.SpuNo).First(&model.TbGoods{}).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
tx := global.MG_DB.Begin()
|
|
err = tx.Model(&model.CollectionGoods{}).Where("spu_no = ? AND create_by = ?", info.SpuNo, uuid).Find(&tmp).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
if tmp.ID == 0 {
|
|
collectionGoods.SpuNo = info.SpuNo
|
|
collectionGoods.CreateBy = uuid
|
|
err = tx.Model(&model.CollectionGoods{}).Create(&collectionGoods).Error
|
|
if err != nil {
|
|
tx.Rollback()
|
|
err = errors.New("收藏失败")
|
|
return
|
|
}
|
|
// 商品收藏数+1
|
|
err = tx.Model(&model.TbGoods{}).Where("spu_no = ?", info.SpuNo).UpdateColumn("collection_num", gorm.Expr("collection_num + 1")).Error
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return
|
|
}
|
|
} else {
|
|
err = tx.Model(&model.CollectionGoods{}).Delete(&tmp).Error
|
|
if err != nil {
|
|
tx.Rollback()
|
|
err = errors.New("取消收藏失败")
|
|
return
|
|
}
|
|
// 任务收藏数-1
|
|
err = tx.Model(&model.TbGoods{}).Where("spu_no = ? AND collection_num > 0", info.SpuNo).UpdateColumn("collection_num", gorm.Expr("collection_num - 1")).Error
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return
|
|
}
|
|
}
|
|
tx.Commit()
|
|
id = collectionGoods.ID
|
|
return
|
|
}
|
|
|
|
func GetCollectionGoodsList(uuid string, info request.SearchCollectionGoods) (err error, list interface{}, total int64) {
|
|
limit := info.PageSize
|
|
offset := info.PageSize * (info.Page - 1)
|
|
db := global.MG_DB.Model(&model.CollectionGoods{}).Joins("INNER JOIN mission ON mission.id = collection_mission.mission_id").
|
|
Where("collection_mission.create_by = ?", uuid)
|
|
err = db.Count(&total).Error
|
|
var res []model.TbGoodsDetail
|
|
err = db.Select("mission.id,mission.title,mission.goods_id,mission.goods_status,mission.num,mission.hire_type,mission.hire_money,mission.hire_ratio,mission.start_time,mission.end_time,mission.status").
|
|
Preload("Goods", func(db *gorm.DB) *gorm.DB {
|
|
return db.Select("id,spu_no,title,title_eng,images,tags")
|
|
}).Limit(limit).Offset(offset).Find(&res).Error
|
|
if err != nil {
|
|
return err, nil, 0
|
|
}
|
|
for i := 0; i < len(res); i++ {
|
|
res[i].CollectStatus = getGoodsCollectStatus(uuid, res[i].SpuNo)
|
|
}
|
|
return err, res, total
|
|
}
|
|
|
|
func getGoodsCollectStatus(uuid string, spuNo string) (result bool) {
|
|
err := global.MG_DB.Model(&model.CollectionGoods{}).Where("create_by = ? AND spu_no = ?", uuid, spuNo).First(&model.CollectionGoods{}).Error
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|