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.
404 lines
13 KiB
404 lines
13 KiB
package serviceGame |
|
|
|
import ( |
|
"context" |
|
"errors" |
|
"fmt" |
|
"github.com/gogf/gf/v2/frame/g" |
|
"github.com/gogf/gf/v2/os/gtime" |
|
"time" |
|
"tyj_admin/api/v1/game" |
|
"tyj_admin/internal/consts" |
|
"tyj_admin/internal/dao" |
|
"tyj_admin/internal/model/do" |
|
"tyj_admin/internal/model/entity" |
|
"tyj_admin/internal/serviceGame/internal" |
|
"tyj_admin/library/libUtils" |
|
"tyj_admin/library/liberr" |
|
) |
|
|
|
type IGameNotice interface { |
|
GetNotice(ctx context.Context, req *game.GetNoticeReq) (res *game.GetNoticeRes, err error) |
|
GetNoticeList(ctx context.Context, req *game.GetNoticeListReq) (res *game.GetNoticeListRes, err error) |
|
AddNotice(ctx context.Context, req *game.AddNoticeReq) (res *game.AddNoticeRes, err error) |
|
DelNoticeLog(ctx context.Context, req *game.DelNoticeLogReq) (res *game.DelNoticeLogRes, err error) |
|
DelNoticeList(ctx context.Context, req *game.DelNoticeListReq) (res *game.DelNoticeListRes, err error) |
|
PopNotice(ctx context.Context, req *game.PopReq) (res *game.PopRes, err error) |
|
GetGameNotice(ctx context.Context, req *game.GetGameNoticeReq) (res *game.GetGameNoticeRes, err error) |
|
GetGameNoticeList(ctx context.Context, req *game.GetGameNoticeListReq) (res *game.GetGameNoticeListRes, err error) |
|
GetGameNoticeLog(ctx context.Context, req *game.GetGameNoticeLogReq) (res *game.GetGameNoticeLogRes, err error) |
|
GetNoticeModel(ctx context.Context, req *game.GetNoticeModelReq) (res *game.GetNoticeModelRes, err error) |
|
AddNoticeModel(ctx context.Context, req *game.AddNoticeModelReq) (res *game.AddNoticeModelRes, err error) |
|
DelNoticeModel(ctx context.Context, req *game.DelNoticeModelReq) (res *game.DelNoticeModelRes, err error) |
|
} |
|
|
|
type gameNoticeImpl struct { |
|
} |
|
|
|
var gameNoticeService = gameNoticeImpl{} |
|
|
|
func GameNotice() IGameNotice { |
|
return &gameNoticeService |
|
} |
|
func (c *gameNoticeImpl) GetNotice(ctx context.Context, req *game.GetNoticeReq) (res *game.GetNoticeRes, err error) { |
|
res = new(game.GetNoticeRes) |
|
g.Try(ctx, func(ctx context.Context) { |
|
model := dao.GameNotice.Ctx(ctx) |
|
model = model.Where("notice_type = ?", req.NoticeType) |
|
model = model.Where("channel=? ", req.Channel) |
|
var noticeList []*entity.GameNotice |
|
|
|
err = model.Scan(¬iceList) |
|
if len(noticeList) >= 1 { |
|
res.Content = noticeList[0].Content |
|
res.Status = noticeList[0].Status |
|
res.Id = noticeList[0].Id |
|
} else { |
|
return |
|
} |
|
|
|
//if res.Status == consts.Notice_White_List { |
|
// ip := libUtils.GetClientIp(ctx) |
|
// count := GameWhiteList().GetWhiteList(ctx, ip) |
|
// res.Status = consts.Notice_Maintenance |
|
// if count == consts.White_List_Belong { |
|
// res.Status = consts.Notice_Normal |
|
// } |
|
//} |
|
}) |
|
return |
|
} |
|
|
|
func (c *gameNoticeImpl) GetNoticeList(ctx context.Context, req *game.GetNoticeListReq) (res *game.GetNoticeListRes, err error) { |
|
res = new(game.GetNoticeListRes) |
|
err = g.Try(ctx, func(ctx context.Context) { |
|
model := dao.GameNotice.Ctx(ctx) |
|
if req.NoticeType != 0 { |
|
model = model.Where("notice_type=?", req.NoticeType) |
|
} |
|
err = model.Page(req.PageNum, req.PageSize).Scan(&res.List) |
|
res.Total, err = model.Count() |
|
return |
|
}) |
|
return |
|
} |
|
|
|
func (c *gameNoticeImpl) GetGameNotice(ctx context.Context, req *game.GetGameNoticeReq) (res *game.GetGameNoticeRes, err error) { |
|
res = new(game.GetGameNoticeRes) |
|
g.Try(ctx, func(ctx context.Context) { |
|
model := dao.GameNotice.Ctx(ctx) |
|
model = model.Where("notice_type = ?", req.NoticeType) |
|
ip := libUtils.GetClientIp(ctx) |
|
|
|
var ipList []*entity.GameNotice |
|
err = model.Where("channel=?", ip).Scan(&ipList) |
|
if len(ipList) > 0 { |
|
res.TimeStamp = int32(ipList[0].CDate) |
|
res.Content = ipList[0].Content |
|
res.Status = ipList[0].Status |
|
|
|
g.Log().Debugf(ctx, "ipList GetGameNotice ip: "+ip, "NoticeType: "+fmt.Sprint(req.NoticeType), "res: "+fmt.Sprint(res.Status)) |
|
return |
|
} |
|
if req.NoticeType == consts.Notice_Type_Pop { |
|
model = model.Where("server=? ", req.ServerId) |
|
} else { |
|
model = model.Where("channel=? ", req.Channel) |
|
} |
|
var noticeList []*entity.GameNotice |
|
var count int |
|
count, err = model.Count() |
|
if count == 0 { |
|
model = dao.GameNotice.Ctx(ctx) |
|
model = model.Where("notice_type = ?", req.NoticeType) |
|
if req.NoticeType == consts.Notice_Type_Pop { |
|
model = model.Where("server=? ", 0) |
|
} else { |
|
model = model.Where("channel=? ", "") |
|
} |
|
} |
|
err = model.Scan(¬iceList) |
|
if len(noticeList) > 1 { |
|
for _, v := range noticeList { |
|
if v.Channel == req.Channel { |
|
res.TimeStamp = int32(v.CDate) |
|
res.Content = v.Content |
|
res.Status = v.Status |
|
break |
|
} |
|
} |
|
} else if len(noticeList) == 1 { |
|
res.TimeStamp = int32(noticeList[0].CDate) |
|
res.Content = noticeList[0].Content |
|
res.Status = noticeList[0].Status |
|
} else if len(noticeList) == 0 { |
|
res.TimeStamp = 0 |
|
res.Content = "" |
|
res.Status = consts.Notice_Normal |
|
} |
|
|
|
if req.NoticeType != consts.Notice_Type_Pop { |
|
if res.Status == consts.Notice_White_List { |
|
whiteIps, e := internal.GetWhiteList(ctx) |
|
g.Log().Debugf(ctx, "GetGameNotice ip"+ip, len(whiteIps), e) |
|
res.Status = consts.Notice_Maintenance |
|
for _, v := range whiteIps { |
|
if v.Ip == ip { |
|
res.Status = consts.Notice_Normal |
|
break |
|
} |
|
} |
|
} |
|
|
|
if req.NoticeType != consts.Notice_Type_Maintenance { |
|
res.Status = consts.Notice_Normal |
|
} |
|
} |
|
|
|
g.Log().Debugf(ctx, "GetGameNotice ip: "+ip, "NoticeType: "+fmt.Sprint(req.NoticeType), "res: "+fmt.Sprint(res.Status)) |
|
}) |
|
return |
|
} |
|
|
|
func (c *gameNoticeImpl) GetGameNoticeList(ctx context.Context, req *game.GetGameNoticeListReq) (res *game.GetGameNoticeListRes, err error) { |
|
res = new(game.GetGameNoticeListRes) |
|
g.Try(ctx, func(ctx context.Context) { |
|
req1 := new(game.GetGameNoticeReq) |
|
req1.NoticeType = consts.Notice_Type_Login |
|
req1.Channel = req.Channel |
|
res1, _ := c.GetGameNotice(ctx, req1) |
|
res.List = append(res.List, game.Notice{Content: res1.Content, Status: res1.Status, TimeStamp: res1.TimeStamp, NoticeType: consts.Notice_Type_Login}) |
|
|
|
req1.NoticeType = consts.Notice_Type_Preview |
|
req1.Channel = req.Channel |
|
res2, _ := c.GetGameNotice(ctx, req1) |
|
res.List = append(res.List, game.Notice{Content: res2.Content, Status: res2.Status, TimeStamp: res2.TimeStamp, NoticeType: consts.Notice_Type_Preview}) |
|
//log.Printf("GetGameNotice res: %s", gjson.MustEncodeString(res.List)) |
|
}) |
|
return |
|
} |
|
|
|
func (c *gameNoticeImpl) GetGameNoticeLog(ctx context.Context, req *game.GetGameNoticeLogReq) (res *game.GetGameNoticeLogRes, err error) { |
|
res = new(game.GetGameNoticeLogRes) |
|
g.Try(ctx, func(ctx context.Context) { |
|
model := dao.GameNoticeLog.Ctx(ctx) |
|
if req.NoticeType != 0 { |
|
model = model.Where("notice_type=", req.NoticeType) |
|
} |
|
if req.Content != "" { |
|
model = model.Where("content LIKE ?", "%"+req.Content+"%") |
|
} |
|
if req.BeginTime != 0 { |
|
model = model.Where("c_date >", gtime.NewFromTimeStamp(req.BeginTime)) |
|
} |
|
if req.EndTime != 0 { |
|
model = model.Where("c_date <", gtime.NewFromTimeStamp(req.EndTime)) |
|
} |
|
|
|
//log.Printf("1model ", req, model) |
|
res.Total, err = model.Count() |
|
if res.Total != 0 { |
|
model = model.Page(req.PageNum, req.PageSize) |
|
//log.Printf("model ", req, model) |
|
err = model.OrderDesc("c_date").Scan(&res.NoticeLog) |
|
} |
|
}) |
|
return |
|
} |
|
|
|
func (c *gameNoticeImpl) AddNotice(ctx context.Context, req *game.AddNoticeReq) (res *game.AddNoticeRes, err error) { |
|
SendTime := req.SendTime |
|
data := do.GameNoticeLog{ |
|
Status: req.Status, |
|
Content: req.Content, |
|
NoticeType: req.NoticeType, |
|
NoticeId: req.NoticeId, |
|
Channel: req.Channel, |
|
SendTime: SendTime, |
|
} |
|
cron := entity.GameNoticeLog{ |
|
Id: req.Id, |
|
Status: req.Status, |
|
Content: req.Content, |
|
NoticeType: req.NoticeType, |
|
NoticeId: req.NoticeId, |
|
Channel: req.Channel, |
|
SendTime: SendTime, |
|
} |
|
if req.Id > 0 { |
|
err = g.Try(ctx, func(ctx context.Context) { |
|
_, e := dao.GameNoticeLog.Ctx(ctx).Where("id=", req.Id).Data(&data).Update() |
|
if e != nil { |
|
liberr.ErrIsNil(ctx, e, "更新通知失败") |
|
return |
|
} |
|
if SendTime > time.Now().UnixMilli() { |
|
GameCron().AddCron(cron) |
|
} |
|
fmt.Println("req >>>>>>>>>> ", req) |
|
return |
|
}) |
|
} else { |
|
err = g.Try(ctx, func(ctx context.Context) { |
|
logId, e := dao.GameNoticeLog.Ctx(ctx).Insert(&data) |
|
if e != nil { |
|
liberr.ErrIsNil(ctx, e, "插入通知日志失败") |
|
return |
|
} |
|
id, e := logId.LastInsertId() |
|
if e != nil { |
|
liberr.ErrIsNil(ctx, e, "插入通知日志失败") |
|
return |
|
} |
|
cron.Id = id |
|
fmt.Println("logId >>>>>>>>>> ", logId) |
|
if SendTime <= time.Now().UnixMilli() { |
|
noticeModel := dao.GameNotice.Ctx(ctx) |
|
noticeList := []entity.GameNotice{} |
|
noticeModel.Where("notice_type=?", req.NoticeType).Where("channel=?", req.Channel).Scan(¬iceList) |
|
if len(noticeList) > 0 { |
|
_, e = noticeModel.Where("id=?", noticeList[0].Id).Data(&do.GameNotice{ |
|
Status: req.Status, |
|
Content: req.Content, |
|
CDate: time.Now().Unix(), |
|
}).Update() |
|
} else { |
|
_, e = noticeModel.Insert(&do.GameNotice{ |
|
Status: req.Status, |
|
Content: req.Content, |
|
NoticeType: req.NoticeType, |
|
Channel: req.Channel, |
|
CDate: time.Now().Unix(), |
|
}) |
|
} |
|
} else { |
|
GameCron().AddCron(cron) |
|
fmt.Println("logId >>>>>>>>>> ", cron.Id, e) |
|
} |
|
liberr.ErrIsNil(ctx, e, "插入通知失败") |
|
}) |
|
} |
|
return |
|
} |
|
|
|
func (c *gameNoticeImpl) DelNoticeLog(ctx context.Context, req *game.DelNoticeLogReq) (res *game.DelNoticeLogRes, err error) { |
|
if req.Id == 0 { |
|
err = errors.New("错误的id") |
|
return |
|
} |
|
|
|
err = g.Try(ctx, func(ctx context.Context) { |
|
_, e := dao.GameNoticeLog.Ctx(ctx).Where("id=", req.Id).Delete() |
|
if e != nil { |
|
liberr.ErrIsNil(ctx, e, "删除通知失败") |
|
return |
|
} |
|
GameCron().DelCron(req.Id) |
|
}) |
|
|
|
return |
|
} |
|
|
|
func (c *gameNoticeImpl) DelNoticeList(ctx context.Context, req *game.DelNoticeListReq) (res *game.DelNoticeListRes, err error) { |
|
if req.Id == 0 { |
|
err = errors.New("错误的id") |
|
return |
|
} |
|
|
|
err = g.Try(ctx, func(ctx context.Context) { |
|
_, e := dao.GameNotice.Ctx(ctx).Where("id=", req.Id).Delete() |
|
if e != nil { |
|
liberr.ErrIsNil(ctx, e, "删除通知失败") |
|
return |
|
} |
|
GameCron().DelCron(req.Id) |
|
}) |
|
|
|
return |
|
} |
|
|
|
func (c *gameNoticeImpl) PopNotice(ctx context.Context, req *game.PopReq) (res *game.PopRes, err error) { |
|
err = g.Try(ctx, func(ctx context.Context) { |
|
if req.PopTo == 1 || req.Type == consts.Notice_Pop_Roll || req.Type == consts.Notice_Pop_Roll_Del { |
|
req.Uids = "" |
|
} |
|
_, e := dao.GameNoticeLog.Ctx(ctx).Insert(&do.GameNoticeLog{ |
|
Status: 0, |
|
ServerId: req.ServerId, |
|
Uids: req.Uids, |
|
PopType: req.Type, |
|
Content: req.Content, |
|
NoticeType: consts.Notice_Type_Pop, |
|
NoticeId: 0, |
|
Channel: "", |
|
SendTime: 0, |
|
}) |
|
if e != nil { |
|
liberr.ErrIsNil(ctx, e, "插入通知日志失败") |
|
return |
|
} |
|
err = internal.BackStageMessage(ctx, req.ServerId, req.Uids, req.Content, req.Type) |
|
if err == nil && req.Type == consts.Notice_Pop_Roll { |
|
dao.GameNotice.Ctx(ctx).Delete(do.GameNotice{NoticeType: consts.Notice_Type_Pop, ServerId: req.ServerId}) |
|
dao.GameNotice.Ctx(ctx).Insert(&do.GameNotice{ |
|
Status: 1, |
|
Content: req.Content, |
|
Channel: "", |
|
NoticeType: consts.Notice_Type_Pop, |
|
ServerId: req.ServerId, |
|
CDate: time.Now().Unix(), |
|
}) |
|
} |
|
if err == nil && req.Type == consts.Notice_Pop_Roll_Del { |
|
dao.GameNotice.Ctx(ctx).Delete(do.GameNotice{NoticeType: consts.Notice_Type_Pop, ServerId: req.ServerId}) |
|
} |
|
}) |
|
|
|
return |
|
} |
|
|
|
func (c *gameNoticeImpl) GetNoticeModel(ctx context.Context, req *game.GetNoticeModelReq) (res *game.GetNoticeModelRes, err error) { |
|
res = new(game.GetNoticeModelRes) |
|
g.Try(ctx, func(ctx context.Context) { |
|
model := dao.GameNoticeModel.Ctx(ctx) |
|
model = model.Where("notice_type = ?", req.NoticeType) |
|
err = model.Scan(&res.NoticeModel) |
|
}) |
|
|
|
return |
|
} |
|
|
|
func (c *gameNoticeImpl) AddNoticeModel(ctx context.Context, req *game.AddNoticeModelReq) (res *game.AddNoticeModelRes, err error) { |
|
if req.Id > 0 { |
|
err = g.Try(ctx, func(ctx context.Context) { |
|
_, e := dao.GameNoticeModel.Ctx(ctx).Where("id=", req.Id).Data(&do.GameNoticeModel{ |
|
Title: req.Title, |
|
Content: req.Content, |
|
}).Update() |
|
liberr.ErrIsNil(ctx, e, "修改通知失败") |
|
}) |
|
} else { |
|
err = g.Try(ctx, func(ctx context.Context) { |
|
_, e := dao.GameNoticeModel.Ctx(ctx).Data(&do.GameNoticeModel{ |
|
Title: req.Title, |
|
Content: req.Content, |
|
NoticeType: req.NoticeType, |
|
ModelType: req.ModelType, |
|
}).Insert() |
|
liberr.ErrIsNil(ctx, e, "插入通知失败") |
|
}) |
|
} |
|
return |
|
} |
|
|
|
func (c *gameNoticeImpl) DelNoticeModel(ctx context.Context, req *game.DelNoticeModelReq) (res *game.DelNoticeModelRes, err error) { |
|
if req.Id > 0 { |
|
err = g.Try(ctx, func(ctx context.Context) { |
|
_, e := dao.GameNoticeModel.Ctx(ctx).Where("id=", req.Id).Delete() |
|
liberr.ErrIsNil(ctx, e, "删除通知失败") |
|
}) |
|
} else { |
|
return res, errors.New("删除通知失败") |
|
} |
|
return |
|
}
|
|
|