作者 yangfu

公告修改

@@ -3,11 +3,11 @@ package controllers @@ -3,11 +3,11 @@ package controllers
3 import ( 3 import (
4 "encoding/json" 4 "encoding/json"
5 "fmt" 5 "fmt"
  6 + "github.com/astaxie/beego"
  7 + "github.com/astaxie/beego/validation"
6 "oppmg/common/log" 8 "oppmg/common/log"
7 "oppmg/protocol" 9 "oppmg/protocol"
8 "strconv" 10 "strconv"
9 -  
10 - "github.com/astaxie/beego"  
11 ) 11 )
12 12
13 //BaseController 基础 13 //BaseController 基础
@@ -56,3 +56,21 @@ func (this *BaseController) GetUserId() int64 { @@ -56,3 +56,21 @@ func (this *BaseController) GetUserId() int64 {
56 userid, _ := strconv.ParseInt(fmt.Sprint(v), 10, 64) 56 userid, _ := strconv.ParseInt(fmt.Sprint(v), 10, 64)
57 return userid 57 return userid
58 } 58 }
  59 +
  60 +//Valid valid struct
  61 +func (this *BaseController) Valid(obj interface{}) (result bool, msg *protocol.ResponseMessage) {
  62 + /*校验*/
  63 + var err error
  64 + valid := validation.Validation{}
  65 + result, err = valid.Valid(obj)
  66 + if err != nil {
  67 + msg = protocol.BadRequestParam("1")
  68 + return
  69 + }
  70 + if !result {
  71 + msg = protocol.BadRequestParam("2")
  72 + return
  73 + }
  74 +
  75 + return
  76 +}
  1 +package controllers
  2 +
  3 +import (
  4 + "encoding/json"
  5 + "oppmg/common/log"
  6 + "oppmg/protocol"
  7 + "oppmg/services/bulletin"
  8 + "strconv"
  9 +)
  10 +
  11 +type BulletinController struct {
  12 + BaseController
  13 +}
  14 +
  15 +//BulletinRelease
  16 +//@router /release [post]
  17 +func (this *BulletinController) BulletinRelease() {
  18 + var msg *protocol.ResponseMessage
  19 + defer func() {
  20 + this.ResposeJson(msg)
  21 + }()
  22 + var request *protocol.BulletinReleaseRequest
  23 + if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
  24 + log.Error("json 解析失败", err)
  25 + msg = protocol.BadRequestParam("1")
  26 + return
  27 + }
  28 + uid := this.GetUserId()
  29 + companyId := this.GetCompanyId()
  30 + if companyId <= 0 {
  31 + msg = protocol.BadRequestParam("1")
  32 + return
  33 + }
  34 + if request.Type != 1 || request.Type != 2 {
  35 + msg = protocol.BadRequestParam("1")
  36 + log.Error("type error :", request.Type)
  37 + return
  38 + }
  39 + if b, m := this.Valid(request); !b {
  40 + msg = m
  41 + return
  42 + }
  43 + rsp, err := bulletin.BulletinRelease(uid, companyId, request)
  44 + msg = protocol.NewReturnResponse(rsp, err)
  45 + return
  46 +}
  47 +
  48 +//BulletinList
  49 +//@router /list [post]
  50 +func (this *BulletinController) BulletinList() {
  51 + var msg *protocol.ResponseMessage
  52 + defer func() {
  53 + this.ResposeJson(msg)
  54 + }()
  55 + var request *protocol.BulletinListRequest
  56 + if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
  57 + log.Error("json 解析失败", err)
  58 + msg = protocol.BadRequestParam("1")
  59 + return
  60 + }
  61 + if b, m := this.Valid(request); !b {
  62 + msg = m
  63 + return
  64 + }
  65 + uid := this.GetUserId()
  66 + companyId := this.GetCompanyId()
  67 + if companyId <= 0 {
  68 + msg = protocol.BadRequestParam("1")
  69 + return
  70 + }
  71 + rsp, err := bulletin.BulletinList(uid, companyId, request)
  72 + msg = protocol.NewReturnResponse(rsp, err)
  73 +}
  74 +
  75 +//GetBulletin
  76 +func (this *BulletinController) GetBulletin() {
  77 + var msg *protocol.ResponseMessage
  78 + defer func() {
  79 + this.ResposeJson(msg)
  80 + }()
  81 + var request *protocol.GetBulletinRequest
  82 + if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
  83 + log.Error("json 解析失败", err)
  84 + msg = protocol.BadRequestParam("1")
  85 + return
  86 + }
  87 + if b, m := this.Valid(request); !b {
  88 + msg = m
  89 + return
  90 + }
  91 + param := this.Ctx.Input.Param(":id")
  92 + id, _ := strconv.ParseInt(param, 10, 64)
  93 + if id == 0 {
  94 + msg = protocol.BadRequestParam("1")
  95 + return
  96 + }
  97 + companyId := this.GetCompanyId()
  98 + if companyId <= 0 {
  99 + msg = protocol.BadRequestParam("1")
  100 + return
  101 + }
  102 + rsp, err := bulletin.GetBulletin(int(id), companyId, request)
  103 + msg = protocol.NewReturnResponse(rsp, err)
  104 +}
  105 +
  106 +//UpdateBulletin
  107 +//@router /update [post]
  108 +func (this *BulletinController) UpdateBulletin() {
  109 + var msg *protocol.ResponseMessage
  110 + defer func() {
  111 + this.ResposeJson(msg)
  112 + }()
  113 + var request *protocol.UpdateBulletinRequest
  114 + if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
  115 + log.Error("json 解析失败", err)
  116 + msg = protocol.BadRequestParam("1")
  117 + return
  118 + }
  119 + if b, m := this.Valid(request); !b {
  120 + msg = m
  121 + return
  122 + }
  123 + companyId := this.GetCompanyId()
  124 + if companyId <= 0 {
  125 + msg = protocol.BadRequestParam("1")
  126 + return
  127 + }
  128 + rsp, err := bulletin.UpdateBulletin(companyId, request)
  129 + msg = protocol.NewReturnResponse(rsp, err)
  130 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "time"
  6 +
  7 + "github.com/astaxie/beego/orm"
  8 +)
  9 +
  10 +type Bulletin struct {
  11 + Id int `orm:"column(id);auto"`
  12 + Title string `orm:"column(title);size(2000);null" description:"标题"`
  13 + Content string `orm:"column(content);null" description:"内容"`
  14 + Cover string `orm:"column(cover);size(255);null" description:"封面地址"`
  15 + W int `orm:"column(w);null" description:"宽"`
  16 + H int `orm:"column(h);null" description:"高"`
  17 + Type int8 `orm:"column(type);null" description:"公告类型(1图+跳转链接、2纯文本)"`
  18 + Receiver string `orm:"column(receiver);null" description:"接收者"`
  19 + QuestionSwitch int8 `orm:"column(question_switch);null" description:"设置问题开关 (是否有问题)"`
  20 + CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
  21 + UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
  22 + AllowClose int8 `orm:"column(allow_close);null" description:"允许关闭公告(0允许,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"`
  25 + Status uint8 `orm:"column(status)" description:"状态 1-下架 2-上架"`
  26 +}
  27 +
  28 +func (t *Bulletin) TableName() string {
  29 + return "bulletin"
  30 +}
  31 +
  32 +func init() {
  33 + orm.RegisterModel(new(Bulletin))
  34 +}
  35 +
  36 +// AddBulletin insert a new Bulletin into database and returns
  37 +// last inserted Id on success.
  38 +func AddBulletin(m *Bulletin) (id int64, err error) {
  39 + o := orm.NewOrm()
  40 + id, err = o.Insert(m)
  41 + return
  42 +}
  43 +
  44 +// GetBulletinById retrieves Bulletin by Id. Returns error if
  45 +// Id doesn't exist
  46 +func GetBulletinById(id int) (v *Bulletin, err error) {
  47 + o := orm.NewOrm()
  48 + v = &Bulletin{Id: id}
  49 + if err = o.Read(v); err == nil {
  50 + return v, nil
  51 + }
  52 + return nil, err
  53 +}
  54 +
  55 +// UpdateBulletin updates Bulletin by Id and returns error if
  56 +// the record to be updated doesn't exist
  57 +func UpdateBulletinById(m *Bulletin) (err error) {
  58 + o := orm.NewOrm()
  59 + v := Bulletin{Id: m.Id}
  60 + // ascertain id exists in the database
  61 + if err = o.Read(&v); err == nil {
  62 + var num int64
  63 + if num, err = o.Update(m); err == nil {
  64 + fmt.Println("Number of records updated in database:", num)
  65 + }
  66 + }
  67 + return
  68 +}
  69 +
  70 +// DeleteBulletin deletes Bulletin by Id and returns error if
  71 +// the record to be deleted doesn't exist
  72 +func DeleteBulletin(id int) (err error) {
  73 + o := orm.NewOrm()
  74 + v := Bulletin{Id: id}
  75 + // ascertain id exists in the database
  76 + if err = o.Read(&v); err == nil {
  77 + var num int64
  78 + if num, err = o.Delete(&Bulletin{Id: id}); err == nil {
  79 + fmt.Println("Number of records deleted in database:", num)
  80 + }
  81 + }
  82 + return
  83 +}
  84 +
  85 +func GetBulletins(companyId int64, status int8, page, pageSize int) (v []*Bulletin, total int, err error) {
  86 + sql := "select * from bulletin where (status=? or ?==0) and company_id =? order by create_at desc limit ?,?"
  87 + o := orm.NewOrm()
  88 + if _, err = o.Raw(sql, status, companyId, page-1, pageSize).QueryRows(&v); err != nil {
  89 + return
  90 + }
  91 +
  92 + sqlTotal := "select count(1) from bulletin where (status=? or ?==0) and company_id =?"
  93 + if _, err = o.Raw(sqlTotal, status, companyId).QueryRows(&total); err != nil {
  94 + return
  95 + }
  96 + return
  97 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "time"
  6 +
  7 + "github.com/astaxie/beego/orm"
  8 +)
  9 +
  10 +type BulletinQuestion struct {
  11 + Id int `orm:"column(id);auto"`
  12 + BulletinId int `orm:"column(bulletin_id);null" description:"公告id"`
  13 + Type int8 `orm:"column(type);null" description:"类型:0-单选,1-多选"`
  14 + Title string `orm:"column(title);size(2000);null" description:"标题"`
  15 + Content string `orm:"column(content);size(2000);null" description:"内容"`
  16 + CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
  17 + UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
  18 + MustRead int8 `orm:"column(must_read);null" description:"公告内容看完才可以关闭 1:是 0:否"`
  19 + MustAnswer int8 `orm:"column(must_answer);null" description:"回答完问题才可以关闭 1:是 0:否"`
  20 +}
  21 +
  22 +func (t *BulletinQuestion) TableName() string {
  23 + return "bulletin_question"
  24 +}
  25 +
  26 +func init() {
  27 + orm.RegisterModel(new(BulletinQuestion))
  28 +}
  29 +
  30 +// AddBulletinQuestion insert a new BulletinQuestion into database and returns
  31 +// last inserted Id on success.
  32 +func AddBulletinQuestion(m *BulletinQuestion) (id int64, err error) {
  33 + o := orm.NewOrm()
  34 + id, err = o.Insert(m)
  35 + return
  36 +}
  37 +
  38 +// GetBulletinQuestionById retrieves BulletinQuestion by Id. Returns error if
  39 +// Id doesn't exist
  40 +func GetBulletinQuestionById(id int) (v *BulletinQuestion, err error) {
  41 + o := orm.NewOrm()
  42 + v = &BulletinQuestion{Id: id}
  43 + if err = o.Read(v); err == nil {
  44 + return v, nil
  45 + }
  46 + return nil, err
  47 +}
  48 +
  49 +// UpdateBulletinQuestion updates BulletinQuestion by Id and returns error if
  50 +// the record to be updated doesn't exist
  51 +func UpdateBulletinQuestionById(m *BulletinQuestion) (err error) {
  52 + o := orm.NewOrm()
  53 + v := BulletinQuestion{Id: m.Id}
  54 + // ascertain id exists in the database
  55 + if err = o.Read(&v); err == nil {
  56 + var num int64
  57 + if num, err = o.Update(m); err == nil {
  58 + fmt.Println("Number of records updated in database:", num)
  59 + }
  60 + }
  61 + return
  62 +}
  63 +
  64 +// DeleteBulletinQuestion deletes BulletinQuestion by Id and returns error if
  65 +// the record to be deleted doesn't exist
  66 +func DeleteBulletinQuestion(id int) (err error) {
  67 + o := orm.NewOrm()
  68 + v := BulletinQuestion{Id: id}
  69 + // ascertain id exists in the database
  70 + if err = o.Read(&v); err == nil {
  71 + var num int64
  72 + if num, err = o.Delete(&BulletinQuestion{Id: id}); err == nil {
  73 + fmt.Println("Number of records deleted in database:", num)
  74 + }
  75 + }
  76 + return
  77 +}
  78 +
  79 +func GetBulletinQuestionByBulletinId(id int) (v *BulletinQuestion, err error) {
  80 + o := orm.NewOrm()
  81 + sql := "select * from bulletin_question where bulletin_id=?"
  82 + if err = o.Raw(sql, id).QueryRow(&v); err == nil {
  83 + return v, nil
  84 + }
  85 + return nil, err
  86 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "time"
  6 +
  7 + "github.com/astaxie/beego/orm"
  8 +)
  9 +
  10 +type BulletinQuestionAnswer struct {
  11 + Id int `orm:"column(id);auto"`
  12 + Answer string `orm:"column(answer);null" description:"答案"`
  13 + BulletinId int `orm:"column(bulletin_id);null" description:"公告id"`
  14 + BulletinQuestionId int `orm:"column(bulletin_question_id);null" description:"公告问题id"`
  15 + Uid int64 `orm:"column(uid);null" description:"用户id"`
  16 + CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
  17 + UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
  18 +}
  19 +
  20 +func (t *BulletinQuestionAnswer) TableName() string {
  21 + return "bulletin_question_answer"
  22 +}
  23 +
  24 +func init() {
  25 + orm.RegisterModel(new(BulletinQuestionAnswer))
  26 +}
  27 +
  28 +// AddBulletinQuestionAnswer insert a new BulletinQuestionAnswer into database and returns
  29 +// last inserted Id on success.
  30 +func AddBulletinQuestionAnswer(m *BulletinQuestionAnswer) (id int64, err error) {
  31 + o := orm.NewOrm()
  32 + id, err = o.Insert(m)
  33 + return
  34 +}
  35 +
  36 +// GetBulletinQuestionAnswerById retrieves BulletinQuestionAnswer by Id. Returns error if
  37 +// Id doesn't exist
  38 +func GetBulletinQuestionAnswerById(id int) (v *BulletinQuestionAnswer, err error) {
  39 + o := orm.NewOrm()
  40 + v = &BulletinQuestionAnswer{Id: id}
  41 + if err = o.Read(v); err == nil {
  42 + return v, nil
  43 + }
  44 + return nil, err
  45 +}
  46 +
  47 +// UpdateBulletinQuestionAnswer updates BulletinQuestionAnswer by Id and returns error if
  48 +// the record to be updated doesn't exist
  49 +func UpdateBulletinQuestionAnswerById(m *BulletinQuestionAnswer) (err error) {
  50 + o := orm.NewOrm()
  51 + v := BulletinQuestionAnswer{Id: m.Id}
  52 + // ascertain id exists in the database
  53 + if err = o.Read(&v); err == nil {
  54 + var num int64
  55 + if num, err = o.Update(m); err == nil {
  56 + fmt.Println("Number of records updated in database:", num)
  57 + }
  58 + }
  59 + return
  60 +}
  61 +
  62 +// DeleteBulletinQuestionAnswer deletes BulletinQuestionAnswer by Id and returns error if
  63 +// the record to be deleted doesn't exist
  64 +func DeleteBulletinQuestionAnswer(id int) (err error) {
  65 + o := orm.NewOrm()
  66 + v := BulletinQuestionAnswer{Id: id}
  67 + // ascertain id exists in the database
  68 + if err = o.Read(&v); err == nil {
  69 + var num int64
  70 + if num, err = o.Delete(&BulletinQuestionAnswer{Id: id}); err == nil {
  71 + fmt.Println("Number of records deleted in database:", num)
  72 + }
  73 + }
  74 + return
  75 +}
@@ -113,3 +113,7 @@ func getUserNameByIds(ids []int64) ([]User, error) { @@ -113,3 +113,7 @@ func getUserNameByIds(ids []int64) ([]User, error) {
113 } 113 }
114 return users, err 114 return users, err
115 } 115 }
  116 +
  117 +func GetUserNameByIds(ids []int64) ([]User, error) {
  118 + return getUserNameByIds(ids)
  119 +}
@@ -87,6 +87,6 @@ func GetUserCompanyBy(userid int64, companyId int64) (*UserCompany, error) { @@ -87,6 +87,6 @@ func GetUserCompanyBy(userid int64, companyId int64) (*UserCompany, error) {
87 } 87 }
88 88
89 func GetUserCompanyByUser(userid int64) ([]UserCompany, error) { 89 func GetUserCompanyByUser(userid int64) ([]UserCompany, error) {
90 - datasql := `` 90 + //datasql := ``
91 return nil, nil 91 return nil, nil
92 } 92 }
  1 +package protocol
  2 +
  3 +/*BulletinRelease */
  4 +type BulletinReleaseRequest struct {
  5 + Id int `json:"id"`
  6 + Type int `json:"type" valid:"Required"`
  7 + Title string `json:"title" valid:"Required"`
  8 + Content string `json:"content" valid:"Required"`
  9 + AllowClose int `json:"allow_close"`
  10 + AllowCondition int `json:"allow_condition"`
  11 + QuestionSwitch int `json:"question_switch"`
  12 + Receiver []string `json:"receiver" valid:"Required"`
  13 + Question Question `json:"question"`
  14 + Cover Cover `json:"cover" valid:"Required"`
  15 +}
  16 +type Question struct {
  17 + Id int `json:"id"`
  18 + Type int `json:"type" valid:"Required"`
  19 + Title string `json:"title" valid:"Required"`
  20 + Content []QuestionContent `json:"content" valid:"Required"`
  21 +}
  22 +type QuestionContent struct {
  23 + Id int
  24 + Content string
  25 +}
  26 +type Cover struct {
  27 + Path string `json:"path"`
  28 + H int `json:"h"`
  29 + W int `json:"w"`
  30 +}
  31 +type BulletinReleaseResponse struct {
  32 +}
  33 +
  34 +/*BulletinList */
  35 +type BulletinListRequest struct {
  36 + Status int8 `json:"status"` //1:待上架 2:上架
  37 + Page int `json:"page"`
  38 + PageSize int `json:"page_size"`
  39 +}
  40 +type BulletinListResponse struct {
  41 + List []*BulletinItem `json:"list"`
  42 + Total int
  43 +}
  44 +
  45 +type BulletinItem struct {
  46 + Id int `json:"id"`
  47 + Type int8 `json:"type"`
  48 + Title string `json:"title"`
  49 + Status int8 `json:"status"`
  50 + Receiver []string `json:"receiver" valid:"Required"`
  51 + CreateAt string `json:"time"`
  52 +}
  53 +
  54 +/*GetBulletin */
  55 +type GetBulletinRequest struct {
  56 +}
  57 +type GetBulletinResponse struct {
  58 + Id int `json:"id"`
  59 + Type int `json:"type" valid:"Required"`
  60 + Title string `json:"title" valid:"Required"`
  61 + Content string `json:"content" valid:"Required"`
  62 + AllowClose int `json:"allow_close"`
  63 + AllowCondition int `json:"allow_condition"`
  64 + QuestionSwitch int `json:"question_switch"`
  65 + Receiver []string `json:"receiver" valid:"Required"`
  66 + Question Question `json:"question"`
  67 + Cover Cover `json:"cover" valid:"Required"`
  68 +}
  69 +
  70 +/*UpdateBulletin */
  71 +type UpdateBulletinRequest struct {
  72 + Id int `json:"id" valid:"Required"`
  73 + Type int `json:"type" valid:"Required"`
  74 + Title string `json:"title" valid:"Required"`
  75 + Content string `json:"content" valid:"Required"`
  76 + AllowClose int `json:"allow_close"`
  77 + AllowCondition int `json:"allow_condition"`
  78 + QuestionSwitch int `json:"question_switch"`
  79 + Receiver []string `json:"receiver" valid:"Required"`
  80 + Question Question `json:"question"`
  81 + Cover Cover `json:"cover" valid:"Required"`
  82 +}
  83 +type UpdateBulletinResponse struct {
  84 +}
@@ -36,6 +36,12 @@ func init() { @@ -36,6 +36,12 @@ func init() {
36 beego.NSRouter("/change_company", &controllers.AuthController{}, "post:ChangeCompany"), 36 beego.NSRouter("/change_company", &controllers.AuthController{}, "post:ChangeCompany"),
37 beego.NSRouter("/refresh_token", &controllers.AuthController{}, "post:RefreshToken"), 37 beego.NSRouter("/refresh_token", &controllers.AuthController{}, "post:RefreshToken"),
38 ), 38 ),
  39 + beego.NSNamespace("/bulletin",
  40 + beego.NSRouter("/release", &controllers.BulletinController{}, "post:BulletinRelease"),
  41 + beego.NSRouter("/list", &controllers.BulletinController{}, "post:BulletinList"),
  42 + beego.NSRouter("/:id([0-9]+)", &controllers.BulletinController{}, "get:GetBulletin"),
  43 + beego.NSRouter("/update", &controllers.BulletinController{}, "get:UpdateBulletin"),
  44 + ),
39 ) 45 )
40 46
41 nsAuth := beego.NewNamespace("/auth", 47 nsAuth := beego.NewNamespace("/auth",
@@ -228,7 +228,7 @@ func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, erro @@ -228,7 +228,7 @@ func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, erro
228 uclientReturn ucenter.ResponseLogin 228 uclientReturn ucenter.ResponseLogin
229 ) 229 )
230 230
231 - _, err := models.GetUserByPhone(account) 231 + _, err = models.GetUserByPhone(account)
232 if err != nil { 232 if err != nil {
233 log.Debug("GetUserByPhone(%s) err:%s", account, err) 233 log.Debug("GetUserByPhone(%s) err:%s", account, err)
234 return logintoken, protocol.NewErrWithMessage("10021") 234 return logintoken, protocol.NewErrWithMessage("10021")
  1 +package bulletin
  2 +
  3 +import (
  4 + "encoding/json"
  5 + orm2 "github.com/astaxie/beego/orm"
  6 + "oppmg/common/log"
  7 + "oppmg/models"
  8 + "oppmg/protocol"
  9 + "strconv"
  10 + "time"
  11 +)
  12 +
  13 +//发布公告
  14 +func BulletinRelease(uid, companyId int64, request *protocol.BulletinReleaseRequest) (rsp *protocol.BulletinReleaseResponse, err error) {
  15 + var (
  16 + bulletin *models.Bulletin
  17 + bulletinQuestion *models.BulletinQuestion
  18 + receiver, questionContent []byte
  19 + id int64
  20 + )
  21 + //TODO:check role_menu
  22 + if receiver, err = json.Marshal(request.Receiver); err != nil {
  23 + log.Error(err.Error())
  24 + return
  25 + }
  26 + bulletin = &models.Bulletin{
  27 + Title: request.Title,
  28 + Type: int8(request.Type),
  29 + Content: request.Content,
  30 + Cover: request.Cover.Path,
  31 + H: request.Cover.H,
  32 + W: request.Cover.W,
  33 + Receiver: string(receiver),
  34 + QuestionSwitch: int8(request.QuestionSwitch),
  35 + AllowCondition: int8(request.AllowCondition),
  36 + AllowClose: int8(request.AllowClose),
  37 + CompanyId: companyId,
  38 + CreateAt: time.Now(),
  39 + UpdateAt: time.Now(),
  40 + }
  41 +
  42 + orm := orm2.NewOrm()
  43 + orm.Begin()
  44 + if id, err = orm.Insert(bulletin); err != nil {
  45 + log.Error(err.Error())
  46 + orm.Rollback()
  47 + }
  48 + if request.QuestionSwitch == 1 {
  49 + if len(request.Question.Content) == 0 {
  50 + err = protocol.NewErrWithMessage("1")
  51 + log.Error("BulletinRelease:question.content not empty.", uid)
  52 + return
  53 + }
  54 + if questionContent, err = json.Marshal(request.Question.Content); err != nil {
  55 + log.Error(err.Error())
  56 + return
  57 + }
  58 + bulletinQuestion = &models.BulletinQuestion{
  59 + BulletinId: int(id),
  60 + Type: int8(request.Question.Type),
  61 + Title: request.Question.Title,
  62 + Content: string(questionContent),
  63 + CreateAt: time.Now(),
  64 + UpdateAt: time.Now(),
  65 + }
  66 + if _, err = orm.Insert(bulletinQuestion); err != nil {
  67 + log.Error(err.Error())
  68 + orm.Rollback()
  69 + }
  70 + }
  71 + //TODO:发送公告消息
  72 + orm.Commit()
  73 + rsp = &protocol.BulletinReleaseResponse{}
  74 + return
  75 +}
  76 +
  77 +//公告列表
  78 +func BulletinList(uid, companyId int64, request *protocol.BulletinListRequest) (rsp *protocol.BulletinListResponse, err error) {
  79 + var (
  80 + list []*models.Bulletin
  81 + total int
  82 + )
  83 + if request.Page == 0 {
  84 + request.Page = 1
  85 + }
  86 + if request.PageSize == 0 {
  87 + request.PageSize = 20
  88 + }
  89 + if list, total, err = models.GetBulletins(companyId, request.Status, request.Page, request.PageSize); err != nil {
  90 + log.Error(err.Error())
  91 + return
  92 + }
  93 + if len(list) == 0 {
  94 + return
  95 + }
  96 + rsp = &protocol.BulletinListResponse{}
  97 + for i := range list {
  98 + bulletin := list[i]
  99 + item := &protocol.BulletinItem{
  100 + Id: bulletin.Id,
  101 + Type: bulletin.Type,
  102 + Title: bulletin.Title,
  103 + Status: int8(bulletin.Status),
  104 + //TODO:user
  105 + Receiver: []string{},
  106 + CreateAt: bulletin.CreateAt.Format("2006-01-02 15:04:05"),
  107 + }
  108 + if item.Receiver, err = getUsersName(bulletin.Receiver); err != nil {
  109 + log.Error(err.Error())
  110 + continue
  111 + }
  112 + rsp.List = append(rsp.List, item)
  113 + }
  114 + rsp.Total = total
  115 + return
  116 +}
  117 +
  118 +func getUsers(idsstr string) (v []models.User, err error) {
  119 + var idlist []string
  120 + var ids []int64
  121 + var id int64
  122 + if err = json.Unmarshal([]byte(idsstr), &idlist); err != nil {
  123 + return
  124 + }
  125 + for i := 0; i < len(idlist); i++ {
  126 + if id, err = strconv.ParseInt(idlist[i], 10, 64); err != nil {
  127 + return
  128 + }
  129 + ids = append(ids, id)
  130 + }
  131 + return models.GetUserNameByIds(ids)
  132 +}
  133 +
  134 +func getUsersName(idsStr string) (v []string, err error) {
  135 + var users []models.User
  136 + if users, err = getUsers(idsStr); err != nil {
  137 + return
  138 + }
  139 + for i := range users {
  140 + v = append(v, users[i].NickName)
  141 + }
  142 + return
  143 +}
  144 +
  145 +//公告详情
  146 +func GetBulletin(id int, companyId int64, request *protocol.GetBulletinRequest) (rsp *protocol.GetBulletinResponse, err error) {
  147 + var (
  148 + bulletin *models.Bulletin
  149 + question *models.BulletinQuestion
  150 + )
  151 + if bulletin, err = models.GetBulletinById(id); err != nil {
  152 + log.Error(err.Error())
  153 + return
  154 + }
  155 + if bulletin.CompanyId != companyId {
  156 + err = protocol.NewErrWithMessage("1")
  157 + log.Error("GetBulletin:company not equal:(%v!=%v)", companyId, bulletin.CompanyId)
  158 + return
  159 + }
  160 + rsp = &protocol.GetBulletinResponse{}
  161 + rsp = &protocol.GetBulletinResponse{
  162 + Id: bulletin.Id,
  163 + Type: int(bulletin.Type),
  164 + Title: bulletin.Title,
  165 + Content: bulletin.Content,
  166 + AllowClose: int(bulletin.AllowClose),
  167 + AllowCondition: int(bulletin.AllowCondition),
  168 + Cover: protocol.Cover{
  169 + Path: bulletin.Cover,
  170 + W: bulletin.W,
  171 + H: bulletin.H,
  172 + },
  173 + }
  174 + if bulletin.QuestionSwitch == 1 {
  175 + if question, err = models.GetBulletinQuestionByBulletinId(bulletin.Id); err != nil {
  176 + log.Error(err.Error())
  177 + return
  178 + }
  179 + rsp.QuestionSwitch = int(bulletin.QuestionSwitch)
  180 + rsp.Question = protocol.Question{
  181 + Type: int(question.Type),
  182 + Title: question.Title,
  183 + }
  184 + if err = json.Unmarshal([]byte(question.Content), &rsp.Question.Content); err != nil {
  185 + log.Error(err.Error())
  186 + return
  187 + }
  188 + }
  189 + if rsp.Receiver, err = getUsersName(bulletin.Receiver); err != nil {
  190 + log.Error(err.Error())
  191 + return
  192 + }
  193 + return
  194 +}
  195 +
  196 +//更新公告
  197 +func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (rsp *protocol.UpdateBulletinResponse, err error) {
  198 + var (
  199 + bulletin *models.Bulletin
  200 + bulletinQuestion *models.BulletinQuestion
  201 + receiver, questionContent []byte
  202 + )
  203 + if bulletin, err = models.GetBulletinById(request.Id); err != nil {
  204 + log.Error(err.Error())
  205 + return
  206 + }
  207 + if bulletin.CompanyId != companyId {
  208 + err = protocol.NewErrWithMessage("1")
  209 + log.Error("GetBulletin:company not equal:(%v!=%v)", companyId, bulletin.CompanyId)
  210 + return
  211 + }
  212 +
  213 + if receiver, err = json.Marshal(request.Receiver); err != nil {
  214 + log.Error(err.Error())
  215 + return
  216 + }
  217 + //update
  218 + {
  219 + bulletin.Title = request.Title
  220 + bulletin.Type = int8(request.Type)
  221 + bulletin.Content = request.Content
  222 + bulletin.Cover = request.Cover.Path
  223 + bulletin.H = request.Cover.H
  224 + bulletin.W = request.Cover.W
  225 + bulletin.Receiver = string(receiver)
  226 + bulletin.QuestionSwitch = int8(request.QuestionSwitch)
  227 + bulletin.AllowCondition = int8(request.AllowCondition)
  228 + bulletin.AllowClose = int8(request.AllowClose)
  229 + bulletin.UpdateAt = time.Now()
  230 + if err = models.UpdateBulletinById(bulletin); err != nil {
  231 + log.Error(err.Error())
  232 + return
  233 + }
  234 + }
  235 +
  236 + if bulletin.QuestionSwitch == 1 {
  237 + if bulletinQuestion, err = models.GetBulletinQuestionByBulletinId(bulletin.Id); err != nil {
  238 + log.Error(err.Error())
  239 + return
  240 + }
  241 + if questionContent, err = json.Marshal(request.Question.Content); err != nil {
  242 + log.Error(err.Error())
  243 + return
  244 + }
  245 + bulletinQuestion.Content = string(questionContent)
  246 + bulletinQuestion.Type = int8(request.Question.Type)
  247 + if err = models.UpdateBulletinQuestionById(bulletinQuestion); err != nil {
  248 + log.Error(err.Error())
  249 + return
  250 + }
  251 + }
  252 + return
  253 +}