作者 yangfu

合并

package models
import (
"errors"
"fmt"
"reflect"
"strings"
"time"
"github.com/astaxie/beego/orm"
)
type Bulletin struct {
Id int `orm:"column(id);auto"`
Title string `orm:"column(title);size(2000)" 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:"公告类型(0图+跳转链接、1链接)"`
Receiver string `orm:"column(receiver);null" description:"接收者"`
QuestionSwitch int8 `orm:"column(question_switch);null" description:"设置问题开关"`
CreateTime time.Time `orm:"column(createTime);type(timestamp);null" description:"创建时间"`
UpdateTime time.Time `orm:"column(updateTime);type(timestamp);null" description:"更新时间"`
AllowClose int8 `orm:"column(allowClose);null" description:"允许关闭公告(0允许,1禁止)"`
CompanyId int `orm:"column(company_id);null" description:"公司Id"`
Status uint8 `orm:"column(status)" description:"状态 0-下架 1- 上架"`
}
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
}
// GetAllBulletin retrieves all Bulletin matches certain condition. Returns empty list if
// no records exist
func GetAllBulletin(query map[string]string, fields []string, sortby []string, order []string,
offset int64, limit int64) (ml []interface{}, err error) {
o := orm.NewOrm()
qs := o.QueryTable(new(Bulletin))
// query k=v
for k, v := range query {
// rewrite dot-notation to Object__Attribute
k = strings.Replace(k, ".", "__", -1)
if strings.Contains(k, "isnull") {
qs = qs.Filter(k, (v == "true" || v == "1"))
} else {
qs = qs.Filter(k, v)
}
}
// order by:
var sortFields []string
if len(sortby) != 0 {
if len(sortby) == len(order) {
// 1) for each sort field, there is an associated order
for i, v := range sortby {
orderby := ""
if order[i] == "desc" {
orderby = "-" + v
} else if order[i] == "asc" {
orderby = v
} else {
return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
}
sortFields = append(sortFields, orderby)
}
qs = qs.OrderBy(sortFields...)
} else if len(sortby) != len(order) && len(order) == 1 {
// 2) there is exactly one order, all the sorted fields will be sorted by this order
for _, v := range sortby {
orderby := ""
if order[0] == "desc" {
orderby = "-" + v
} else if order[0] == "asc" {
orderby = v
} else {
return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
}
sortFields = append(sortFields, orderby)
}
} else if len(sortby) != len(order) && len(order) != 1 {
return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1")
}
} else {
if len(order) != 0 {
return nil, errors.New("Error: unused 'order' fields")
}
}
var l []Bulletin
qs = qs.OrderBy(sortFields...)
if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil {
if len(fields) == 0 {
for _, v := range l {
ml = append(ml, v)
}
} else {
// trim unused fields
for _, v := range l {
m := make(map[string]interface{})
val := reflect.ValueOf(v)
for _, fname := range fields {
m[fname] = val.FieldByName(fname).Interface()
}
ml = append(ml, m)
}
}
return ml, 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
}
... ...
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)" description:"公告id"`
Type int8 `orm:"column(type)" description:"类型:0-单选,1-多选"`
Title string `orm:"column(title);size(2000)" description:"标题"`
Option string `orm:"column(option);size(2000)" description:"内容"`
CreateTime time.Time `orm:"column(createTime);type(timestamp)" description:"创建时间"`
UpdateTime time.Time `orm:"column(updateTime);type(timestamp)" description:"更新时间"`
}
func (t *BulletinQuestion) TableName() string {
return "bulletin_question"
}
func init() {
orm.RegisterModel(new(BulletinQuestion))
}
//BulletinQuestionOption 公告问题选项内容的结构
type BulletinQuestionOption struct {
Id int `json:"id"` //选项id
Content string `json:"content"` //选项描述
}
// 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
}
... ...
package models
import (
"fmt"
"time"
"github.com/astaxie/beego/orm"
)
type BulletinQuestionAnswer struct {
Id int `orm:"column(id);auto"`
Answer string `orm:"column(answer)" description:"答案"`
BulletinId int `orm:"column(bulletin_id)" description:"公告id"`
BulletinQuestionId int `orm:"column(bulletin_question_id)" description:"公告问题id"`
Uid int64 `orm:"column(uid)" description:"用户id"`
CreateTime time.Time `orm:"column(createTime);type(timestamp)" description:"创建时间"`
UpdateTime time.Time `orm:"column(updateTime);type(timestamp)" description:"更新时间"`
}
func (t *BulletinQuestionAnswer) TableName() string {
return "bulletin_question_answer"
}
func init() {
orm.RegisterModel(new(BulletinQuestionAnswer))
}
//公告问题用户反馈结果
type BulletinAnswerResult struct {
VoteResult []int `json:"vote_result"` //问题选项
EditContent string `json:"edit_content"` //自定义编辑内容
}
// 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
}
... ...
package models
import (
"errors"
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
"reflect"
"strings"
"time"
"github.com/astaxie/beego/orm"
)
type Commend struct {
Id int64 `orm:"column(id);auto" description:"表彰编号"`
Content string `orm:"column(content);size(255)" description:"表彰内容"`
CommendAt time.Time `orm:"column(commend_at);type(timestamp)" description:"表彰时间"`
UserId int64 `orm:"column(user_id)" description:"表user.id 表彰用户id"`
CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司id"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);auto_now_add" description:"创建时间"`
EnableStatus int8 `orm:"column(enable_status)" description:"状态 1可见 2不可见"`
}
func (t *Commend) TableName() string {
return "commend"
}
func init() {
orm.RegisterModel(new(Commend))
}
// AddCommend insert a new Commend into database and returns
// last inserted Id on success.
func AddCommend(m *Commend) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetCommendById retrieves Commend by Id. Returns error if
// Id doesn't exist
func GetCommendById(id int64) (v *Commend, err error) {
o := orm.NewOrm()
v = &Commend{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// GetAllCommend retrieves all Commend matches certain condition. Returns empty list if
// no records exist
func GetAllCommend(query map[string]string, fields []string, sortby []string, order []string,
offset int64, limit int64) (ml []interface{}, err error) {
o := orm.NewOrm()
qs := o.QueryTable(new(Commend))
// query k=v
for k, v := range query {
// rewrite dot-notation to Object__Attribute
k = strings.Replace(k, ".", "__", -1)
if strings.Contains(k, "isnull") {
qs = qs.Filter(k, (v == "true" || v == "1"))
} else {
qs = qs.Filter(k, v)
}
}
// order by:
var sortFields []string
if len(sortby) != 0 {
if len(sortby) == len(order) {
// 1) for each sort field, there is an associated order
for i, v := range sortby {
orderby := ""
if order[i] == "desc" {
orderby = "-" + v
} else if order[i] == "asc" {
orderby = v
} else {
return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
}
sortFields = append(sortFields, orderby)
}
qs = qs.OrderBy(sortFields...)
} else if len(sortby) != len(order) && len(order) == 1 {
// 2) there is exactly one order, all the sorted fields will be sorted by this order
for _, v := range sortby {
orderby := ""
if order[0] == "desc" {
orderby = "-" + v
} else if order[0] == "asc" {
orderby = v
} else {
return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
}
sortFields = append(sortFields, orderby)
}
} else if len(sortby) != len(order) && len(order) != 1 {
return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1")
}
} else {
if len(order) != 0 {
return nil, errors.New("Error: unused 'order' fields")
}
}
var l []Commend
qs = qs.OrderBy(sortFields...)
if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil {
if len(fields) == 0 {
for _, v := range l {
ml = append(ml, v)
}
} else {
// trim unused fields
for _, v := range l {
m := make(map[string]interface{})
val := reflect.ValueOf(v)
for _, fname := range fields {
m[fname] = val.FieldByName(fname).Interface()
}
ml = append(ml, m)
}
}
return ml, nil
}
return nil, err
}
// UpdateCommend updates Commend by Id and returns error if
// the record to be updated doesn't exist
func UpdateCommendById(m *Commend) (err error) {
o := orm.NewOrm()
v := Commend{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
}
// DeleteCommend deletes Commend by Id and returns error if
// the record to be deleted doesn't exist
func DeleteCommend(id int64) (err error) {
o := orm.NewOrm()
v := Commend{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&Commend{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
func GetCommends(companyId int,lastId int,pageSize int)(v []*Commend,total int, err error) {
sql :=mybeego.NewSqlExutor().Table("commend").Order("create_at desc")
sql.Where(fmt.Sprintf("company_id=%d",companyId))
if pageSize>0{
sql.Limit(0,pageSize)
}
if lastId>0{
sql.Where(fmt.Sprintf("id>%d",lastId))
}
if total, err = sql.Querys(&v); err == nil {
return
}
return
}
... ...
package models
import (
"errors"
"fmt"
"reflect"
"strings"
"time"
"github.com/astaxie/beego/orm"
)
type Company struct {
Id int64 `orm:"column(id);auto"`
Name string `orm:"column(name);size(40)"`
UserId int64 `orm:"column(user_id)"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);auto_now"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp)"`
DeleteAt time.Time `orm:"column(delete_at);type(timestamp)"`
Logo string `orm:"column(logo);size(255)"`
}
func (t *Company) TableName() string {
return "company"
}
func init() {
orm.RegisterModel(new(Company))
}
// AddCompany insert a new Company into database and returns
// last inserted Id on success.
func AddCompany(m *Company) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetCompanyById retrieves Company by Id. Returns error if
// Id doesn't exist
func GetCompanyById(id int64) (v *Company, err error) {
o := orm.NewOrm()
v = &Company{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// GetAllCompany retrieves all Company matches certain condition. Returns empty list if
// no records exist
func GetAllCompany(query map[string]string, fields []string, sortby []string, order []string,
offset int64, limit int64) (ml []interface{}, err error) {
o := orm.NewOrm()
qs := o.QueryTable(new(Company))
// query k=v
for k, v := range query {
// rewrite dot-notation to Object__Attribute
k = strings.Replace(k, ".", "__", -1)
if strings.Contains(k, "isnull") {
qs = qs.Filter(k, (v == "true" || v == "1"))
} else {
qs = qs.Filter(k, v)
}
}
// order by:
var sortFields []string
if len(sortby) != 0 {
if len(sortby) == len(order) {
// 1) for each sort field, there is an associated order
for i, v := range sortby {
orderby := ""
if order[i] == "desc" {
orderby = "-" + v
} else if order[i] == "asc" {
orderby = v
} else {
return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
}
sortFields = append(sortFields, orderby)
}
qs = qs.OrderBy(sortFields...)
} else if len(sortby) != len(order) && len(order) == 1 {
// 2) there is exactly one order, all the sorted fields will be sorted by this order
for _, v := range sortby {
orderby := ""
if order[0] == "desc" {
orderby = "-" + v
} else if order[0] == "asc" {
orderby = v
} else {
return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
}
sortFields = append(sortFields, orderby)
}
} else if len(sortby) != len(order) && len(order) != 1 {
return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1")
}
} else {
if len(order) != 0 {
return nil, errors.New("Error: unused 'order' fields")
}
}
var l []Company
qs = qs.OrderBy(sortFields...)
if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil {
if len(fields) == 0 {
for _, v := range l {
ml = append(ml, v)
}
} else {
// trim unused fields
for _, v := range l {
m := make(map[string]interface{})
val := reflect.ValueOf(v)
for _, fname := range fields {
m[fname] = val.FieldByName(fname).Interface()
}
ml = append(ml, m)
}
}
return ml, nil
}
return nil, err
}
// UpdateCompany updates Company by Id and returns error if
// the record to be updated doesn't exist
func UpdateCompanyById(m *Company) (err error) {
o := orm.NewOrm()
v := Company{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
}
// DeleteCompany deletes Company by Id and returns error if
// the record to be deleted doesn't exist
func DeleteCompany(id int64) (err error) {
o := orm.NewOrm()
v := Company{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&Company{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
... ...
package models
import (
"errors"
"fmt"
"reflect"
"strings"
"time"
"github.com/astaxie/beego/orm"
)
type Department struct {
Id int `orm:"column(id);auto"`
CompanyId int `orm:"column(company_id)" description:"公司id"`
Name string `orm:"column(name);size(30)" description:"部门名称"`
CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
ParentId int `orm:"column(parent_id)" description:"父级id"`
Relation string `orm:"column(relation);size(400)" description:"父子级关系树"`
DeleteAt time.Time `orm:"column(delete_at);type(timestamp)" description:"删除时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
Member int `orm:"column(member)" description:"成员数量"`
Admin int `orm:"column(admin);null" description:"部门负责人id"`
}
func (t *Department) TableName() string {
return "department"
}
func init() {
orm.RegisterModel(new(Department))
}
// AddDepartment insert a new Department into database and returns
// last inserted Id on success.
func AddDepartment(m *Department) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetDepartmentById retrieves Department by Id. Returns error if
// Id doesn't exist
func GetDepartmentById(id int) (v *Department, err error) {
o := orm.NewOrm()
v = &Department{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// GetAllDepartment retrieves all Department matches certain condition. Returns empty list if
// no records exist
func GetAllDepartment(query map[string]string, fields []string, sortby []string, order []string,
offset int64, limit int64) (ml []interface{}, err error) {
o := orm.NewOrm()
qs := o.QueryTable(new(Department))
// query k=v
for k, v := range query {
// rewrite dot-notation to Object__Attribute
k = strings.Replace(k, ".", "__", -1)
if strings.Contains(k, "isnull") {
qs = qs.Filter(k, (v == "true" || v == "1"))
} else {
qs = qs.Filter(k, v)
}
}
// order by:
var sortFields []string
if len(sortby) != 0 {
if len(sortby) == len(order) {
// 1) for each sort field, there is an associated order
for i, v := range sortby {
orderby := ""
if order[i] == "desc" {
orderby = "-" + v
} else if order[i] == "asc" {
orderby = v
} else {
return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
}
sortFields = append(sortFields, orderby)
}
qs = qs.OrderBy(sortFields...)
} else if len(sortby) != len(order) && len(order) == 1 {
// 2) there is exactly one order, all the sorted fields will be sorted by this order
for _, v := range sortby {
orderby := ""
if order[0] == "desc" {
orderby = "-" + v
} else if order[0] == "asc" {
orderby = v
} else {
return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
}
sortFields = append(sortFields, orderby)
}
} else if len(sortby) != len(order) && len(order) != 1 {
return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1")
}
} else {
if len(order) != 0 {
return nil, errors.New("Error: unused 'order' fields")
}
}
var l []Department
qs = qs.OrderBy(sortFields...)
if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil {
if len(fields) == 0 {
for _, v := range l {
ml = append(ml, v)
}
} else {
// trim unused fields
for _, v := range l {
m := make(map[string]interface{})
val := reflect.ValueOf(v)
for _, fname := range fields {
m[fname] = val.FieldByName(fname).Interface()
}
ml = append(ml, m)
}
}
return ml, nil
}
return nil, err
}
// UpdateDepartment updates Department by Id and returns error if
// the record to be updated doesn't exist
func UpdateDepartmentById(m *Department) (err error) {
o := orm.NewOrm()
v := Department{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
}
// DeleteDepartment deletes Department by Id and returns error if
// the record to be deleted doesn't exist
func DeleteDepartment(id int) (err error) {
o := orm.NewOrm()
v := Department{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&Department{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
... ...
package models
import (
"errors"
"fmt"
"reflect"
"strings"
"time"
"github.com/astaxie/beego/orm"
)
type Position struct {
Id int `orm:"column(id);auto" description:"职位表id"`
CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"`
Name string `orm:"column(name);size(100)" description:"职位名称"`
ParentId int `orm:"column(parent_id)" description:"父级id"`
Relation string `orm:"column(relation);size(400)" description:"父子级关系树"`
CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
EnableStatus string `orm:"column(enable_status);size(255)" description:"有效状态 1:有效 0:无效"`
}
func (t *Position) TableName() string {
return "position"
}
func init() {
orm.RegisterModel(new(Position))
}
// AddPosition insert a new Position into database and returns
// last inserted Id on success.
func AddPosition(m *Position) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetPositionById retrieves Position by Id. Returns error if
// Id doesn't exist
func GetPositionById(id int) (v *Position, err error) {
o := orm.NewOrm()
v = &Position{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// GetAllPosition retrieves all Position matches certain condition. Returns empty list if
// no records exist
func GetAllPosition(query map[string]string, fields []string, sortby []string, order []string,
offset int64, limit int64) (ml []interface{}, err error) {
o := orm.NewOrm()
qs := o.QueryTable(new(Position))
// query k=v
for k, v := range query {
// rewrite dot-notation to Object__Attribute
k = strings.Replace(k, ".", "__", -1)
if strings.Contains(k, "isnull") {
qs = qs.Filter(k, (v == "true" || v == "1"))
} else {
qs = qs.Filter(k, v)
}
}
// order by:
var sortFields []string
if len(sortby) != 0 {
if len(sortby) == len(order) {
// 1) for each sort field, there is an associated order
for i, v := range sortby {
orderby := ""
if order[i] == "desc" {
orderby = "-" + v
} else if order[i] == "asc" {
orderby = v
} else {
return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
}
sortFields = append(sortFields, orderby)
}
qs = qs.OrderBy(sortFields...)
} else if len(sortby) != len(order) && len(order) == 1 {
// 2) there is exactly one order, all the sorted fields will be sorted by this order
for _, v := range sortby {
orderby := ""
if order[0] == "desc" {
orderby = "-" + v
} else if order[0] == "asc" {
orderby = v
} else {
return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
}
sortFields = append(sortFields, orderby)
}
} else if len(sortby) != len(order) && len(order) != 1 {
return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1")
}
} else {
if len(order) != 0 {
return nil, errors.New("Error: unused 'order' fields")
}
}
var l []Position
qs = qs.OrderBy(sortFields...)
if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil {
if len(fields) == 0 {
for _, v := range l {
ml = append(ml, v)
}
} else {
// trim unused fields
for _, v := range l {
m := make(map[string]interface{})
val := reflect.ValueOf(v)
for _, fname := range fields {
m[fname] = val.FieldByName(fname).Interface()
}
ml = append(ml, m)
}
}
return ml, nil
}
return nil, err
}
// UpdatePosition updates Position by Id and returns error if
// the record to be updated doesn't exist
func UpdatePositionById(m *Position) (err error) {
o := orm.NewOrm()
v := Position{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
}
// DeletePosition deletes Position by Id and returns error if
// the record to be deleted doesn't exist
func DeletePosition(id int) (err error) {
o := orm.NewOrm()
v := Position{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&Position{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
... ...
... ... @@ -84,3 +84,24 @@ func (e ErrWithMessage) ParseToMessage() *ResponseMessage {
Data: nil,
}
}
func SearchErr(code int) ErrorCode {
return errmessge.Search(code)
}
func NewReturnResponse(data interface{}, eRR error) *ResponseMessage {
var msg *ResponseMessage
if eRR == nil {
msg = NewMesage(0)
msg.Data = data
return msg
}
// fmt.Println("日志:" + eRR.Error())
if x, ok := eRR.(CustomErrParse); ok {
return x.ParseToMessage()
}
return NewMesage(1)
}
func BadRequestParam(code int) *ResponseMessage {
return NewMesage(code)
}
... ...
... ... @@ -41,9 +41,6 @@ func NewReturnResponse(data interface{}, eRR error) *ResponseMessage {
return NewMesage(1)
}
func BadRequestParam(code int) *ResponseMessage {
return NewMesage(code)
}
func InitMessageCode() {
// messages := []struct {
... ...
... ... @@ -9,43 +9,43 @@ func init() {
beego.GlobalControllerRouter["opp/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:AuthController"],
beego.ControllerComments{
Method: "AccessToken",
Router: `/accessToken`,
Method: "AccessToken",
Router: `/accessToken`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["opp/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:AuthController"],
beego.ControllerComments{
Method: "Login",
Router: `/login`,
Method: "Login",
Router: `/login`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["opp/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:AuthController"],
beego.ControllerComments{
Method: "RefreshToken",
Router: `/refreshToken`,
Method: "RefreshToken",
Router: `/refreshToken`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["opp/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:AuthController"],
beego.ControllerComments{
Method: "SmsCode",
Router: `/smsCode`,
Method: "SmsCode",
Router: `/smsCode`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["opp/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:AuthController"],
beego.ControllerComments{
Method: "UpdateDevice",
Router: `/updateDevice`,
Method: "UpdateDevice",
Router: `/updateDevice`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["opp/controllers/v1:CommendController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:CommendController"],
beego.ControllerComments{
... ... @@ -57,18 +57,18 @@ func init() {
beego.GlobalControllerRouter["opp/controllers/v1:UploadController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:UploadController"],
beego.ControllerComments{
Method: "Image",
Router: `/image`,
Method: "Image",
Router: `/image`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["opp/controllers/v1:UploadController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:UploadController"],
beego.ControllerComments{
Method: "Voice",
Router: `/voice`,
Method: "Voice",
Router: `/voice`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
MethodParams: param.Make(),
Params: nil})
}
... ...