chance_favorite.go 4.4 KB
package models

import (
	"fmt"
	"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
	"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
	"time"

	"github.com/astaxie/beego/orm"
)

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 公司编号"`
	MarkFlag   int       `orm:"column(mark_flag)" description:"类型 1:点赞 2:收藏"`
	SourceType   int       `orm:"column(source_type)" description:"来源类型 1:机会 2:评论"`
	SourceId     int64     `orm:"column(source_id)" description:"来源id 机会编号/评论编号"`
	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:"创建时间"`
	UpdateAt     time.Time `orm:"column(update_at);type(timestamp);null" description:"删除时间"`
}

func (t *ChanceFavorite) TableName() string {
	return "chance_favorite"
}

func init() {
	orm.RegisterModel(new(ChanceFavorite))
}

// AddChanceFavorite insert a new ChanceFavorite into database and returns
// last inserted Id on success.
func AddChanceFavorite(m *ChanceFavorite) (id int64, err error) {
	o := orm.NewOrm()
	id, err = o.Insert(m)
	return
}

// GetChanceFavoriteById retrieves ChanceFavorite by Id. Returns error if
// Id doesn't exist
func GetChanceFavoriteById(id int64) (v *ChanceFavorite, err error) {
	o := orm.NewOrm()
	v = &ChanceFavorite{Id: id}
	if err = o.Read(v); err == nil {
		return v, 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) {
	o := orm.NewOrm()
	v := ChanceFavorite{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
}

// DeleteChanceFavorite deletes ChanceFavorite by Id and returns error if
// the record to be deleted doesn't exist
func DeleteChanceFavorite(id int64) (err error) {
	o := orm.NewOrm()
	v := ChanceFavorite{Id: id}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Delete(&ChanceFavorite{Id: id}); err == nil {
			fmt.Println("Number of records deleted in database:", num)
		}
	}
	return
}

func GetChanceFavorites(userId, companyId int64, markFlag, 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", companyId))
	if chanceType > 0 {
		sql.Where(fmt.Sprintf("chance_type=%d", chanceType))
	}
	if markFlag > 0 {
		sql.Where(fmt.Sprintf("mark_flag&%d>0", markFlag))
	}
	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

}

func ExitsChanceFavorite(userId, companyId int64, sourceId int64, markFlag int) (exits bool, err error) {
	sql := mybeego.NewSqlExutor().Table("chance_favorite")
	sql.Where(fmt.Sprintf("source_id=%d", sourceId)).
		Where(fmt.Sprintf("user_id=%d", userId)).
		Where(fmt.Sprintf("company_id=%d", companyId)).
		Where(fmt.Sprintf("enable_status=1"))
	if markFlag>0{
		sql.Where(fmt.Sprintf("(mark_flag&%d)>0", markFlag))
	}
	return sql.QueryExists()
}


func UpdateChanceFavorite(userId, companyId int64,sourceId int64,markFlag int)(err error){
	o := orm.NewOrm()
	sql := `update chance_favorite set mark_flag = mark_flag ^ ? 
where user_id =? and company_id =? and source_id=? ` //
	if _,err = o.Raw(sql,markFlag,userId,companyId,sourceId).Exec(); err == nil {
		return
	}
	return
}

func GetChanceFavorite(userId, companyId int64,sourceId int64,sourceType int)(v *ChanceFavorite,err error){
	o :=orm.NewOrm()
	sql :=`select * from chance_favorite where user_id =? and company_id =? and source_id=? and source_type=?`
	if err = o.Raw(sql,userId,companyId,sourceId,sourceType).QueryRow(&v);err!=nil{
		return
	}
	return
}