...
|
...
|
@@ -2,11 +2,15 @@ package bulletin |
|
|
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
orm2 "github.com/astaxie/beego/orm"
|
|
|
"oppmg/common/log"
|
|
|
"oppmg/models"
|
|
|
"oppmg/protocol"
|
|
|
"oppmg/services/agg"
|
|
|
"oppmg/utils"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
|
)
|
|
|
|
...
|
...
|
@@ -17,7 +21,11 @@ func BulletinRelease(uid, companyId int64, request *protocol.BulletinReleaseRequ |
|
|
bulletinQuestion *models.BulletinQuestion
|
|
|
receiver, questionContent []byte
|
|
|
id int64
|
|
|
status int8 = protocol.BulletinUnRelease
|
|
|
)
|
|
|
if request.IsPublish == 1 {
|
|
|
status = protocol.BulletinRelease
|
|
|
}
|
|
|
//TODO:check role_menu
|
|
|
if receiver, err = json.Marshal(request.Receiver); err != nil {
|
|
|
log.Error(err.Error())
|
...
|
...
|
@@ -37,7 +45,7 @@ func BulletinRelease(uid, companyId int64, request *protocol.BulletinReleaseRequ |
|
|
CompanyId: companyId,
|
|
|
CreateAt: time.Now(),
|
|
|
UpdateAt: time.Now(),
|
|
|
Status: protocol.BulletinUnRelease,
|
|
|
Status: status,
|
|
|
}
|
|
|
|
|
|
orm := orm2.NewOrm()
|
...
|
...
|
@@ -45,6 +53,7 @@ func BulletinRelease(uid, companyId int64, request *protocol.BulletinReleaseRequ |
|
|
if id, err = orm.Insert(bulletin); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
orm.Rollback()
|
|
|
return
|
|
|
}
|
|
|
if request.QuestionSwitch == 1 {
|
|
|
if len(request.Question.Content) == 0 {
|
...
|
...
|
@@ -72,14 +81,81 @@ func BulletinRelease(uid, companyId int64, request *protocol.BulletinReleaseRequ |
|
|
if _, err = orm.Insert(bulletinQuestion); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
orm.Rollback()
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
//TODO:发送公告消息
|
|
|
if request.IsPublish == 1 {
|
|
|
if err = sendBulletinUserMsg(orm, request.Receiver, companyId, int64(id), bulletin.Title); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
orm.Rollback()
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
orm.Commit()
|
|
|
rsp = &protocol.BulletinReleaseResponse{}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
func getBulletinReceiverIds(orm orm2.Ormer, receivers []protocol.VisibleObject, companyId int64, sourceId int64, message string) (ids []int64, err error) {
|
|
|
var (
|
|
|
//ids []int64
|
|
|
obj protocol.VisibleObject
|
|
|
id int64
|
|
|
did []string
|
|
|
)
|
|
|
sql1 := `
|
|
|
select id from user_company where company_id=? and id in (
|
|
|
select user_company_id from user_department where company_id=? and department_id in (%v) and enable=1
|
|
|
)
|
|
|
`
|
|
|
for i := range receivers {
|
|
|
obj = receivers[i]
|
|
|
id, _ = strconv.ParseInt(obj.Id, 10, 64)
|
|
|
if obj.Type == models.VisibleObject_Department {
|
|
|
did = append(did, obj.Id)
|
|
|
}
|
|
|
if obj.Type == models.VisibleObject_User {
|
|
|
id, _ = strconv.ParseInt(obj.Id, 10, 64)
|
|
|
ids = append(ids, id)
|
|
|
log.Debug("sendBulletinUserMsg: receiver_id:%v", id)
|
|
|
continue
|
|
|
}
|
|
|
}
|
|
|
var userCompanyIds []int64
|
|
|
if err = utils.ExecuteQueryAllWithOrmer(orm, &userCompanyIds, fmt.Sprintf(sql1, strings.Join(did, ",")), companyId, companyId); err != nil && err != orm2.ErrNoRows {
|
|
|
return
|
|
|
}
|
|
|
if len(userCompanyIds) > 0 {
|
|
|
ids = append(ids, userCompanyIds...)
|
|
|
log.Debug("sendBulletinUserMsg: department_id:%v receiver_ids:%v", did, userCompanyIds)
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//发送公告消息
|
|
|
func sendBulletinUserMsg(orm orm2.Ormer, receivers []protocol.VisibleObject, companyId int64, sourceId int64, message string) (err error) {
|
|
|
var (
|
|
|
ids []int64
|
|
|
sended = make(map[int64]int64)
|
|
|
)
|
|
|
if ids, err = getBulletinReceiverIds(orm, receivers, companyId, sourceId, message); err != nil {
|
|
|
return
|
|
|
}
|
|
|
for i := range ids {
|
|
|
if _, ok := sended[ids[i]]; ok {
|
|
|
continue
|
|
|
}
|
|
|
sended[ids[i]] = ids[i]
|
|
|
msg := models.NewUserMsg(companyId, ids[i], models.MsgTypeBulletin, models.SourceTypeBulletin, sourceId, message)
|
|
|
if _, err = orm.Insert(msg); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//公告列表
|
|
|
func BulletinList(uid, companyId int64, request *protocol.BulletinListRequest) (rsp *protocol.BulletinListResponse, err error) {
|
|
|
var (
|
...
|
...
|
@@ -195,7 +271,7 @@ func GetBulletin(id int, companyId int64, request *protocol.GetBulletinRequest) |
|
|
return
|
|
|
}
|
|
|
}
|
|
|
if rsp.Receiver, err = getUsersName(bulletin.Receiver); err != nil {
|
|
|
if rsp.Receiver, err = agg.GetVisibleObject(bulletin.Receiver); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return
|
|
|
}
|
...
|
...
|
@@ -266,3 +342,58 @@ func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (r |
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//操作公告
|
|
|
func OperateBulletin(companyId int64, request *protocol.OperateBulletinRequest) (rsp *protocol.OperateBulletinResponse, err error) {
|
|
|
var (
|
|
|
bulletin *models.Bulletin
|
|
|
receiver []protocol.VisibleObject
|
|
|
orm = orm2.NewOrm()
|
|
|
status int
|
|
|
)
|
|
|
if bulletin, err = models.GetBulletinById(request.Id); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return
|
|
|
}
|
|
|
if bulletin.CompanyId != companyId {
|
|
|
err = protocol.NewErrWithMessage("10027")
|
|
|
log.Error("company_id:%v want:%v", companyId, bulletin.CompanyId)
|
|
|
return
|
|
|
}
|
|
|
if int(bulletin.Status) == request.CmdType {
|
|
|
log.Warn("公告状态已经是:%v 无需操作", request.CmdType)
|
|
|
return
|
|
|
}
|
|
|
if len(bulletin.Receiver) == 0 {
|
|
|
return
|
|
|
}
|
|
|
if err = json.Unmarshal([]byte(bulletin.Receiver), &receiver); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return
|
|
|
}
|
|
|
orm.Begin()
|
|
|
if request.CmdType == protocol.BulletinUnRelease { //下架
|
|
|
status = protocol.BulletinUnRelease
|
|
|
if err = utils.ExecuteSQLWithOrmer(orm, models.DeleteUserMsgSql, companyId, models.MsgTypeBulletin, bulletin.Id); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
orm.Rollback()
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
if request.CmdType == protocol.BulletinRelease { //上架
|
|
|
status = protocol.BulletinRelease
|
|
|
if err = sendBulletinUserMsg(orm, receiver, companyId, int64(bulletin.Id), bulletin.Title); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
orm.Rollback()
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
if err = utils.UpdateTableByMap(bulletin, map[string]interface{}{"Status": int8(status)}); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
orm.Rollback()
|
|
|
return
|
|
|
}
|
|
|
orm.Commit()
|
|
|
rsp = &protocol.OperateBulletinResponse{}
|
|
|
return
|
|
|
} |
...
|
...
|
|