special_award.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/growup/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. const (
  11. _addAwardSQL = `INSERT INTO special_award
  12. (award_id,award_name,cycle_start,cycle_end,announce_date,display_status,total_quota,total_bonus,open_status,open_time,created_by)
  13. VALUES (%d,'%s','%s','%s','%s',?,?,?,1,'%s','%s')`
  14. _updateAwardSQL = "UPDATE special_award SET %s WHERE award_id=?"
  15. _queryAwardSQL = `SELECT id,award_id,award_name,cycle_start,cycle_end,announce_date,display_status,total_quota,total_bonus,open_status,open_time,created_by,ctime FROM special_award`
  16. _pageListAwardSQL = _queryAwardSQL + " ORDER BY id LIMIT ?,?"
  17. _selectAwardSQL = _queryAwardSQL + " WHERE award_id=?"
  18. _selectAwardForUpdateSQL = _queryAwardSQL + " WHERE award_id=? FOR UPDATE"
  19. _listDivisionSQL = "SELECT award_id,division_id,division_name,tag_id FROM special_award_division WHERE %s AND is_deleted=0"
  20. _listPrizeSQL = "SELECT award_id,prize_id,bonus,quota FROM special_award_prize WHERE award_id = ? AND is_deleted=0"
  21. _listWinnerSQL = "SELECT mid,division_id,prize_id,tag_id FROM special_award_winner WHERE award_id = ? AND is_deleted=0 %s"
  22. _listRecordSQL = "SELECT award_id,mid,tag_id FROM special_award_record WHERE award_id = ? AND is_deleted=0 %s"
  23. _listResourceSQL = "SELECT resource_type,resource_index,content FROM special_award_resource WHERE award_id=? AND is_deleted=0"
  24. _groupCountWinnerSQL = "SELECT award_id, count(distinct mid) from special_award_winner where %s AND is_deleted=0 GROUP BY award_id"
  25. _countWinnerSQL = "SELECT COUNT(mid) from special_award_winner WHERE award_id = ? %s AND is_deleted=0"
  26. _countAwardSQL = "SELECT COUNT(distinct award_id) from special_award"
  27. )
  28. // AddAward tx
  29. func AddAward(tx *sql.Tx, awardID int64, awardName, cycleStart, cycleEnd, announceDate, openTime string,
  30. displayStatus, totalWinner, totalBonus int, creater string) (int64, error) {
  31. res, err := tx.Exec(fmt.Sprintf(_addAwardSQL, awardID, awardName, cycleStart, cycleEnd, announceDate, openTime, creater),
  32. displayStatus, totalWinner, totalBonus)
  33. if err != nil {
  34. log.Error("dao#AddAward, tx.Exec err(%v)", err)
  35. return 0, err
  36. }
  37. return res.RowsAffected()
  38. }
  39. // Award query
  40. func (d *Dao) Award(c context.Context, awardID int64) (data *model.Award, err error) {
  41. data = &model.Award{}
  42. err = d.rddb.QueryRow(c, _selectAwardSQL, awardID).Scan(
  43. &data.ID, &data.AwardID, &data.AwardName, &data.CycleStart, &data.CycleEnd, &data.AnnounceDate,
  44. &data.DisplayStatus, &data.TotalQuota, &data.TotalBonus,
  45. &data.OpenStatus, &data.OpenTime, &data.CreatedBy, &data.CTime)
  46. if err != nil {
  47. if err == sql.ErrNoRows {
  48. data = nil
  49. err = nil
  50. return
  51. }
  52. log.Error("dao.Award, d.rddb.QueryRow err(%v)", err)
  53. }
  54. return
  55. }
  56. // SelectAwardForUpdate tx
  57. func SelectAwardForUpdate(tx *sql.Tx, awardID int64) (award *model.Award, err error) {
  58. award = &model.Award{}
  59. err = tx.QueryRow(_selectAwardForUpdateSQL, awardID).Scan(
  60. &award.ID, &award.AwardID, &award.AwardName, &award.CycleStart, &award.CycleEnd, &award.AnnounceDate,
  61. &award.DisplayStatus, &award.TotalQuota, &award.TotalBonus,
  62. &award.OpenStatus, &award.OpenTime, &award.CreatedBy, &award.CTime)
  63. if err != nil {
  64. if err == sql.ErrNoRows {
  65. err = nil
  66. award = nil
  67. return
  68. }
  69. log.Error("dao.SelectAwardForUpdate awardID{%d} err(%v)", awardID, err)
  70. }
  71. return
  72. }
  73. // UpdateAward tx
  74. func UpdateAward(tx *sql.Tx, awardID int64, values string) (int64, error) {
  75. if values == "" {
  76. return 0, nil
  77. }
  78. res, err := tx.Exec(fmt.Sprintf(_updateAwardSQL, values), awardID)
  79. if err != nil {
  80. log.Error("dao#UpdateAward, tx.Exec err(%v)", err)
  81. return 0, err
  82. }
  83. return res.RowsAffected()
  84. }
  85. // Award tx query
  86. func Award(tx *sql.Tx, awardID int64) (data *model.Award, err error) {
  87. data = &model.Award{}
  88. err = tx.QueryRow(_selectAwardSQL, awardID).Scan(&data.ID,
  89. &data.AwardID, &data.AwardName, &data.CycleStart, &data.CycleEnd, &data.AnnounceDate,
  90. &data.DisplayStatus, &data.TotalQuota, &data.TotalBonus,
  91. &data.OpenStatus, &data.OpenTime, &data.CreatedBy, &data.CTime)
  92. if err != nil {
  93. if err == sql.ErrNoRows {
  94. data = nil
  95. err = nil
  96. return
  97. }
  98. log.Error("dao.Award, tx.QueryRow err(%v)", err)
  99. }
  100. return
  101. }
  102. // ListAwardsDivision tx
  103. func ListAwardsDivision(tx *sql.Tx, where string) (res []*model.AwardDivision, err error) {
  104. res = make([]*model.AwardDivision, 0)
  105. rows, err := tx.Query(fmt.Sprintf(_listDivisionSQL, where))
  106. if err != nil {
  107. log.Error("dao.ListAwardsDivision tx.Query where{%s} err(%v)", where, err)
  108. return
  109. }
  110. defer rows.Close()
  111. for rows.Next() {
  112. data := &model.AwardDivision{}
  113. if err = rows.Scan(&data.AwardID, &data.DivisionID, &data.DivisionName, &data.TagID); err != nil {
  114. log.Error("dao.ListAwardsDivision rows.Scan where{%s} err(%v)", where, err)
  115. return
  116. }
  117. res = append(res, data)
  118. }
  119. err = rows.Err()
  120. return
  121. }
  122. // ListDivision tx
  123. func ListDivision(tx *sql.Tx, awardID int64) (res []*model.AwardDivision, err error) {
  124. return ListAwardsDivision(tx, fmt.Sprintf("award_id=%d", awardID))
  125. }
  126. // DivisionInfo tx query divisionID-to-divisionModel k-v pairs
  127. func DivisionInfo(tx *sql.Tx, awardID int64) (res map[int64]*model.AwardDivision, err error) {
  128. res = make(map[int64]*model.AwardDivision)
  129. rows, err := tx.Query(fmt.Sprintf(_listDivisionSQL, fmt.Sprintf("award_id=%d", awardID)))
  130. if err != nil {
  131. log.Error("dao.DivisionInfo tx.Query awardID{%d} err(%v)", awardID, err)
  132. return
  133. }
  134. defer rows.Close()
  135. for rows.Next() {
  136. data := &model.AwardDivision{}
  137. if err = rows.Scan(&data.AwardID, &data.DivisionID, &data.DivisionName, &data.TagID); err != nil {
  138. log.Error("dao.DivisionInfo rows.Scan awardID{%d} err(%v)", awardID, err)
  139. return
  140. }
  141. res[data.DivisionID] = data
  142. }
  143. err = rows.Err()
  144. return
  145. }
  146. // ListPrize tx
  147. func ListPrize(tx *sql.Tx, awardID int64) (res []*model.AwardPrize, err error) {
  148. res = make([]*model.AwardPrize, 0)
  149. rows, err := tx.Query(_listPrizeSQL, awardID)
  150. if err != nil {
  151. log.Error("dao.ListPrize, tx.Query err(%v)", err)
  152. return
  153. }
  154. defer rows.Close()
  155. for rows.Next() {
  156. data := &model.AwardPrize{}
  157. if err = rows.Scan(&data.AwardID, &data.PrizeID, &data.Bonus, &data.Quota); err != nil {
  158. log.Error("dao.ListPrize, rows.Scan err(%v)", err)
  159. return
  160. }
  161. res = append(res, data)
  162. }
  163. err = rows.Err()
  164. return
  165. }
  166. // ListResource tx resource_type->resource_index->resource_content
  167. func ListResource(tx *sql.Tx, awardID int64) (res map[int]map[int]string, err error) {
  168. res = make(map[int]map[int]string)
  169. rows, err := tx.Query(_listResourceSQL, awardID)
  170. if err != nil {
  171. log.Error("dao.ListResource, tx.Query err(%v)", err)
  172. return
  173. }
  174. defer rows.Close()
  175. for rows.Next() {
  176. var tp, index int
  177. var content string
  178. if err = rows.Scan(&tp, &index, &content); err != nil {
  179. log.Error("dao.ListPrize, rows.Scan err(%v)", err)
  180. return
  181. }
  182. if _, ok := res[tp]; !ok {
  183. res[tp] = make(map[int]string)
  184. }
  185. res[tp][index] = content
  186. }
  187. err = rows.Err()
  188. return
  189. }
  190. // PrizeInfo tx query prizeID-to-prizeModel k-v pairs
  191. func PrizeInfo(tx *sql.Tx, awardID int64) (res map[int64]*model.AwardPrize, err error) {
  192. res = make(map[int64]*model.AwardPrize)
  193. rows, err := tx.Query(_listPrizeSQL, awardID)
  194. if err != nil {
  195. log.Error("dao.PrizeInfo, tx.Query err(%v)", err)
  196. return
  197. }
  198. defer rows.Close()
  199. for rows.Next() {
  200. data := &model.AwardPrize{}
  201. if err = rows.Scan(&data.AwardID, &data.PrizeID, &data.Bonus, &data.Quota); err != nil {
  202. log.Error("dao.PrizeInfo, rows.Scan err(%v)", err)
  203. return
  204. }
  205. res[data.PrizeID] = data
  206. }
  207. err = rows.Err()
  208. return
  209. }
  210. // CountAward tx
  211. func CountAward(tx *sql.Tx) (total int64, err error) {
  212. err = tx.QueryRow(_countAwardSQL).Scan(&total)
  213. if err != nil {
  214. log.Error("dao.CountAward, tx.Query err(%v)", err)
  215. }
  216. return
  217. }
  218. // CountAwardWinner tx
  219. func CountAwardWinner(tx *sql.Tx, awardID int64, where string) (total int64, err error) {
  220. err = tx.QueryRow(fmt.Sprintf(_countWinnerSQL, where), awardID).Scan(&total)
  221. if err != nil {
  222. log.Error("dao.CountAwardWinner, awardID(%d), where(%s) err(%v)", awardID, where, err)
  223. }
  224. return
  225. }
  226. // GroupCountAwardWinner tx query awardID-to-winnerCount k-v pairs
  227. func GroupCountAwardWinner(tx *sql.Tx, where string) (res map[int64]int, err error) {
  228. res = make(map[int64]int)
  229. rows, err := tx.Query(fmt.Sprintf(_groupCountWinnerSQL, where))
  230. if err != nil {
  231. log.Error("dao.GroupCountAwardWinner, tx.Query err(%v)", err)
  232. return
  233. }
  234. defer rows.Close()
  235. for rows.Next() {
  236. var awardID int64
  237. var winnerC int
  238. if err = rows.Scan(&awardID, &winnerC); err != nil {
  239. log.Error("dao.GroupCountAwardWinner, rows.Scan err(%v)", err)
  240. return
  241. }
  242. res[awardID] = winnerC
  243. }
  244. return
  245. }
  246. // AwardWinnerAll tx query winnerModel list
  247. func AwardWinnerAll(tx *sql.Tx, awardID int64) (res []*model.AwardWinner, err error) {
  248. res = make([]*model.AwardWinner, 0)
  249. rows, err := tx.Query(fmt.Sprintf(_listWinnerSQL, ""), awardID)
  250. if err != nil {
  251. log.Error("dao.AwardWinnerAll, tx.Query err(%v)", err)
  252. return
  253. }
  254. defer rows.Close()
  255. for rows.Next() {
  256. data := &model.AwardWinner{}
  257. if err = rows.Scan(&data.MID, &data.DivisionID, &data.PrizeID, &data.TagID); err != nil {
  258. log.Error("dao.AwardWinnerAll, rows.Scan err(%v)", err)
  259. return
  260. }
  261. res = append(res, data)
  262. }
  263. err = rows.Err()
  264. return
  265. }
  266. // AwardDivisionInfo .
  267. func (d *Dao) AwardDivisionInfo(c context.Context, awardID int64) (res map[int64]*model.AwardDivision, err error) {
  268. res = make(map[int64]*model.AwardDivision)
  269. rows, err := d.rddb.Query(c, fmt.Sprintf(_listDivisionSQL, fmt.Sprintf("award_id=%d", awardID)))
  270. if err != nil {
  271. log.Error("dao.DivisionInfo tx.Query awardID{%d} err(%v)", awardID, err)
  272. return
  273. }
  274. defer rows.Close()
  275. for rows.Next() {
  276. data := &model.AwardDivision{}
  277. if err = rows.Scan(&data.AwardID, &data.DivisionID, &data.DivisionName, &data.TagID); err != nil {
  278. log.Error("dao.DivisionInfo rows.Scan awardID{%d} err(%v)", awardID, err)
  279. return
  280. }
  281. res[data.DivisionID] = data
  282. }
  283. err = rows.Err()
  284. return
  285. }
  286. // ListAwardRecord .
  287. func (d *Dao) ListAwardRecord(c context.Context, awardID int64, where string) (res []*model.AwardRecord, err error) {
  288. res = make([]*model.AwardRecord, 0)
  289. rows, err := d.rddb.Query(c, fmt.Sprintf(_listRecordSQL, where), awardID)
  290. if err != nil {
  291. log.Error("dao.ListAwardRecord, db.Query awardID(%d) where(%s) err(%v)", awardID, where, err)
  292. return
  293. }
  294. defer rows.Close()
  295. for rows.Next() {
  296. data := &model.AwardRecord{}
  297. if err = rows.Scan(&data.AwardID, &data.MID, &data.TagID); err != nil {
  298. log.Error("dao.ListAwardRecord, rows.Scan awardID(%d) where(%s) err(%v)", awardID, where, err)
  299. return
  300. }
  301. res = append(res, data)
  302. }
  303. err = rows.Err()
  304. return
  305. }
  306. // QueryAwardWinner tx query mid-to-winnerModel k-v pairs
  307. func QueryAwardWinner(tx *sql.Tx, awardID int64, where string) (res map[int64]*model.AwardWinner, err error) {
  308. res = make(map[int64]*model.AwardWinner)
  309. str := fmt.Sprintf(_listWinnerSQL, where)
  310. rows, err := tx.Query(str, awardID)
  311. if err != nil {
  312. log.Error("dao.QueryAwardWinner, tx.Query awardID(%d) sql(%s) err(%v)", awardID, str, err)
  313. return
  314. }
  315. defer rows.Close()
  316. for rows.Next() {
  317. data := &model.AwardWinner{}
  318. if err = rows.Scan(&data.MID, &data.DivisionID, &data.PrizeID, &data.TagID); err != nil {
  319. log.Error("dao.QueryAwardWinner, rows.Scan awardID(%d) sql(%s) err(%v)", awardID, str, err)
  320. return
  321. }
  322. res[data.MID] = data
  323. }
  324. err = rows.Err()
  325. return
  326. }
  327. // ListAward .
  328. func (d *Dao) ListAward(c context.Context) (res []*model.Award, err error) {
  329. res = make([]*model.Award, 0)
  330. rows, err := d.rddb.Query(c, _queryAwardSQL)
  331. if err != nil {
  332. log.Error("dao.ListAward, db.Query err(%v)", err)
  333. return
  334. }
  335. defer rows.Close()
  336. for rows.Next() {
  337. data := &model.Award{}
  338. if err = rows.Scan(
  339. &data.ID,
  340. &data.AwardID, &data.AwardName, &data.CycleStart, &data.CycleEnd, &data.AnnounceDate,
  341. &data.DisplayStatus, &data.TotalQuota, &data.TotalBonus,
  342. &data.OpenStatus, &data.OpenTime, &data.CreatedBy, &data.CTime); err != nil {
  343. log.Error("dao.ListAward, rows.Scan err(%v)", err)
  344. return
  345. }
  346. res = append(res, data)
  347. }
  348. err = rows.Err()
  349. return
  350. }
  351. // ListAward tx
  352. func ListAward(tx *sql.Tx, from, limit int) (res []*model.Award, err error) {
  353. res = make([]*model.Award, 0)
  354. rows, err := tx.Query(_pageListAwardSQL, from, limit)
  355. if err != nil {
  356. log.Error("dao.ListAward, db.Query err(%v)", err)
  357. return
  358. }
  359. defer rows.Close()
  360. for rows.Next() {
  361. data := &model.Award{}
  362. if err = rows.Scan(&data.ID,
  363. &data.AwardID, &data.AwardName, &data.CycleStart, &data.CycleEnd, &data.AnnounceDate,
  364. &data.DisplayStatus, &data.TotalQuota, &data.TotalBonus,
  365. &data.OpenStatus, &data.OpenTime, &data.CreatedBy, &data.CTime); err != nil {
  366. log.Error("dao.ListAward, rows.Scan err(%v)", err)
  367. return
  368. }
  369. res = append(res, data)
  370. }
  371. err = rows.Err()
  372. return
  373. }
  374. // DelWinner tx
  375. func DelWinner(tx *sql.Tx, awardID int64, where string) (rows int64, err error) {
  376. w := fmt.Sprintf("award_id=%d %s", awardID, where)
  377. return delDataTemplate(tx, "special_award_winner", w, "dao#DelWinner")
  378. }
  379. // DelWinnerAll tx
  380. func DelWinnerAll(tx *sql.Tx, awardID int64) (int64, error) {
  381. where := fmt.Sprintf("award_id=%d", awardID)
  382. return delDataTemplate(tx, "special_award_winner", where, "dao#DelWinnerAll")
  383. }
  384. // DelDivisionAll tx
  385. func DelDivisionAll(tx *sql.Tx, awardID int64) (int64, error) {
  386. where := fmt.Sprintf("award_id=%d", awardID)
  387. return delDataTemplate(tx, "special_award_division", where, "dao#DelDivisionAll")
  388. }
  389. // DelDivisionsExclude tx
  390. func DelDivisionsExclude(tx *sql.Tx, awardID int64, divisionIDs []int64) (int64, error) {
  391. where := fmt.Sprintf("award_id=%d AND division_id NOT IN (%s)", awardID, xstr.JoinInts(divisionIDs))
  392. return delDataTemplate(tx, "special_award_division", where, "dao#DelDivisionsExclude")
  393. }
  394. // DelPrizeAll tx
  395. func DelPrizeAll(tx *sql.Tx, awardID int64) (int64, error) {
  396. where := fmt.Sprintf("award_id=%d", awardID)
  397. return delDataTemplate(tx, "special_award_prize", where, "dao#DelPrizeAll")
  398. }
  399. // DelPrizesExclude tx
  400. func DelPrizesExclude(tx *sql.Tx, awardID int64, prizeIDs []int64) (int64, error) {
  401. where := fmt.Sprintf("award_id=%d AND prize_id NOT IN (%s)", awardID, xstr.JoinInts(prizeIDs))
  402. return delDataTemplate(tx, "special_award_prize", where, "dao#DelPrizesExclude")
  403. }
  404. // DelResources tx
  405. func DelResources(tx *sql.Tx, where string) (int64, error) {
  406. return delDataTemplate(tx, "special_award_resource", where, "dao#DelResource")
  407. }
  408. // SaveWinners tx
  409. func SaveWinners(tx *sql.Tx, fields, values string) (rows int64, err error) {
  410. rows, err = saveDataTemplate(tx, "special_award_winner", fields, values, "dao#SaveWinners")
  411. return
  412. }
  413. // SaveDivisions tx
  414. func SaveDivisions(tx *sql.Tx, fields, values string) (rows int64, err error) {
  415. rows, err = saveDataTemplate(tx, "special_award_division", fields, values, "dao#SaveDivision")
  416. return
  417. }
  418. // SaveResource tx
  419. func SaveResource(tx *sql.Tx, fields, values string) (rows int64, err error) {
  420. rows, err = saveDataTemplate(tx, "special_award_resource", fields, values, "dao#SaveResource")
  421. return
  422. }
  423. // SavePrizes tx
  424. func SavePrizes(tx *sql.Tx, fields, values string) (rows int64, err error) {
  425. rows, err = saveDataTemplate(tx, "special_award_prize", fields, values, "dao#SavePrizes")
  426. return
  427. }
  428. func saveDataTemplate(tx *sql.Tx, tableName, fields, values, funcName string) (rows int64, err error) {
  429. str := fmt.Sprintf("INSERT INTO %s(%s) VALUES %s", tableName, fields, values)
  430. res, err := tx.Exec(str)
  431. if err != nil {
  432. log.Error("%s exec(%s) err(%v)", funcName, str, err)
  433. return
  434. }
  435. return res.RowsAffected()
  436. }
  437. func delDataTemplate(tx *sql.Tx, tableName, where, funcName string) (rows int64, err error) {
  438. str := fmt.Sprintf("UPDATE %s SET is_deleted = 1 WHERE %s", tableName, where)
  439. res, err := tx.Exec(str)
  440. if err != nil {
  441. log.Error("%s exec(%s) err(%v)", funcName, str, err)
  442. return
  443. }
  444. return res.RowsAffected()
  445. }