作者 唐旭辉

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

@@ -123,3 +123,29 @@ func (this *BulletinController) UpdateBulletin() { @@ -123,3 +123,29 @@ func (this *BulletinController) UpdateBulletin() {
123 rsp, err := bulletin.UpdateBulletin(companyId, request) 123 rsp, err := bulletin.UpdateBulletin(companyId, request)
124 msg = protocol.NewReturnResponse(rsp, err) 124 msg = protocol.NewReturnResponse(rsp, err)
125 } 125 }
  126 +
  127 +//OperateBulletin
  128 +//@router /operateBulletin [post]
  129 +func (this *BulletinController) OperateBulletin() {
  130 + var msg *protocol.ResponseMessage
  131 + defer func() {
  132 + this.ResposeJson(msg)
  133 + }()
  134 + var request *protocol.OperateBulletinRequest
  135 + if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
  136 + log.Error("json 解析失败", err)
  137 + msg = protocol.BadRequestParam("1")
  138 + return
  139 + }
  140 + if b, m := this.Valid(request); !b {
  141 + msg = m
  142 + return
  143 + }
  144 + companyId := this.GetCompanyId()
  145 + if companyId <= 0 {
  146 + msg = protocol.BadRequestParam("1")
  147 + return
  148 + }
  149 + rsp, err := bulletin.OperateBulletin(companyId, request)
  150 + msg = protocol.NewReturnResponse(rsp, err)
  151 +}
@@ -22,7 +22,7 @@ type Bulletin struct { @@ -22,7 +22,7 @@ type Bulletin struct {
22 AllowClose int8 `orm:"column(allow_close);null" description:"允许关闭公告(0允许关闭窗口,1阅读完才能关闭,2选项打勾后才能关闭)"` 22 AllowClose int8 `orm:"column(allow_close);null" description:"允许关闭公告(0允许关闭窗口,1阅读完才能关闭,2选项打勾后才能关闭)"`
23 //AllowCondition int8 `orm:"column(allow_condition);null" description:"关闭条件 (1(bit 0):公告内容查看完 2(bit 1):回答完问题)"` 23 //AllowCondition int8 `orm:"column(allow_condition);null" description:"关闭条件 (1(bit 0):公告内容查看完 2(bit 1):回答完问题)"`
24 CompanyId int64 `orm:"column(company_id);null" description:"公司Id"` 24 CompanyId int64 `orm:"column(company_id);null" description:"公司Id"`
25 - Status uint8 `orm:"column(status)" description:"状态 1-下架 2-上架"` 25 + Status int8 `orm:"column(status)" description:"状态 1-下架 2-上架"`
26 } 26 }
27 27
28 func (t *Bulletin) TableName() string { 28 func (t *Bulletin) TableName() string {
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "oppmg/utils"
  6 + "time"
  7 +
  8 + "github.com/astaxie/beego/orm"
  9 +)
  10 +
  11 +type UserMsg struct {
  12 + Id int64 `orm:"column(id);pk" description:"消息表id"`
  13 + CompanyId int64 `orm:"column(company_id)" description:"公司编号"`
  14 + ReceiveUserId int64 `orm:"column(receive_user_id)" description:"接收用户id"`
  15 + MsgType int `orm:"column(msg_type)" description:"消息类型 1.公司公告 2.表彰通知 4.互动消息 8.机会审核"`
  16 + Message string `orm:"column(message)" description:"消息内容"`
  17 + SourceType int `orm:"column(source_type)" description:"来源类型 1:机会 2:评论 "`
  18 + SourceId int64 `orm:"column(source_id)" description:"来源id (机会编号 /评论编号)"`
  19 + IsPublic int8 `orm:"column(is_public)" description:"1:公开 0:不公开"`
  20 + IsRead int8 `orm:"column(is_read)" description:"1:已读 0:未读"`
  21 + CreateAt time.Time `orm:"column(create_at);type(timestamp);auto_now" description:"创建时间"`
  22 +}
  23 +
  24 +const (
  25 + MsgTypeBulletin = 1 //公告
  26 + MsgTypeCommend = 2 //表彰
  27 + MsgTypeInteraction = 4 //互动消息
  28 + MsgTypeAudit = 8 //机会审核
  29 +)
  30 +const (
  31 + SourceTypeChance = 1
  32 + SourceTypeComment = 2
  33 + SourceTypeBulletin = 3
  34 +)
  35 +
  36 +const (
  37 + DeleteUserMsgSql = "delete from user_msg where company_id=? and msg_type=? and source_id=?"
  38 +)
  39 +
  40 +func (t *UserMsg) TableName() string {
  41 + return "user_msg"
  42 +}
  43 +
  44 +func init() {
  45 + orm.RegisterModel(new(UserMsg))
  46 +}
  47 +
  48 +// AddUserMsg insert a new UserMsg into database and returns
  49 +// last inserted Id on success.
  50 +func AddUserMsg(m *UserMsg) (id int64, err error) {
  51 + o := orm.NewOrm()
  52 + id, err = o.Insert(m)
  53 + return
  54 +}
  55 +
  56 +// GetUserMsgById retrieves UserMsg by Id. Returns error if
  57 +// Id doesn't exist
  58 +func GetUserMsgById(id int64) (v *UserMsg, err error) {
  59 + o := orm.NewOrm()
  60 + v = &UserMsg{Id: id}
  61 + if err = o.Read(v); err == nil {
  62 + return v, nil
  63 + }
  64 + return nil, err
  65 +}
  66 +
  67 +// UpdateUserMsg updates UserMsg by Id and returns error if
  68 +// the record to be updated doesn't exist
  69 +func UpdateUserMsgById(m *UserMsg) (err error) {
  70 + o := orm.NewOrm()
  71 + v := UserMsg{Id: m.Id}
  72 + // ascertain id exists in the database
  73 + if err = o.Read(&v); err == nil {
  74 + var num int64
  75 + if num, err = o.Update(m); err == nil {
  76 + fmt.Println("Number of records updated in database:", num)
  77 + }
  78 + }
  79 + return
  80 +}
  81 +
  82 +// DeleteUserMsg deletes UserMsg by Id and returns error if
  83 +// the record to be deleted doesn't exist
  84 +func DeleteUserMsg(id int64) (err error) {
  85 + o := orm.NewOrm()
  86 + v := UserMsg{Id: id}
  87 + // ascertain id exists in the database
  88 + if err = o.Read(&v); err == nil {
  89 + var num int64
  90 + if num, err = o.Delete(&UserMsg{Id: id}); err == nil {
  91 + fmt.Println("Number of records deleted in database:", num)
  92 + }
  93 + }
  94 + return
  95 +}
  96 +
  97 +func NewUserMsg(companId, userCompanyId int64, msgType int, sourceType int, sourceId int64, message string) *UserMsg {
  98 + return &UserMsg{
  99 + Id: utils.GenerateIDBySonyflake(),
  100 + CompanyId: companId,
  101 + ReceiveUserId: userCompanyId,
  102 + MsgType: msgType,
  103 + SourceType: sourceType,
  104 + SourceId: sourceId,
  105 + Message: message,
  106 + CreateAt: time.Now(),
  107 + }
  108 +}
@@ -18,10 +18,11 @@ type BulletinReleaseRequest struct { @@ -18,10 +18,11 @@ type BulletinReleaseRequest struct {
18 Content string `json:"content" valid:"Required"` 18 Content string `json:"content" valid:"Required"`
19 AllowClose int `json:"allow_close"` 19 AllowClose int `json:"allow_close"`
20 //AllowCondition int `json:"allow_condition"` 20 //AllowCondition int `json:"allow_condition"`
21 - QuestionSwitch int `json:"question_switch"`  
22 - Receiver []string `json:"receiver" valid:"Required"`  
23 - Question Question `json:"question"`  
24 - Cover Cover `json:"cover" valid:"Required"` 21 + QuestionSwitch int `json:"question_switch"`
  22 + Receiver []VisibleObject `json:"receiver" valid:"Required"`
  23 + Question Question `json:"question"`
  24 + Cover Cover `json:"cover" valid:"Required"`
  25 + IsPublish int `json:"is_publish"` //是否直接发布 0:否 1:直接发布
25 } 26 }
26 type Question struct { 27 type Question struct {
27 Id int `json:"id"` 28 Id int `json:"id"`
@@ -72,10 +73,10 @@ type GetBulletinResponse struct { @@ -72,10 +73,10 @@ type GetBulletinResponse struct {
72 Content string `json:"content" valid:"Required"` 73 Content string `json:"content" valid:"Required"`
73 AllowClose int `json:"allow_close"` 74 AllowClose int `json:"allow_close"`
74 //AllowCondition int `json:"allow_condition"` 75 //AllowCondition int `json:"allow_condition"`
75 - QuestionSwitch int `json:"question_switch"`  
76 - Receiver []Receiver `json:"receiver" valid:"Required"`  
77 - Question Question `json:"question"`  
78 - Cover Cover `json:"cover" valid:"Required"` 76 + QuestionSwitch int `json:"question_switch"`
  77 + Receiver []VisibleObject `json:"receiver" valid:"Required"`
  78 + Question Question `json:"question"`
  79 + Cover Cover `json:"cover" valid:"Required"`
79 } 80 }
80 81
81 type Receiver struct { 82 type Receiver struct {
@@ -98,3 +99,11 @@ type UpdateBulletinRequest struct { @@ -98,3 +99,11 @@ type UpdateBulletinRequest struct {
98 } 99 }
99 type UpdateBulletinResponse struct { 100 type UpdateBulletinResponse struct {
100 } 101 }
  102 +
  103 +/*OperateBulletin */
  104 +type OperateBulletinRequest struct {
  105 + Id int `json:"id" valid:"Required"`
  106 + CmdType int `json:"cmd_type" valid:"Required"` //1:下架 2.上架
  107 +}
  108 +type OperateBulletinResponse struct {
  109 +}
@@ -50,6 +50,7 @@ func init() { @@ -50,6 +50,7 @@ func init() {
50 beego.NSRouter("/list", &controllers.BulletinController{}, "post:BulletinList"), 50 beego.NSRouter("/list", &controllers.BulletinController{}, "post:BulletinList"),
51 beego.NSRouter("/get", &controllers.BulletinController{}, "post:GetBulletin"), 51 beego.NSRouter("/get", &controllers.BulletinController{}, "post:GetBulletin"),
52 beego.NSRouter("/update", &controllers.BulletinController{}, "post:UpdateBulletin"), 52 beego.NSRouter("/update", &controllers.BulletinController{}, "post:UpdateBulletin"),
  53 + beego.NSRouter("/operate", &controllers.BulletinController{}, "post:OperateBulletin"),
53 ), 54 ),
54 beego.NSNamespace("/common", 55 beego.NSNamespace("/common",
55 beego.NSRouter("/department", &controllers.CommonController{}, "post:SelectorDepartment"), 56 beego.NSRouter("/department", &controllers.CommonController{}, "post:SelectorDepartment"),
@@ -2,11 +2,15 @@ package bulletin @@ -2,11 +2,15 @@ package bulletin
2 2
3 import ( 3 import (
4 "encoding/json" 4 "encoding/json"
  5 + "fmt"
5 orm2 "github.com/astaxie/beego/orm" 6 orm2 "github.com/astaxie/beego/orm"
6 "oppmg/common/log" 7 "oppmg/common/log"
7 "oppmg/models" 8 "oppmg/models"
8 "oppmg/protocol" 9 "oppmg/protocol"
  10 + "oppmg/services/agg"
  11 + "oppmg/utils"
9 "strconv" 12 "strconv"
  13 + "strings"
10 "time" 14 "time"
11 ) 15 )
12 16
@@ -17,7 +21,11 @@ func BulletinRelease(uid, companyId int64, request *protocol.BulletinReleaseRequ @@ -17,7 +21,11 @@ func BulletinRelease(uid, companyId int64, request *protocol.BulletinReleaseRequ
17 bulletinQuestion *models.BulletinQuestion 21 bulletinQuestion *models.BulletinQuestion
18 receiver, questionContent []byte 22 receiver, questionContent []byte
19 id int64 23 id int64
  24 + status int8 = protocol.BulletinUnRelease
20 ) 25 )
  26 + if request.IsPublish == 1 {
  27 + status = protocol.BulletinRelease
  28 + }
21 //TODO:check role_menu 29 //TODO:check role_menu
22 if receiver, err = json.Marshal(request.Receiver); err != nil { 30 if receiver, err = json.Marshal(request.Receiver); err != nil {
23 log.Error(err.Error()) 31 log.Error(err.Error())
@@ -37,7 +45,7 @@ func BulletinRelease(uid, companyId int64, request *protocol.BulletinReleaseRequ @@ -37,7 +45,7 @@ func BulletinRelease(uid, companyId int64, request *protocol.BulletinReleaseRequ
37 CompanyId: companyId, 45 CompanyId: companyId,
38 CreateAt: time.Now(), 46 CreateAt: time.Now(),
39 UpdateAt: time.Now(), 47 UpdateAt: time.Now(),
40 - Status: protocol.BulletinUnRelease, 48 + Status: status,
41 } 49 }
42 50
43 orm := orm2.NewOrm() 51 orm := orm2.NewOrm()
@@ -45,6 +53,7 @@ func BulletinRelease(uid, companyId int64, request *protocol.BulletinReleaseRequ @@ -45,6 +53,7 @@ func BulletinRelease(uid, companyId int64, request *protocol.BulletinReleaseRequ
45 if id, err = orm.Insert(bulletin); err != nil { 53 if id, err = orm.Insert(bulletin); err != nil {
46 log.Error(err.Error()) 54 log.Error(err.Error())
47 orm.Rollback() 55 orm.Rollback()
  56 + return
48 } 57 }
49 if request.QuestionSwitch == 1 { 58 if request.QuestionSwitch == 1 {
50 if len(request.Question.Content) == 0 { 59 if len(request.Question.Content) == 0 {
@@ -72,14 +81,81 @@ func BulletinRelease(uid, companyId int64, request *protocol.BulletinReleaseRequ @@ -72,14 +81,81 @@ func BulletinRelease(uid, companyId int64, request *protocol.BulletinReleaseRequ
72 if _, err = orm.Insert(bulletinQuestion); err != nil { 81 if _, err = orm.Insert(bulletinQuestion); err != nil {
73 log.Error(err.Error()) 82 log.Error(err.Error())
74 orm.Rollback() 83 orm.Rollback()
  84 + return
75 } 85 }
76 } 86 }
77 //TODO:发送公告消息 87 //TODO:发送公告消息
  88 + if request.IsPublish == 1 {
  89 + if err = sendBulletinUserMsg(orm, request.Receiver, companyId, int64(id), bulletin.Title); err != nil {
  90 + log.Error(err.Error())
  91 + orm.Rollback()
  92 + return
  93 + }
  94 + }
78 orm.Commit() 95 orm.Commit()
79 rsp = &protocol.BulletinReleaseResponse{} 96 rsp = &protocol.BulletinReleaseResponse{}
80 return 97 return
81 } 98 }
82 99
  100 +func getBulletinReceiverIds(orm orm2.Ormer, receivers []protocol.VisibleObject, companyId int64, sourceId int64, message string) (ids []int64, err error) {
  101 + var (
  102 + //ids []int64
  103 + obj protocol.VisibleObject
  104 + id int64
  105 + did []string
  106 + )
  107 + sql1 := `
  108 + select id from user_company where company_id=? and id in (
  109 + select user_company_id from user_department where company_id=? and department_id in (%v) and enable=1
  110 + )
  111 + `
  112 + for i := range receivers {
  113 + obj = receivers[i]
  114 + id, _ = strconv.ParseInt(obj.Id, 10, 64)
  115 + if obj.Type == models.VisibleObject_Department {
  116 + did = append(did, obj.Id)
  117 + }
  118 + if obj.Type == models.VisibleObject_User {
  119 + id, _ = strconv.ParseInt(obj.Id, 10, 64)
  120 + ids = append(ids, id)
  121 + log.Debug("sendBulletinUserMsg: receiver_id:%v", id)
  122 + continue
  123 + }
  124 + }
  125 + var userCompanyIds []int64
  126 + if err = utils.ExecuteQueryAllWithOrmer(orm, &userCompanyIds, fmt.Sprintf(sql1, strings.Join(did, ",")), companyId, companyId); err != nil && err != orm2.ErrNoRows {
  127 + return
  128 + }
  129 + if len(userCompanyIds) > 0 {
  130 + ids = append(ids, userCompanyIds...)
  131 + log.Debug("sendBulletinUserMsg: department_id:%v receiver_ids:%v", did, userCompanyIds)
  132 + }
  133 + return
  134 +}
  135 +
  136 +//发送公告消息
  137 +func sendBulletinUserMsg(orm orm2.Ormer, receivers []protocol.VisibleObject, companyId int64, sourceId int64, message string) (err error) {
  138 + var (
  139 + ids []int64
  140 + sended = make(map[int64]int64)
  141 + )
  142 + if ids, err = getBulletinReceiverIds(orm, receivers, companyId, sourceId, message); err != nil {
  143 + return
  144 + }
  145 + for i := range ids {
  146 + if _, ok := sended[ids[i]]; ok {
  147 + continue
  148 + }
  149 + sended[ids[i]] = ids[i]
  150 + msg := models.NewUserMsg(companyId, ids[i], models.MsgTypeBulletin, models.SourceTypeBulletin, sourceId, message)
  151 + if _, err = orm.Insert(msg); err != nil {
  152 + log.Error(err.Error())
  153 + return
  154 + }
  155 + }
  156 + return
  157 +}
  158 +
83 //公告列表 159 //公告列表
84 func BulletinList(uid, companyId int64, request *protocol.BulletinListRequest) (rsp *protocol.BulletinListResponse, err error) { 160 func BulletinList(uid, companyId int64, request *protocol.BulletinListRequest) (rsp *protocol.BulletinListResponse, err error) {
85 var ( 161 var (
@@ -195,7 +271,7 @@ func GetBulletin(id int, companyId int64, request *protocol.GetBulletinRequest) @@ -195,7 +271,7 @@ func GetBulletin(id int, companyId int64, request *protocol.GetBulletinRequest)
195 return 271 return
196 } 272 }
197 } 273 }
198 - if rsp.Receiver, err = getUsersName(bulletin.Receiver); err != nil { 274 + if rsp.Receiver, err = agg.GetVisibleObject(bulletin.Receiver); err != nil {
199 log.Error(err.Error()) 275 log.Error(err.Error())
200 return 276 return
201 } 277 }
@@ -266,3 +342,58 @@ func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (r @@ -266,3 +342,58 @@ func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (r
266 } 342 }
267 return 343 return
268 } 344 }
  345 +
  346 +//操作公告
  347 +func OperateBulletin(companyId int64, request *protocol.OperateBulletinRequest) (rsp *protocol.OperateBulletinResponse, err error) {
  348 + var (
  349 + bulletin *models.Bulletin
  350 + receiver []protocol.VisibleObject
  351 + orm = orm2.NewOrm()
  352 + status int
  353 + )
  354 + if bulletin, err = models.GetBulletinById(request.Id); err != nil {
  355 + log.Error(err.Error())
  356 + return
  357 + }
  358 + if bulletin.CompanyId != companyId {
  359 + err = protocol.NewErrWithMessage("10027")
  360 + log.Error("company_id:%v want:%v", companyId, bulletin.CompanyId)
  361 + return
  362 + }
  363 + if int(bulletin.Status) == request.CmdType {
  364 + log.Warn("公告状态已经是:%v 无需操作", request.CmdType)
  365 + return
  366 + }
  367 + if len(bulletin.Receiver) == 0 {
  368 + return
  369 + }
  370 + if err = json.Unmarshal([]byte(bulletin.Receiver), &receiver); err != nil {
  371 + log.Error(err.Error())
  372 + return
  373 + }
  374 + orm.Begin()
  375 + if request.CmdType == protocol.BulletinUnRelease { //下架
  376 + status = protocol.BulletinUnRelease
  377 + if err = utils.ExecuteSQLWithOrmer(orm, models.DeleteUserMsgSql, companyId, models.MsgTypeBulletin, bulletin.Id); err != nil {
  378 + log.Error(err.Error())
  379 + orm.Rollback()
  380 + return
  381 + }
  382 + }
  383 + if request.CmdType == protocol.BulletinRelease { //上架
  384 + status = protocol.BulletinRelease
  385 + if err = sendBulletinUserMsg(orm, receiver, companyId, int64(bulletin.Id), bulletin.Title); err != nil {
  386 + log.Error(err.Error())
  387 + orm.Rollback()
  388 + return
  389 + }
  390 + }
  391 + if err = utils.UpdateTableByMap(bulletin, map[string]interface{}{"Status": int8(status)}); err != nil {
  392 + log.Error(err.Error())
  393 + orm.Rollback()
  394 + return
  395 + }
  396 + orm.Commit()
  397 + rsp = &protocol.OperateBulletinResponse{}
  398 + return
  399 +}