作者 唐旭辉

Merge branch 'dev' of http://gitlab.fjmaimaimai.com/mmm-go/oppmg into dev

... ... @@ -123,3 +123,29 @@ func (this *BulletinController) UpdateBulletin() {
rsp, err := bulletin.UpdateBulletin(companyId, request)
msg = protocol.NewReturnResponse(rsp, err)
}
//OperateBulletin
//@router /operateBulletin [post]
func (this *BulletinController) OperateBulletin() {
var msg *protocol.ResponseMessage
defer func() {
this.ResposeJson(msg)
}()
var request *protocol.OperateBulletinRequest
if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
log.Error("json 解析失败", err)
msg = protocol.BadRequestParam("1")
return
}
if b, m := this.Valid(request); !b {
msg = m
return
}
companyId := this.GetCompanyId()
if companyId <= 0 {
msg = protocol.BadRequestParam("1")
return
}
rsp, err := bulletin.OperateBulletin(companyId, request)
msg = protocol.NewReturnResponse(rsp, err)
}
... ...
... ... @@ -22,7 +22,7 @@ type Bulletin struct {
AllowClose int8 `orm:"column(allow_close);null" description:"允许关闭公告(0允许关闭窗口,1阅读完才能关闭,2选项打勾后才能关闭)"`
//AllowCondition int8 `orm:"column(allow_condition);null" description:"关闭条件 (1(bit 0):公告内容查看完 2(bit 1):回答完问题)"`
CompanyId int64 `orm:"column(company_id);null" description:"公司Id"`
Status uint8 `orm:"column(status)" description:"状态 1-下架 2-上架"`
Status int8 `orm:"column(status)" description:"状态 1-下架 2-上架"`
}
func (t *Bulletin) TableName() string {
... ...
package models
import (
"fmt"
"oppmg/utils"
"time"
"github.com/astaxie/beego/orm"
)
type UserMsg struct {
Id int64 `orm:"column(id);pk" description:"消息表id"`
CompanyId int64 `orm:"column(company_id)" description:"公司编号"`
ReceiveUserId int64 `orm:"column(receive_user_id)" description:"接收用户id"`
MsgType int `orm:"column(msg_type)" description:"消息类型 1.公司公告 2.表彰通知 4.互动消息 8.机会审核"`
Message string `orm:"column(message)" description:"消息内容"`
SourceType int `orm:"column(source_type)" description:"来源类型 1:机会 2:评论 "`
SourceId int64 `orm:"column(source_id)" description:"来源id (机会编号 /评论编号)"`
IsPublic int8 `orm:"column(is_public)" description:"1:公开 0:不公开"`
IsRead int8 `orm:"column(is_read)" description:"1:已读 0:未读"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);auto_now" description:"创建时间"`
}
const (
MsgTypeBulletin = 1 //公告
MsgTypeCommend = 2 //表彰
MsgTypeInteraction = 4 //互动消息
MsgTypeAudit = 8 //机会审核
)
const (
SourceTypeChance = 1
SourceTypeComment = 2
SourceTypeBulletin = 3
)
const (
DeleteUserMsgSql = "delete from user_msg where company_id=? and msg_type=? and source_id=?"
)
func (t *UserMsg) TableName() string {
return "user_msg"
}
func init() {
orm.RegisterModel(new(UserMsg))
}
// AddUserMsg insert a new UserMsg into database and returns
// last inserted Id on success.
func AddUserMsg(m *UserMsg) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetUserMsgById retrieves UserMsg by Id. Returns error if
// Id doesn't exist
func GetUserMsgById(id int64) (v *UserMsg, err error) {
o := orm.NewOrm()
v = &UserMsg{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// UpdateUserMsg updates UserMsg by Id and returns error if
// the record to be updated doesn't exist
func UpdateUserMsgById(m *UserMsg) (err error) {
o := orm.NewOrm()
v := UserMsg{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num)
}
}
return
}
// DeleteUserMsg deletes UserMsg by Id and returns error if
// the record to be deleted doesn't exist
func DeleteUserMsg(id int64) (err error) {
o := orm.NewOrm()
v := UserMsg{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&UserMsg{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
func NewUserMsg(companId, userCompanyId int64, msgType int, sourceType int, sourceId int64, message string) *UserMsg {
return &UserMsg{
Id: utils.GenerateIDBySonyflake(),
CompanyId: companId,
ReceiveUserId: userCompanyId,
MsgType: msgType,
SourceType: sourceType,
SourceId: sourceId,
Message: message,
CreateAt: time.Now(),
}
}
... ...
... ... @@ -18,10 +18,11 @@ type BulletinReleaseRequest struct {
Content string `json:"content" valid:"Required"`
AllowClose int `json:"allow_close"`
//AllowCondition int `json:"allow_condition"`
QuestionSwitch int `json:"question_switch"`
Receiver []string `json:"receiver" valid:"Required"`
Question Question `json:"question"`
Cover Cover `json:"cover" valid:"Required"`
QuestionSwitch int `json:"question_switch"`
Receiver []VisibleObject `json:"receiver" valid:"Required"`
Question Question `json:"question"`
Cover Cover `json:"cover" valid:"Required"`
IsPublish int `json:"is_publish"` //是否直接发布 0:否 1:直接发布
}
type Question struct {
Id int `json:"id"`
... ... @@ -72,10 +73,10 @@ type GetBulletinResponse struct {
Content string `json:"content" valid:"Required"`
AllowClose int `json:"allow_close"`
//AllowCondition int `json:"allow_condition"`
QuestionSwitch int `json:"question_switch"`
Receiver []Receiver `json:"receiver" valid:"Required"`
Question Question `json:"question"`
Cover Cover `json:"cover" valid:"Required"`
QuestionSwitch int `json:"question_switch"`
Receiver []VisibleObject `json:"receiver" valid:"Required"`
Question Question `json:"question"`
Cover Cover `json:"cover" valid:"Required"`
}
type Receiver struct {
... ... @@ -98,3 +99,11 @@ type UpdateBulletinRequest struct {
}
type UpdateBulletinResponse struct {
}
/*OperateBulletin */
type OperateBulletinRequest struct {
Id int `json:"id" valid:"Required"`
CmdType int `json:"cmd_type" valid:"Required"` //1:下架 2.上架
}
type OperateBulletinResponse struct {
}
... ...
... ... @@ -50,6 +50,7 @@ func init() {
beego.NSRouter("/list", &controllers.BulletinController{}, "post:BulletinList"),
beego.NSRouter("/get", &controllers.BulletinController{}, "post:GetBulletin"),
beego.NSRouter("/update", &controllers.BulletinController{}, "post:UpdateBulletin"),
beego.NSRouter("/operate", &controllers.BulletinController{}, "post:OperateBulletin"),
),
beego.NSNamespace("/common",
beego.NSRouter("/department", &controllers.CommonController{}, "post:SelectorDepartment"),
... ...
... ... @@ -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
}
... ...