task.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. package http
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "net/http"
  6. "go-common/app/admin/main/creative/model/task"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/render"
  11. "go-common/library/xstr"
  12. )
  13. func taskList(c *bm.Context) {
  14. var err error
  15. v := new(struct {
  16. Type int8 `form:"type"`
  17. })
  18. if err = c.Bind(v); err != nil {
  19. return
  20. }
  21. res, err := svc.TaskList(v.Type)
  22. if err != nil {
  23. c.JSON(nil, err)
  24. return
  25. }
  26. c.JSON(res, nil)
  27. }
  28. func taskPre(c *bm.Context) {
  29. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  30. "code": 0,
  31. "message": "0",
  32. "data": map[string]interface{}{
  33. "target": task.TargetMap,
  34. "root_type": task.TaskRootNameMap,
  35. "group_type": task.TaskGroupNameMap,
  36. },
  37. }))
  38. }
  39. func viewGroup(c *bm.Context) {
  40. var err error
  41. v := new(struct {
  42. ID int64 `form:"id"`
  43. })
  44. if err = c.Bind(v); err != nil {
  45. return
  46. }
  47. if v.ID == 0 {
  48. c.JSON(nil, ecode.RequestErr)
  49. return
  50. }
  51. res, err := svc.TaskGroup(v.ID)
  52. if err != nil {
  53. c.JSON(nil, err)
  54. return
  55. }
  56. c.JSON(res, nil)
  57. }
  58. func addGroup(c *bm.Context) {
  59. var err error
  60. v := new(task.TaskGroup)
  61. if err = c.Bind(v); err != nil {
  62. log.Error("addGroup c.Bind v(%+v) error(%v)", v, err)
  63. return
  64. }
  65. var rewardsIDs []int64
  66. if v.RewardIDs != "" {
  67. if rewardsIDs, err = xstr.SplitInts(v.RewardIDs); err != nil {
  68. log.Error("addGroup xstr.SplitInts task v.RewardIDs(%s) rewardsIDs(%+v)|error(%v)", v.RewardIDs, rewardsIDs, err)
  69. c.JSON(nil, ecode.RequestErr)
  70. return
  71. }
  72. }
  73. id, err := svc.AddTaskGroup(v, rewardsIDs)
  74. if err != nil {
  75. c.JSON(nil, err)
  76. return
  77. }
  78. uid, uname := getUIDName(c)
  79. svc.SendTaskLog(c, &task.LogParam{UID: uid, UName: uname, Action: "添加任务组", OID: id, Content: v})
  80. c.JSON(map[string]interface{}{
  81. "id": id,
  82. }, nil)
  83. }
  84. func editGroup(c *bm.Context) {
  85. var err error
  86. v := new(task.TaskGroup)
  87. if err = c.Bind(v); err != nil {
  88. log.Error("editGroup c.Bind v(%+v) error(%v)", v, err)
  89. return
  90. }
  91. if v.ID == 0 {
  92. c.JSON(nil, ecode.RequestErr)
  93. return
  94. }
  95. var rewardsIDs []int64
  96. if v.RewardIDs != "" {
  97. if rewardsIDs, err = xstr.SplitInts(v.RewardIDs); err != nil {
  98. log.Error("editGroup xstr.SplitInts task v.RewardIDs(%s) rewardsIDs(%+v)|error(%v)", v.RewardIDs, rewardsIDs, err)
  99. c.JSON(nil, ecode.RequestErr)
  100. return
  101. }
  102. }
  103. id, err := svc.EditTaskGroup(v, rewardsIDs)
  104. if err != nil {
  105. c.JSON(nil, err)
  106. return
  107. }
  108. uid, uname := getUIDName(c)
  109. svc.SendTaskLog(c, &task.LogParam{UID: uid, UName: uname, Action: "编辑任务组", OID: id, Content: v})
  110. c.JSON(map[string]interface{}{
  111. "id": id,
  112. }, nil)
  113. }
  114. func orderGroup(c *bm.Context) {
  115. var err error
  116. v := new(task.OrderTask)
  117. if err = c.Bind(v); err != nil {
  118. log.Error("orderGroup c.Bind v(%+v) error(%v)", v, err)
  119. return
  120. }
  121. uid, uname := getUIDName(c)
  122. svc.SendTaskLog(c, &task.LogParam{UID: uid, UName: uname, Action: "移动任务组顺序", OID: v.ID, Content: v})
  123. c.JSON(nil, svc.OrderTaskGroup(v))
  124. }
  125. func upStateGroup(c *bm.Context) {
  126. var err error
  127. v := new(struct {
  128. ID int64 `form:"id" validate:"required"`
  129. State int8 `form:"state"`
  130. })
  131. if err = c.Bind(v); err != nil {
  132. log.Error("upStateGroup c.Bind v(%+v) error(%v)", v, err)
  133. return
  134. }
  135. if v.ID == 0 {
  136. c.JSON(nil, ecode.RequestErr)
  137. return
  138. }
  139. uid, uname := getUIDName(c)
  140. svc.SendTaskLog(c, &task.LogParam{UID: uid, UName: uname, Action: "更新任务组状态", OID: v.ID, Content: v})
  141. c.JSON(nil, svc.UpStateGroup(v.ID, v.State))
  142. }
  143. func viewSubtask(c *bm.Context) {
  144. var err error
  145. v := new(struct {
  146. ID int64 `form:"id"`
  147. })
  148. if err = c.Bind(v); err != nil {
  149. return
  150. }
  151. if v.ID == 0 {
  152. c.JSON(nil, ecode.RequestErr)
  153. return
  154. }
  155. res, err := svc.Task(v.ID)
  156. if err != nil {
  157. c.JSON(nil, err)
  158. return
  159. }
  160. c.JSON(res, nil)
  161. }
  162. func addSubtask(c *bm.Context) {
  163. var err error
  164. v := new(task.Task)
  165. if err = c.Bind(v); err != nil {
  166. log.Error("addSubtask c.Bind v(%+v) error(%v)", v, err)
  167. return
  168. }
  169. var rewardsIDs []int64
  170. if v.RewardIDs != "" {
  171. if rewardsIDs, err = xstr.SplitInts(v.RewardIDs); err != nil {
  172. log.Error("addSubtask xstr.SplitInts task v.RewardIDs(%s) rewardsIDs(%+v)|error(%v)", v.RewardIDs, rewardsIDs, err)
  173. c.JSON(nil, ecode.RequestErr)
  174. return
  175. }
  176. }
  177. id, err := svc.AddSubtask(v, rewardsIDs)
  178. if err != nil {
  179. c.JSON(nil, err)
  180. return
  181. }
  182. uid, uname := getUIDName(c)
  183. svc.SendTaskLog(c, &task.LogParam{UID: uid, UName: uname, Action: "添加子任务", OID: id, Content: v})
  184. c.JSON(map[string]interface{}{
  185. "id": id,
  186. }, nil)
  187. }
  188. func editSubtask(c *bm.Context) {
  189. var err error
  190. v := new(task.Task)
  191. if err = c.Bind(v); err != nil {
  192. log.Error("editSubtask c.Bind v(%+v) error(%v)", v, err)
  193. return
  194. }
  195. if v.ID == 0 {
  196. log.Error("editSubtask v.ID(%d)", v.ID)
  197. c.JSON(nil, ecode.RequestErr)
  198. return
  199. }
  200. var rewardsIDs []int64
  201. if v.RewardIDs != "" {
  202. if rewardsIDs, err = xstr.SplitInts(v.RewardIDs); err != nil {
  203. log.Error("addSubtask xstr.SplitInts task v.RewardIDs(%s) rewardsIDs(%+v)|error(%v)", v.RewardIDs, rewardsIDs, err)
  204. c.JSON(nil, ecode.RequestErr)
  205. return
  206. }
  207. }
  208. id, err := svc.EditSubtask(v, rewardsIDs)
  209. if err != nil {
  210. c.JSON(nil, err)
  211. return
  212. }
  213. uid, uname := getUIDName(c)
  214. svc.SendTaskLog(c, &task.LogParam{UID: uid, UName: uname, Action: "编辑子任务", OID: id, Content: v})
  215. c.JSON(map[string]interface{}{
  216. "id": id,
  217. }, nil)
  218. }
  219. func orderSubtask(c *bm.Context) {
  220. var err error
  221. v := new(task.OrderTask)
  222. if err = c.Bind(v); err != nil {
  223. log.Error("orderSubtask c.Bind v(%+v) error(%v)", v, err)
  224. return
  225. }
  226. uid, uname := getUIDName(c)
  227. svc.SendTaskLog(c, &task.LogParam{UID: uid, UName: uname, Action: "移动子任务顺序", OID: v.ID, Content: v})
  228. c.JSON(nil, svc.OrderSubTask(v))
  229. }
  230. func upStateSubtask(c *bm.Context) {
  231. var err error
  232. v := new(struct {
  233. ID int64 `form:"id" validate:"required"`
  234. State int8 `form:"state"`
  235. })
  236. if err = c.Bind(v); err != nil {
  237. log.Error("upStateSubtask c.Bind v(%+v) error(%v)", v, err)
  238. return
  239. }
  240. if v.ID == 0 {
  241. c.JSON(nil, ecode.RequestErr)
  242. return
  243. }
  244. uid, uname := getUIDName(c)
  245. svc.SendTaskLog(c, &task.LogParam{UID: uid, UName: uname, Action: "更新子任务状态", OID: v.ID, Content: v})
  246. c.JSON(nil, svc.UpStateSubTask(v.ID, v.State))
  247. }
  248. func transferSubtask(c *bm.Context) {
  249. var err error
  250. v := new(struct {
  251. ID int64 `form:"id" validate:"required"`
  252. GroupID int64 `form:"group_id" validate:"required"`
  253. })
  254. if err = c.Bind(v); err != nil {
  255. log.Error("transferSubtask c.Bind v(%+v) error(%v)", v, err)
  256. return
  257. }
  258. if v.ID == 0 || v.GroupID == 0 {
  259. c.JSON(nil, ecode.RequestErr)
  260. return
  261. }
  262. uid, uname := getUIDName(c)
  263. svc.SendTaskLog(c, &task.LogParam{UID: uid, UName: uname, Action: "转移子任务到别的组", OID: v.ID, Content: v})
  264. c.JSON(nil, svc.TransferSubtask(v.ID, v.GroupID))
  265. }
  266. func rewardTree(c *bm.Context) {
  267. c.JSON(svc.RewardTree(), nil)
  268. }
  269. func viewReward(c *bm.Context) {
  270. var err error
  271. v := new(struct {
  272. ID int64 `form:"id"`
  273. })
  274. if err = c.Bind(v); err != nil {
  275. return
  276. }
  277. if v.ID == 0 {
  278. c.JSON(nil, ecode.RequestErr)
  279. return
  280. }
  281. rd, err := svc.ViewReward(v.ID)
  282. if err != nil {
  283. c.JSON(nil, err)
  284. return
  285. }
  286. c.JSON(rd, nil)
  287. }
  288. func addReward(c *bm.Context) {
  289. var err error
  290. v := new(task.Reward)
  291. if err = c.Bind(v); err != nil {
  292. log.Error("addReward c.Bind v(%+v) error(%v)", v, err)
  293. return
  294. }
  295. id, err := svc.AddReward(v)
  296. if err != nil {
  297. c.JSON(nil, err)
  298. return
  299. }
  300. uid, uname := getUIDName(c)
  301. svc.SendTaskLog(c, &task.LogParam{UID: uid, UName: uname, Action: "添加奖励", OID: id, Content: v})
  302. c.JSON(map[string]interface{}{
  303. "id": id,
  304. }, nil)
  305. }
  306. func editReward(c *bm.Context) {
  307. var err error
  308. v := new(task.Reward)
  309. if err = c.Bind(v); err != nil {
  310. log.Error("editReward c.Bind v(%+v) error(%v)", v, err)
  311. return
  312. }
  313. if v.ID == 0 {
  314. c.JSON(nil, ecode.RequestErr)
  315. return
  316. }
  317. id, err := svc.EditReward(v)
  318. if err != nil {
  319. c.JSON(nil, err)
  320. return
  321. }
  322. uid, uname := getUIDName(c)
  323. svc.SendTaskLog(c, &task.LogParam{UID: uid, UName: uname, Action: "编辑奖励", OID: id, Content: v})
  324. c.JSON(map[string]interface{}{
  325. "id": id,
  326. }, nil)
  327. }
  328. func upStateReward(c *bm.Context) {
  329. var err error
  330. v := new(struct {
  331. ID int64 `form:"id" validate:"required"`
  332. State int8 `form:"state"`
  333. })
  334. if err = c.Bind(v); err != nil {
  335. log.Error("upStateReward c.Bind v(%+v) error(%v)", v, err)
  336. return
  337. }
  338. if v.ID == 0 {
  339. c.JSON(nil, ecode.RequestErr)
  340. return
  341. }
  342. uid, uname := getUIDName(c)
  343. svc.SendTaskLog(c, &task.LogParam{UID: uid, UName: uname, Action: "更新奖励状态", OID: v.ID, Content: v})
  344. c.JSON(nil, svc.UpStateReward(v.ID, v.State))
  345. }
  346. func listGiftReward(c *bm.Context) {
  347. res, err := svc.ListGiftReward()
  348. if err != nil {
  349. c.JSON(nil, err)
  350. return
  351. }
  352. c.JSON(res, nil)
  353. }
  354. func viewGiftReward(c *bm.Context) {
  355. var err error
  356. v := new(task.GiftReward)
  357. if err = c.Bind(v); err != nil {
  358. return
  359. }
  360. if v.RootType == 0 || v.TaskType == 0 {
  361. c.JSON(nil, ecode.RequestErr)
  362. return
  363. }
  364. res, err := svc.ViewGiftReward(v)
  365. if err != nil {
  366. c.JSON(nil, err)
  367. return
  368. }
  369. c.JSON(res, nil)
  370. }
  371. func addGiftReward(c *bm.Context) {
  372. var err error
  373. v := new(task.GiftReward)
  374. if err = c.Bind(v); err != nil {
  375. log.Error("addGiftReward c.Bind v(%+v) error(%v)", v, err)
  376. return
  377. }
  378. var rewardsIDs []int64
  379. if v.RewardIDs == "" {
  380. c.JSON(nil, ecode.RequestErr)
  381. return
  382. }
  383. if rewardsIDs, err = xstr.SplitInts(v.RewardIDs); err != nil || len(rewardsIDs) == 0 { //注意礼包配置,奖励必传
  384. log.Error("addGiftReward xstr.SplitInts task v.RewardIDs(%s) rewardsIDs(%+v)|error(%v)", v.RewardIDs, rewardsIDs, err)
  385. c.JSON(nil, ecode.RequestErr)
  386. return
  387. }
  388. rows, err := svc.AddGiftReward(v, rewardsIDs)
  389. if err != nil {
  390. c.JSON(nil, err)
  391. return
  392. }
  393. uid, uname := getUIDName(c)
  394. svc.SendTaskLog(c, &task.LogParam{UID: uid, UName: uname, Action: "添加礼包", OID: 0, Content: v})
  395. c.JSON(map[string]interface{}{
  396. "rows": rows,
  397. }, nil)
  398. }
  399. func editGiftReward(c *bm.Context) {
  400. var err error
  401. v := new(task.GiftReward)
  402. if err = c.Bind(v); err != nil {
  403. log.Error("editGiftReward c.Bind v(%+v) error(%v)", v, err)
  404. return
  405. }
  406. if v.RootType == 0 || v.TaskType == 0 {
  407. c.JSON(nil, ecode.RequestErr)
  408. return
  409. }
  410. var rewardsIDs []int64
  411. if v.RewardIDs == "" {
  412. c.JSON(nil, ecode.RequestErr)
  413. return
  414. }
  415. if rewardsIDs, err = xstr.SplitInts(v.RewardIDs); err != nil || len(rewardsIDs) == 0 { //注意礼包配置,奖励必传
  416. log.Error("editGiftReward xstr.SplitInts task v.RewardIDs(%s) rewardsIDs(%+v)|error(%v)", v.RewardIDs, rewardsIDs, err)
  417. c.JSON(nil, ecode.RequestErr)
  418. return
  419. }
  420. rows, err := svc.EditGiftReward(v, rewardsIDs)
  421. if err != nil {
  422. c.JSON(nil, err)
  423. return
  424. }
  425. uid, uname := getUIDName(c)
  426. svc.SendTaskLog(c, &task.LogParam{UID: uid, UName: uname, Action: "编辑礼包", OID: 0, Content: v})
  427. c.JSON(map[string]interface{}{
  428. "rows": rows,
  429. }, nil)
  430. }
  431. func upStateGiftReward(c *bm.Context) {
  432. var err error
  433. v := new(task.GiftReward)
  434. if err = c.Bind(v); err != nil {
  435. log.Error("upStateGiftReward c.Bind v(%+v) error(%v)", v, err)
  436. return
  437. }
  438. if v.RootType == 0 || v.TaskType == 0 {
  439. c.JSON(nil, ecode.RequestErr)
  440. return
  441. }
  442. rows, err := svc.UpGiftReward(v)
  443. if err != nil {
  444. c.JSON(nil, err)
  445. return
  446. }
  447. uid, uname := getUIDName(c)
  448. svc.SendTaskLog(c, &task.LogParam{UID: uid, UName: uname, Action: "更新礼包状态", OID: 0, Content: v})
  449. c.JSON(map[string]interface{}{
  450. "rows": rows,
  451. }, nil)
  452. }
  453. func batchOnline(c *bm.Context) {
  454. var err error
  455. bs, err := ioutil.ReadAll(c.Request.Body)
  456. if err != nil {
  457. log.Error("batchOnline ioutil.ReadAll error(%v)", err)
  458. c.JSON(nil, ecode.RequestErr)
  459. return
  460. }
  461. c.Request.Body.Close()
  462. var v []*task.TaskGroup
  463. err = json.Unmarshal(bs, &v)
  464. if err != nil {
  465. log.Error("batchOnline json.Unmarshal v(%+v) error(%v)", v, err)
  466. c.JSON(nil, ecode.RequestErr)
  467. return
  468. }
  469. err = svc.BatchOnline(v)
  470. if err != nil {
  471. c.JSON(nil, err)
  472. return
  473. }
  474. uid, uname := getUIDName(c)
  475. svc.SendTaskLog(c, &task.LogParam{UID: uid, UName: uname, Action: "批量刷新上线", OID: 0, Content: v})
  476. c.JSON(nil, nil)
  477. }