互动
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.

80 lines
4.7 KiB

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. ActivityId int64 `json:"activity_id" xorm:"not null default 0 comment('主活动id') INT(11)"`
  10. RehearsalId int64 `json:"rehearsal_id" xorm:"not null default(0) comment('彩排id/0正式') INT(11)"`
  11. ShakeRedEnvelopeActivityId int64 `json:"shake_red_envelope_activity_id" xorm:"not null default 0 comment('摇红包活动id') INT(11)"`
  12. ShakeRedEnvelopeRuleId int64 `json:"shake_red_envelope_rule_id" xorm:"not null default 0 comment('摇红包规则id') INT(11)"`
  13. UserId int64 `json:"user_id" xorm:"not null default 0 comment('用户id') INT(11)"`
  14. IsValid bool `json:"is_valid" xorm:"-"`
  15. Name string `json:"name" xorm:"not null default '' comment('红包名字') VARCHAR(255)"`
  16. UserName string `json:"user_name" xorm:"not null default '' comment('用户名字') VARCHAR(255)"`
  17. UserPhone string `json:"user_phone" xorm:"not null default '' comment('用户电话') VARCHAR(18)"`
  18. Amount float64 `json:"amount" xorm:"not null default '0' comment('金额') VARCHAR(18)"`
  19. AreaId int64 `json:"area_id" xorm:"not null default 0 comment('地区id') INT(11)"`
  20. AreaName string `json:"area_name" xorm:"not null default '' comment('地区名字') VARCHAR(18)"`
  21. IsDraw int `json:"is_draw" xorm:"not null default(0) comment('-1未被摇中,0已被摇中,1已被提现')"`
  22. Reason string `json:"reason" xorm:"not null default '' comment('失败原因') VARCHAR(32)"`
  23. MchBillno string `json:"mch_billno" xorm:"not null default 0 comment('')"`
  24. ReOpenid string `json:"re_openid" xorm:"not null default '' comment('接收红包用户openid') VARCHAR(32)"`
  25. Status string `json:"status" xorm:"not null default '' comment('红包状态') VARCHAR(16)"`
  26. RefundTime string `json:"refund_time" xorm:"not null default '' comment('红包退款时间') VARCHAR(32)"`
  27. Version int `json:"version" xorm:"not null version comment('乐观锁') INT(11)"`
  28. IsDelete bool `json:"-" xorm:"default(0)" description:"是否删除"`
  29. CreatedAt time.Time `json:"-" xorm:"created" description:"创建时间"`
  30. UpdatedAt time.Time `json:"-" xorm:"updated" description:"更新时间"`
  31. }
  32. func (t *ShakeRedEnvelopeRecord) TableName() string {
  33. return ShakeRedEnvelopeRecordTableName
  34. }
  35. func (t *ShakeRedEnvelopeRecord) ExistRecord(reid, aid, srid, said, uid int64) (bool, error) {
  36. return core.GetXormAuto().Where("is_delete=0 and rehearsal_id=? and activity_id=? and "+
  37. "shake_red_envelope_rule_id=? and shake_red_envelope_activity_id=? and user_id=?",
  38. reid, aid, srid, said, uid).Exist(t)
  39. }
  40. func (t *ShakeRedEnvelopeRecord) Total(aid, rid, said, srid int64) (int64, error) {
  41. return core.GetXormAuto().Where("is_delete=0 and activity_id=? and rehearsal_id=? and "+
  42. "shake_red_envelope_activity_id=? and shake_red_envelope_rule_id=?", aid, rid, said, srid).Count(t)
  43. }
  44. func (t *ShakeRedEnvelopeRecord) Sum(aid, rid, said, srid int64) (float64, error) {
  45. return core.GetXormAuto().Where("is_delete=0 and activity_id=? and rehearsal_id=? and "+
  46. "shake_red_envelope_activity_id=? and shake_red_envelope_rule_id=?", aid, rid, said, srid).
  47. Sum(t, "amount")
  48. }
  49. func (t *ShakeRedEnvelopeRecord) Count(aid, rid, said, srid int64, status int64) (int64, error) {
  50. return core.GetXormAuto().Where("is_delete=0 and is_draw=? and activity_id=? and rehearsal_id=? and "+
  51. "shake_red_envelope_activity_id=? and shake_red_envelope_rule_id=?", status, aid, rid, said, srid).Count(t)
  52. }
  53. func (t *ShakeRedEnvelopeRecord) GetByRuleId(ruleId, rehearsalId int64) (bool, error) {
  54. return core.GetXormAuto().Where("is_delete=0 and is_draw=-1 and shake_red_envelope_rule_id=? and rehearsal_id=?",
  55. ruleId, rehearsalId).Get(t)
  56. }
  57. func (t *ShakeRedEnvelopeRecord) UpdateAllColsById(id int64) (int64, error) {
  58. return core.GetXormAuto().Where("is_delete=0 and id=?", id).AllCols().Update(t)
  59. }
  60. func GetRedEnvelopesByUserId(userId int64) ([]*ShakeRedEnvelopeRecord, error) {
  61. records := make([]*ShakeRedEnvelopeRecord, 0)
  62. err := core.GetXormAuto().Where("user_id=? and is_delete=0 and is_draw<>-1", userId).
  63. Desc("created_at").Find(&records)
  64. for index := range records {
  65. records[index].IsValid = true
  66. }
  67. return records, err
  68. }