123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- package game
-
- import (
- "support-game/models"
- "support-game/models/model"
- "support-game/utils"
- "time"
-
- "github.com/go-xorm/xorm"
- )
-
- // SupportDao 当前数据库操作对象
- type SupportDao struct {
- ctx *utils.Context
- db *xorm.Session
- }
-
- // NewSupportDao 初始化DAO
- func NewSupportDao(ctx *utils.Context) *SupportDao {
- return &SupportDao{
- ctx: ctx,
- db: ctx.DB,
- }
- }
-
- // GetCustomerByOpenID 根据openid获取用户信息
- func (m *SupportDao) GetCustomerByOpenID(openid string) (*model.TaCustomer, error) {
- var customer = model.TaCustomer{}
- _, err := m.db.Where("openid=?", openid).Get(&customer)
- return &customer, err
- }
-
- // SaveCustomer 保存用户信息
- func (m *SupportDao) SaveCustomer(customer model.TaCustomer) (*model.TaCustomer, error) {
- customer.CustomerId = utils.GetGUID()
- customer.CreateDate = time.Now()
- _, err := m.db.Insert(customer)
- return &customer, err
- }
-
- // UpdateCustomer 修改用户信息
- func (m *SupportDao) UpdateCustomer(customer *model.TaCustomer, cols []string) error {
- _, err := m.db.Cols(cols...).Where("customer_id=?", customer.CustomerId).Update(customer)
- return err
- }
-
- // UpdateRecord 修改活动信息
- func (m *SupportDao) UpdateRecord(record *model.TaGameRecord, cols []string) error {
- _, err := m.db.Cols(cols...).Where("record_id=?", record.RecordId).Update(record)
- return err
- }
-
- // GetCustomerByID 根据id获取用户信息
- func (m *SupportDao) GetCustomerByID(id string) (*model.TaCustomer, error) {
- var customer = model.TaCustomer{}
- _, err := m.db.Where("customer_id=?", id).Get(&customer)
- return &customer, err
- }
-
- // GetRecordByID 根据id获取记录
- func (m *SupportDao) GetRecordByID(recordid string) (*model.TaGameRecord, error) {
- var record model.TaGameRecord
- _, err := m.db.Where("record_id=?", recordid).Get(&record)
- return &record, err
- }
-
- // RecordSupport 助力记录
- type RecordSupport struct {
- model.TaRecordSuppot `xorm:"extends"`
- CustomerName string
- Name string
- HeadImg string
- }
-
- // GetSupportsByRecord 获取助力信息
- func (m *SupportDao) GetSupportsByRecord(recordid string) ([]RecordSupport, error) {
- var supports []RecordSupport
- 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=?`
- err := m.db.Sql(sql, recordid).Find(&supports)
- return supports, err
- }
-
- // GetTodayQuota 获取今天成功的活动
- func (m *SupportDao) GetTodayQuota(gameid string) ([]model.TaGameRecord, error) {
- var records []model.TaGameRecord
- 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=?`
- err := m.db.Sql(sql, models.STATUS_NORMAL, gameid).Find(&records)
- return records, err
- }
-
- // GetTodayCustomerRecord 获取客户今天参与的活动
- func (m *SupportDao) GetTodayCustomerRecord(customerid, gameid string) ([]model.TaGameRecord, error) {
- var records []model.TaGameRecord
- 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=?`
- err := m.db.Sql(sql, customerid, gameid).Find(&records)
- return records, err
- }
-
- // GetCustomerQuota 获取用户成功的活动
- func (m *SupportDao) GetCustomerQuota(customerid, gameid string) ([]model.TaGameRecord, error) {
- var records []model.TaGameRecord
- sql := `select * from ta_game_record where customer_id=? and status = ? and game_id=?`
- err := m.db.Sql(sql, customerid, models.STATUS_NORMAL, gameid).Find(&records)
- return records, err
- }
-
- // AddRecord 新增活动
- func (m *SupportDao) AddRecord(record model.TaGameRecord) (*model.TaGameRecord, error) {
- record.RecordId = utils.GetGUID()
- record.CreateDate = time.Now()
- record.Status = models.STATUS_READY
- _, err := m.db.Insert(record)
- return &record, err
- }
-
- // AddSupport 新增助力
- func (m *SupportDao) AddSupport(support model.TaRecordSuppot) (*model.TaRecordSuppot, error) {
- support.CreateDate = time.Now()
- support.SupportId = utils.GetGUID()
- _, err := m.db.Insert(support)
- return &support, err
- }
-
- // GetCustomerSupport 获取用户当前活动的助力信息
- func (m *SupportDao) GetCustomerSupport(recordid, customerid string) ([]model.TaRecordSuppot, error) {
- var supports []model.TaRecordSuppot
- err := m.db.Where("record_id=?", recordid).And("customer_id=?", customerid).Find(&supports)
- return supports, err
- }
-
- // GetGameByID 获取主活动信息
- func (m *SupportDao) GetGameByID(gameid string) (*model.TaGame, error) {
- var game model.TaGame
- _, err := m.db.Where("game_id=?", gameid).Get(&game)
- return &game, err
- }
-
- // GetGameNote 获取活动今日信息
- func (m *SupportDao) GetGameNote(gameid string) (*model.TaGameNotes, error) {
- var note model.TaGameNotes
- _, err := m.db.Where("game_id=?", gameid).And("date_format(game_date,'%Y-%m-%d') = date_format(now(),'%Y-%m-%d')").Get(¬e)
- return ¬e, err
- }
-
- // UpdateLeftNum 更新活动剩余次数
- func (m *SupportDao) UpdateLeftNum(gameid string) (int64, error) {
- 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`
- s, err := m.db.Exec(sql, gameid)
- if err != nil {
- return 0, err
- }
- row, _ := s.RowsAffected()
- return row, nil
- }
|