|
|
package models
|
|
|
|
|
|
import (
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"reflect"
|
|
|
"strings"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
|
|
|
"time"
|
|
|
|
|
|
"github.com/astaxie/beego/orm"
|
...
|
...
|
@@ -13,10 +12,11 @@ import ( |
|
|
type ChanceFavorite struct {
|
|
|
Id int64 `orm:"column(id);pk" description:"点赞编号"`
|
|
|
UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"`
|
|
|
CompanyId int64 `orm:"column(company_id)" description:"company.id 公司编号"`
|
|
|
ObjectType int `orm:"column(object_type)" description:"类型 1:点赞 2:收藏"`
|
|
|
SourceType int `orm:"column(source_type)" description:"来源类型 1:机会 2:评论"`
|
|
|
SourceId int64 `orm:"column(source_id)" description:"来源id 机会编号/评论编号"`
|
|
|
ChanceTypeId int `orm:"column(chance_type_id)" description:"机会类型编号 - 附加 "`
|
|
|
ChanceType int `orm:"column(chance_type)" description:"机会类型编号 - 附加 "`
|
|
|
EnableStatus int `orm:"column(enable_status)" description:"1:有效 0:无效"`
|
|
|
CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
|
|
|
DeleteAt time.Time `orm:"column(delete_at);type(timestamp);null" description:"删除时间"`
|
...
|
...
|
@@ -49,84 +49,6 @@ func GetChanceFavoriteById(id int64) (v *ChanceFavorite, err error) { |
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
// GetAllChanceFavorite retrieves all ChanceFavorite matches certain condition. Returns empty list if
|
|
|
// no records exist
|
|
|
func GetAllChanceFavorite(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(ChanceFavorite))
|
|
|
// 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 []ChanceFavorite
|
|
|
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
|
|
|
}
|
|
|
|
|
|
// UpdateChanceFavorite updates ChanceFavorite by Id and returns error if
|
|
|
// the record to be updated doesn't exist
|
|
|
func UpdateChanceFavoriteById(m *ChanceFavorite) (err error) {
|
...
|
...
|
@@ -156,3 +78,27 @@ func DeleteChanceFavorite(id int64) (err error) { |
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
func GetChanceFavorites(userId, companyId int64, objectType, chanceType int, lastId int64, pageSize int) (v []*ChanceFavorite, total int, err error) {
|
|
|
sql := mybeego.NewSqlExutor().Table("chance_favorite").Order("create_at desc")
|
|
|
sql.Where(fmt.Sprintf("user_id=%d", userId))
|
|
|
sql.Where(fmt.Sprintf("company_id=%d", userId))
|
|
|
if chanceType > 0 {
|
|
|
sql.Where(fmt.Sprintf("chance_type=%d", chanceType))
|
|
|
}
|
|
|
if objectType > 0 {
|
|
|
sql.Where(fmt.Sprintf("object_type&%d>0", objectType))
|
|
|
}
|
|
|
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
|
|
|
}
|
|
|
log.Error(sql.Strings())
|
|
|
return
|
|
|
|
|
|
} |
...
|
...
|
|