作者 yangfu

公告修改

... ... @@ -3,11 +3,11 @@ package controllers
import (
"encoding/json"
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/validation"
"oppmg/common/log"
"oppmg/protocol"
"strconv"
"github.com/astaxie/beego"
)
//BaseController 基础
... ... @@ -56,3 +56,21 @@ func (this *BaseController) GetUserId() int64 {
userid, _ := strconv.ParseInt(fmt.Sprint(v), 10, 64)
return userid
}
//Valid valid struct
func (this *BaseController) Valid(obj interface{}) (result bool, msg *protocol.ResponseMessage) {
/*校验*/
var err error
valid := validation.Validation{}
result, err = valid.Valid(obj)
if err != nil {
msg = protocol.BadRequestParam("1")
return
}
if !result {
msg = protocol.BadRequestParam("2")
return
}
return
}
... ...
package controllers
import (
"encoding/json"
"oppmg/common/log"
"oppmg/protocol"
"oppmg/services/bulletin"
"strconv"
)
type BulletinController struct {
BaseController
}
//BulletinRelease
//@router /release [post]
func (this *BulletinController) BulletinRelease() {
var msg *protocol.ResponseMessage
defer func() {
this.ResposeJson(msg)
}()
var request *protocol.BulletinReleaseRequest
if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
log.Error("json 解析失败", err)
msg = protocol.BadRequestParam("1")
return
}
uid := this.GetUserId()
companyId := this.GetCompanyId()
if companyId <= 0 {
msg = protocol.BadRequestParam("1")
return
}
if request.Type != 1 || request.Type != 2 {
msg = protocol.BadRequestParam("1")
log.Error("type error :", request.Type)
return
}
if b, m := this.Valid(request); !b {
msg = m
return
}
rsp, err := bulletin.BulletinRelease(uid, companyId, request)
msg = protocol.NewReturnResponse(rsp, err)
return
}
//BulletinList
//@router /list [post]
func (this *BulletinController) BulletinList() {
var msg *protocol.ResponseMessage
defer func() {
this.ResposeJson(msg)
}()
var request *protocol.BulletinListRequest
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
}
uid := this.GetUserId()
companyId := this.GetCompanyId()
if companyId <= 0 {
msg = protocol.BadRequestParam("1")
return
}
rsp, err := bulletin.BulletinList(uid, companyId, request)
msg = protocol.NewReturnResponse(rsp, err)
}
//GetBulletin
func (this *BulletinController) GetBulletin() {
var msg *protocol.ResponseMessage
defer func() {
this.ResposeJson(msg)
}()
var request *protocol.GetBulletinRequest
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
}
param := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(param, 10, 64)
if id == 0 {
msg = protocol.BadRequestParam("1")
return
}
companyId := this.GetCompanyId()
if companyId <= 0 {
msg = protocol.BadRequestParam("1")
return
}
rsp, err := bulletin.GetBulletin(int(id), companyId, request)
msg = protocol.NewReturnResponse(rsp, err)
}
//UpdateBulletin
//@router /update [post]
func (this *BulletinController) UpdateBulletin() {
var msg *protocol.ResponseMessage
defer func() {
this.ResposeJson(msg)
}()
var request *protocol.UpdateBulletinRequest
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.UpdateBulletin(companyId, request)
msg = protocol.NewReturnResponse(rsp, err)
}
... ...
package models
import (
"fmt"
"time"
"github.com/astaxie/beego/orm"
)
type Bulletin struct {
Id int `orm:"column(id);auto"`
Title string `orm:"column(title);size(2000);null" description:"标题"`
Content string `orm:"column(content);null" description:"内容"`
Cover string `orm:"column(cover);size(255);null" description:"封面地址"`
W int `orm:"column(w);null" description:"宽"`
H int `orm:"column(h);null" description:"高"`
Type int8 `orm:"column(type);null" description:"公告类型(1图+跳转链接、2纯文本)"`
Receiver string `orm:"column(receiver);null" description:"接收者"`
QuestionSwitch int8 `orm:"column(question_switch);null" description:"设置问题开关 (是否有问题)"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
AllowClose int8 `orm:"column(allow_close);null" description:"允许关闭公告(0允许,1禁止)"`
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-上架"`
}
func (t *Bulletin) TableName() string {
return "bulletin"
}
func init() {
orm.RegisterModel(new(Bulletin))
}
// AddBulletin insert a new Bulletin into database and returns
// last inserted Id on success.
func AddBulletin(m *Bulletin) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetBulletinById retrieves Bulletin by Id. Returns error if
// Id doesn't exist
func GetBulletinById(id int) (v *Bulletin, err error) {
o := orm.NewOrm()
v = &Bulletin{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// 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()
v := Bulletin{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
}
// DeleteBulletin deletes Bulletin by Id and returns error if
// the record to be deleted doesn't exist
func DeleteBulletin(id int) (err error) {
o := orm.NewOrm()
v := Bulletin{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&Bulletin{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
func GetBulletins(companyId int64, status int8, page, pageSize int) (v []*Bulletin, total int, err error) {
sql := "select * from bulletin where (status=? or ?==0) and company_id =? order by create_at desc limit ?,?"
o := orm.NewOrm()
if _, err = o.Raw(sql, status, companyId, page-1, pageSize).QueryRows(&v); err != nil {
return
}
sqlTotal := "select count(1) from bulletin where (status=? or ?==0) and company_id =?"
if _, err = o.Raw(sqlTotal, status, companyId).QueryRows(&total); err != nil {
return
}
return
}
... ...
package models
import (
"fmt"
"time"
"github.com/astaxie/beego/orm"
)
type BulletinQuestion struct {
Id int `orm:"column(id);auto"`
BulletinId int `orm:"column(bulletin_id);null" description:"公告id"`
Type int8 `orm:"column(type);null" description:"类型:0-单选,1-多选"`
Title string `orm:"column(title);size(2000);null" description:"标题"`
Content string `orm:"column(content);size(2000);null" description:"内容"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
MustRead int8 `orm:"column(must_read);null" description:"公告内容看完才可以关闭 1:是 0:否"`
MustAnswer int8 `orm:"column(must_answer);null" description:"回答完问题才可以关闭 1:是 0:否"`
}
func (t *BulletinQuestion) TableName() string {
return "bulletin_question"
}
func init() {
orm.RegisterModel(new(BulletinQuestion))
}
// AddBulletinQuestion insert a new BulletinQuestion into database and returns
// last inserted Id on success.
func AddBulletinQuestion(m *BulletinQuestion) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetBulletinQuestionById retrieves BulletinQuestion by Id. Returns error if
// Id doesn't exist
func GetBulletinQuestionById(id int) (v *BulletinQuestion, err error) {
o := orm.NewOrm()
v = &BulletinQuestion{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// 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()
v := BulletinQuestion{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
}
// DeleteBulletinQuestion deletes BulletinQuestion by Id and returns error if
// the record to be deleted doesn't exist
func DeleteBulletinQuestion(id int) (err error) {
o := orm.NewOrm()
v := BulletinQuestion{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&BulletinQuestion{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
func GetBulletinQuestionByBulletinId(id int) (v *BulletinQuestion, err error) {
o := orm.NewOrm()
sql := "select * from bulletin_question where bulletin_id=?"
if err = o.Raw(sql, id).QueryRow(&v); err == nil {
return v, nil
}
return nil, err
}
... ...
package models
import (
"fmt"
"time"
"github.com/astaxie/beego/orm"
)
type BulletinQuestionAnswer struct {
Id int `orm:"column(id);auto"`
Answer string `orm:"column(answer);null" description:"答案"`
BulletinId int `orm:"column(bulletin_id);null" description:"公告id"`
BulletinQuestionId int `orm:"column(bulletin_question_id);null" description:"公告问题id"`
Uid int64 `orm:"column(uid);null" description:"用户id"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
}
func (t *BulletinQuestionAnswer) TableName() string {
return "bulletin_question_answer"
}
func init() {
orm.RegisterModel(new(BulletinQuestionAnswer))
}
// AddBulletinQuestionAnswer insert a new BulletinQuestionAnswer into database and returns
// last inserted Id on success.
func AddBulletinQuestionAnswer(m *BulletinQuestionAnswer) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetBulletinQuestionAnswerById retrieves BulletinQuestionAnswer by Id. Returns error if
// Id doesn't exist
func GetBulletinQuestionAnswerById(id int) (v *BulletinQuestionAnswer, err error) {
o := orm.NewOrm()
v = &BulletinQuestionAnswer{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// UpdateBulletinQuestionAnswer updates BulletinQuestionAnswer by Id and returns error if
// the record to be updated doesn't exist
func UpdateBulletinQuestionAnswerById(m *BulletinQuestionAnswer) (err error) {
o := orm.NewOrm()
v := BulletinQuestionAnswer{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
}
// DeleteBulletinQuestionAnswer deletes BulletinQuestionAnswer by Id and returns error if
// the record to be deleted doesn't exist
func DeleteBulletinQuestionAnswer(id int) (err error) {
o := orm.NewOrm()
v := BulletinQuestionAnswer{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&BulletinQuestionAnswer{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
... ...
... ... @@ -113,3 +113,7 @@ func getUserNameByIds(ids []int64) ([]User, error) {
}
return users, err
}
func GetUserNameByIds(ids []int64) ([]User, error) {
return getUserNameByIds(ids)
}
... ...
... ... @@ -87,6 +87,6 @@ func GetUserCompanyBy(userid int64, companyId int64) (*UserCompany, error) {
}
func GetUserCompanyByUser(userid int64) ([]UserCompany, error) {
datasql := ``
//datasql := ``
return nil, nil
}
... ...
package protocol
/*BulletinRelease */
type BulletinReleaseRequest struct {
Id int `json:"id"`
Type int `json:"type" valid:"Required"`
Title string `json:"title" valid:"Required"`
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"`
}
type Question struct {
Id int `json:"id"`
Type int `json:"type" valid:"Required"`
Title string `json:"title" valid:"Required"`
Content []QuestionContent `json:"content" valid:"Required"`
}
type QuestionContent struct {
Id int
Content string
}
type Cover struct {
Path string `json:"path"`
H int `json:"h"`
W int `json:"w"`
}
type BulletinReleaseResponse struct {
}
/*BulletinList */
type BulletinListRequest struct {
Status int8 `json:"status"` //1:待上架 2:上架
Page int `json:"page"`
PageSize int `json:"page_size"`
}
type BulletinListResponse struct {
List []*BulletinItem `json:"list"`
Total int
}
type BulletinItem struct {
Id int `json:"id"`
Type int8 `json:"type"`
Title string `json:"title"`
Status int8 `json:"status"`
Receiver []string `json:"receiver" valid:"Required"`
CreateAt string `json:"time"`
}
/*GetBulletin */
type GetBulletinRequest struct {
}
type GetBulletinResponse struct {
Id int `json:"id"`
Type int `json:"type" valid:"Required"`
Title string `json:"title" valid:"Required"`
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"`
}
/*UpdateBulletin */
type UpdateBulletinRequest struct {
Id int `json:"id" valid:"Required"`
Type int `json:"type" valid:"Required"`
Title string `json:"title" valid:"Required"`
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"`
}
type UpdateBulletinResponse struct {
}
... ...
... ... @@ -36,6 +36,12 @@ func init() {
beego.NSRouter("/change_company", &controllers.AuthController{}, "post:ChangeCompany"),
beego.NSRouter("/refresh_token", &controllers.AuthController{}, "post:RefreshToken"),
),
beego.NSNamespace("/bulletin",
beego.NSRouter("/release", &controllers.BulletinController{}, "post:BulletinRelease"),
beego.NSRouter("/list", &controllers.BulletinController{}, "post:BulletinList"),
beego.NSRouter("/:id([0-9]+)", &controllers.BulletinController{}, "get:GetBulletin"),
beego.NSRouter("/update", &controllers.BulletinController{}, "get:UpdateBulletin"),
),
)
nsAuth := beego.NewNamespace("/auth",
... ...
... ... @@ -228,7 +228,7 @@ func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, erro
uclientReturn ucenter.ResponseLogin
)
_, err := models.GetUserByPhone(account)
_, err = models.GetUserByPhone(account)
if err != nil {
log.Debug("GetUserByPhone(%s) err:%s", account, err)
return logintoken, protocol.NewErrWithMessage("10021")
... ...
package bulletin
import (
"encoding/json"
orm2 "github.com/astaxie/beego/orm"
"oppmg/common/log"
"oppmg/models"
"oppmg/protocol"
"strconv"
"time"
)
//发布公告
func BulletinRelease(uid, companyId int64, request *protocol.BulletinReleaseRequest) (rsp *protocol.BulletinReleaseResponse, err error) {
var (
bulletin *models.Bulletin
bulletinQuestion *models.BulletinQuestion
receiver, questionContent []byte
id int64
)
//TODO:check role_menu
if receiver, err = json.Marshal(request.Receiver); err != nil {
log.Error(err.Error())
return
}
bulletin = &models.Bulletin{
Title: request.Title,
Type: int8(request.Type),
Content: request.Content,
Cover: request.Cover.Path,
H: request.Cover.H,
W: request.Cover.W,
Receiver: string(receiver),
QuestionSwitch: int8(request.QuestionSwitch),
AllowCondition: int8(request.AllowCondition),
AllowClose: int8(request.AllowClose),
CompanyId: companyId,
CreateAt: time.Now(),
UpdateAt: time.Now(),
}
orm := orm2.NewOrm()
orm.Begin()
if id, err = orm.Insert(bulletin); err != nil {
log.Error(err.Error())
orm.Rollback()
}
if request.QuestionSwitch == 1 {
if len(request.Question.Content) == 0 {
err = protocol.NewErrWithMessage("1")
log.Error("BulletinRelease:question.content not empty.", uid)
return
}
if questionContent, err = json.Marshal(request.Question.Content); err != nil {
log.Error(err.Error())
return
}
bulletinQuestion = &models.BulletinQuestion{
BulletinId: int(id),
Type: int8(request.Question.Type),
Title: request.Question.Title,
Content: string(questionContent),
CreateAt: time.Now(),
UpdateAt: time.Now(),
}
if _, err = orm.Insert(bulletinQuestion); err != nil {
log.Error(err.Error())
orm.Rollback()
}
}
//TODO:发送公告消息
orm.Commit()
rsp = &protocol.BulletinReleaseResponse{}
return
}
//公告列表
func BulletinList(uid, companyId int64, request *protocol.BulletinListRequest) (rsp *protocol.BulletinListResponse, err error) {
var (
list []*models.Bulletin
total int
)
if request.Page == 0 {
request.Page = 1
}
if request.PageSize == 0 {
request.PageSize = 20
}
if list, total, err = models.GetBulletins(companyId, request.Status, request.Page, request.PageSize); err != nil {
log.Error(err.Error())
return
}
if len(list) == 0 {
return
}
rsp = &protocol.BulletinListResponse{}
for i := range list {
bulletin := list[i]
item := &protocol.BulletinItem{
Id: bulletin.Id,
Type: bulletin.Type,
Title: bulletin.Title,
Status: int8(bulletin.Status),
//TODO:user
Receiver: []string{},
CreateAt: bulletin.CreateAt.Format("2006-01-02 15:04:05"),
}
if item.Receiver, err = getUsersName(bulletin.Receiver); err != nil {
log.Error(err.Error())
continue
}
rsp.List = append(rsp.List, item)
}
rsp.Total = total
return
}
func getUsers(idsstr string) (v []models.User, err error) {
var idlist []string
var ids []int64
var id int64
if err = json.Unmarshal([]byte(idsstr), &idlist); err != nil {
return
}
for i := 0; i < len(idlist); i++ {
if id, err = strconv.ParseInt(idlist[i], 10, 64); err != nil {
return
}
ids = append(ids, id)
}
return models.GetUserNameByIds(ids)
}
func getUsersName(idsStr string) (v []string, err error) {
var users []models.User
if users, err = getUsers(idsStr); err != nil {
return
}
for i := range users {
v = append(v, users[i].NickName)
}
return
}
//公告详情
func GetBulletin(id int, companyId int64, request *protocol.GetBulletinRequest) (rsp *protocol.GetBulletinResponse, err error) {
var (
bulletin *models.Bulletin
question *models.BulletinQuestion
)
if bulletin, err = models.GetBulletinById(id); err != nil {
log.Error(err.Error())
return
}
if bulletin.CompanyId != companyId {
err = protocol.NewErrWithMessage("1")
log.Error("GetBulletin:company not equal:(%v!=%v)", companyId, bulletin.CompanyId)
return
}
rsp = &protocol.GetBulletinResponse{}
rsp = &protocol.GetBulletinResponse{
Id: bulletin.Id,
Type: int(bulletin.Type),
Title: bulletin.Title,
Content: bulletin.Content,
AllowClose: int(bulletin.AllowClose),
AllowCondition: int(bulletin.AllowCondition),
Cover: protocol.Cover{
Path: bulletin.Cover,
W: bulletin.W,
H: bulletin.H,
},
}
if bulletin.QuestionSwitch == 1 {
if question, err = models.GetBulletinQuestionByBulletinId(bulletin.Id); err != nil {
log.Error(err.Error())
return
}
rsp.QuestionSwitch = int(bulletin.QuestionSwitch)
rsp.Question = protocol.Question{
Type: int(question.Type),
Title: question.Title,
}
if err = json.Unmarshal([]byte(question.Content), &rsp.Question.Content); err != nil {
log.Error(err.Error())
return
}
}
if rsp.Receiver, err = getUsersName(bulletin.Receiver); err != nil {
log.Error(err.Error())
return
}
return
}
//更新公告
func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (rsp *protocol.UpdateBulletinResponse, err error) {
var (
bulletin *models.Bulletin
bulletinQuestion *models.BulletinQuestion
receiver, questionContent []byte
)
if bulletin, err = models.GetBulletinById(request.Id); err != nil {
log.Error(err.Error())
return
}
if bulletin.CompanyId != companyId {
err = protocol.NewErrWithMessage("1")
log.Error("GetBulletin:company not equal:(%v!=%v)", companyId, bulletin.CompanyId)
return
}
if receiver, err = json.Marshal(request.Receiver); err != nil {
log.Error(err.Error())
return
}
//update
{
bulletin.Title = request.Title
bulletin.Type = int8(request.Type)
bulletin.Content = request.Content
bulletin.Cover = request.Cover.Path
bulletin.H = request.Cover.H
bulletin.W = request.Cover.W
bulletin.Receiver = string(receiver)
bulletin.QuestionSwitch = int8(request.QuestionSwitch)
bulletin.AllowCondition = int8(request.AllowCondition)
bulletin.AllowClose = int8(request.AllowClose)
bulletin.UpdateAt = time.Now()
if err = models.UpdateBulletinById(bulletin); err != nil {
log.Error(err.Error())
return
}
}
if bulletin.QuestionSwitch == 1 {
if bulletinQuestion, err = models.GetBulletinQuestionByBulletinId(bulletin.Id); err != nil {
log.Error(err.Error())
return
}
if questionContent, err = json.Marshal(request.Question.Content); err != nil {
log.Error(err.Error())
return
}
bulletinQuestion.Content = string(questionContent)
bulletinQuestion.Type = int8(request.Question.Type)
if err = models.UpdateBulletinQuestionById(bulletinQuestion); err != nil {
log.Error(err.Error())
return
}
}
return
}
... ...