互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
4.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package models
  2. import (
  3. "github.com/ouxuanserver/osmanthuswine/src/core"
  4. "time"
  5. )
  6. const ShakeRedEnvelopeRecordTableName = TableNamePrefix + "shake_red_envelope_record"
  7. type ShakeRedEnvelopeRecord struct {
  8. Id int64 `json:"id" xorm:"not null pk autoincr comment('主键') INT(11)"`
  9. IsDelete bool `json:"-" xorm:"default(0)" description:"是否删除"`
  10. CreatedAt time.Time `json:"-" xorm:"created" description:"创建时间"`
  11. UpdatedAt time.Time `json:"-" xorm:"updated" description:"更新时间"`
  12. ActivityId int64 `json:"activity_id" xorm:"not null default 0 comment('主活动id') INT(11)"`
  13. RehearsalId int64 `json:"rehearsal_id" xorm:"not null default(0) comment('彩排id/0正式') INT(11)"`
  14. RedEnvelopeType int `json:"red_envelope_type" xorm:"not null default 0 comment('红包类型[0摇红包1直播红包]')"`
  15. LiveRedEnvelopeRuleId int64 `json:"live_red_envelope_rule_id,omitempty" xorm:"not null default 0 comment('直播红包id') INT(11)"`
  16. ShakeRedEnvelopeActivityId int64 `json:"shake_red_envelope_activity_id,omitempty" xorm:"not null default 0 comment('摇红包活动id') INT(11)"`
  17. ShakeRedEnvelopeRuleId int64 `json:"shake_red_envelope_rule_id,omitempty" xorm:"not null default 0 comment('摇红包规则id') INT(11)"`
  18. AreaId int64 `json:"area_id" xorm:"not null default 0 comment('地区id') INT(11)"`
  19. AreaName string `json:"area_name" xorm:"not null default '' comment('地区名字') VARCHAR(18)"`
  20. Name string `json:"name" xorm:"not null default '' comment('红包名字') VARCHAR(255)"`
  21. UserId int64 `json:"user_id" xorm:"not null default 0 comment('用户id') INT(11)"`
  22. Amount float64 `json:"amount" xorm:"not null default '0' comment('金额') DECIMAL(18,2)"`
  23. IsDraw int `json:"is_draw" xorm:"not null default(0) comment('-1未被摇中,0已被摇中,1已被提现,2提现失败')"`
  24. TransferType int `json:"transfer_type" xorm:"not null default 0 comment('0微信红包 1 微信转账') TINYINT(1)"`
  25. PartnerTradeNo string `json:"partner_trade_no" xorm:"not null default '' comment('转账账单no') VARCHAR(128)"`
  26. Version int `json:"version" xorm:"not null version comment('乐观锁') INT(11)"`
  27. // 无关变量
  28. // Status string `json:"status" xorm:"not null default '' comment('红包状态') VARCHAR(16)"`
  29. IsValid bool `json:"is_valid,omitempty" xorm:"-"`
  30. }
  31. func (t *ShakeRedEnvelopeRecord) TableName() string {
  32. return ShakeRedEnvelopeRecordTableName
  33. }
  34. func (t *ShakeRedEnvelopeRecord) ExistRecord(reid, aid, srid, said, uid int64) (bool, error) {
  35. return core.GetXormAuto().Where("is_delete=0 and rehearsal_id=? and activity_id=? and "+
  36. "shake_red_envelope_rule_id=? and shake_red_envelope_activity_id=? and user_id=?",
  37. reid, aid, srid, said, uid).Exist(t)
  38. }
  39. func (t *ShakeRedEnvelopeRecord) Add() (int64, error) {
  40. return core.GetXormAuto().InsertOne(t)
  41. }
  42. func (t *ShakeRedEnvelopeRecord) Total(aid, rid, said, srid int64) (int64, error) {
  43. return core.GetXormAuto().Where("is_delete=0 and activity_id=? and rehearsal_id=? and "+
  44. "shake_red_envelope_activity_id=? and shake_red_envelope_rule_id=?", aid, rid, said, srid).Count(t)
  45. }
  46. func (t *ShakeRedEnvelopeRecord) Sum(aid, rid, said, srid int64) (float64, error) {
  47. return core.GetXormAuto().Where("is_delete=0 and activity_id=? and rehearsal_id=? and "+
  48. "shake_red_envelope_activity_id=? and shake_red_envelope_rule_id=?", aid, rid, said, srid).
  49. Sum(t, "amount")
  50. }
  51. func (t *ShakeRedEnvelopeRecord) Count(aid, rid, said, srid int64, status int64) (int64, error) {
  52. return core.GetXormAuto().Where("is_delete=0 and is_draw=? and activity_id=? and rehearsal_id=? and "+
  53. "shake_red_envelope_activity_id=? and shake_red_envelope_rule_id=?", status, aid, rid, said, srid).Count(t)
  54. }
  55. func (t *ShakeRedEnvelopeRecord) GetByRuleId(ruleId, rehearsalId int64) (bool, error) {
  56. return core.GetXormAuto().Where("is_delete=0 and is_draw=-1 and shake_red_envelope_rule_id=? and rehearsal_id=?",
  57. ruleId, rehearsalId).Get(t)
  58. }
  59. func (t *ShakeRedEnvelopeRecord) UpdateAllColsById(id int64) (int64, error) {
  60. return core.GetXormAuto().Where("is_delete=0 and id=?", id).AllCols().Update(t)
  61. }
  62. func GetRedEnvelopesByUserId(userId int64) ([]*ShakeRedEnvelopeRecord, error) {
  63. records := make([]*ShakeRedEnvelopeRecord, 0)
  64. err := core.GetXormAuto().Where("user_id=? and is_delete=0 and is_draw<>-1", userId).
  65. Desc("created_at").Find(&records)
  66. for index := range records {
  67. records[index].IsValid = true
  68. }
  69. return records, err
  70. }