|
|
package bulletin
|
|
|
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
orm2 "github.com/astaxie/beego/orm"
|
|
|
"oppmg/common/log"
|
|
|
"oppmg/models"
|
|
|
"oppmg/protocol"
|
|
|
"strconv"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
//发布公告
|
|
|
func BulletinRelease(uid, companyId int64, request *protocol.BulletinReleaseRequest) (rsp *protocol.BulletinReleaseResponse, err error) {
|
|
|
var (
|
|
|
bulletin *models.Bulletin
|
|
|
bulletinQuestion *models.BulletinQuestion
|
|
|
receiver, questionContent []byte
|
|
|
id int64
|
|
|
)
|
|
|
//TODO:check role_menu
|
|
|
if receiver, err = json.Marshal(request.Receiver); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return
|
|
|
}
|
|
|
bulletin = &models.Bulletin{
|
|
|
Title: request.Title,
|
|
|
Type: int8(request.Type),
|
|
|
Content: request.Content,
|
|
|
Cover: request.Cover.Path,
|
|
|
H: request.Cover.H,
|
|
|
W: request.Cover.W,
|
|
|
Receiver: string(receiver),
|
|
|
QuestionSwitch: int8(request.QuestionSwitch),
|
|
|
AllowCondition: int8(request.AllowCondition),
|
|
|
AllowClose: int8(request.AllowClose),
|
|
|
CompanyId: companyId,
|
|
|
CreateAt: time.Now(),
|
|
|
UpdateAt: time.Now(),
|
|
|
}
|
|
|
|
|
|
orm := orm2.NewOrm()
|
|
|
orm.Begin()
|
|
|
if id, err = orm.Insert(bulletin); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
orm.Rollback()
|
|
|
}
|
|
|
if request.QuestionSwitch == 1 {
|
|
|
if len(request.Question.Content) == 0 {
|
|
|
err = protocol.NewErrWithMessage("1")
|
|
|
log.Error("BulletinRelease:question.content not empty.", uid)
|
|
|
return
|
|
|
}
|
|
|
if questionContent, err = json.Marshal(request.Question.Content); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return
|
|
|
}
|
|
|
bulletinQuestion = &models.BulletinQuestion{
|
|
|
BulletinId: int(id),
|
|
|
Type: int8(request.Question.Type),
|
|
|
Title: request.Question.Title,
|
|
|
Content: string(questionContent),
|
|
|
CreateAt: time.Now(),
|
|
|
UpdateAt: time.Now(),
|
|
|
}
|
|
|
if _, err = orm.Insert(bulletinQuestion); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
orm.Rollback()
|
|
|
}
|
|
|
}
|
|
|
//TODO:发送公告消息
|
|
|
orm.Commit()
|
|
|
rsp = &protocol.BulletinReleaseResponse{}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//公告列表
|
|
|
func BulletinList(uid, companyId int64, request *protocol.BulletinListRequest) (rsp *protocol.BulletinListResponse, err error) {
|
|
|
var (
|
|
|
list []*models.Bulletin
|
|
|
total int
|
|
|
)
|
|
|
if request.Page == 0 {
|
|
|
request.Page = 1
|
|
|
}
|
|
|
if request.PageSize == 0 {
|
|
|
request.PageSize = 20
|
|
|
}
|
|
|
if list, total, err = models.GetBulletins(companyId, request.Status, request.Page, request.PageSize); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return
|
|
|
}
|
|
|
if len(list) == 0 {
|
|
|
return
|
|
|
}
|
|
|
rsp = &protocol.BulletinListResponse{}
|
|
|
for i := range list {
|
|
|
bulletin := list[i]
|
|
|
item := &protocol.BulletinItem{
|
|
|
Id: bulletin.Id,
|
|
|
Type: bulletin.Type,
|
|
|
Title: bulletin.Title,
|
|
|
Status: int8(bulletin.Status),
|
|
|
//TODO:user
|
|
|
Receiver: []string{},
|
|
|
CreateAt: bulletin.CreateAt.Format("2006-01-02 15:04:05"),
|
|
|
}
|
|
|
if item.Receiver, err = getUsersName(bulletin.Receiver); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
continue
|
|
|
}
|
|
|
rsp.List = append(rsp.List, item)
|
|
|
}
|
|
|
rsp.Total = total
|
|
|
return
|
|
|
}
|
|
|
|
|
|
func getUsers(idsstr string) (v []models.User, err error) {
|
|
|
var idlist []string
|
|
|
var ids []int64
|
|
|
var id int64
|
|
|
if err = json.Unmarshal([]byte(idsstr), &idlist); err != nil {
|
|
|
return
|
|
|
}
|
|
|
for i := 0; i < len(idlist); i++ {
|
|
|
if id, err = strconv.ParseInt(idlist[i], 10, 64); err != nil {
|
|
|
return
|
|
|
}
|
|
|
ids = append(ids, id)
|
|
|
}
|
|
|
return models.GetUserNameByIds(ids)
|
|
|
}
|
|
|
|
|
|
func getUsersName(idsStr string) (v []string, err error) {
|
|
|
var users []models.User
|
|
|
if users, err = getUsers(idsStr); err != nil {
|
|
|
return
|
|
|
}
|
|
|
for i := range users {
|
|
|
v = append(v, users[i].NickName)
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//公告详情
|
|
|
func GetBulletin(id int, companyId int64, request *protocol.GetBulletinRequest) (rsp *protocol.GetBulletinResponse, err error) {
|
|
|
var (
|
|
|
bulletin *models.Bulletin
|
|
|
question *models.BulletinQuestion
|
|
|
)
|
|
|
if bulletin, err = models.GetBulletinById(id); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return
|
|
|
}
|
|
|
if bulletin.CompanyId != companyId {
|
|
|
err = protocol.NewErrWithMessage("1")
|
|
|
log.Error("GetBulletin:company not equal:(%v!=%v)", companyId, bulletin.CompanyId)
|
|
|
return
|
|
|
}
|
|
|
rsp = &protocol.GetBulletinResponse{}
|
|
|
rsp = &protocol.GetBulletinResponse{
|
|
|
Id: bulletin.Id,
|
|
|
Type: int(bulletin.Type),
|
|
|
Title: bulletin.Title,
|
|
|
Content: bulletin.Content,
|
|
|
AllowClose: int(bulletin.AllowClose),
|
|
|
AllowCondition: int(bulletin.AllowCondition),
|
|
|
Cover: protocol.Cover{
|
|
|
Path: bulletin.Cover,
|
|
|
W: bulletin.W,
|
|
|
H: bulletin.H,
|
|
|
},
|
|
|
}
|
|
|
if bulletin.QuestionSwitch == 1 {
|
|
|
if question, err = models.GetBulletinQuestionByBulletinId(bulletin.Id); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return
|
|
|
}
|
|
|
rsp.QuestionSwitch = int(bulletin.QuestionSwitch)
|
|
|
rsp.Question = protocol.Question{
|
|
|
Type: int(question.Type),
|
|
|
Title: question.Title,
|
|
|
}
|
|
|
if err = json.Unmarshal([]byte(question.Content), &rsp.Question.Content); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
if rsp.Receiver, err = getUsersName(bulletin.Receiver); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//更新公告
|
|
|
func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (rsp *protocol.UpdateBulletinResponse, err error) {
|
|
|
var (
|
|
|
bulletin *models.Bulletin
|
|
|
bulletinQuestion *models.BulletinQuestion
|
|
|
receiver, questionContent []byte
|
|
|
)
|
|
|
if bulletin, err = models.GetBulletinById(request.Id); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return
|
|
|
}
|
|
|
if bulletin.CompanyId != companyId {
|
|
|
err = protocol.NewErrWithMessage("1")
|
|
|
log.Error("GetBulletin:company not equal:(%v!=%v)", companyId, bulletin.CompanyId)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
if receiver, err = json.Marshal(request.Receiver); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return
|
|
|
}
|
|
|
//update
|
|
|
{
|
|
|
bulletin.Title = request.Title
|
|
|
bulletin.Type = int8(request.Type)
|
|
|
bulletin.Content = request.Content
|
|
|
bulletin.Cover = request.Cover.Path
|
|
|
bulletin.H = request.Cover.H
|
|
|
bulletin.W = request.Cover.W
|
|
|
bulletin.Receiver = string(receiver)
|
|
|
bulletin.QuestionSwitch = int8(request.QuestionSwitch)
|
|
|
bulletin.AllowCondition = int8(request.AllowCondition)
|
|
|
bulletin.AllowClose = int8(request.AllowClose)
|
|
|
bulletin.UpdateAt = time.Now()
|
|
|
if err = models.UpdateBulletinById(bulletin); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if bulletin.QuestionSwitch == 1 {
|
|
|
if bulletinQuestion, err = models.GetBulletinQuestionByBulletinId(bulletin.Id); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return
|
|
|
}
|
|
|
if questionContent, err = json.Marshal(request.Question.Content); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return
|
|
|
}
|
|
|
bulletinQuestion.Content = string(questionContent)
|
|
|
bulletinQuestion.Type = int8(request.Question.Type)
|
|
|
if err = models.UpdateBulletinQuestionById(bulletinQuestion); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
return
|
|
|
} |
...
|
...
|
|