123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package game
  2. import (
  3. "support-game/models"
  4. "support-game/models/model"
  5. "support-game/utils"
  6. "time"
  7. "github.com/go-xorm/xorm"
  8. )
  9. // SupportDao 当前数据库操作对象
  10. type SupportDao struct {
  11. ctx *utils.Context
  12. db *xorm.Session
  13. }
  14. // NewSupportDao 初始化DAO
  15. func NewSupportDao(ctx *utils.Context) *SupportDao {
  16. return &SupportDao{
  17. ctx: ctx,
  18. db: ctx.DB,
  19. }
  20. }
  21. // GetCustomerByOpenID 根据openid获取用户信息
  22. func (m *SupportDao) GetCustomerByOpenID(openid string) (*model.TaCustomer, error) {
  23. var customer = model.TaCustomer{}
  24. _, err := m.db.Where("openid=?", openid).Get(&customer)
  25. return &customer, err
  26. }
  27. // SaveCustomer 保存用户信息
  28. func (m *SupportDao) SaveCustomer(customer model.TaCustomer) (*model.TaCustomer, error) {
  29. customer.CustomerId = utils.GetGUID()
  30. customer.CreateDate = time.Now()
  31. _, err := m.db.Insert(customer)
  32. return &customer, err
  33. }
  34. // UpdateCustomer 修改用户信息
  35. func (m *SupportDao) UpdateCustomer(customer *model.TaCustomer, cols []string) error {
  36. _, err := m.db.Cols(cols...).Where("customer_id=?", customer.CustomerId).Update(customer)
  37. return err
  38. }
  39. // UpdateRecord 修改活动信息
  40. func (m *SupportDao) UpdateRecord(record *model.TaGameRecord, cols []string) error {
  41. _, err := m.db.Cols(cols...).Where("record_id=?", record.RecordId).Update(record)
  42. return err
  43. }
  44. // GetCustomerByID 根据id获取用户信息
  45. func (m *SupportDao) GetCustomerByID(id string) (*model.TaCustomer, error) {
  46. var customer = model.TaCustomer{}
  47. _, err := m.db.Where("customer_id=?", id).Get(&customer)
  48. return &customer, err
  49. }
  50. // GetRecordByID 根据id获取记录
  51. func (m *SupportDao) GetRecordByID(recordid string) (*model.TaGameRecord, error) {
  52. var record model.TaGameRecord
  53. _, err := m.db.Where("record_id=?", recordid).Get(&record)
  54. return &record, err
  55. }
  56. // RecordSupport 助力记录
  57. type RecordSupport struct {
  58. model.TaRecordSuppot `xorm:"extends"`
  59. CustomerName string
  60. Name string
  61. HeadImg string
  62. }
  63. // GetSupportsByRecord 获取助力信息
  64. func (m *SupportDao) GetSupportsByRecord(recordid string) ([]RecordSupport, error) {
  65. var supports []RecordSupport
  66. sql := `select a.*,b.customer_name,b.name,b.head_img from ta_record_suppot a inner join ta_customer b on a.customer_id=b.customer_id where record_id=?`
  67. err := m.db.Sql(sql, recordid).Find(&supports)
  68. return supports, err
  69. }
  70. // GetTodayQuota 获取今天成功的活动
  71. func (m *SupportDao) GetTodayQuota(gameid string) ([]model.TaGameRecord, error) {
  72. var records []model.TaGameRecord
  73. sql := `select * from ta_game_record where date_format(create_date,'%Y-%m-%d') = date_format(now(),'%Y-%m-%d') and status = ? and game_id=?`
  74. err := m.db.Sql(sql, models.STATUS_NORMAL, gameid).Find(&records)
  75. return records, err
  76. }
  77. // GetTodayCustomerRecord 获取客户今天参与的活动
  78. func (m *SupportDao) GetTodayCustomerRecord(customerid, gameid string) ([]model.TaGameRecord, error) {
  79. var records []model.TaGameRecord
  80. sql := `select * from ta_game_record where date_format(create_date,'%Y-%m-%d') = date_format(now(),'%Y-%m-%d') and customer_id=? and game_id=?`
  81. err := m.db.Sql(sql, customerid, gameid).Find(&records)
  82. return records, err
  83. }
  84. // GetCustomerQuota 获取用户成功的活动
  85. func (m *SupportDao) GetCustomerQuota(customerid, gameid string) ([]model.TaGameRecord, error) {
  86. var records []model.TaGameRecord
  87. sql := `select * from ta_game_record where customer_id=? and status = ? and game_id=?`
  88. err := m.db.Sql(sql, customerid, models.STATUS_NORMAL, gameid).Find(&records)
  89. return records, err
  90. }
  91. // AddRecord 新增活动
  92. func (m *SupportDao) AddRecord(record model.TaGameRecord) (*model.TaGameRecord, error) {
  93. record.RecordId = utils.GetGUID()
  94. record.CreateDate = time.Now()
  95. record.Status = models.STATUS_READY
  96. _, err := m.db.Insert(record)
  97. return &record, err
  98. }
  99. // AddSupport 新增助力
  100. func (m *SupportDao) AddSupport(support model.TaRecordSuppot) (*model.TaRecordSuppot, error) {
  101. support.CreateDate = time.Now()
  102. support.SupportId = utils.GetGUID()
  103. _, err := m.db.Insert(support)
  104. return &support, err
  105. }
  106. // GetCustomerSupport 获取用户当前活动的助力信息
  107. func (m *SupportDao) GetCustomerSupport(recordid, customerid string) ([]model.TaRecordSuppot, error) {
  108. var supports []model.TaRecordSuppot
  109. err := m.db.Where("record_id=?", recordid).And("customer_id=?", customerid).Find(&supports)
  110. return supports, err
  111. }
  112. // GetGameByID 获取主活动信息
  113. func (m *SupportDao) GetGameByID(gameid string) (*model.TaGame, error) {
  114. var game model.TaGame
  115. _, err := m.db.Where("game_id=?", gameid).Get(&game)
  116. return &game, err
  117. }
  118. // GetGameNote 获取活动今日信息
  119. func (m *SupportDao) GetGameNote(gameid string) (*model.TaGameNotes, error) {
  120. var note model.TaGameNotes
  121. _, err := m.db.Where("game_id=?", gameid).And("date_format(game_date,'%Y-%m-%d') = date_format(now(),'%Y-%m-%d')").Get(&note)
  122. return &note, err
  123. }
  124. // UpdateLeftNum 更新活动剩余次数
  125. func (m *SupportDao) UpdateLeftNum(gameid string) (int64, error) {
  126. sql := `update ta_game_notes set left_num = left_num-1 where date_format(game_date,'%Y-%m-%d') = date_format(now(),'%Y-%m-%d') and game_id=? and left_num>0`
  127. s, err := m.db.Exec(sql, gameid)
  128. if err != nil {
  129. return 0, err
  130. }
  131. row, _ := s.RowsAffected()
  132. return row, nil
  133. }