|
@@ -0,0 +1,146 @@
|
|
|
+package bullet_chat
|
|
|
+
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+ "errors"
|
|
|
+ "hongze/hongze_yb/models/tables/yb_bullet_chat"
|
|
|
+ "hongze/hongze_yb/models/tables/yb_community_video"
|
|
|
+ "hongze/hongze_yb/models/tables/yb_config"
|
|
|
+ "hongze/hongze_yb/models/tables/yb_road_video"
|
|
|
+ "math/rand"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+const (
|
|
|
+ ConfBulletChatColor = "bullet_chat_color"
|
|
|
+)
|
|
|
+
|
|
|
+var (
|
|
|
+ SourceBulletChatVideo int
|
|
|
+ SourceBulletChatRoadVideo int
|
|
|
+)
|
|
|
+
|
|
|
+func init() {
|
|
|
+ SourceBulletChatVideo = yb_bullet_chat.SourceBulletVideo
|
|
|
+ SourceBulletChatRoadVideo = yb_bullet_chat.SourceBulletChatVideo
|
|
|
+}
|
|
|
+
|
|
|
+// BulletChatColor 弹幕颜色
|
|
|
+type BulletChatColor struct {
|
|
|
+ Id int `json:"id" description:"颜色ID"`
|
|
|
+ Color string `json:"color" description:"颜色#"`
|
|
|
+ Remark string `json:"remark" description:"备注"`
|
|
|
+}
|
|
|
+
|
|
|
+// GetRandomColor 随机获取弹幕颜色
|
|
|
+func GetRandomColor() (color *BulletChatColor, err error) {
|
|
|
+ // 读取配置
|
|
|
+ var cond string
|
|
|
+ var pars []interface{}
|
|
|
+ cond += `config_code = ?`
|
|
|
+ pars = append(pars, ConfBulletChatColor)
|
|
|
+ confDao := new(yb_config.YbConfig)
|
|
|
+ conf, e := confDao.Fetch(cond, pars)
|
|
|
+ if e != nil {
|
|
|
+ err = errors.New("获取弹幕颜色配置失败, Err: " + e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if conf.ConfigID <= 0 {
|
|
|
+ err = errors.New("获取弹幕颜色配置失败, ID为空")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if conf.ConfigValue == "" {
|
|
|
+ err = errors.New("弹幕颜色配置有误")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ colors := make([]*BulletChatColor, 0)
|
|
|
+ if e = json.Unmarshal([]byte(conf.ConfigValue), &colors); e != nil {
|
|
|
+ err = errors.New("弹幕颜色配置格式有误, Err: " + e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ defaultColor := &BulletChatColor{
|
|
|
+ Id: 1,
|
|
|
+ Color: "#FFFFFF",
|
|
|
+ Remark: "白色",
|
|
|
+ }
|
|
|
+ colorLen := len(colors)
|
|
|
+ if colorLen == 0 {
|
|
|
+ color = defaultColor
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 随机获取
|
|
|
+ rand.Seed(time.Now().UnixNano())
|
|
|
+ x := rand.Intn(colorLen)
|
|
|
+ color = colors[x]
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// GetListMapBySourceAndIds 通过来源及IDs获取弹幕列表Map
|
|
|
+func GetListMapBySourceAndIds(source int, primaryIds []int) (idBulletsMap map[int][]*yb_bullet_chat.YbBulletChat, err error) {
|
|
|
+ var cond string
|
|
|
+ var pars []interface{}
|
|
|
+ cond += `source = ? AND primary_id IN ?`
|
|
|
+ pars = append(pars, source, primaryIds)
|
|
|
+
|
|
|
+ list, e := yb_bullet_chat.GetList(cond, pars)
|
|
|
+ if e != nil {
|
|
|
+ err = errors.New("获取弹幕列表失败, Err: " + e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ idBulletsMap = make(map[int][]*yb_bullet_chat.YbBulletChat, 0)
|
|
|
+ for i := range primaryIds {
|
|
|
+ idBulletsMap[primaryIds[i]] = make([]*yb_bullet_chat.YbBulletChat, 0)
|
|
|
+ }
|
|
|
+ for i := range list {
|
|
|
+ idBulletsMap[list[i].PrimaryID] = append(idBulletsMap[list[i].PrimaryID], list[i])
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// CreateBulletChat 新增弹幕
|
|
|
+func CreateBulletChat(userId, primaryId, source, sourceAgent int, seconds float64, content string) (item *yb_bullet_chat.YbBulletChat, err error) {
|
|
|
+ // 随机颜色
|
|
|
+ color, e := GetRandomColor()
|
|
|
+ if e != nil {
|
|
|
+ err = errors.New("获取弹幕随机颜色失败, Err: " + e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ title := ""
|
|
|
+ if source == 1 {
|
|
|
+ vd, e := yb_community_video.GetItemById(primaryId)
|
|
|
+ if e != nil {
|
|
|
+ err = errors.New("获取视频失败, Err: " + e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ title = vd.Title
|
|
|
+ } else {
|
|
|
+ rv, e := yb_road_video.GetItemById(primaryId)
|
|
|
+ if e != nil {
|
|
|
+ err = errors.New("获取路演视频失败, Err: " + e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ title = rv.Title
|
|
|
+ }
|
|
|
+
|
|
|
+ nowTime := time.Now().Local()
|
|
|
+ bc := new(yb_bullet_chat.YbBulletChat)
|
|
|
+ bc.UserID = userId
|
|
|
+ bc.PrimaryID = primaryId
|
|
|
+ bc.Content = content
|
|
|
+ bc.Seconds = seconds
|
|
|
+ bc.Color = color.Color
|
|
|
+ bc.Title = title
|
|
|
+ bc.Source = source
|
|
|
+ bc.SourceAgent = sourceAgent
|
|
|
+ bc.CreateTime = nowTime
|
|
|
+ bc.ModifyTime = nowTime
|
|
|
+ if e := bc.Create(); e != nil {
|
|
|
+ err = errors.New("新增弹幕失败, Err: " + e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ item = bc
|
|
|
+ return
|
|
|
+}
|