task.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. package service
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "time"
  7. "go-common/app/admin/main/creative/model/task"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. )
  11. //TaskGroupRewards for task group & rewards.
  12. func (s *Service) TaskGroupRewards(ids []int64) (res map[int64][]*task.TaskGroupReward, err error) {
  13. tgrs := []*task.TaskGroupReward{}
  14. if err = s.DB.Model(&task.TaskGroupReward{}).Where("task_group_id IN (?)", ids).Where("state>=0").Find(&tgrs).Error; err != nil {
  15. log.Error("s.TaskGroupRewards ids(%+v) error(%v)", ids, err)
  16. }
  17. if len(tgrs) == 0 {
  18. return
  19. }
  20. res = make(map[int64][]*task.TaskGroupReward)
  21. for _, v := range tgrs {
  22. if v != nil {
  23. res[v.TaskGroupID] = append(res[v.TaskGroupID], v)
  24. }
  25. }
  26. return
  27. }
  28. func (s *Service) getRewards(rids []int64) (res []*task.RewardResult) {
  29. _, rewardMap := s.loadRewards()
  30. if len(rids) == 0 || len(rewardMap) == 0 {
  31. return
  32. }
  33. sort.Slice(rids, func(i, j int) bool {
  34. return rids[i] < rids[j]
  35. })
  36. res = make([]*task.RewardResult, 0, len(rids))
  37. for _, rid := range rids {
  38. if v, ok := rewardMap[rid]; ok {
  39. res = append(res, &task.RewardResult{
  40. RewardID: v.ID,
  41. RewardName: v.Name,
  42. })
  43. }
  44. }
  45. return
  46. }
  47. //AddTaskGroup for add task group.
  48. func (s *Service) AddTaskGroup(v *task.TaskGroup, rewardsIDs []int64) (id int64, err error) {
  49. now := time.Now().Format("2006-01-02 15:04:05")
  50. v.CTime = now
  51. tx := s.DB.Begin()
  52. v.State = task.StateHide
  53. if err = tx.Create(v).Error; err != nil {
  54. log.Error("addGroup error(%v)", err)
  55. tx.Rollback()
  56. return
  57. }
  58. if err = tx.Model(&task.TaskGroup{}).Where("id=?", v.ID).Updates(map[string]interface{}{
  59. "rank": v.ID,
  60. }).Error; err != nil {
  61. log.Error("addGroup error(%v)", err)
  62. tx.Rollback()
  63. return
  64. }
  65. if len(rewardsIDs) > 0 {
  66. valReward := make([]string, 0, len(rewardsIDs))
  67. valRewardArgs := make([]interface{}, 0)
  68. for _, rid := range rewardsIDs {
  69. valReward = append(valReward, "(?, ?, ?, ?, ?, ?)")
  70. valRewardArgs = append(valRewardArgs, v.ID, rid, task.StateNormal, v.Comment, now, now)
  71. }
  72. sqlRewardStr := fmt.Sprintf("INSERT INTO newcomers_grouptask_reward (task_group_id, reward_id, state, comment, ctime, mtime) VALUES %s", strings.Join(valReward, ","))
  73. if err = tx.Exec(sqlRewardStr, valRewardArgs...).Error; err != nil {
  74. log.Error("addGroup link reward error(%v)", err)
  75. tx.Rollback()
  76. return
  77. }
  78. }
  79. tx.Commit()
  80. return v.ID, nil
  81. }
  82. //EditTaskGroup for edit task group.
  83. func (s *Service) EditTaskGroup(v *task.TaskGroup, rewardsIDs []int64) (id int64, err error) {
  84. now := time.Now().Format("2006-01-02 15:04:05")
  85. tg := &task.TaskGroup{}
  86. if err = s.DB.Model(&task.TaskGroup{}).Where("id=?", v.ID).Find(tg).Error; err != nil {
  87. log.Error("EditTaskGroup link reward error(%v)", err)
  88. return
  89. }
  90. if tg == nil {
  91. return
  92. }
  93. tx := s.DB.Begin()
  94. if err = tx.Model(&task.TaskGroup{}).Where("id=?", v.ID).Updates(map[string]interface{}{
  95. "root_type": v.RootType,
  96. "type": v.Type,
  97. }).Error; err != nil {
  98. log.Error("editGroup error(%v)", err)
  99. tx.Rollback()
  100. return
  101. }
  102. if len(rewardsIDs) > 0 {
  103. var tgr task.TaskGroupReward
  104. if err = tx.Where("task_group_id =?", v.ID).Delete(&tgr).Error; err != nil {
  105. log.Error("editGroup delete old group id(%d)|error(%v)", v.ID, err)
  106. tx.Rollback()
  107. return
  108. }
  109. valReward := make([]string, 0, len(rewardsIDs))
  110. valRewardArgs := make([]interface{}, 0)
  111. for _, rid := range rewardsIDs {
  112. valReward = append(valReward, "(?, ?, ?, ?, ?, ?)")
  113. valRewardArgs = append(valRewardArgs, v.ID, rid, task.StateNormal, v.Comment, now, now)
  114. }
  115. sqlRewardStr := fmt.Sprintf("INSERT INTO newcomers_grouptask_reward (task_group_id, reward_id, state, comment, ctime, mtime) VALUES %s ON DUPLICATE KEY UPDATE task_group_id=VALUES(task_group_id), reward_id=VALUES(reward_id)", strings.Join(valReward, ","))
  116. if err = tx.Exec(sqlRewardStr, valRewardArgs...).Error; err != nil {
  117. log.Error("editGroup link reward error(%v)", err)
  118. tx.Rollback()
  119. return
  120. }
  121. }
  122. tx.Commit()
  123. return v.ID, nil
  124. }
  125. //OrderTaskGroup for order task group.
  126. func (s *Service) OrderTaskGroup(v *task.OrderTask) (err error) {
  127. tg := &task.TaskGroup{}
  128. if err = s.DB.Find(tg, v.ID).Error; err != nil {
  129. log.Error("orderGroup error(%v)", err)
  130. return
  131. }
  132. stg := &task.TaskGroup{}
  133. if err = s.DB.Find(stg, v.SwitchID).Error; err != nil {
  134. log.Error("orderGroup error(%v)", err)
  135. return
  136. }
  137. tx := s.DB.Begin()
  138. if err = tx.Model(&task.TaskGroup{}).Where("id=?", v.ID).Updates(
  139. map[string]interface{}{
  140. "rank": v.SwitchRank,
  141. },
  142. ).Error; err != nil {
  143. log.Error("orderGroup error(%v)", err)
  144. tx.Rollback()
  145. return
  146. }
  147. if err = tx.Model(&task.TaskGroup{}).Where("id=?", v.SwitchID).Updates(
  148. map[string]interface{}{
  149. "rank": v.Rank,
  150. },
  151. ).Error; err != nil {
  152. log.Error("orderGroup error(%v)", err)
  153. tx.Rollback()
  154. return
  155. }
  156. tx.Commit()
  157. return
  158. }
  159. //UpStateGroup for update task group.
  160. func (s *Service) UpStateGroup(id int64, state int8) (err error) {
  161. tg := &task.TaskGroup{}
  162. if err = s.DB.Find(tg, id).Error; err != nil {
  163. log.Error("UpStateGroup id(%d) error(%v)", id, err)
  164. return
  165. }
  166. if tg.ID == 0 {
  167. err = ecode.NothingFound
  168. return
  169. }
  170. if err = s.DB.Model(&task.TaskGroup{}).Where("id=?", id).Updates(map[string]interface{}{
  171. "state": state,
  172. }).Error; err != nil {
  173. log.Error("UpStateGroup id(%d) state(%d) error(%v)", id, state, err)
  174. return
  175. }
  176. return
  177. }
  178. //TaskGroup for task group.
  179. func (s *Service) TaskGroup(id int64) (res *task.TaskGroup, err error) {
  180. var tg task.TaskGroup
  181. if err = s.DB.Model(&task.TaskGroup{}).Where("id=?", id).Find(&tg).Error; err != nil {
  182. log.Error("s.TaskGroup id (%d) error(%v)", id, err)
  183. return
  184. }
  185. if tg.ID == 0 {
  186. return
  187. }
  188. tgrsMap, _ := s.TaskGroupRewards([]int64{id})
  189. if rs, ok := tgrsMap[id]; ok {
  190. rids := make([]int64, 0, len(rs))
  191. for _, r := range rs {
  192. if r != nil {
  193. rids = append(rids, r.RewardID)
  194. }
  195. }
  196. tg.Reward = s.getRewards(rids)
  197. }
  198. res = &tg
  199. return
  200. }
  201. //AddSubtask for add sub task.
  202. func (s *Service) AddSubtask(v *task.Task, rewardsIDs []int64) (id int64, err error) {
  203. now := time.Now().Format("2006-01-02 15:04:05")
  204. v.CTime = now
  205. v.MTime = now
  206. v.State = task.StateHide
  207. tx := s.DB.Begin()
  208. if err = tx.Create(v).Error; err != nil {
  209. log.Error("AddSubtask error(%v)", err)
  210. tx.Rollback()
  211. return
  212. }
  213. if v.ID == 0 {
  214. log.Error("AddSubtask v.ID(%d)", v.ID)
  215. tx.Rollback()
  216. return
  217. }
  218. if err = tx.Model(&task.Task{}).Where("id=?", v.ID).Updates(map[string]interface{}{
  219. "rank": v.ID,
  220. }).Error; err != nil {
  221. log.Error("AddSubtask error(%v)", err)
  222. tx.Rollback()
  223. return
  224. }
  225. if len(rewardsIDs) > 0 {
  226. valReward := make([]string, 0, len(rewardsIDs))
  227. valRewardArgs := make([]interface{}, 0)
  228. for _, rid := range rewardsIDs {
  229. valReward = append(valReward, "(?, ?, ?, ?, ?, ?)")
  230. valRewardArgs = append(valRewardArgs, v.ID, rid, task.StateNormal, v.Comment, now, now)
  231. }
  232. sqlRewardStr := fmt.Sprintf("INSERT INTO newcomers_task_reward (task_id, reward_id, state, comment, ctime, mtime) VALUES %s", strings.Join(valReward, ","))
  233. if err = tx.Exec(sqlRewardStr, valRewardArgs...).Error; err != nil {
  234. log.Error("AddSubtask link reward error(%v)", err)
  235. tx.Rollback()
  236. return
  237. }
  238. }
  239. tx.Commit()
  240. return v.ID, nil
  241. }
  242. //EditSubtask for edit sub task.
  243. func (s *Service) EditSubtask(v *task.Task, rewardsIDs []int64) (id int64, err error) {
  244. tk := &task.Task{}
  245. if err = s.DB.Model(&task.TaskGroup{}).Where("id=?", v.ID).Find(tk).Error; err != nil {
  246. return
  247. }
  248. if tk == nil {
  249. err = ecode.NothingFound
  250. return
  251. }
  252. now := time.Now().Format("2006-01-02 15:04:05")
  253. v.CTime = tk.CTime
  254. v.MTime = now
  255. v.State = tk.State //编辑不更新状态
  256. tx := s.DB.Begin()
  257. if err = tx.Save(v).Error; err != nil { //Save将包括执行更新SQL时的所有字段,即使它没有更改
  258. log.Error("editSubtask error(%v)", err)
  259. tx.Rollback()
  260. return
  261. }
  262. if len(rewardsIDs) > 0 {
  263. var tr task.TaskReward
  264. if err = tx.Where("task_id =?", v.ID).Delete(&tr).Error; err != nil {
  265. log.Error("editSubtask delete old task id(%d)|error(%v)", v.ID, err)
  266. tx.Rollback()
  267. return
  268. }
  269. valReward := make([]string, 0, len(rewardsIDs))
  270. valRewardArgs := make([]interface{}, 0)
  271. for _, rid := range rewardsIDs {
  272. valReward = append(valReward, "(?, ?, ?, ?, ?, ?)")
  273. valRewardArgs = append(valRewardArgs, v.ID, rid, task.StateNormal, v.Comment, now, now)
  274. }
  275. sqlRewardStr := fmt.Sprintf("INSERT INTO newcomers_task_reward (task_id, reward_id, state, comment, ctime, mtime) VALUES %s ON DUPLICATE KEY UPDATE task_id=VALUES(task_id), reward_id=VALUES(reward_id)", strings.Join(valReward, ","))
  276. if err = tx.Exec(sqlRewardStr, valRewardArgs...).Error; err != nil {
  277. log.Error("editSubtask link reward error(%v)", err)
  278. tx.Rollback()
  279. return
  280. }
  281. }
  282. tx.Commit()
  283. return v.ID, nil
  284. }
  285. //OrderSubTask for order sub task.
  286. func (s *Service) OrderSubTask(v *task.OrderTask) (err error) {
  287. tg := &task.Task{}
  288. if err = s.DB.Find(tg, v.ID).Error; err != nil {
  289. log.Error("OrderSubTask error(%v)", err)
  290. return
  291. }
  292. stg := &task.Task{}
  293. if err = s.DB.Find(stg, v.SwitchID).Error; err != nil {
  294. log.Error("OrderSubTask error(%v)", err)
  295. return
  296. }
  297. tx := s.DB.Begin()
  298. if err = tx.Model(&task.Task{}).Where("id=?", v.ID).Updates(
  299. map[string]interface{}{
  300. "rank": v.SwitchRank,
  301. },
  302. ).Error; err != nil {
  303. log.Error("OrderSubTask error(%v)", err)
  304. tx.Rollback()
  305. return
  306. }
  307. if err = tx.Model(&task.Task{}).Where("id=?", v.SwitchID).Updates(
  308. map[string]interface{}{
  309. "rank": v.Rank,
  310. },
  311. ).Error; err != nil {
  312. log.Error("OrderSubTask error(%v)", err)
  313. tx.Rollback()
  314. return
  315. }
  316. tx.Commit()
  317. return
  318. }
  319. //UpStateSubTask for update sub task state.
  320. func (s *Service) UpStateSubTask(id int64, state int8) (err error) {
  321. tg := &task.Task{}
  322. if err = s.DB.Find(tg, id).Error; err != nil {
  323. return
  324. }
  325. if tg.ID == 0 {
  326. err = ecode.NothingFound
  327. return
  328. }
  329. if err = s.DB.Model(&task.Task{}).Where("id=?", id).Updates(map[string]interface{}{
  330. "state": state,
  331. }).Error; err != nil {
  332. log.Error("UpStateSubTask id(%d) state(%d) error(%v)", id, state, err)
  333. return
  334. }
  335. return
  336. }
  337. //TransferSubtask for transfer one sub task from some one group to another one.
  338. func (s *Service) TransferSubtask(id, gid int64) (err error) {
  339. tg := &task.TaskGroup{}
  340. if err = s.DB.Find(tg, gid).Error; err != nil {
  341. return
  342. }
  343. if tg.ID == 0 {
  344. err = ecode.CreativeNewcomerGroupIDErr
  345. return
  346. }
  347. tk := &task.Task{}
  348. if err = s.DB.Find(tk, id).Error; err != nil {
  349. return
  350. }
  351. if tk.ID == 0 {
  352. err = ecode.NothingFound
  353. return
  354. }
  355. if err = s.DB.Model(&task.Task{}).Where("id=?", id).Updates(map[string]interface{}{
  356. "group_id": gid,
  357. }).Error; err != nil {
  358. log.Error("transferSubtask id(%+v) gid(%d) error(%v)", id, gid, err)
  359. return
  360. }
  361. return
  362. }
  363. //Task for task.
  364. func (s *Service) Task(id int64) (res *task.Task, err error) {
  365. var t task.Task
  366. if err = s.DB.Model(&task.Task{}).Where("id=?", id).Find(&t).Error; err != nil {
  367. log.Error("s.Task id (%d) error(%v)", id, err)
  368. return
  369. }
  370. if t.ID == 0 {
  371. return
  372. }
  373. trsMap, _ := s.TaskRewards([]int64{id})
  374. if rs, ok := trsMap[id]; ok {
  375. rids := make([]int64, 0, len(rs))
  376. for _, r := range rs {
  377. if r != nil {
  378. rids = append(rids, r.RewardID)
  379. }
  380. }
  381. t.Reward = s.getRewards(rids)
  382. }
  383. res = &t
  384. return
  385. }
  386. //TaskRewards for task & rewards.
  387. func (s *Service) TaskRewards(ids []int64) (res map[int64][]*task.TaskReward, err error) {
  388. trs := []*task.TaskReward{}
  389. if err = s.DB.Model(&task.TaskReward{}).Where("state>=0 AND task_id IN (?)", ids).Find(&trs).Error; err != nil {
  390. log.Error("s.TaskRewards ids(%+v) error(%v)", ids, err)
  391. return
  392. }
  393. if len(trs) == 0 {
  394. return
  395. }
  396. res = make(map[int64][]*task.TaskReward)
  397. for _, v := range trs {
  398. if v != nil {
  399. res[v.TaskID] = append(res[v.TaskID], v)
  400. }
  401. }
  402. return
  403. }
  404. //TasksByGroupIDsMap for task map
  405. func (s *Service) TasksByGroupIDsMap(gids []int64) (res map[int64][]*task.Task, err error) {
  406. tks := []*task.Task{}
  407. if err = s.DB.Model(&task.Task{}).Where("state>=0 AND type>0 AND group_id IN (?)", gids).Find(&tks).Error; err != nil {
  408. log.Error("s.TasksByGroupIDsMap id (%+v) error(%v)", gids, err)
  409. return
  410. }
  411. if len(tks) == 0 {
  412. return
  413. }
  414. tkMap := make(map[int64]*task.Task)
  415. tgMap := make(map[int64][]*task.Task)
  416. ids := make([]int64, 0, len(tks))
  417. for _, v := range tks {
  418. if v == nil {
  419. continue
  420. }
  421. if v.Rank == 0 {
  422. v.Rank = v.ID
  423. }
  424. ids = append(ids, v.ID)
  425. tkMap[v.ID] = v
  426. tgMap[v.GroupID] = append(tgMap[v.GroupID], v)
  427. }
  428. trsMap, _ := s.TaskRewards(ids)
  429. for _, id := range ids {
  430. rs, ok := trsMap[id]
  431. if !ok || len(rs) == 0 {
  432. continue
  433. }
  434. rids := make([]int64, 0, len(rs))
  435. for _, r := range rs {
  436. if r != nil {
  437. rids = append(rids, r.RewardID)
  438. }
  439. }
  440. tkMap[id].Reward = s.getRewards(rids)
  441. }
  442. res = make(map[int64][]*task.Task)
  443. for _, gid := range gids {
  444. tks, ok := tgMap[gid]
  445. if !ok || len(tks) == 0 {
  446. continue
  447. }
  448. for _, tk := range tks {
  449. if v, ok := tkMap[tk.ID]; ok {
  450. res[gid] = append(res[gid], v)
  451. }
  452. }
  453. }
  454. for _, v := range res {
  455. sort.Slice(v, func(i, j int) bool {
  456. return v[i].Rank < v[j].Rank
  457. })
  458. }
  459. return
  460. }
  461. //TaskList for task list
  462. func (s *Service) TaskList(ty int8) (res []*task.TaskGroup, err error) {
  463. tgs := []*task.TaskGroup{}
  464. db := s.DB.Model(&task.TaskGroup{}).Where("state>=0 AND root_type>0 AND type>0")
  465. if ty > 0 {
  466. db = db.Where("type=?", ty)
  467. }
  468. if err = db.Find(&tgs).Error; err != nil {
  469. log.Error("TaskList %v\n", err)
  470. return
  471. }
  472. if len(tgs) == 0 {
  473. return
  474. }
  475. gids := make([]int64, 0, len(tgs))
  476. tgMap := make(map[int64]*task.TaskGroup)
  477. for _, v := range tgs {
  478. if v == nil {
  479. continue
  480. }
  481. gids = append(gids, v.ID)
  482. tgMap[v.ID] = v
  483. }
  484. tgrsMap, _ := s.TaskGroupRewards(gids)
  485. for _, id := range gids {
  486. if rs, ok := tgrsMap[id]; ok {
  487. if len(rs) == 0 {
  488. continue
  489. }
  490. rids := make([]int64, 0, len(rs))
  491. for _, r := range rs {
  492. if r != nil {
  493. rids = append(rids, r.RewardID)
  494. }
  495. }
  496. tgMap[id].Reward = s.getRewards(rids)
  497. }
  498. }
  499. tkMap, _ := s.TasksByGroupIDsMap(gids)
  500. res = tgs
  501. for _, v := range res {
  502. if v == nil {
  503. continue
  504. }
  505. if g, ok := tgMap[v.ID]; ok {
  506. v.Reward = g.Reward
  507. }
  508. if tks, ok := tkMap[v.ID]; ok {
  509. v.Tasks = tks
  510. }
  511. }
  512. sort.Slice(res, func(i, j int) bool {
  513. return res[i].Rank < res[j].Rank
  514. })
  515. return
  516. }
  517. //loadRewards for task reward.
  518. func (s *Service) loadRewards() (res []*task.Reward, rwMap map[int64]*task.Reward) {
  519. var (
  520. rds = []*task.Reward{}
  521. err error
  522. )
  523. if err = s.DB.Order("id ASC").Find(&rds).Error; err != nil {
  524. log.Error("loadRewards error(%v)", err)
  525. return
  526. }
  527. res = make([]*task.Reward, 0, len(rds))
  528. rwMap = make(map[int64]*task.Reward)
  529. top := make(map[int64]*task.Reward)
  530. for _, v := range rds {
  531. if v == nil {
  532. continue
  533. }
  534. rwMap[v.ID] = v
  535. if v.ParentID == 0 {
  536. top[v.ID] = v //映射一级对象
  537. res = append(res, v) //追加一级对象
  538. }
  539. }
  540. for _, ch := range rds {
  541. if ch == nil {
  542. continue
  543. }
  544. if p, ok := top[ch.ParentID]; ok && p != nil && p.Type == ch.Type { //为一级对象增加子对象,注:要append满足条件的ch,若append p 则会造成递归导致stack overflow
  545. p.Children = append(p.Children, ch)
  546. }
  547. }
  548. return
  549. }
  550. //RewardTree for reward tree.
  551. func (s *Service) RewardTree() (res []*task.Reward) {
  552. res, _ = s.loadRewards()
  553. return
  554. }
  555. //ViewReward for view one reward.
  556. func (s *Service) ViewReward(id int64) (res *task.Reward, err error) {
  557. rd := task.Reward{}
  558. if err = s.DB.Model(&task.Reward{}).Where("id=?", id).Find(&rd).Error; err != nil {
  559. return
  560. }
  561. res = &rd
  562. return
  563. }
  564. //AddReward for add one reward.
  565. func (s *Service) AddReward(v *task.Reward) (id int64, err error) {
  566. now := time.Now().Format("2006-01-02 15:04:05")
  567. v.CTime = now
  568. v.MTime = now
  569. if err = s.DB.Create(v).Error; err != nil {
  570. log.Error("AddReward v(%+v) error(%v)", v, err)
  571. return
  572. }
  573. return v.ID, nil
  574. }
  575. //EditReward for edit one reward.
  576. func (s *Service) EditReward(v *task.Reward) (id int64, err error) {
  577. rd := &task.Reward{}
  578. if err = s.DB.Model(&task.Reward{}).Where("id=?", v.ID).Find(rd).Error; err != nil {
  579. return
  580. }
  581. if rd == nil {
  582. err = ecode.NothingFound
  583. return
  584. }
  585. v.CTime = rd.CTime
  586. v.MTime = time.Now().Format("2006-01-02 15:04:05")
  587. v.State = rd.State //编辑不更新状态
  588. if err = s.DB.Save(v).Error; err != nil {
  589. log.Error("EditReward v(%+v) error(%v)", v, err)
  590. return
  591. }
  592. return v.ID, nil
  593. }
  594. //UpStateReward for update reward state.
  595. func (s *Service) UpStateReward(id int64, state int8) (err error) {
  596. rd := &task.Reward{}
  597. if err = s.DB.Find(rd, id).Error; err != nil {
  598. return
  599. }
  600. if rd.ID == 0 {
  601. err = ecode.NothingFound
  602. return
  603. }
  604. if rd.ParentID != 0 { //如果是子分类直接更新并返回
  605. if err = s.DB.Model(&task.Reward{}).Where("id=?", id).Updates(map[string]interface{}{
  606. "state": state,
  607. }).Error; err != nil {
  608. log.Error("UpStateReward parent id(%d) state(%d) error(%v)", id, state, err)
  609. }
  610. return
  611. }
  612. rds := []*task.Reward{}
  613. if err = s.DB.Model(&task.Reward{}).Where("parent_id=?", id).Find(&rds).Error; err != nil {
  614. log.Error("UpStateReward get childen by parent id(%d) error(%v)", rd.ParentID, err)
  615. return
  616. }
  617. ids := make([]int64, 0, len(rds)+1)
  618. for _, v := range rds {
  619. if v != nil {
  620. ids = append(ids, v.ID) //追加子分类id
  621. }
  622. }
  623. ids = append(ids, id) //追加父分类id
  624. if err = s.DB.Model(&task.Reward{}).Where("id IN (?)", ids).Updates(map[string]interface{}{
  625. "state": state,
  626. }).Error; err != nil {
  627. log.Error("UpStateReward childen ids(%+v) error(%v)", ids, err)
  628. }
  629. return
  630. }
  631. //ViewGiftReward for view gift.
  632. func (s *Service) ViewGiftReward(v *task.GiftReward) (res *task.GiftReward, err error) {
  633. var gfs []*task.GiftReward
  634. if err = s.DB.Model(&task.GiftReward{}).Where("root_type=? AND task_type=?", v.RootType, v.TaskType).Find(&gfs).Error; err != nil {
  635. log.Error("ViewGiftReward v(%+v) error(%v)", v, err)
  636. return
  637. }
  638. if len(gfs) == 0 {
  639. return
  640. }
  641. res = &task.GiftReward{
  642. RootType: v.RootType,
  643. TaskType: v.TaskType,
  644. }
  645. var (
  646. state int8
  647. comment string
  648. )
  649. rids := make([]int64, 0, len(gfs))
  650. for _, gf := range gfs {
  651. if gf != nil {
  652. state = gf.State
  653. comment = gf.Comment
  654. rids = append(rids, gf.RewardID)
  655. }
  656. }
  657. res.State = state
  658. res.Comment = comment
  659. res.Reward = s.getRewards(rids)
  660. return
  661. }
  662. //ListGiftReward for get gift list.
  663. func (s *Service) ListGiftReward() (res []*task.GiftReward, err error) {
  664. gfs := []*task.GiftReward{}
  665. if err = s.DB.Model(&task.GiftReward{}).Where("root_type>0 AND task_type>0").Find(&gfs).Error; err != nil {
  666. log.Error("ListGiftReward error(%v)", err)
  667. return
  668. }
  669. if len(gfs) == 0 {
  670. return
  671. }
  672. gfMap := make(map[int64][]*task.GiftReward)
  673. for _, v := range gfs {
  674. if v != nil {
  675. gfMap[v.TaskType] = append(gfMap[v.TaskType], v)
  676. }
  677. }
  678. tys := make([]int64, 0, len(gfMap))
  679. for k := range gfMap {
  680. tys = append(tys, k)
  681. }
  682. sort.Slice(tys, func(i, j int) bool {
  683. return tys[i] < tys[j]
  684. })
  685. res = make([]*task.GiftReward, 0, len(tys))
  686. for _, ty := range tys {
  687. gfs, ok := gfMap[ty]
  688. if !ok && len(gfs) == 0 {
  689. continue
  690. }
  691. var rt uint8
  692. if task.CheckRootType(uint8(ty)) {
  693. rt = task.TaskManagement
  694. } else {
  695. rt = task.AchievementManagement
  696. }
  697. re := &task.GiftReward{
  698. RootType: rt,
  699. TaskType: ty,
  700. }
  701. var (
  702. state int8
  703. comment string
  704. )
  705. rids := make([]int64, 0, len(gfs))
  706. for _, gf := range gfs {
  707. if gf != nil {
  708. state = gf.State
  709. comment = gf.Comment
  710. rids = append(rids, gf.RewardID)
  711. }
  712. }
  713. re.State = state
  714. re.Comment = comment
  715. re.Reward = s.getRewards(rids)
  716. res = append(res, re)
  717. }
  718. return
  719. }
  720. //AddGiftReward for add gift rewards.
  721. func (s *Service) AddGiftReward(v *task.GiftReward, rewardsIDs []int64) (rows int64, err error) {
  722. var gfs []*task.GiftReward
  723. if err = s.DB.Model(&task.GiftReward{}).Where("root_type=? AND task_type=? AND reward_id IN (?)", v.RootType, v.TaskType, rewardsIDs).Find(&gfs).Error; err != nil {
  724. log.Error("UpGiftReward v(%+v) error(%v)", v, err)
  725. return
  726. }
  727. if len(gfs) != 0 {
  728. hitMap := make(map[int64]struct{})
  729. for _, gf := range gfs {
  730. hitMap[gf.RewardID] = struct{}{}
  731. }
  732. for _, rid := range rewardsIDs {
  733. if _, ok := hitMap[rid]; ok {
  734. err = ecode.CreativeNewcomerDuplicateGiftRewardIDErr
  735. log.Error("AddGiftReward rid(%d) error(%v)", rid, err)
  736. return
  737. }
  738. }
  739. }
  740. valReward := make([]string, 0, len(rewardsIDs))
  741. valRewardArgs := make([]interface{}, 0)
  742. now := time.Now().Format("2006-01-02 15:04:05")
  743. for _, rid := range rewardsIDs {
  744. valReward = append(valReward, "(?, ?, ?, ?, ?, ?, ?)")
  745. valRewardArgs = append(valRewardArgs, v.RootType, v.TaskType, rid, task.StateNormal, v.Comment, now, now)
  746. }
  747. sqlRewardStr := fmt.Sprintf("INSERT INTO newcomers_gift_reward (root_type, task_type, reward_id, state, comment, ctime, mtime) VALUES %s", strings.Join(valReward, ","))
  748. if err = s.DB.Exec(sqlRewardStr, valRewardArgs...).Error; err != nil {
  749. log.Error("AddGiftReward error(%v)", err)
  750. return
  751. }
  752. return s.DB.RowsAffected, nil
  753. }
  754. //EditGiftReward for edit gift rewards.
  755. func (s *Service) EditGiftReward(v *task.GiftReward, rewardsIDs []int64) (rows int64, err error) {
  756. var (
  757. gfs []*task.GiftReward
  758. state int8
  759. )
  760. if err = s.DB.Model(&task.GiftReward{}).Where("root_type=? AND task_type=?", v.RootType, v.TaskType).Find(&gfs).Error; err != nil {
  761. log.Error("EditGiftReward v(%+v) error(%v)", v, err)
  762. return
  763. }
  764. if len(gfs) == 0 {
  765. err = ecode.NothingFound
  766. return
  767. }
  768. for _, gf := range gfs { //获取原来的状态
  769. state = gf.State
  770. break
  771. }
  772. var gf task.GiftReward
  773. if err = s.DB.Model(&task.GiftReward{}).Where("root_type =?", v.RootType).Where("task_type =?", v.TaskType).Delete(&gf).Error; err != nil {
  774. log.Error("EditGiftReward delete old id(%d)|error(%v)", v.ID, err)
  775. return
  776. }
  777. valReward := make([]string, 0, len(rewardsIDs))
  778. valRewardArgs := make([]interface{}, 0)
  779. now := time.Now().Format("2006-01-02 15:04:05")
  780. for _, rid := range rewardsIDs {
  781. valReward = append(valReward, "(?, ?, ?, ?, ?, ?, ?)")
  782. valRewardArgs = append(valRewardArgs, v.RootType, v.TaskType, rid, state, v.Comment, now, now)
  783. }
  784. sqlRewardStr := fmt.Sprintf("INSERT INTO newcomers_gift_reward (root_type, task_type, reward_id, state, comment, ctime, mtime) VALUES %s", strings.Join(valReward, ","))
  785. if err = s.DB.Exec(sqlRewardStr, valRewardArgs...).Error; err != nil {
  786. log.Error("EditGiftReward error(%v)", err)
  787. return
  788. }
  789. return s.DB.RowsAffected, nil
  790. }
  791. //UpGiftReward for update gift reward.
  792. func (s *Service) UpGiftReward(v *task.GiftReward) (rows int64, err error) {
  793. var gfs []*task.GiftReward
  794. if err = s.DB.Model(&task.GiftReward{}).Where("root_type=? AND task_type=?", v.RootType, v.TaskType).Find(&gfs).Error; err != nil {
  795. log.Error("UpGiftReward v(%+v) error(%v)", v, err)
  796. return
  797. }
  798. if len(gfs) == 0 {
  799. return
  800. }
  801. if err = s.DB.Model(&task.GiftReward{}).Where("root_type=? AND task_type=?", v.RootType, v.TaskType).Updates(map[string]interface{}{
  802. "state": v.State,
  803. }).Error; err != nil {
  804. log.Error("UpGiftReward v(%+v) error(%v)", v, err)
  805. return
  806. }
  807. return s.DB.RowsAffected, nil
  808. }
  809. //BatchOnline for subtask & grouptask with state,rank and so on.
  810. func (s *Service) BatchOnline(tgs []*task.TaskGroup) (err error) {
  811. tasks := make([]*task.Task, 0)
  812. groups := make([]*task.TaskGroup, 0, len(tgs))
  813. for _, v := range tgs {
  814. groups = append(groups, &task.TaskGroup{
  815. ID: v.ID,
  816. Rank: v.Rank,
  817. State: v.State,
  818. RootType: v.RootType,
  819. Type: v.Type,
  820. })
  821. tasks = append(tasks, v.Tasks...)
  822. }
  823. valGroups := make([]string, 0, len(groups))
  824. valGroupArgs := make([]interface{}, 0)
  825. for _, v := range groups {
  826. valGroups = append(valGroups, "(?, ?, ?)")
  827. valGroupArgs = append(valGroupArgs, v.ID, v.State, v.Rank)
  828. }
  829. sqlGroupStr := fmt.Sprintf("INSERT INTO newcomers_task_group (id, state, rank) VALUES %s ON DUPLICATE KEY UPDATE state=VALUES(state), rank=VALUES(rank)", strings.Join(valGroups, ","))
  830. if err = s.DB.Exec(sqlGroupStr, valGroupArgs...).Error; err != nil {
  831. log.Error("BatchOnline update groups error(%v)", err)
  832. return
  833. }
  834. valTasks := make([]string, 0, len(tasks))
  835. valTaskArgs := make([]interface{}, 0)
  836. for _, v := range tasks {
  837. valTasks = append(valTasks, "(?, ?, ?, ?)")
  838. valTaskArgs = append(valTaskArgs, v.ID, v.State, v.Rank, v.GroupID)
  839. }
  840. sqlTaskStr := fmt.Sprintf("INSERT INTO newcomers_task (id, state, rank, group_id) VALUES %s ON DUPLICATE KEY UPDATE state=VALUES(state), rank=VALUES(rank), group_id=VALUES(group_id)", strings.Join(valTasks, ","))
  841. if err = s.DB.Exec(sqlTaskStr, valTaskArgs...).Error; err != nil {
  842. log.Error("BatchOnline update tasks error(%v)", err)
  843. }
  844. return
  845. }