special_award.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "go-common/app/interface/main/growup/model"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. const (
  11. _openAwardsSQL = "SELECT award_id,award_name,cycle_start FROM special_award WHERE open_status=2"
  12. _joinedAwardsSQL = "SELECT award_id,award_name,cycle_start,cycle_end,open_status FROM special_award WHERE award_id IN (%s)"
  13. _awardScheSQL = "SELECT award_name,cycle_start,cycle_end,announce_date FROM special_award WHERE award_id=?"
  14. _awardResourceSQL = "SELECT resource_type,content,resource_index FROM special_award_resource WHERE award_id=? AND is_deleted=0"
  15. _awardWinnerSQL = "SELECT mid FROM special_award_winner WHERE award_id=? AND is_deleted=0"
  16. _winnerIDsSQL = "SELECT award_id,prize_id FROM special_award_winner WHERE mid=? AND is_deleted=0"
  17. _winnerDivisionSQL = "SELECT award_id,division_name FROM special_award_winner WHERE mid=? AND is_deleted=0"
  18. _awardBonusSQL = "SELECT bonus FROM special_award_prize WHERE award_id=? AND prize_id=?"
  19. _inSpecialAwardRecordSQL = "INSERT INTO special_award_record(mid,award_id) VALUES(?,?)"
  20. _joinedCountSQL = "SELECT count(*) FROM special_award_record WHERE mid=? AND award_id=?"
  21. _specialAwardsSQL = "SELECT award_id, award_name, cycle_start, cycle_end, announce_date FROM special_award WHERE display_status = 2 AND is_deleted = 0"
  22. _awardDivisionSQL = "SELECT division_name FROM special_award_division WHERE award_id = ? AND is_deleted = 0"
  23. _awardWinRecordSQL = "SELECT award_id FROM special_award_winner WHERE mid = ? AND is_deleted = 0"
  24. _awardJoinRecordSQL = "SELECT award_id FROM special_award_record WHERE mid = ? AND is_deleted = 0"
  25. )
  26. // PastAwards get award basic info
  27. func (d *Dao) PastAwards(c context.Context) (as []*model.SimpleSpecialAward, err error) {
  28. rows, err := d.db.Query(c, _openAwardsSQL)
  29. if err != nil {
  30. return
  31. }
  32. defer rows.Close()
  33. as = make([]*model.SimpleSpecialAward, 0)
  34. for rows.Next() {
  35. a := &model.SimpleSpecialAward{}
  36. err = rows.Scan(&a.AwardID, &a.AwardName, &a.CycleStart)
  37. if err != nil {
  38. return
  39. }
  40. as = append(as, a)
  41. }
  42. err = rows.Err()
  43. return
  44. }
  45. // JoinedSpecialAwards get joined awards
  46. func (d *Dao) JoinedSpecialAwards(c context.Context, awardIDs []int64) (sas []*model.SpecialAward, err error) {
  47. rows, err := d.db.Query(c, fmt.Sprintf(_joinedAwardsSQL, xstr.JoinInts(awardIDs)))
  48. if err != nil {
  49. return
  50. }
  51. defer rows.Close()
  52. sas = make([]*model.SpecialAward, 0)
  53. for rows.Next() {
  54. sa := &model.SpecialAward{}
  55. err = rows.Scan(&sa.AwardID, &sa.AwardName, &sa.CycleStart, &sa.CycleEnd, &sa.OpenStatus)
  56. if err != nil {
  57. log.Error("row scan error(%v)", err)
  58. return
  59. }
  60. sas = append(sas, sa)
  61. }
  62. err = rows.Err()
  63. return
  64. }
  65. // GetAwardSchedule get special award by award id
  66. func (d *Dao) GetAwardSchedule(c context.Context, awardID int64) (award *model.SpecialAward, err error) {
  67. award = &model.SpecialAward{}
  68. row := d.db.QueryRow(c, _awardScheSQL, awardID)
  69. if err = row.Scan(&award.AwardName, &award.CycleStart, &award.CycleEnd, &award.AnnounceDate); err != nil {
  70. if err == sql.ErrNoRows {
  71. err = nil
  72. } else {
  73. log.Error("row scan error(%v)", err)
  74. }
  75. }
  76. return
  77. }
  78. // GetResources get special award resource map[type]map[index]content
  79. func (d *Dao) GetResources(c context.Context, awardID int64) (res map[int]map[int]string, err error) {
  80. rows, err := d.db.Query(c, _awardResourceSQL, awardID)
  81. if err != nil {
  82. log.Error("GetResources d.db.Query error(%v)", err)
  83. return
  84. }
  85. defer rows.Close()
  86. res = make(map[int]map[int]string)
  87. for rows.Next() {
  88. var rtype, index int
  89. var content string
  90. err = rows.Scan(&rtype, &content, &index)
  91. if err != nil {
  92. log.Error("GetDivisions rows.Scan error(%v)", err)
  93. return
  94. }
  95. if cs, ok := res[rtype]; ok {
  96. cs[index] = content
  97. } else {
  98. m := make(map[int]string)
  99. m[index] = content
  100. res[rtype] = m
  101. }
  102. }
  103. err = rows.Err()
  104. return
  105. }
  106. // GetWinners get winner mids by award_id
  107. func (d *Dao) GetWinners(c context.Context, awardID int64) (mids []int64, err error) {
  108. rows, err := d.db.Query(c, _awardWinnerSQL, awardID)
  109. if err != nil {
  110. log.Error("GetWinners d.db.Query error(%v)", err)
  111. return
  112. }
  113. defer rows.Close()
  114. mids = make([]int64, 0)
  115. for rows.Next() {
  116. var mid int64
  117. err = rows.Scan(&mid)
  118. if err != nil {
  119. log.Error("GetWinners rows.Scan error(%v)", err)
  120. return
  121. }
  122. mids = append(mids, mid)
  123. }
  124. err = rows.Err()
  125. return
  126. }
  127. // AwardIDsByWinner get winner's record, am map[award_id]prize_id
  128. func (d *Dao) AwardIDsByWinner(c context.Context, mid int64) (am map[int64]int64, err error) {
  129. rows, err := d.db.Query(c, _winnerIDsSQL, mid)
  130. if err != nil {
  131. return
  132. }
  133. defer rows.Close()
  134. am = make(map[int64]int64)
  135. for rows.Next() {
  136. var awardID, prizeID int64
  137. err = rows.Scan(&awardID, &prizeID)
  138. if err != nil {
  139. log.Error("rows scan error(%v)", err)
  140. return
  141. }
  142. am[awardID] = prizeID
  143. }
  144. err = rows.Err()
  145. return
  146. }
  147. // DivisionName division name by mid, names map[award_id]division_name
  148. func (d *Dao) DivisionName(c context.Context, mid int64) (names map[int64]string, err error) {
  149. rows, err := d.db.Query(c, _winnerDivisionSQL, mid)
  150. if err != nil {
  151. return
  152. }
  153. defer rows.Close()
  154. names = make(map[int64]string)
  155. for rows.Next() {
  156. var awardID int64
  157. var name string
  158. err = rows.Scan(&awardID, &name)
  159. if err != nil {
  160. log.Error("rows scan error(%v)", err)
  161. return
  162. }
  163. names[awardID] = name
  164. }
  165. err = rows.Err()
  166. return
  167. }
  168. // JoinedCount get signed up count by mid, award_id
  169. func (d *Dao) JoinedCount(c context.Context, mid, awardID int64) (count int64, err error) {
  170. row := d.db.QueryRow(c, _joinedCountSQL, mid, awardID)
  171. if err = row.Scan(&count); err != nil {
  172. if err == sql.ErrNoRows {
  173. err = nil
  174. } else {
  175. log.Error("row scan error(%v)", err)
  176. }
  177. }
  178. return
  179. }
  180. // AwardBonus get bonus by award_id and prize_id
  181. func (d *Dao) AwardBonus(c context.Context, awardID, prizeID int64) (bonus int64, err error) {
  182. row := d.db.QueryRow(c, _awardBonusSQL, awardID, prizeID)
  183. if err = row.Scan(&bonus); err != nil {
  184. if err == sql.ErrNoRows {
  185. err = nil
  186. } else {
  187. log.Error("row scan error(%v)", err)
  188. }
  189. }
  190. return
  191. }
  192. // AddToAwardRecord add to award record
  193. func (d *Dao) AddToAwardRecord(c context.Context, mid, awardID int64) (rows int64, err error) {
  194. res, err := d.db.Exec(c, _inSpecialAwardRecordSQL, mid, awardID)
  195. if err != nil {
  196. return
  197. }
  198. return res.RowsAffected()
  199. }
  200. // GetSpecialAwards get all display sepcial award
  201. func (d *Dao) GetSpecialAwards(c context.Context) (awards []*model.SpecialAward, err error) {
  202. awards = make([]*model.SpecialAward, 0)
  203. rows, err := d.db.Query(c, _specialAwardsSQL)
  204. if err != nil {
  205. log.Error("GetSpecialAwards d.db.Query error(%v)", err)
  206. return
  207. }
  208. defer rows.Close()
  209. for rows.Next() {
  210. sa := &model.SpecialAward{}
  211. err = rows.Scan(&sa.AwardID, &sa.AwardName, &sa.CycleStart, &sa.CycleEnd, &sa.AnnounceDate)
  212. if err != nil {
  213. log.Error("GetSpecialAwards rows.Scan error(%v)", err)
  214. return
  215. }
  216. awards = append(awards, sa)
  217. }
  218. err = rows.Err()
  219. return
  220. }
  221. // GetSpecialAwardDivision get special award division name
  222. func (d *Dao) GetSpecialAwardDivision(c context.Context, awardID int64) (divisions []string, err error) {
  223. divisions = make([]string, 0)
  224. rows, err := d.db.Query(c, _awardDivisionSQL, awardID)
  225. if err != nil {
  226. log.Error("GetSpecialAwardDivision d.db.Query error(%v)", err)
  227. return
  228. }
  229. defer rows.Close()
  230. for rows.Next() {
  231. var name string
  232. err = rows.Scan(&name)
  233. if err != nil {
  234. log.Error("GetSpecialAwardDivision rows.Scan error(%v)", err)
  235. return
  236. }
  237. divisions = append(divisions, name)
  238. }
  239. err = rows.Err()
  240. return
  241. }
  242. // GetAwardWinRecord get award win record
  243. func (d *Dao) GetAwardWinRecord(c context.Context, mid int64) (awardIDs map[int64]bool, err error) {
  244. awardIDs = make(map[int64]bool)
  245. rows, err := d.db.Query(c, _awardWinRecordSQL, mid)
  246. if err != nil {
  247. log.Error("GetAwardWinRecord d.db.Query error(%v)", err)
  248. return
  249. }
  250. defer rows.Close()
  251. for rows.Next() {
  252. var id int64
  253. err = rows.Scan(&id)
  254. if err != nil {
  255. log.Error("GetAwardWinRecord rows.Scan error(%v)", err)
  256. return
  257. }
  258. awardIDs[id] = true
  259. }
  260. err = rows.Err()
  261. return
  262. }
  263. // GetAwardJoinRecord get award join record
  264. func (d *Dao) GetAwardJoinRecord(c context.Context, mid int64) (awardIDs map[int64]bool, err error) {
  265. awardIDs = make(map[int64]bool)
  266. rows, err := d.db.Query(c, _awardJoinRecordSQL, mid)
  267. if err != nil {
  268. log.Error("GetAwardJoinRecord d.db.Query error(%v)", err)
  269. return
  270. }
  271. defer rows.Close()
  272. for rows.Next() {
  273. var id int64
  274. err = rows.Scan(&id)
  275. if err != nil {
  276. log.Error("GetAwardJoinRecord rows.Scan error(%v)", err)
  277. return
  278. }
  279. awardIDs[id] = true
  280. }
  281. err = rows.Err()
  282. return
  283. }