chance_favorite.go 5.1 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:"删除时间"`
	ChanceId     int64     `orm:"column(chance_id)" description:"机会编号"`
	CollectTime  time.Time `orm:"column(collect_time);type(timestamp);null" description:"收藏时间"`
	ZanTime      time.Time `orm:"column(zan_time);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
}

//按1.用户id  2.公司id 3.标记类型 4.机会类型编号 5.最后编号 6.页数
//获取用户点赞收藏机会
func GetChanceFavorites(userId, companyId int64, markFlag, sourceType int, sourceId int64, lastId int64, pageSize int) (v []*ChanceFavorite, total int, err error) {
	sql := mybeego.NewSqlExutor().Table("chance_favorite").Order("create_at desc")
	if userId > 0 {
		sql.Where(fmt.Sprintf("user_id=%d", userId))
	}
	sql.Where(fmt.Sprintf("company_id=%d", companyId))
	if sourceType > 0 {
		sql.Where(fmt.Sprintf("source_type=%d", sourceType))
	}
	if sourceId > 0 {
		sql.Where(fmt.Sprintf("source_id=%d", sourceId))
	}
	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 ^ ? ,update_at=now(),zan_time=now()
where user_id =? and company_id =? and source_id=? ` //
	if markFlag == 2 {
		sql = `update chance_favorite set mark_flag = mark_flag ^ ? ,update_at=now(),collect_time=now()
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
}