welfare.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package dao
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "go-common/app/admin/main/vip/model"
  7. "go-common/library/log"
  8. "github.com/jinzhu/gorm"
  9. "github.com/pkg/errors"
  10. )
  11. const (
  12. _welfareTypeTable = "vip_welfare_type"
  13. _welfareTable = "vip_welfare"
  14. _codeBatchTable = "vip_welfare_code_batch"
  15. _welfareCodeTable = "vip_welfare_code"
  16. _notDelete = 0
  17. _nobody = 0
  18. _noTid = 0
  19. _batchInsertWelfareCode = "INSERT INTO vip_welfare_code (bid, wid, code) VALUES %s"
  20. )
  21. // WelfareTypeAdd add welfare type
  22. func (d *Dao) WelfareTypeAdd(wt *model.WelfareType) (err error) {
  23. if err = d.vip.Save(wt).Error; err != nil {
  24. err = errors.Wrapf(err, "WelfareTypeAdd(%+v)", wt)
  25. }
  26. return
  27. }
  28. // WelfareTypeUpd update welfare type
  29. func (d *Dao) WelfareTypeUpd(wt *model.WelfareType) (err error) {
  30. if err = d.vip.Table(_welfareTypeTable).Where("id = ? and state = ?", wt.ID, _notDelete).
  31. Update(map[string]interface{}{
  32. "oper_id": wt.OperID,
  33. "oper_name": wt.OperName,
  34. "state": wt.State,
  35. "name": wt.Name,
  36. }).Error; err != nil {
  37. err = errors.Wrapf(err, "WelfareTypeUpd(%+v)", wt)
  38. }
  39. return
  40. }
  41. // WelfareTypeState delete welfare type
  42. func (d *Dao) WelfareTypeState(tx *gorm.DB, id, state, operId int, operName string) (err error) {
  43. if err = tx.Table(_welfareTypeTable).Where("id = ?", id).
  44. Update(map[string]interface{}{
  45. "oper_id": operId,
  46. "oper_name": operName,
  47. "state": state,
  48. }).Error; err != nil {
  49. err = errors.Wrapf(err, "WelfareTypeState id(%v) state(%v)", id, state)
  50. }
  51. return
  52. }
  53. // WelfareTypeList get welfare type list
  54. func (d *Dao) WelfareTypeList() (wts []*model.WelfareTypeRes, err error) {
  55. if err = d.vip.Table(_welfareTypeTable).Where("state = ?", _notDelete).Find(&wts).Error; err != nil {
  56. err = errors.Wrapf(err, "WelfareTypeList")
  57. }
  58. return
  59. }
  60. // WelfareAdd add welfare
  61. func (d *Dao) WelfareAdd(wt *model.Welfare) (err error) {
  62. if err = d.vip.Save(wt).Error; err != nil {
  63. err = errors.Wrapf(err, "WelfareAdd(%+v)", wt)
  64. }
  65. return
  66. }
  67. // WelfareUpd update welfare
  68. func (d *Dao) WelfareUpd(wt *model.WelfareReq) (err error) {
  69. if err = d.vip.Table(_welfareTable).Where("id = ? and state = ?", wt.ID, _notDelete).
  70. Update(map[string]interface{}{
  71. "welfare_name": wt.WelfareName,
  72. "welfare_desc": wt.WelfareDesc,
  73. "homepage_uri": wt.HomepageUri,
  74. "backdrop_uri": wt.BackdropUri,
  75. "recommend": wt.Recommend,
  76. "rank": wt.Rank,
  77. "tid": wt.Tid,
  78. "stime": wt.Stime,
  79. "etime": wt.Etime,
  80. "usage_form": wt.UsageForm,
  81. "receive_rate": wt.ReceiveRate,
  82. "receive_uri": wt.ReceiveUri,
  83. "vip_type": wt.VipType,
  84. "oper_id": wt.OperID,
  85. "oper_name": wt.OperName,
  86. }).Error; err != nil {
  87. err = errors.Wrapf(err, "WelfareUpd(%+v)", wt)
  88. }
  89. return
  90. }
  91. // WelfareState delete welfare
  92. func (d *Dao) WelfareState(id, state, operId int, operName string) (err error) {
  93. if err = d.vip.Table(_welfareTable).Where("id = ?", id).
  94. Update(map[string]interface{}{
  95. "oper_id": operId,
  96. "oper_name": operName,
  97. "state": state,
  98. }).Error; err != nil {
  99. err = errors.Wrapf(err, "WelfareState id(%v) state(%v)", id, state)
  100. }
  101. return
  102. }
  103. // ResetWelfareTid reset welfare tid to 0
  104. func (d *Dao) ResetWelfareTid(tx *gorm.DB, tid int) (err error) {
  105. if err = tx.Table(_welfareTable).Where("tid = ?", tid).Update("tid", _noTid).Error; err != nil {
  106. err = errors.Wrapf(err, "ResetWelfareTid(%v)", tid)
  107. }
  108. return
  109. }
  110. // WelfareList get welfare list
  111. func (d *Dao) WelfareList(tid int) (ws []*model.WelfareRes, err error) {
  112. db := d.vip.Table(_welfareTable)
  113. if tid != 0 {
  114. db = db.Where("tid = ?", tid)
  115. }
  116. if err = db.Where("state = ?", _notDelete).Order("recommend desc, rank").Find(&ws).Error; err != nil {
  117. err = errors.Wrapf(err, "WelfareList(%+v)", tid)
  118. }
  119. return
  120. }
  121. // WelfareBatchSave add welfare batch
  122. func (d *Dao) WelfareBatchSave(wcb *model.WelfareCodeBatch) (err error) {
  123. if err = d.vip.Table(_codeBatchTable).Save(wcb).Error; err != nil {
  124. err = errors.Wrapf(err, "WelfareBatchSave(%+v)", wcb)
  125. }
  126. return
  127. }
  128. // WelfareBatchList get welfare list
  129. func (d *Dao) WelfareBatchList(wid int) (wbs []*model.WelfareBatchRes, err error) {
  130. if err = d.vip.Table(_codeBatchTable).Where("wid = ? and state = ?", wid, _notDelete).Find(&wbs).Error; err != nil {
  131. err = errors.Wrapf(err, "WelfareBatchList(%+v)", wid)
  132. }
  133. return
  134. }
  135. // WelfareBatchState delete welfare batch
  136. func (d *Dao) WelfareBatchState(tx *gorm.DB, id, state, operId int, operName string) (err error) {
  137. if err = tx.Table(_codeBatchTable).Where("id = ?", id).
  138. Update(map[string]interface{}{
  139. "oper_id": operId,
  140. "oper_name": operName,
  141. "state": state,
  142. }).Error; err != nil {
  143. err = errors.Wrapf(err, "WelfareBatchState(%+v)", id)
  144. }
  145. return
  146. }
  147. // WelfareCodeBatchInsert insert welfare batch code
  148. func (d *Dao) WelfareCodeBatchInsert(wcs []*model.WelfareCode) (err error) {
  149. log.Info("WelfareCodeBatchInsert start time (%s)", time.Now())
  150. var (
  151. buff = make([]*model.WelfareCode, 2000)
  152. buffEnd = 0
  153. )
  154. for _, wc := range wcs {
  155. buff[buffEnd] = wc
  156. buffEnd++
  157. if buffEnd >= 2000 {
  158. buffEnd = 0
  159. stmt, valueArgs := getBatchInsertSQL(buff)
  160. if err = d.vip.Exec(stmt, valueArgs...).Error; err != nil {
  161. return
  162. }
  163. }
  164. }
  165. if buffEnd > 0 {
  166. stmt, valueArgs := getBatchInsertSQL(buff[:buffEnd])
  167. buffEnd = 0
  168. if err = d.vip.Exec(stmt, valueArgs...).Error; err != nil {
  169. return
  170. }
  171. }
  172. log.Info("WelfareCodeBatchInsert end time (%s)", time.Now())
  173. return
  174. }
  175. // WelfareCodeStatus delete welfare batch code
  176. func (d *Dao) WelfareCodeStatus(tx *gorm.DB, bid, state int) (err error) {
  177. if err = tx.Table(_welfareCodeTable).Where("bid = ? and mid = ?", bid, _nobody).
  178. Update("state", state).Error; err != nil {
  179. err = errors.Wrapf(err, "WelfareCodeStatus(%+v) (%+v)", bid, state)
  180. }
  181. return
  182. }
  183. func getBatchInsertSQL(buff []*model.WelfareCode) (stmt string, valueArgs []interface{}) {
  184. values := []string{}
  185. for _, b := range buff {
  186. values = append(values, "(?,?,?)")
  187. valueArgs = append(valueArgs, b.Bid, b.Wid, b.Code)
  188. }
  189. stmt = fmt.Sprintf(_batchInsertWelfareCode, strings.Join(values, ","))
  190. return
  191. }