作者 唐旭辉

新增

  1 +package models
  2 +
  3 +import (
  4 + "errors"
  5 + "fmt"
  6 + "reflect"
  7 + "strings"
  8 + "time"
  9 +
  10 + "github.com/astaxie/beego/orm"
  11 +)
  12 +
  13 +type Bulletin struct {
  14 + Id int `orm:"column(id);auto"`
  15 + Title string `orm:"column(title);size(2000)" description:"标题"`
  16 + Content string `orm:"column(content);null" description:"内容"`
  17 + Cover string `orm:"column(cover);size(255);null" description:"封面地址"`
  18 + W int `orm:"column(w);null" description:"宽"`
  19 + H int `orm:"column(h);null" description:"高"`
  20 + Type int8 `orm:"column(type);null" description:"公告类型(0图+跳转链接、1链接)"`
  21 + Receiver string `orm:"column(receiver);null" description:"接收者"`
  22 + QuestionSwitch int8 `orm:"column(question_switch);null" description:"设置问题开关"`
  23 + CreateTime time.Time `orm:"column(createTime);type(timestamp);null" description:"创建时间"`
  24 + UpdateTime time.Time `orm:"column(updateTime);type(timestamp);null" description:"更新时间"`
  25 + AllowClose int8 `orm:"column(allowClose);null" description:"允许关闭公告(0允许,1禁止)"`
  26 + CompanyId int `orm:"column(company_id);null" description:"公司Id"`
  27 + Status uint8 `orm:"column(status)" description:"状态 0-下架 1- 上架"`
  28 +}
  29 +
  30 +func (t *Bulletin) TableName() string {
  31 + return "bulletin"
  32 +}
  33 +
  34 +func init() {
  35 + orm.RegisterModel(new(Bulletin))
  36 +}
  37 +
  38 +// AddBulletin insert a new Bulletin into database and returns
  39 +// last inserted Id on success.
  40 +func AddBulletin(m *Bulletin) (id int64, err error) {
  41 + o := orm.NewOrm()
  42 + id, err = o.Insert(m)
  43 + return
  44 +}
  45 +
  46 +// GetBulletinById retrieves Bulletin by Id. Returns error if
  47 +// Id doesn't exist
  48 +func GetBulletinById(id int) (v *Bulletin, err error) {
  49 + o := orm.NewOrm()
  50 + v = &Bulletin{Id: id}
  51 + if err = o.Read(v); err == nil {
  52 + return v, nil
  53 + }
  54 + return nil, err
  55 +}
  56 +
  57 +// GetAllBulletin retrieves all Bulletin matches certain condition. Returns empty list if
  58 +// no records exist
  59 +func GetAllBulletin(query map[string]string, fields []string, sortby []string, order []string,
  60 + offset int64, limit int64) (ml []interface{}, err error) {
  61 + o := orm.NewOrm()
  62 + qs := o.QueryTable(new(Bulletin))
  63 + // query k=v
  64 + for k, v := range query {
  65 + // rewrite dot-notation to Object__Attribute
  66 + k = strings.Replace(k, ".", "__", -1)
  67 + if strings.Contains(k, "isnull") {
  68 + qs = qs.Filter(k, (v == "true" || v == "1"))
  69 + } else {
  70 + qs = qs.Filter(k, v)
  71 + }
  72 + }
  73 + // order by:
  74 + var sortFields []string
  75 + if len(sortby) != 0 {
  76 + if len(sortby) == len(order) {
  77 + // 1) for each sort field, there is an associated order
  78 + for i, v := range sortby {
  79 + orderby := ""
  80 + if order[i] == "desc" {
  81 + orderby = "-" + v
  82 + } else if order[i] == "asc" {
  83 + orderby = v
  84 + } else {
  85 + return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
  86 + }
  87 + sortFields = append(sortFields, orderby)
  88 + }
  89 + qs = qs.OrderBy(sortFields...)
  90 + } else if len(sortby) != len(order) && len(order) == 1 {
  91 + // 2) there is exactly one order, all the sorted fields will be sorted by this order
  92 + for _, v := range sortby {
  93 + orderby := ""
  94 + if order[0] == "desc" {
  95 + orderby = "-" + v
  96 + } else if order[0] == "asc" {
  97 + orderby = v
  98 + } else {
  99 + return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
  100 + }
  101 + sortFields = append(sortFields, orderby)
  102 + }
  103 + } else if len(sortby) != len(order) && len(order) != 1 {
  104 + return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1")
  105 + }
  106 + } else {
  107 + if len(order) != 0 {
  108 + return nil, errors.New("Error: unused 'order' fields")
  109 + }
  110 + }
  111 +
  112 + var l []Bulletin
  113 + qs = qs.OrderBy(sortFields...)
  114 + if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil {
  115 + if len(fields) == 0 {
  116 + for _, v := range l {
  117 + ml = append(ml, v)
  118 + }
  119 + } else {
  120 + // trim unused fields
  121 + for _, v := range l {
  122 + m := make(map[string]interface{})
  123 + val := reflect.ValueOf(v)
  124 + for _, fname := range fields {
  125 + m[fname] = val.FieldByName(fname).Interface()
  126 + }
  127 + ml = append(ml, m)
  128 + }
  129 + }
  130 + return ml, nil
  131 + }
  132 + return nil, err
  133 +}
  134 +
  135 +// UpdateBulletin updates Bulletin by Id and returns error if
  136 +// the record to be updated doesn't exist
  137 +func UpdateBulletinById(m *Bulletin) (err error) {
  138 + o := orm.NewOrm()
  139 + v := Bulletin{Id: m.Id}
  140 + // ascertain id exists in the database
  141 + if err = o.Read(&v); err == nil {
  142 + var num int64
  143 + if num, err = o.Update(m); err == nil {
  144 + fmt.Println("Number of records updated in database:", num)
  145 + }
  146 + }
  147 + return
  148 +}
  149 +
  150 +// DeleteBulletin deletes Bulletin by Id and returns error if
  151 +// the record to be deleted doesn't exist
  152 +func DeleteBulletin(id int) (err error) {
  153 + o := orm.NewOrm()
  154 + v := Bulletin{Id: id}
  155 + // ascertain id exists in the database
  156 + if err = o.Read(&v); err == nil {
  157 + var num int64
  158 + if num, err = o.Delete(&Bulletin{Id: id}); err == nil {
  159 + fmt.Println("Number of records deleted in database:", num)
  160 + }
  161 + }
  162 + return
  163 +}
  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)" description:"公告id"`
  13 + Type int8 `orm:"column(type)" description:"类型:0-单选,1-多选"`
  14 + Title string `orm:"column(title);size(2000)" description:"标题"`
  15 + Option string `orm:"column(option);size(2000)" description:"内容"`
  16 + CreateTime time.Time `orm:"column(createTime);type(timestamp)" description:"创建时间"`
  17 + UpdateTime time.Time `orm:"column(updateTime);type(timestamp)" description:"更新时间"`
  18 +}
  19 +
  20 +func (t *BulletinQuestion) TableName() string {
  21 + return "bulletin_question"
  22 +}
  23 +
  24 +func init() {
  25 + orm.RegisterModel(new(BulletinQuestion))
  26 +}
  27 +
  28 +//BulletinQuestionOption 公告问题选项内容的结构
  29 +type BulletinQuestionOption struct {
  30 + Id int `json:"id"` //选项id
  31 + Content string `json:"content"` //选项描述
  32 +}
  33 +
  34 +// AddBulletinQuestion insert a new BulletinQuestion into database and returns
  35 +// last inserted Id on success.
  36 +func AddBulletinQuestion(m *BulletinQuestion) (id int64, err error) {
  37 + o := orm.NewOrm()
  38 + id, err = o.Insert(m)
  39 + return
  40 +}
  41 +
  42 +// GetBulletinQuestionById retrieves BulletinQuestion by Id. Returns error if
  43 +// Id doesn't exist
  44 +func GetBulletinQuestionById(id int) (v *BulletinQuestion, err error) {
  45 + o := orm.NewOrm()
  46 + v = &BulletinQuestion{Id: id}
  47 + if err = o.Read(v); err == nil {
  48 + return v, nil
  49 + }
  50 + return nil, err
  51 +}
  52 +
  53 +// UpdateBulletinQuestion updates BulletinQuestion by Id and returns error if
  54 +// the record to be updated doesn't exist
  55 +func UpdateBulletinQuestionById(m *BulletinQuestion) (err error) {
  56 + o := orm.NewOrm()
  57 + v := BulletinQuestion{Id: m.Id}
  58 + // ascertain id exists in the database
  59 + if err = o.Read(&v); err == nil {
  60 + var num int64
  61 + if num, err = o.Update(m); err == nil {
  62 + fmt.Println("Number of records updated in database:", num)
  63 + }
  64 + }
  65 + return
  66 +}
  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)" description:"答案"`
  13 + BulletinId int `orm:"column(bulletin_id)" description:"公告id"`
  14 + BulletinQuestionId int `orm:"column(bulletin_question_id)" description:"公告问题id"`
  15 + Uid int64 `orm:"column(uid)" description:"用户id"`
  16 + CreateTime time.Time `orm:"column(createTime);type(timestamp)" description:"创建时间"`
  17 + UpdateTime time.Time `orm:"column(updateTime);type(timestamp)" 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 +//公告问题用户反馈结果
  29 +type BulletinAnswerResult struct {
  30 + VoteResult []int `json:"vote_result"` //问题选项
  31 + EditContent string `json:"edit_content"` //自定义编辑内容
  32 +}
  33 +
  34 +// AddBulletinQuestionAnswer insert a new BulletinQuestionAnswer into database and returns
  35 +// last inserted Id on success.
  36 +func AddBulletinQuestionAnswer(m *BulletinQuestionAnswer) (id int64, err error) {
  37 + o := orm.NewOrm()
  38 + id, err = o.Insert(m)
  39 + return
  40 +}
  41 +
  42 +// GetBulletinQuestionAnswerById retrieves BulletinQuestionAnswer by Id. Returns error if
  43 +// Id doesn't exist
  44 +func GetBulletinQuestionAnswerById(id int) (v *BulletinQuestionAnswer, err error) {
  45 + o := orm.NewOrm()
  46 + v = &BulletinQuestionAnswer{Id: id}
  47 + if err = o.Read(v); err == nil {
  48 + return v, nil
  49 + }
  50 + return nil, err
  51 +}
  52 +
  53 +// UpdateBulletinQuestionAnswer updates BulletinQuestionAnswer by Id and returns error if
  54 +// the record to be updated doesn't exist
  55 +func UpdateBulletinQuestionAnswerById(m *BulletinQuestionAnswer) (err error) {
  56 + o := orm.NewOrm()
  57 + v := BulletinQuestionAnswer{Id: m.Id}
  58 + // ascertain id exists in the database
  59 + if err = o.Read(&v); err == nil {
  60 + var num int64
  61 + if num, err = o.Update(m); err == nil {
  62 + fmt.Println("Number of records updated in database:", num)
  63 + }
  64 + }
  65 + return
  66 +}
  67 +
  68 +// DeleteBulletinQuestionAnswer deletes BulletinQuestionAnswer by Id and returns error if
  69 +// the record to be deleted doesn't exist
  70 +func DeleteBulletinQuestionAnswer(id int) (err error) {
  71 + o := orm.NewOrm()
  72 + v := BulletinQuestionAnswer{Id: id}
  73 + // ascertain id exists in the database
  74 + if err = o.Read(&v); err == nil {
  75 + var num int64
  76 + if num, err = o.Delete(&BulletinQuestionAnswer{Id: id}); err == nil {
  77 + fmt.Println("Number of records deleted in database:", num)
  78 + }
  79 + }
  80 + return
  81 +}
@@ -84,3 +84,24 @@ func (e ErrWithMessage) ParseToMessage() *ResponseMessage { @@ -84,3 +84,24 @@ func (e ErrWithMessage) ParseToMessage() *ResponseMessage {
84 Data: nil, 84 Data: nil,
85 } 85 }
86 } 86 }
  87 +
  88 +func SearchErr(code int) ErrorCode {
  89 + return errmessge.Search(code)
  90 +}
  91 +func NewReturnResponse(data interface{}, eRR error) *ResponseMessage {
  92 + var msg *ResponseMessage
  93 + if eRR == nil {
  94 + msg = NewMesage(0)
  95 + msg.Data = data
  96 + return msg
  97 + }
  98 + // fmt.Println("日志:" + eRR.Error())
  99 + if x, ok := eRR.(CustomErrParse); ok {
  100 + return x.ParseToMessage()
  101 + }
  102 + return NewMesage(1)
  103 +}
  104 +
  105 +func BadRequestParam(code int) *ResponseMessage {
  106 + return NewMesage(code)
  107 +}
@@ -20,27 +20,6 @@ var errmessge ErrorMap = map[int]string{ @@ -20,27 +20,6 @@ var errmessge ErrorMap = map[int]string{
20 4142: "Uuid已存在,请求失败", 20 4142: "Uuid已存在,请求失败",
21 } 21 }
22 22
23 -func SearchErr(code int) ErrorCode {  
24 - return errmessge.Search(code)  
25 -}  
26 -func NewReturnResponse(data interface{}, eRR error) *ResponseMessage {  
27 - var msg *ResponseMessage  
28 - if eRR == nil {  
29 - msg = NewMesage(0)  
30 - msg.Data = data  
31 - return msg  
32 - }  
33 - // fmt.Println("日志:" + eRR.Error())  
34 - if x, ok := eRR.(CustomErrParse); ok {  
35 - return x.ParseToMessage()  
36 - }  
37 - return NewMesage(1)  
38 -}  
39 -  
40 -func BadRequestParam(code int) *ResponseMessage {  
41 - return NewMesage(code)  
42 -}  
43 -  
44 func InitMessageCode() { 23 func InitMessageCode() {
45 // messages := []struct { 24 // messages := []struct {
46 // Code int 25 // Code int
@@ -9,58 +9,58 @@ func init() { @@ -9,58 +9,58 @@ func init() {
9 9
10 beego.GlobalControllerRouter["opp/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:AuthController"], 10 beego.GlobalControllerRouter["opp/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:AuthController"],
11 beego.ControllerComments{ 11 beego.ControllerComments{
12 - Method: "AccessToken",  
13 - Router: `/accessToken`, 12 + Method: "AccessToken",
  13 + Router: `/accessToken`,
14 AllowHTTPMethods: []string{"post"}, 14 AllowHTTPMethods: []string{"post"},
15 - MethodParams: param.Make(),  
16 - Params: nil}) 15 + MethodParams: param.Make(),
  16 + Params: nil})
17 17
18 beego.GlobalControllerRouter["opp/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:AuthController"], 18 beego.GlobalControllerRouter["opp/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:AuthController"],
19 beego.ControllerComments{ 19 beego.ControllerComments{
20 - Method: "Login",  
21 - Router: `/login`, 20 + Method: "Login",
  21 + Router: `/login`,
22 AllowHTTPMethods: []string{"post"}, 22 AllowHTTPMethods: []string{"post"},
23 - MethodParams: param.Make(),  
24 - Params: nil}) 23 + MethodParams: param.Make(),
  24 + Params: nil})
25 25
26 beego.GlobalControllerRouter["opp/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:AuthController"], 26 beego.GlobalControllerRouter["opp/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:AuthController"],
27 beego.ControllerComments{ 27 beego.ControllerComments{
28 - Method: "RefreshToken",  
29 - Router: `/refreshToken`, 28 + Method: "RefreshToken",
  29 + Router: `/refreshToken`,
30 AllowHTTPMethods: []string{"post"}, 30 AllowHTTPMethods: []string{"post"},
31 - MethodParams: param.Make(),  
32 - Params: nil}) 31 + MethodParams: param.Make(),
  32 + Params: nil})
33 33
34 beego.GlobalControllerRouter["opp/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:AuthController"], 34 beego.GlobalControllerRouter["opp/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:AuthController"],
35 beego.ControllerComments{ 35 beego.ControllerComments{
36 - Method: "SmsCode",  
37 - Router: `/smsCode`, 36 + Method: "SmsCode",
  37 + Router: `/smsCode`,
38 AllowHTTPMethods: []string{"post"}, 38 AllowHTTPMethods: []string{"post"},
39 - MethodParams: param.Make(),  
40 - Params: nil}) 39 + MethodParams: param.Make(),
  40 + Params: nil})
41 41
42 beego.GlobalControllerRouter["opp/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:AuthController"], 42 beego.GlobalControllerRouter["opp/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:AuthController"],
43 beego.ControllerComments{ 43 beego.ControllerComments{
44 - Method: "UpdateDevice",  
45 - Router: `/updateDevice`, 44 + Method: "UpdateDevice",
  45 + Router: `/updateDevice`,
46 AllowHTTPMethods: []string{"post"}, 46 AllowHTTPMethods: []string{"post"},
47 - MethodParams: param.Make(),  
48 - Params: nil}) 47 + MethodParams: param.Make(),
  48 + Params: nil})
49 49
50 beego.GlobalControllerRouter["opp/controllers/v1:UploadController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:UploadController"], 50 beego.GlobalControllerRouter["opp/controllers/v1:UploadController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:UploadController"],
51 beego.ControllerComments{ 51 beego.ControllerComments{
52 - Method: "Image",  
53 - Router: `/image`, 52 + Method: "Image",
  53 + Router: `/image`,
54 AllowHTTPMethods: []string{"post"}, 54 AllowHTTPMethods: []string{"post"},
55 - MethodParams: param.Make(),  
56 - Params: nil}) 55 + MethodParams: param.Make(),
  56 + Params: nil})
57 57
58 beego.GlobalControllerRouter["opp/controllers/v1:UploadController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:UploadController"], 58 beego.GlobalControllerRouter["opp/controllers/v1:UploadController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:UploadController"],
59 beego.ControllerComments{ 59 beego.ControllerComments{
60 - Method: "Voice",  
61 - Router: `/voice`, 60 + Method: "Voice",
  61 + Router: `/voice`,
62 AllowHTTPMethods: []string{"post"}, 62 AllowHTTPMethods: []string{"post"},
63 - MethodParams: param.Make(),  
64 - Params: nil}) 63 + MethodParams: param.Make(),
  64 + Params: nil})
65 65
66 } 66 }