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.
44 lines
914 B
44 lines
914 B
5 years ago
|
package topic
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"gorm.io/gorm"
|
||
|
"live/app/model/topic"
|
||
|
)
|
||
|
|
||
|
type Substance struct {
|
||
|
}
|
||
|
|
||
|
// @Title 添加参与人
|
||
|
func (s *Substance) AddSubstance(userId, topicId uint, orm ...*gorm.DB) bool {
|
||
|
topicSubstance := &topic.TopicSubstance{}
|
||
|
if len(orm) > 0 {
|
||
|
topicSubstance.SetDb(orm[0])
|
||
|
}
|
||
|
// 查询记录
|
||
|
result := topicSubstance.GetSubstanceByUserIdAndTopicId(userId, topicId)
|
||
|
if result.Id <= 0 {
|
||
|
resId := int64(0)
|
||
|
|
||
|
// 事物处理
|
||
|
err := topicSubstance.Transaction(func(tx *gorm.DB) error {
|
||
|
topicSubstance.SetDb(tx)
|
||
|
subId := topicSubstance.AddSubstance(userId, topicId)
|
||
|
if subId > 0 {
|
||
|
// 处理统计数据
|
||
|
topicModel := &topic.Topic{}
|
||
|
topicModel.SetDb(topicSubstance.GetDb())
|
||
|
resId = topicModel.AddSubstance(topicId)
|
||
|
if resId > 0 {
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
return errors.New("网络异常")
|
||
|
})
|
||
|
if err != nil {
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
return true
|
||
|
}
|