task.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. package newcomer
  2. import (
  3. "context"
  4. "fmt"
  5. "database/sql"
  6. "go-common/app/interface/main/creative/model/newcomer"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. "strings"
  10. )
  11. const (
  12. // select
  13. _getUserTaskBindSQL = "SELECT count(id) FROM newcomers_task_user_%s WHERE mid=?"
  14. _getGiftRewardSQL = "SELECT reward_id FROM newcomers_gift_reward WHERE state=0 AND task_type=?"
  15. _getTaskGroupRewardSQL = "SELECT reward_id FROM newcomers_grouptask_reward WHERE state=0 AND task_group_id=?"
  16. _getIsReceivedSQL = "SELECT count(id) FROM newcomers_reward_receive WHERE mid=?"
  17. _getRewardReceiveStateSQL = "SELECT task_group_id FROM newcomers_reward_receive WHERE mid=?"
  18. _getRewardReceiveSQL = "SELECT id, mid, task_gift_id, task_group_id, reward_id, reward_type, state, receive_time, ctime, mtime FROM newcomers_reward_receive WHERE mid=? AND state IN (0,1,2) ORDER BY receive_time ASC"
  19. _getRewardSQL = "SELECT id, parent_id, type, state, is_active, prize_id, prize_unit, expire, name, logo, comment, unlock_logo, name_extra, ctime, mtime FROM newcomers_reward WHERE state=0"
  20. _getUserTaskByMIDSQL = "SELECT id, mid, task_id, task_group_id, task_type, state, task_bind_time, ctime, mtime FROM newcomers_task_user_%s WHERE state=-1 AND mid=?"
  21. _getTaskSQL = "SELECT id, group_id, type, state, target_type, target_value, title, `desc`, comment, ctime, mtime, rank, extra, fan_range, up_time, down_time FROM newcomers_task WHERE state IN (0,1) ORDER BY rank ASC"
  22. _getAllTaskGroupRewardSQL = "SELECT id,task_group_id,reward_id,state,comment,ctime,mtime FROM newcomers_grouptask_reward WHERE state=0"
  23. _getAllGiftRewardSQL = "SELECT id,root_type,task_type,reward_id,state,comment,ctime,mtime FROM newcomers_gift_reward WHERE state=0"
  24. _getUserTaskSQL = "SELECT task_id,state FROM newcomers_task_user_%s WHERE mid=?"
  25. _getRewardReceiveByIDSQL = "SELECT id, mid, task_gift_id, task_group_id, reward_id, reward_type, state, receive_time, ctime, mtime FROM newcomers_reward_receive WHERE mid=? AND id=?"
  26. _getTaskGroupSQL = "SELECT id, rank, state, root_type, type, ctime, mtime FROM newcomers_task_group WHERE state=0"
  27. _getTaskRewardSQL = "SELECT id,task_id,reward_id,state,comment,ctime,mtime FROM newcomers_task_reward WHERE state=0"
  28. _getRewardReceiveByOldInfo = "SELECT id, mid, oid, type, reward_id, reward_type, state, ctime, mtime FROM newcomers_reward_receive_%s WHERE mid=? AND oid=? AND type=? AND reward_id=?"
  29. // insert
  30. // _inRewardReceiveSQL = "INSERT INTO newcomers_reward_receive (mid, task_gift_id, task_group_id, reward_id, reward_type, state, receive_time, ctime, mtime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
  31. // update
  32. _upRewardReceiveSQL = "UPDATE newcomers_reward_receive SET state=? WHERE mid=? AND id=?"
  33. _upRewardReceiveSQL2 = "UPDATE newcomers_reward_receive_%s SET state=? WHERE mid=? AND id=?"
  34. // update
  35. _upUserTaskSQL = "UPDATE newcomers_task_user_%s SET state=? WHERE mid=? AND task_id=?"
  36. )
  37. // getTableName by mid%100
  38. func getTableName(mid int64) string {
  39. return fmt.Sprintf("%02d", mid%100)
  40. }
  41. // UserTaskBind determine if the user has bound the task
  42. func (d *Dao) UserTaskBind(c context.Context, mid int64) (res int64, err error) {
  43. row := d.db.QueryRow(c, fmt.Sprintf(_getUserTaskBindSQL, getTableName(mid)), mid)
  44. if err = row.Scan(&res); err != nil {
  45. log.Error("UserTaskBind d.db.QueryRow error(%v)", err)
  46. return
  47. }
  48. return
  49. }
  50. // IsRewardReceived if the basic reward and the gift award have been received
  51. func (d *Dao) IsRewardReceived(c context.Context, mid int64, rid int64, rewardType int8) (res bool, err error) {
  52. sqlStr := _getIsReceivedSQL
  53. if rewardType == newcomer.RewardGiftType {
  54. sqlStr += fmt.Sprintf(" AND task_gift_id=?")
  55. } else {
  56. sqlStr += fmt.Sprintf(" AND task_group_id=?")
  57. }
  58. row := d.db.QueryRow(c, sqlStr, mid, rid)
  59. count := 0
  60. if err = row.Scan(&count); err != nil {
  61. log.Error("isRewardReceived d.db.Query error(%v)", err)
  62. return
  63. }
  64. if count > 0 {
  65. return true, nil
  66. }
  67. return
  68. }
  69. // RewardReceivedGroup get []task_group_id by []group_id
  70. func (d *Dao) RewardReceivedGroup(c context.Context, mid int64, ids []int64) (res []int, err error) {
  71. if len(ids) == 0 {
  72. return
  73. }
  74. sqlStr := _getRewardReceiveStateSQL
  75. sqlStr += fmt.Sprintf(" AND task_group_id IN (%s)", xstr.JoinInts(ids))
  76. rows, err := d.db.Query(c, sqlStr, mid)
  77. if err != nil {
  78. log.Error("RewardReceivedGroup d.db.Query error(%v)", err)
  79. return
  80. }
  81. defer rows.Close()
  82. res = make([]int, 0)
  83. for rows.Next() {
  84. gid := 0
  85. if err = rows.Scan(&gid); err != nil {
  86. log.Error("RewardReceivedGroup rows.Scan error(%v)", err)
  87. return
  88. }
  89. res = append(res, gid)
  90. }
  91. return
  92. }
  93. // GiftRewards get the rewards of gift by task_type
  94. func (d *Dao) GiftRewards(c context.Context, taskType int8) (res []int64, err error) {
  95. rows, err := d.db.Query(c, _getGiftRewardSQL, taskType)
  96. if err != nil {
  97. log.Error("GiftRewards d.db.Query error(%v)", err)
  98. return
  99. }
  100. defer rows.Close()
  101. res = make([]int64, 0)
  102. for rows.Next() {
  103. var r int64
  104. if err = rows.Scan(&r); err != nil {
  105. log.Error("GiftRewards rows.Scan error(%v)", err)
  106. return
  107. }
  108. res = append(res, r)
  109. }
  110. return
  111. }
  112. // TaskGroupRewards get the rewards of gift by group_id
  113. func (d *Dao) TaskGroupRewards(c context.Context, groupID int64) (res []int64, err error) {
  114. rows, err := d.db.Query(c, _getTaskGroupRewardSQL, groupID)
  115. if err != nil {
  116. log.Error("TaskGroupRewards d.db.Query error(%v)", err)
  117. return
  118. }
  119. defer rows.Close()
  120. res = make([]int64, 0)
  121. for rows.Next() {
  122. var r int64
  123. if err = rows.Scan(&r); err != nil {
  124. log.Error("TaskGroupRewards rows.Scan error(%v)", err)
  125. return
  126. }
  127. res = append(res, r)
  128. }
  129. return
  130. }
  131. // RewardCompleteState judge the completion of the task
  132. func (d *Dao) RewardCompleteState(c context.Context, mid int64, tids []int64) (res int64, err error) {
  133. sqlStr := fmt.Sprintf(_getUserTaskSQL, getTableName(mid))
  134. sqlStr += fmt.Sprintf(" AND task_id IN (%s)", xstr.JoinInts(tids))
  135. rows, err := d.db.Query(c, sqlStr, mid)
  136. if err != nil {
  137. log.Error("RewardCompleteState d.db.Query error(%v)", err)
  138. return
  139. }
  140. defer rows.Close()
  141. res = 0
  142. for rows.Next() {
  143. t := &newcomer.Task{}
  144. if err = rows.Scan(&t.ID, &t.CompleteSate); err != nil {
  145. log.Error("RewardCompleteState rows.Scan error(%v)", err)
  146. return
  147. }
  148. if t.CompleteSate == newcomer.TaskIncomplete {
  149. res++
  150. }
  151. }
  152. return
  153. }
  154. // RewardReceive add reward receive records.
  155. func (d *Dao) RewardReceive(c context.Context, places string, args []interface{}) (int64, error) {
  156. var (
  157. res sql.Result
  158. err error
  159. )
  160. sqlStr := fmt.Sprintf("INSERT INTO newcomers_reward_receive(mid, task_gift_id, task_group_id, reward_id, reward_type, state) VALUES %s", places)
  161. res, err = d.db.Exec(c, sqlStr, args...)
  162. if err != nil {
  163. log.Error("RewardReceive tx.Exec error(%v)", err)
  164. return 0, err
  165. }
  166. return res.LastInsertId()
  167. }
  168. // RewardActivate activate reward.
  169. func (d *Dao) RewardActivate(c context.Context, mid, id int64) (int64, error) {
  170. res, err := d.db.Exec(c, _upRewardReceiveSQL, newcomer.RewardActivatedNotClick, mid, id)
  171. if err != nil {
  172. log.Error("RewardActivate d.db.Exec mid(%d) id(%d) error(%v)", mid, id, err)
  173. return 0, err
  174. }
  175. return res.RowsAffected()
  176. }
  177. //RewardReceives get reward receive by mid.
  178. func (d *Dao) RewardReceives(c context.Context, mid int64) (res map[int8][]*newcomer.RewardReceive, err error) {
  179. rows, err := d.db.Query(c, _getRewardReceiveSQL, mid)
  180. if err != nil {
  181. log.Error("RewardReceives d.db.Query error(%v)", err)
  182. return
  183. }
  184. defer rows.Close()
  185. res = make(map[int8][]*newcomer.RewardReceive)
  186. for rows.Next() {
  187. r := &newcomer.RewardReceive{}
  188. if err = rows.Scan(&r.ID, &r.MID, &r.TaskGiftID, &r.TaskGroupID, &r.RewardID, &r.RewardType, &r.State, &r.ReceiveTime, &r.CTime, &r.MTime); err != nil {
  189. log.Error("RewardReceives rows.Scan error(%v)", err)
  190. return
  191. }
  192. res[r.RewardType] = append(res[r.RewardType], r)
  193. }
  194. return
  195. }
  196. //Rewards get all reward receive.
  197. func (d *Dao) Rewards(c context.Context) (res []*newcomer.Reward, err error) {
  198. rows, err := d.db.Query(c, _getRewardSQL)
  199. if err != nil {
  200. log.Error("Rewards d.db.Query error(%v)", err)
  201. return
  202. }
  203. defer rows.Close()
  204. res = make([]*newcomer.Reward, 0)
  205. for rows.Next() {
  206. r := &newcomer.Reward{}
  207. if err = rows.Scan(&r.ID, &r.ParentID, &r.Type, &r.State, &r.IsActive, &r.PriceID, &r.PrizeUnit, &r.Expire, &r.Name, &r.Logo, &r.Comment, &r.UnlockLogo, &r.NameExtra, &r.CTime, &r.MTime); err != nil {
  208. log.Error("Rewards rows.Scan error(%v)", err)
  209. return
  210. }
  211. res = append(res, r)
  212. }
  213. return
  214. }
  215. // BindTasks user bind tasks
  216. func (d *Dao) BindTasks(c context.Context, mid int64, places string, args []interface{}) (res int64, err error) {
  217. sqlStr := fmt.Sprintf("INSERT INTO newcomers_task_user_%s(mid , task_id , task_group_id , task_type , state) VALUES %s", getTableName(mid), places)
  218. result, err := d.db.Exec(c, sqlStr, args...)
  219. if err != nil {
  220. log.Error("BindTasks d.db.Exec error(%v)", err)
  221. return
  222. }
  223. return result.LastInsertId()
  224. }
  225. // GiftRewardCount get received gift reward count
  226. func (d *Dao) GiftRewardCount(c context.Context, mid int64, ids []int64) (res int64, err error) {
  227. if len(ids) == 0 {
  228. return
  229. }
  230. sqlStr := fmt.Sprintf("SELECT count(DISTINCT task_gift_id) FROM newcomers_reward_receive WHERE mid=? AND task_gift_id IN (%s)", xstr.JoinInts(ids))
  231. row := d.db.QueryRow(c, sqlStr, mid)
  232. err = row.Scan(&res)
  233. if err != nil {
  234. log.Error("GiftRewardCount d.db.QueryRow error(%v)", err)
  235. return
  236. }
  237. return
  238. }
  239. // BaseRewardCount get received base reward count
  240. func (d *Dao) BaseRewardCount(c context.Context, mid int64, ids []int64) (res int64, err error) {
  241. if len(ids) == 0 {
  242. return
  243. }
  244. sqlStr := fmt.Sprintf("SELECT count(DISTINCT task_group_id) FROM newcomers_reward_receive WHERE mid=? AND task_group_id IN (%s)", xstr.JoinInts(ids))
  245. row := d.db.QueryRow(c, sqlStr, mid)
  246. err = row.Scan(&res)
  247. if err != nil {
  248. log.Error("BaseRewardCount d.db.QueryRow error(%v)", err)
  249. return
  250. }
  251. return
  252. }
  253. //Tasks get all task.
  254. func (d *Dao) Tasks(c context.Context) (res []*newcomer.Task, err error) {
  255. rows, err := d.db.Query(c, _getTaskSQL)
  256. if err != nil {
  257. log.Error("Tasks d.db.Query error(%v)", err)
  258. return
  259. }
  260. defer rows.Close()
  261. res = make([]*newcomer.Task, 0)
  262. for rows.Next() {
  263. r := &newcomer.Task{}
  264. if err = rows.Scan(&r.ID, &r.GroupID, &r.Type, &r.State, &r.TargetType, &r.TargetValue, &r.Title, &r.Desc, &r.Comment, &r.CTime, &r.MTime, &r.Rank, &r.Extra, &r.FanRange, &r.UpTime, &r.DownTime); err != nil {
  265. log.Error("Tasks rows.Scan error(%v)", err)
  266. return
  267. }
  268. res = append(res, r)
  269. }
  270. return
  271. }
  272. //UserTasksByMID get user unfinish task by mid.
  273. func (d *Dao) UserTasksByMID(c context.Context, mid int64) (res []*newcomer.UserTask, err error) {
  274. rows, err := d.db.Query(c, fmt.Sprintf(_getUserTaskByMIDSQL, getTableName(mid)), mid)
  275. if err != nil {
  276. log.Error("UserTasksByMID d.db.Query error(%v)", err)
  277. return
  278. }
  279. defer rows.Close()
  280. res = make([]*newcomer.UserTask, 0)
  281. for rows.Next() {
  282. r := &newcomer.UserTask{}
  283. if err = rows.Scan(&r.ID, &r.MID, &r.TaskID, &r.TaskGroupID, &r.TaskType, &r.State, &r.TaskBindTime, &r.CTime, &r.MTime); err != nil {
  284. log.Error("UserTasksByMID rows.Scan error(%v)", err)
  285. return
  286. }
  287. res = append(res, r)
  288. }
  289. return
  290. }
  291. // UpUserTask update user task finish state
  292. func (d *Dao) UpUserTask(c context.Context, mid, tid int64) (int64, error) {
  293. res, err := d.db.Exec(c, fmt.Sprintf(_upUserTaskSQL, getTableName(mid)), 0, mid, tid)
  294. if err != nil {
  295. log.Error("RewardActivate d.db.Exec mid(%d) id(%d) error(%v)", mid, tid, err)
  296. return 0, err
  297. }
  298. return res.RowsAffected()
  299. }
  300. // UserTaskType Determining the type of task that users have
  301. func (d *Dao) UserTaskType(c context.Context, mid int64) (res int8, err error) {
  302. sqlStr := fmt.Sprintf("SELECT count(DISTINCT task_type) FROM newcomers_task_user_%s WHERE mid=?", getTableName(mid))
  303. row := d.db.QueryRow(c, sqlStr, mid)
  304. if err = row.Scan(&res); err != nil {
  305. log.Error("UserTaskType row.Scan(&res) mid(%d)|error(%v)", mid, err)
  306. return
  307. }
  308. return
  309. }
  310. // AllTaskGroupRewards get all TaskGroupRewards for cache
  311. func (d *Dao) AllTaskGroupRewards(c context.Context) (res map[int64][]*newcomer.TaskGroupReward, err error) {
  312. rows, err := d.db.Query(c, _getAllTaskGroupRewardSQL)
  313. if err != nil {
  314. log.Error("AllTaskGroupRewards d.db.Query error(%v)", err)
  315. return
  316. }
  317. defer rows.Close()
  318. res = make(map[int64][]*newcomer.TaskGroupReward)
  319. for rows.Next() {
  320. t := &newcomer.TaskGroupReward{}
  321. if err = rows.Scan(&t.ID, &t.TaskGroupID, &t.RewardID, &t.State, &t.Comment, &t.CTime, &t.MTime); err != nil {
  322. log.Error("AllTaskGroupRewards rows.Scan error(%v)", err)
  323. return
  324. }
  325. res[t.TaskGroupID] = append(res[t.TaskGroupID], t)
  326. }
  327. return
  328. }
  329. // AllGiftRewards get all GiftRewards for cache
  330. func (d *Dao) AllGiftRewards(c context.Context) (res map[int8][]*newcomer.GiftReward, err error) {
  331. rows, err := d.db.Query(c, _getAllGiftRewardSQL)
  332. if err != nil {
  333. log.Error("AllTaskGroupRewards d.db.Query error(%v)", err)
  334. return
  335. }
  336. defer rows.Close()
  337. res = make(map[int8][]*newcomer.GiftReward)
  338. for rows.Next() {
  339. t := &newcomer.GiftReward{}
  340. if err = rows.Scan(&t.ID, &t.RootType, &t.TaskType, &t.RewardID, &t.State, &t.Comment, &t.CTime, &t.MTime); err != nil {
  341. log.Error("AllTaskGroupRewards rows.Scan error(%v)", err)
  342. return
  343. }
  344. res[t.TaskType] = append(res[t.TaskType], t)
  345. }
  346. return
  347. }
  348. // UserTasks get user tasks
  349. func (d *Dao) UserTasks(c context.Context, mid int64) (res []*newcomer.Task, err error) {
  350. sqlStr := fmt.Sprintf(_getUserTaskSQL, getTableName(mid))
  351. rows, err := d.db.Query(c, sqlStr, mid)
  352. if err != nil {
  353. log.Error("UserTasks d.db.Query mid(%d)|error(%v)", mid, err)
  354. return
  355. }
  356. defer rows.Close()
  357. res = make([]*newcomer.Task, 0)
  358. for rows.Next() {
  359. t := &newcomer.Task{}
  360. if err = rows.Scan(&t.ID, &t.CompleteSate); err != nil {
  361. log.Error("UserTasks rows.Scan mid(%d)|error(%v)", mid, err)
  362. return
  363. }
  364. res = append(res, t)
  365. }
  366. return
  367. }
  368. // RewardReceiveByID get rewardReceive by receiveID
  369. func (d *Dao) RewardReceiveByID(c context.Context, mid, receiveID int64) (res *newcomer.RewardReceive, err error) {
  370. row := d.db.QueryRow(c, _getRewardReceiveByIDSQL, mid, receiveID)
  371. res = &newcomer.RewardReceive{}
  372. if err = row.Scan(&res.ID, &res.MID, &res.TaskGiftID, &res.TaskGroupID, &res.RewardID, &res.RewardType, &res.State, &res.ReceiveTime, &res.CTime, &res.MTime); err != nil {
  373. log.Error("RewardIDByReceiveID row.Scan error(%v)", err)
  374. return
  375. }
  376. return
  377. }
  378. // TaskGroups get task-group
  379. func (d *Dao) TaskGroups(c context.Context) (res []*newcomer.TaskGroupEntity, err error) {
  380. rows, err := d.db.Query(c, _getTaskGroupSQL)
  381. if err != nil {
  382. log.Error("TaskGroups d.db.Query error(%v)", err)
  383. return
  384. }
  385. defer rows.Close()
  386. res = make([]*newcomer.TaskGroupEntity, 0)
  387. for rows.Next() {
  388. t := &newcomer.TaskGroupEntity{}
  389. if err = rows.Scan(&t.ID, &t.Rank, &t.State, &t.RootType, &t.Type, &t.CTime, &t.MTime); err != nil {
  390. log.Error("TaskGroups rows.Scan error(%v)", err)
  391. return
  392. }
  393. res = append(res, t)
  394. }
  395. return
  396. }
  397. // TaskRewards get task-rewards
  398. func (d *Dao) TaskRewards(c context.Context) (res map[int64][]*newcomer.TaskRewardEntity, err error) {
  399. rows, err := d.db.Query(c, _getTaskRewardSQL)
  400. if err != nil {
  401. log.Error("TaskRewards d.db.Query error(%v)", err)
  402. return
  403. }
  404. defer rows.Close()
  405. res = make(map[int64][]*newcomer.TaskRewardEntity)
  406. for rows.Next() {
  407. t := &newcomer.TaskRewardEntity{}
  408. if err = rows.Scan(&t.ID, &t.TaskID, &t.RewardID, &t.State, &t.Comment, &t.CTime, &t.MTime); err != nil {
  409. log.Error("TaskRewards rows.Scan error(%v)", err)
  410. return
  411. }
  412. res[t.TaskID] = append(res[t.TaskID], t)
  413. }
  414. return
  415. }
  416. // RewardReceive2 add reward receive records.
  417. func (d *Dao) RewardReceive2(c context.Context, mid int64, rrs []*newcomer.RewardReceive2) (res int64, err error) {
  418. if len(rrs) == 0 {
  419. return 0, nil
  420. }
  421. tx, err := d.db.Begin(c)
  422. if err != nil {
  423. log.Error("RewardReceive2 d.db.Begin mid(%d)|error(%v)", mid, err)
  424. return
  425. }
  426. place := make([]string, 0)
  427. args := make([]interface{}, 0)
  428. for _, v := range rrs {
  429. place = append(place, "(?, ?, ?, ?, ? ,?)")
  430. if v.Type == newcomer.RewardBaseType {
  431. args = append(args, v.MID, 0, v.OID, v.RewardID, v.RewardType, v.State)
  432. } else if v.Type == newcomer.RewardGiftType {
  433. args = append(args, v.MID, v.OID, 0, v.RewardID, v.RewardType, v.State)
  434. }
  435. }
  436. placeStr := strings.Join(place, ",")
  437. sqlStr := fmt.Sprintf("INSERT INTO newcomers_reward_receive(mid, task_gift_id, task_group_id, reward_id, reward_type, state) VALUES %s", placeStr)
  438. _, err = tx.Exec(sqlStr, args...)
  439. if err != nil {
  440. if rollbackErr := tx.Rollback(); rollbackErr != nil {
  441. log.Error("RewardReceive2 tx.Rollback mid(%d)|error(%v)", mid, rollbackErr)
  442. }
  443. log.Error("RewardReceive2 tx.Exec mid(%d)|error(%v)", mid, err)
  444. return
  445. }
  446. // insert to new table
  447. placeNew := make([]string, 0)
  448. argsNew := make([]interface{}, 0)
  449. for _, v := range rrs {
  450. placeNew = append(placeNew, "(?, ?, ?, ?, ? ,?)")
  451. argsNew = append(argsNew, v.MID, v.OID, v.Type, v.RewardID, v.RewardType, v.State)
  452. }
  453. placeStrNew := strings.Join(placeNew, ",")
  454. sqlStrNew := fmt.Sprintf("INSERT INTO newcomers_reward_receive_%s(mid, oid, type, reward_id, reward_type, state) VALUES %s", getTableName(mid), placeStrNew)
  455. result, err := tx.Exec(sqlStrNew, argsNew...)
  456. if err != nil {
  457. if rollbackErr := tx.Rollback(); rollbackErr != nil {
  458. log.Error("RewardReceive2 tx.Rollback mid(%d)|error(%v)", mid, rollbackErr)
  459. }
  460. log.Error("RewardReceive2 tx.Exec mid(%d)|error(%v)", mid, err)
  461. return
  462. }
  463. if err = tx.Commit(); err != nil {
  464. log.Error("RewardReceive2 tx.Commit() mid(%d)|error(%v)", mid, err)
  465. return
  466. }
  467. return result.LastInsertId()
  468. }
  469. // RewardReceiveByOldInfo get rewardReceive2 by old info
  470. func (d *Dao) RewardReceiveByOldInfo(c context.Context, r *newcomer.RewardReceive) (res *newcomer.RewardReceive2, err error) {
  471. sqlStr := fmt.Sprintf(_getRewardReceiveByOldInfo, getTableName(r.MID))
  472. var (
  473. oid int64
  474. ty int8
  475. )
  476. if r.TaskGiftID == 0 && r.TaskGroupID != 0 {
  477. oid = r.TaskGroupID
  478. ty = newcomer.RewardBaseType
  479. } else if r.TaskGroupID == 0 && r.TaskGiftID != 0 {
  480. oid = r.TaskGiftID
  481. ty = newcomer.RewardGiftType
  482. }
  483. row := d.db.QueryRow(c, sqlStr, r.MID, oid, ty, r.RewardID)
  484. res = &newcomer.RewardReceive2{}
  485. if err = row.Scan(&res.ID, &res.MID, &res.OID, &res.Type, &res.RewardID, &res.RewardType, &res.State, &res.CTime, &res.MTime); err != nil {
  486. log.Error("RewardReceiveByOldInfo row.Scan error(%v)", err)
  487. return
  488. }
  489. return
  490. }
  491. // RewardActivate2 activate reward double write
  492. func (d *Dao) RewardActivate2(c context.Context, mid, oid, nid int64) (int64, error) {
  493. tx, err := d.db.Begin(c)
  494. if err != nil {
  495. log.Error("RewardActivate2 d.db.Begin mid(%d)|error(%v)", mid, err)
  496. return 0, err
  497. }
  498. _, err = tx.Exec(_upRewardReceiveSQL, newcomer.RewardActivatedNotClick, mid, oid)
  499. if err != nil {
  500. if rollbackErr := tx.Rollback(); rollbackErr != nil {
  501. log.Error("RewardActivate2 tx.Rollback mid(%d)|error(%v)", mid, rollbackErr)
  502. }
  503. log.Error("RewardActivate2 tx.Exec mid(%d)|error(%v)", mid, err)
  504. return 0, err
  505. }
  506. // update new table
  507. sqlStr := fmt.Sprintf(_upRewardReceiveSQL2, getTableName(mid))
  508. res, err := tx.Exec(sqlStr, newcomer.RewardActivatedNotClick, mid, nid)
  509. if err != nil {
  510. if rollbackErr := tx.Rollback(); rollbackErr != nil {
  511. log.Error("RewardActivate2 tx.Rollback mid(%d)|error(%v)", mid, rollbackErr)
  512. }
  513. log.Error("RewardActivate2 tx.Exec mid(%d)|error(%v)", mid, err)
  514. return 0, err
  515. }
  516. if err = tx.Commit(); err != nil {
  517. log.Error("RewardActivate2 tx.Commit() mid(%d)|error(%v)", mid, err)
  518. return 0, err
  519. }
  520. return res.RowsAffected()
  521. }