作者 yangfu

公告修改

... ... @@ -19,7 +19,7 @@ mysql_db_name = "${MYSQL_DB_NAME||opportunity_dev}"
##redis相关配置
redis_add = "${REDIS_HOST||127.0.0.1}"
redis_add_port = "${REDIS_PORT||6379}"
redis_auth = ""
redis_auth = "123456"
##log相关配置
##out_put:"console","file"
log_output = "file"
... ...
... ... @@ -56,8 +56,8 @@ func GetBulletinById(id int) (v *Bulletin, err error) {
// UpdateBulletin updates Bulletin by Id and returns error if
// the record to be updated doesn't exist
func UpdateBulletinById(m *Bulletin) (err error) {
o := orm.NewOrm()
func UpdateBulletinById(o orm.Ormer, m *Bulletin) (err error) {
//o := orm.NewOrm()
v := Bulletin{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
... ...
... ... @@ -46,8 +46,8 @@ func GetBulletinQuestionById(id int) (v *BulletinQuestion, err error) {
// UpdateBulletinQuestion updates BulletinQuestion by Id and returns error if
// the record to be updated doesn't exist
func UpdateBulletinQuestionById(m *BulletinQuestion) (err error) {
o := orm.NewOrm()
func UpdateBulletinQuestionById(o orm.Ormer, m *BulletinQuestion) (err error) {
//o := orm.NewOrm()
v := BulletinQuestion{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
... ...
... ... @@ -33,8 +33,15 @@ const (
SourceTypeBulletin = 3
)
//列表是否展示(公告列表)
const (
SqlDeleteUserMsg = "delete from user_msg where company_id=? and msg_type=? and source_id=?"
Public = 1 //公开
NoPublic = 0 //不公开
)
const (
SqlDeleteUserMsg = "delete from user_msg where company_id=? and msg_type=? and source_id=?"
SqlUpdateUserMsgPublic = "update user_msg set is_public=? where company_id=? and msg_type=? and source_id=?"
)
func (t *UserMsg) TableName() string {
... ... @@ -94,6 +101,17 @@ func DeleteUserMsg(id int64) (err error) {
return
}
//获取userMsg
func GetUserMsgBy(userCompanyId int64, msgType int, sourceType int, sourceId int64) (v *UserMsg, err error) {
sql := "select * from user_msg where receive_user_id=? and msg_type=? and source_type=? and source_id=?"
o := orm.NewOrm()
if err = o.Raw(sql, userCompanyId, msgType, sourceType, sourceId).QueryRow(&v); err != nil {
//log.Error(err.Error())
return
}
return
}
func NewUserMsg(companId, userCompanyId int64, msgType int, sourceType int, sourceId int64, message string) *UserMsg {
return &UserMsg{
Id: utils.GenerateIDBySonyflake(),
... ... @@ -104,5 +122,6 @@ func NewUserMsg(companId, userCompanyId int64, msgType int, sourceType int, sour
SourceId: sourceId,
Message: message,
CreateAt: time.Now(),
IsPublic: Public,
}
}
... ...
... ... @@ -169,15 +169,24 @@ func sendBulletinUserMsg(orm orm2.Ormer, receivers []protocol.VisibleObject, com
return
}
}
if err = utils.ExecuteSQLWithOrmer(orm, models.SqlUpdateUserMsgPublic, models.Public, companyId, models.MsgTypeBulletin, sourceId); err != nil {
log.Error(err.Error())
orm.Rollback()
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
//这条消息不存在,插入一条消息
if _, e := models.GetUserMsgBy(ids[i], models.MsgTypeBulletin, models.SourceTypeBulletin, sourceId); e != nil && e == orm2.ErrNoRows {
msg := models.NewUserMsg(companyId, ids[i], models.MsgTypeBulletin, models.SourceTypeBulletin, sourceId, message)
if _, err = orm.Insert(msg); err != nil {
orm.Rollback()
log.Error(err.Error())
return
}
}
}
return
... ... @@ -345,6 +354,17 @@ func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (r
log.Error(err.Error())
return
}
orm := orm2.NewOrm()
orm.Begin()
if request.IsPublish == 1 && bulletin.Status == protocol.BulletinUnRelease {
if err = sendBulletinUserMsg(orm, request.Receiver, companyId, int64(bulletin.Id), bulletin.Title, request.AllPeo); err != nil {
log.Error(err.Error())
orm.Rollback()
return
}
}
//update
{
bulletin.Title = request.Title
... ... @@ -360,8 +380,10 @@ func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (r
if request.IsPublish == 1 && bulletin.Status == protocol.BulletinUnRelease {
bulletin.Status = protocol.BulletinRelease
}
if err = models.UpdateBulletinById(bulletin); err != nil {
if err = models.UpdateBulletinById(orm, bulletin); err != nil {
log.Error(err.Error())
orm.Rollback()
return
}
}
... ... @@ -371,6 +393,7 @@ func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (r
validQuestion(&request.Question)
if err != nil && err != orm2.ErrNoRows {
log.Error(err.Error())
orm.Rollback()
return
}
if err == nil {
... ... @@ -388,8 +411,9 @@ func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (r
bulletinQuestion.Content = string(questionContent)
bulletinQuestion.Type = int8(request.Question.Type)
bulletinQuestion.UpdateAt = time.Now()
if err = models.UpdateBulletinQuestionById(bulletinQuestion); err != nil {
if err = models.UpdateBulletinQuestionById(orm, bulletinQuestion); err != nil {
log.Error(err.Error())
orm.Rollback()
return
}
}
... ... @@ -403,22 +427,16 @@ func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (r
CreateAt: time.Now(),
UpdateAt: time.Now(),
}
_, err = models.AddBulletinQuestion(bulletinQuestion)
_, err = orm.Insert(bulletinQuestion)
if err != nil {
orm.Rollback()
log.Error("添加问题失败:%s", err)
return
}
}
}
orm := orm2.NewOrm()
if request.IsPublish == 1 && bulletin.Status == protocol.BulletinUnRelease {
if err = sendBulletinUserMsg(orm, request.Receiver, companyId, int64(bulletin.Id), bulletin.Title, request.AllPeo); err != nil {
log.Error(err.Error())
orm.Rollback()
return
}
}
orm.Commit()
return
}
... ... @@ -453,7 +471,7 @@ func OperateBulletin(companyId int64, request *protocol.OperateBulletinRequest)
orm.Begin()
if request.CmdType == protocol.BulletinUnRelease { //下架
status = protocol.BulletinUnRelease
if err = utils.ExecuteSQLWithOrmer(orm, models.SqlDeleteUserMsg, companyId, models.MsgTypeBulletin, bulletin.Id); err != nil {
if err = utils.ExecuteSQLWithOrmer(orm, models.SqlUpdateUserMsgPublic, models.NoPublic, companyId, models.MsgTypeBulletin, bulletin.Id); err != nil {
log.Error(err.Error())
orm.Rollback()
return
... ...