作者 yangfu

公告修改

@@ -19,7 +19,7 @@ mysql_db_name = "${MYSQL_DB_NAME||opportunity_dev}" @@ -19,7 +19,7 @@ mysql_db_name = "${MYSQL_DB_NAME||opportunity_dev}"
19 ##redis相关配置 19 ##redis相关配置
20 redis_add = "${REDIS_HOST||127.0.0.1}" 20 redis_add = "${REDIS_HOST||127.0.0.1}"
21 redis_add_port = "${REDIS_PORT||6379}" 21 redis_add_port = "${REDIS_PORT||6379}"
22 -redis_auth = "" 22 +redis_auth = "123456"
23 ##log相关配置 23 ##log相关配置
24 ##out_put:"console","file" 24 ##out_put:"console","file"
25 log_output = "file" 25 log_output = "file"
@@ -56,8 +56,8 @@ func GetBulletinById(id int) (v *Bulletin, err error) { @@ -56,8 +56,8 @@ func GetBulletinById(id int) (v *Bulletin, err error) {
56 56
57 // UpdateBulletin updates Bulletin by Id and returns error if 57 // UpdateBulletin updates Bulletin by Id and returns error if
58 // the record to be updated doesn't exist 58 // the record to be updated doesn't exist
59 -func UpdateBulletinById(m *Bulletin) (err error) {  
60 - o := orm.NewOrm() 59 +func UpdateBulletinById(o orm.Ormer, m *Bulletin) (err error) {
  60 + //o := orm.NewOrm()
61 v := Bulletin{Id: m.Id} 61 v := Bulletin{Id: m.Id}
62 // ascertain id exists in the database 62 // ascertain id exists in the database
63 if err = o.Read(&v); err == nil { 63 if err = o.Read(&v); err == nil {
@@ -46,8 +46,8 @@ func GetBulletinQuestionById(id int) (v *BulletinQuestion, err error) { @@ -46,8 +46,8 @@ func GetBulletinQuestionById(id int) (v *BulletinQuestion, err error) {
46 46
47 // UpdateBulletinQuestion updates BulletinQuestion by Id and returns error if 47 // UpdateBulletinQuestion updates BulletinQuestion by Id and returns error if
48 // the record to be updated doesn't exist 48 // the record to be updated doesn't exist
49 -func UpdateBulletinQuestionById(m *BulletinQuestion) (err error) {  
50 - o := orm.NewOrm() 49 +func UpdateBulletinQuestionById(o orm.Ormer, m *BulletinQuestion) (err error) {
  50 + //o := orm.NewOrm()
51 v := BulletinQuestion{Id: m.Id} 51 v := BulletinQuestion{Id: m.Id}
52 // ascertain id exists in the database 52 // ascertain id exists in the database
53 if err = o.Read(&v); err == nil { 53 if err = o.Read(&v); err == nil {
@@ -33,8 +33,15 @@ const ( @@ -33,8 +33,15 @@ const (
33 SourceTypeBulletin = 3 33 SourceTypeBulletin = 3
34 ) 34 )
35 35
  36 +//列表是否展示(公告列表)
  37 +const (
  38 + Public = 1 //公开
  39 + NoPublic = 0 //不公开
  40 +)
  41 +
36 const ( 42 const (
37 SqlDeleteUserMsg = "delete from user_msg where company_id=? and msg_type=? and source_id=?" 43 SqlDeleteUserMsg = "delete from user_msg where company_id=? and msg_type=? and source_id=?"
  44 + SqlUpdateUserMsgPublic = "update user_msg set is_public=? where company_id=? and msg_type=? and source_id=?"
38 ) 45 )
39 46
40 func (t *UserMsg) TableName() string { 47 func (t *UserMsg) TableName() string {
@@ -94,6 +101,17 @@ func DeleteUserMsg(id int64) (err error) { @@ -94,6 +101,17 @@ func DeleteUserMsg(id int64) (err error) {
94 return 101 return
95 } 102 }
96 103
  104 +//获取userMsg
  105 +func GetUserMsgBy(userCompanyId int64, msgType int, sourceType int, sourceId int64) (v *UserMsg, err error) {
  106 + sql := "select * from user_msg where receive_user_id=? and msg_type=? and source_type=? and source_id=?"
  107 + o := orm.NewOrm()
  108 + if err = o.Raw(sql, userCompanyId, msgType, sourceType, sourceId).QueryRow(&v); err != nil {
  109 + //log.Error(err.Error())
  110 + return
  111 + }
  112 + return
  113 +}
  114 +
97 func NewUserMsg(companId, userCompanyId int64, msgType int, sourceType int, sourceId int64, message string) *UserMsg { 115 func NewUserMsg(companId, userCompanyId int64, msgType int, sourceType int, sourceId int64, message string) *UserMsg {
98 return &UserMsg{ 116 return &UserMsg{
99 Id: utils.GenerateIDBySonyflake(), 117 Id: utils.GenerateIDBySonyflake(),
@@ -104,5 +122,6 @@ func NewUserMsg(companId, userCompanyId int64, msgType int, sourceType int, sour @@ -104,5 +122,6 @@ func NewUserMsg(companId, userCompanyId int64, msgType int, sourceType int, sour
104 SourceId: sourceId, 122 SourceId: sourceId,
105 Message: message, 123 Message: message,
106 CreateAt: time.Now(), 124 CreateAt: time.Now(),
  125 + IsPublic: Public,
107 } 126 }
108 } 127 }
@@ -169,17 +169,26 @@ func sendBulletinUserMsg(orm orm2.Ormer, receivers []protocol.VisibleObject, com @@ -169,17 +169,26 @@ func sendBulletinUserMsg(orm orm2.Ormer, receivers []protocol.VisibleObject, com
169 return 169 return
170 } 170 }
171 } 171 }
  172 + if err = utils.ExecuteSQLWithOrmer(orm, models.SqlUpdateUserMsgPublic, models.Public, companyId, models.MsgTypeBulletin, sourceId); err != nil {
  173 + log.Error(err.Error())
  174 + orm.Rollback()
  175 + return
  176 + }
172 for i := range ids { 177 for i := range ids {
173 if _, ok := sended[ids[i]]; ok { 178 if _, ok := sended[ids[i]]; ok {
174 continue 179 continue
175 } 180 }
176 sended[ids[i]] = ids[i] 181 sended[ids[i]] = ids[i]
  182 + //这条消息不存在,插入一条消息
  183 + if _, e := models.GetUserMsgBy(ids[i], models.MsgTypeBulletin, models.SourceTypeBulletin, sourceId); e != nil && e == orm2.ErrNoRows {
177 msg := models.NewUserMsg(companyId, ids[i], models.MsgTypeBulletin, models.SourceTypeBulletin, sourceId, message) 184 msg := models.NewUserMsg(companyId, ids[i], models.MsgTypeBulletin, models.SourceTypeBulletin, sourceId, message)
178 if _, err = orm.Insert(msg); err != nil { 185 if _, err = orm.Insert(msg); err != nil {
  186 + orm.Rollback()
179 log.Error(err.Error()) 187 log.Error(err.Error())
180 return 188 return
181 } 189 }
182 } 190 }
  191 + }
183 return 192 return
184 } 193 }
185 194
@@ -345,6 +354,17 @@ func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (r @@ -345,6 +354,17 @@ func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (r
345 log.Error(err.Error()) 354 log.Error(err.Error())
346 return 355 return
347 } 356 }
  357 + orm := orm2.NewOrm()
  358 + orm.Begin()
  359 +
  360 + if request.IsPublish == 1 && bulletin.Status == protocol.BulletinUnRelease {
  361 + if err = sendBulletinUserMsg(orm, request.Receiver, companyId, int64(bulletin.Id), bulletin.Title, request.AllPeo); err != nil {
  362 + log.Error(err.Error())
  363 + orm.Rollback()
  364 + return
  365 + }
  366 + }
  367 +
348 //update 368 //update
349 { 369 {
350 bulletin.Title = request.Title 370 bulletin.Title = request.Title
@@ -360,8 +380,10 @@ func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (r @@ -360,8 +380,10 @@ func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (r
360 if request.IsPublish == 1 && bulletin.Status == protocol.BulletinUnRelease { 380 if request.IsPublish == 1 && bulletin.Status == protocol.BulletinUnRelease {
361 bulletin.Status = protocol.BulletinRelease 381 bulletin.Status = protocol.BulletinRelease
362 } 382 }
363 - if err = models.UpdateBulletinById(bulletin); err != nil { 383 +
  384 + if err = models.UpdateBulletinById(orm, bulletin); err != nil {
364 log.Error(err.Error()) 385 log.Error(err.Error())
  386 + orm.Rollback()
365 return 387 return
366 } 388 }
367 } 389 }
@@ -371,6 +393,7 @@ func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (r @@ -371,6 +393,7 @@ func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (r
371 validQuestion(&request.Question) 393 validQuestion(&request.Question)
372 if err != nil && err != orm2.ErrNoRows { 394 if err != nil && err != orm2.ErrNoRows {
373 log.Error(err.Error()) 395 log.Error(err.Error())
  396 + orm.Rollback()
374 return 397 return
375 } 398 }
376 if err == nil { 399 if err == nil {
@@ -388,8 +411,9 @@ func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (r @@ -388,8 +411,9 @@ func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (r
388 bulletinQuestion.Content = string(questionContent) 411 bulletinQuestion.Content = string(questionContent)
389 bulletinQuestion.Type = int8(request.Question.Type) 412 bulletinQuestion.Type = int8(request.Question.Type)
390 bulletinQuestion.UpdateAt = time.Now() 413 bulletinQuestion.UpdateAt = time.Now()
391 - if err = models.UpdateBulletinQuestionById(bulletinQuestion); err != nil { 414 + if err = models.UpdateBulletinQuestionById(orm, bulletinQuestion); err != nil {
392 log.Error(err.Error()) 415 log.Error(err.Error())
  416 + orm.Rollback()
393 return 417 return
394 } 418 }
395 } 419 }
@@ -403,22 +427,16 @@ func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (r @@ -403,22 +427,16 @@ func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (r
403 CreateAt: time.Now(), 427 CreateAt: time.Now(),
404 UpdateAt: time.Now(), 428 UpdateAt: time.Now(),
405 } 429 }
406 - _, err = models.AddBulletinQuestion(bulletinQuestion) 430 + _, err = orm.Insert(bulletinQuestion)
407 if err != nil { 431 if err != nil {
  432 + orm.Rollback()
408 log.Error("添加问题失败:%s", err) 433 log.Error("添加问题失败:%s", err)
409 return 434 return
410 } 435 }
411 } 436 }
412 437
413 } 438 }
414 - orm := orm2.NewOrm()  
415 - if request.IsPublish == 1 && bulletin.Status == protocol.BulletinUnRelease {  
416 - if err = sendBulletinUserMsg(orm, request.Receiver, companyId, int64(bulletin.Id), bulletin.Title, request.AllPeo); err != nil {  
417 - log.Error(err.Error())  
418 - orm.Rollback()  
419 - return  
420 - }  
421 - } 439 + orm.Commit()
422 return 440 return
423 } 441 }
424 442
@@ -453,7 +471,7 @@ func OperateBulletin(companyId int64, request *protocol.OperateBulletinRequest) @@ -453,7 +471,7 @@ func OperateBulletin(companyId int64, request *protocol.OperateBulletinRequest)
453 orm.Begin() 471 orm.Begin()
454 if request.CmdType == protocol.BulletinUnRelease { //下架 472 if request.CmdType == protocol.BulletinUnRelease { //下架
455 status = protocol.BulletinUnRelease 473 status = protocol.BulletinUnRelease
456 - if err = utils.ExecuteSQLWithOrmer(orm, models.SqlDeleteUserMsg, companyId, models.MsgTypeBulletin, bulletin.Id); err != nil { 474 + if err = utils.ExecuteSQLWithOrmer(orm, models.SqlUpdateUserMsgPublic, models.NoPublic, companyId, models.MsgTypeBulletin, bulletin.Id); err != nil {
457 log.Error(err.Error()) 475 log.Error(err.Error())
458 orm.Rollback() 476 orm.Rollback()
459 return 477 return