assist.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. package http
  2. //assist 创作中心协管相关
  3. import (
  4. "go-common/app/service/main/assist/model/assist"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/http/blademaster/render"
  9. "go-common/library/xstr"
  10. "net/http"
  11. "strconv"
  12. "time"
  13. )
  14. func assists(c *bm.Context) {
  15. midStr := c.Request.Form.Get("mid")
  16. mid, err := strconv.ParseInt(midStr, 10, 64)
  17. if err != nil {
  18. log.Error("strconv.ParseInt(%s) error(%v)", midStr, err)
  19. c.JSON(nil, ecode.RequestErr)
  20. return
  21. }
  22. assists, err := assSvc.Assists(c, mid)
  23. if err != nil {
  24. log.Error("assistSvc.Assists(%v) error(%v)", assists, mid)
  25. return
  26. }
  27. c.JSON(assists, nil)
  28. }
  29. func assistsMids(c *bm.Context) {
  30. params := c.Request.Form
  31. midStr := params.Get("mid")
  32. mid, err := strconv.ParseInt(midStr, 10, 64)
  33. if err != nil {
  34. log.Error("strconv.ParseInt(%s) error(%v)", midStr, err)
  35. c.JSON(nil, ecode.RequestErr)
  36. return
  37. }
  38. assmidsStr := params.Get("assmids")
  39. assmids, err := xstr.SplitInts(assmidsStr)
  40. if err != nil {
  41. log.Error("strconv.ParseInt(%s) error(%v)", assmidsStr, err)
  42. c.JSON(nil, ecode.RequestErr)
  43. return
  44. }
  45. if len(assmids) > 20 {
  46. log.Error("assmids(%d) number gt 20", len(assmids))
  47. c.JSON(nil, ecode.RequestErr)
  48. return
  49. }
  50. asByMids, err := assSvc.AssistsMidsTotal(c, mid, assmids)
  51. if err != nil {
  52. log.Error("assistSvc.AssistsMidsTotal(%v), mids(%v), assmids(%v), error(%v)", asByMids, mid, assmids, err)
  53. c.JSON(nil, err)
  54. return
  55. }
  56. c.JSON(asByMids, nil)
  57. }
  58. func assistInfo(c *bm.Context) {
  59. params := c.Request.Form
  60. midStr := params.Get("mid")
  61. mid, err := strconv.ParseInt(midStr, 10, 64)
  62. if err != nil {
  63. log.Error("strconv.ParseInt(%s) error(%v)", midStr, err)
  64. c.JSON(nil, ecode.RequestErr)
  65. return
  66. }
  67. assistMidStr := params.Get("assist_mid")
  68. assistMid, err := strconv.ParseInt(assistMidStr, 10, 64)
  69. if err != nil {
  70. log.Error("strconv.ParseInt(%s) error(%v)", assistMidStr, err)
  71. c.JSON(nil, ecode.RequestErr)
  72. return
  73. }
  74. typeStr := params.Get("type")
  75. tp, err := strconv.ParseInt(typeStr, 10, 64)
  76. if err != nil {
  77. log.Error("strconv.ParseInt(%s) error(%v)", typeStr, err)
  78. c.JSON(nil, ecode.RequestErr)
  79. return
  80. }
  81. assist, err := assSvc.Assist(c, mid, assistMid, tp)
  82. if err != nil {
  83. c.JSON(nil, err)
  84. log.Error("assSvc.Assist(%s) error(%v)|mid(%d)|assistMid(%d)", assist, err, mid, assistMid)
  85. return
  86. }
  87. c.JSON(assist, nil)
  88. }
  89. func assistIDs(c *bm.Context) {
  90. params := c.Request.Form
  91. midStr := params.Get("mid")
  92. mid, err := strconv.ParseInt(midStr, 10, 64)
  93. if err != nil {
  94. log.Error("strconv.ParseInt(%s) error(%v)", midStr, err)
  95. c.JSON(nil, ecode.RequestErr)
  96. return
  97. }
  98. ids, err := assSvc.AssistIDs(c, mid)
  99. if err != nil {
  100. c.JSON(nil, err)
  101. return
  102. }
  103. c.JSON(ids, nil)
  104. }
  105. func assistAdd(c *bm.Context) {
  106. params := c.Request.Form
  107. midStr := params.Get("mid")
  108. // mid
  109. mid, err := strconv.ParseInt(midStr, 10, 64)
  110. if err != nil {
  111. log.Error("strconv.ParseInt(%s) error(%v)", midStr, err)
  112. c.JSON(nil, ecode.RequestErr)
  113. return
  114. }
  115. assistMidStr := params.Get("assist_mid")
  116. assistMid, err := strconv.ParseInt(assistMidStr, 10, 64)
  117. if err != nil || assistMid == 0 {
  118. log.Error("strconv.ParseInt(%s) error(%v)", assistMidStr, err)
  119. c.JSON(nil, ecode.RequestErr)
  120. return
  121. }
  122. if err = assSvc.AddAssist(c, mid, assistMid); err != nil {
  123. c.JSON(nil, err)
  124. return
  125. }
  126. c.JSON(map[string]interface{}{
  127. "mid": mid,
  128. "assist_mid": assistMid,
  129. }, nil)
  130. }
  131. func assistDel(c *bm.Context) {
  132. params := c.Request.Form
  133. midStr := params.Get("mid")
  134. mid, err := strconv.ParseInt(midStr, 10, 64)
  135. if err != nil {
  136. log.Error("strconv.ParseInt(%s) error(%v)", midStr, err)
  137. c.JSON(nil, ecode.RequestErr)
  138. return
  139. }
  140. assistMidStr := params.Get("assist_mid")
  141. assistMid, err := strconv.ParseInt(assistMidStr, 10, 64)
  142. if err != nil {
  143. log.Error("strconv.ParseInt(%s) error(%v)", assistMidStr, err)
  144. c.JSON(nil, ecode.RequestErr)
  145. return
  146. }
  147. if err := assSvc.DelAssist(c, mid, assistMid); err != nil {
  148. c.JSON(nil, err)
  149. return
  150. }
  151. c.JSON(map[string]interface{}{
  152. "mid": mid,
  153. "assist_mid": assistMid,
  154. }, nil)
  155. }
  156. // exist to be assist from follower
  157. func assistExit(c *bm.Context) {
  158. params := c.Request.Form
  159. midStr := params.Get("mid")
  160. mid, err := strconv.ParseInt(midStr, 10, 64)
  161. if err != nil {
  162. log.Error("strconv.ParseInt(%s) error(%v)", midStr, err)
  163. c.JSON(nil, ecode.RequestErr)
  164. return
  165. }
  166. assistMidStr := params.Get("assist_mid")
  167. assistMid, err := strconv.ParseInt(assistMidStr, 10, 64)
  168. if err != nil {
  169. log.Error("strconv.ParseInt(%s) error(%v)", assistMidStr, err)
  170. c.JSON(nil, ecode.RequestErr)
  171. return
  172. }
  173. if err := assSvc.Exit(c, mid, assistMid); err != nil {
  174. c.JSON(nil, err)
  175. return
  176. }
  177. c.JSON(map[string]interface{}{
  178. "mid": mid,
  179. "assist_mid": assistMid,
  180. }, nil)
  181. }
  182. func assistLogs(c *bm.Context) {
  183. params := c.Request.Form
  184. midStr := params.Get("mid")
  185. var (
  186. err error
  187. mid, assistMid, ps, pn, total, bgnCtime, endCtime int64
  188. assistLogs []*assist.Log
  189. )
  190. mid, err = strconv.ParseInt(midStr, 10, 64)
  191. if err != nil {
  192. log.Error("strconv.ParseInt(%s) error(%v)", midStr, err)
  193. c.JSON(nil, ecode.RequestErr)
  194. return
  195. }
  196. assistMidStr := params.Get("assist_mid")
  197. assistMid, _ = strconv.ParseInt(assistMidStr, 10, 64)
  198. pnStr := params.Get("pn")
  199. psStr := params.Get("ps")
  200. ps, err = strconv.ParseInt(psStr, 10, 64)
  201. if err != nil || ps <= 10 {
  202. ps = 10
  203. }
  204. pn, err = strconv.ParseInt(pnStr, 10, 64)
  205. if err != nil || pn < 1 {
  206. pn = 1
  207. }
  208. bgnCtimeStr := params.Get("stime")
  209. bgnCtime, err = strconv.ParseInt(bgnCtimeStr, 10, 64)
  210. if err != nil || bgnCtime <= 0 {
  211. bgnCtime = time.Now().Add(-time.Hour * 72).Unix()
  212. }
  213. endCtimeStr := params.Get("etime")
  214. endCtime, err = strconv.ParseInt(endCtimeStr, 10, 64)
  215. if err != nil || endCtime <= 0 {
  216. endCtime = time.Now().Unix()
  217. }
  218. formatedBgnCtime := time.Unix(bgnCtime, 0)
  219. formatedEndCtime := time.Unix(endCtime, 0)
  220. assistLogs, err = assSvc.Logs(c, mid, assistMid, formatedBgnCtime, formatedEndCtime, int((pn-1)*ps), int(ps))
  221. if err != nil {
  222. log.Error("assistSvc.AssistLogs(%v) error(%v)", assistLogs, err)
  223. return
  224. }
  225. total, err = assSvc.LogCnt(c, mid, assistMid, formatedBgnCtime, formatedEndCtime)
  226. if err != nil {
  227. log.Error("assSvc.LogCnt: mid (%d),assistMid (%d),bgnctime (%v),endctime (%v):error(%v)", mid, assistMid, formatedBgnCtime, formatedEndCtime, err)
  228. return
  229. }
  230. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  231. "code": 0,
  232. "message": "0",
  233. "data": assistLogs,
  234. "pager": map[string]int64{
  235. "current": pn,
  236. "size": ps,
  237. "total": total,
  238. },
  239. }))
  240. }
  241. func assistLogAdd(c *bm.Context) {
  242. params := c.Request.Form
  243. midStr := params.Get("mid")
  244. mid, err := strconv.ParseInt(midStr, 10, 64)
  245. if err != nil {
  246. log.Error("strconv.ParseInt(%s) error(%v)", midStr, err)
  247. c.JSON(nil, ecode.RequestErr)
  248. return
  249. }
  250. assistMidStr := params.Get("assist_mid")
  251. assistMid, err := strconv.ParseInt(assistMidStr, 10, 64)
  252. if err != nil {
  253. log.Error("strconv.ParseInt(%s) error(%v)", assistMidStr, err)
  254. c.JSON(nil, ecode.RequestErr)
  255. return
  256. }
  257. tpStr := params.Get("type")
  258. tp, err := strconv.ParseInt(tpStr, 10, 64)
  259. if err != nil {
  260. log.Error("strconv.ParseInt(%s) error(%v)", tpStr, err)
  261. c.JSON(nil, ecode.RequestErr)
  262. return
  263. }
  264. actStr := params.Get("action")
  265. act, err := strconv.ParseInt(actStr, 10, 64)
  266. if err != nil {
  267. log.Error("strconv.ParseInt(%s) error(%v)", act, err)
  268. c.JSON(nil, ecode.RequestErr)
  269. return
  270. }
  271. subIDStr := params.Get("subject_id")
  272. subID, err := strconv.ParseInt(subIDStr, 10, 64)
  273. if err != nil || subID <= 0 {
  274. log.Error("strconv.ParseInt(%s) error(%v)", subIDStr, err)
  275. c.JSON(nil, ecode.RequestErr)
  276. return
  277. }
  278. objIDStr := params.Get("object_id")
  279. if len(objIDStr) == 0 {
  280. log.Error("objIDStr length eq zero(%s)", objIDStr)
  281. c.JSON(nil, ecode.RequestErr)
  282. return
  283. }
  284. detail := params.Get("detail")
  285. if len(detail) == 0 {
  286. log.Error("detail len is zero (%s) error(%v)", detail, err)
  287. c.JSON(nil, ecode.RequestErr)
  288. return
  289. }
  290. if err = assSvc.AddLog(c, mid, assistMid, tp, act, subID, objIDStr, detail); err != nil {
  291. c.JSON(nil, err)
  292. return
  293. }
  294. c.JSON(map[string]interface{}{
  295. "mid": mid,
  296. "assist_mid": assistMid,
  297. "type": tp,
  298. "action": act,
  299. "subject_id": subID,
  300. "object_id": objIDStr,
  301. }, nil)
  302. }
  303. func assistLogCancel(c *bm.Context) {
  304. params := c.Request.Form
  305. midStr := params.Get("mid")
  306. mid, err := strconv.ParseInt(midStr, 10, 64)
  307. if err != nil {
  308. log.Error("strconv.ParseInt(%s) error(%v)", midStr, err)
  309. c.JSON(nil, ecode.RequestErr)
  310. return
  311. }
  312. logIDStr := params.Get("log_id")
  313. logID, err := strconv.ParseInt(logIDStr, 10, 64)
  314. if err != nil || logID <= 0 {
  315. log.Error("strconv.ParseInt(%s) error(%v)", logIDStr, err)
  316. c.JSON(nil, ecode.RequestErr)
  317. return
  318. }
  319. assistMidStr := params.Get("assist_mid")
  320. assistMid, _ := strconv.ParseInt(assistMidStr, 10, 64)
  321. if assistMid < 1 {
  322. log.Error("strconv.ParseInt(%s) error(%v)", assistMid, err)
  323. c.JSON(nil, ecode.RequestErr)
  324. return
  325. }
  326. if err := assSvc.CancelLog(c, mid, assistMid, logID); err != nil {
  327. c.JSON(nil, err)
  328. return
  329. }
  330. c.JSON(map[string]interface{}{
  331. "mid": mid,
  332. "assist_mid": assistMid,
  333. "log_id": logID,
  334. }, nil)
  335. }
  336. func assistLogInfo(c *bm.Context) {
  337. params := c.Request.Form
  338. logIDStr := params.Get("log_id")
  339. logID, err := strconv.ParseInt(logIDStr, 10, 64)
  340. if err != nil || logID <= 0 {
  341. log.Error("strconv.ParseInt(%s) error(%v)", logIDStr, err)
  342. c.JSON(nil, ecode.RequestErr)
  343. return
  344. }
  345. midStr := params.Get("mid")
  346. mid, err := strconv.ParseInt(midStr, 10, 64)
  347. if err != nil {
  348. log.Error("strconv.ParseInt(%s) error(%v)", midStr, err)
  349. c.JSON(nil, ecode.RequestErr)
  350. return
  351. }
  352. assistMidStr := params.Get("assist_mid")
  353. assistMid, _ := strconv.ParseInt(assistMidStr, 10, 64)
  354. if assistMid < 1 {
  355. log.Error("strconv.ParseInt(%s) error(%v)", assistMid, err)
  356. c.JSON(nil, ecode.RequestErr)
  357. return
  358. }
  359. logInfo, err := assSvc.LogInfo(c, logID, mid, assistMid)
  360. if err != nil {
  361. c.JSON(nil, err)
  362. log.Error("assSvc.Assist(%s) error(%v)|logId(%d)|mid(%d)|assistMid(%d)", logInfo, err, logID, mid, assistMid)
  363. return
  364. }
  365. c.JSON(logInfo, nil)
  366. }
  367. func assistUps(c *bm.Context) {
  368. params := c.Request.Form
  369. assistMidStr := params.Get("assist_mid")
  370. assistMid, err := strconv.ParseInt(assistMidStr, 10, 64)
  371. if err != nil {
  372. log.Error("strconv.ParseInt(%s) error(%v)", assistMidStr, err)
  373. c.JSON(nil, ecode.RequestErr)
  374. return
  375. }
  376. pnStr := params.Get("pn")
  377. psStr := params.Get("ps")
  378. pn, err := strconv.ParseInt(pnStr, 10, 64)
  379. if err != nil || pn < 1 {
  380. pn = 1
  381. }
  382. ps, err := strconv.ParseInt(psStr, 10, 64)
  383. if err != nil || ps <= 20 {
  384. ps = 20
  385. }
  386. assistUpsPager, err := assSvc.AssistUps(c, assistMid, pn, ps)
  387. if err != nil {
  388. c.JSON(nil, err)
  389. return
  390. }
  391. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  392. "code": 0,
  393. "message": "",
  394. "data": assistUpsPager.Data,
  395. "pager": assistUpsPager.Pager,
  396. }))
  397. }
  398. func assistLogObj(c *bm.Context) {
  399. params := c.Request.Form
  400. objIDStr := params.Get("object_id")
  401. objID, err := strconv.ParseInt(objIDStr, 10, 64)
  402. if err != nil || objID <= 0 {
  403. log.Error("strconv.ParseInt(%s) error(%v)", objIDStr, err)
  404. c.JSON(nil, ecode.RequestErr)
  405. return
  406. }
  407. midStr := params.Get("mid")
  408. mid, err := strconv.ParseInt(midStr, 10, 64)
  409. if err != nil || mid <= 0 {
  410. log.Error("strconv.ParseInt(%s) error(%v)", midStr, err)
  411. c.JSON(nil, ecode.RequestErr)
  412. return
  413. }
  414. tpStr := params.Get("type")
  415. tp, err := strconv.ParseInt(tpStr, 10, 64)
  416. if err != nil {
  417. log.Error("strconv.ParseInt(%s) error(%v)", tpStr, err)
  418. c.JSON(nil, ecode.RequestErr)
  419. return
  420. }
  421. actStr := params.Get("action")
  422. act, _ := strconv.ParseInt(actStr, 10, 64)
  423. if act < 1 {
  424. log.Error("strconv.ParseInt(%s) error(%v)", act, err)
  425. c.JSON(nil, ecode.RequestErr)
  426. return
  427. }
  428. logInfo, err := assSvc.LogObj(c, mid, objID, tp, act)
  429. if err != nil {
  430. c.JSON(nil, err)
  431. log.Error("assSvc.LogObj(%s) error(%v)|mid(%d)|logId(%d)|tp(%d)|act(%d)", logInfo, err, mid, objID, tp, act)
  432. return
  433. }
  434. c.JSON(logInfo, nil)
  435. }