up.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. package http
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/csv"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "go-common/app/service/main/up/model"
  11. "go-common/app/service/main/up/service"
  12. "go-common/library/ecode"
  13. "go-common/library/log"
  14. bm "go-common/library/net/http/blademaster"
  15. "go-common/library/net/metadata"
  16. "github.com/siddontang/go-mysql/mysql"
  17. )
  18. func register(ctx *bm.Context) {
  19. param := ctx.Request.Form
  20. midStr := param.Get("mid")
  21. fromStr := param.Get("from")
  22. isAuthorStr := param.Get("is_author")
  23. // check params
  24. mid, err := strconv.ParseInt(midStr, 10, 64)
  25. if err != nil {
  26. log.Error("strconv.ParseInt(%s) error(%v)", midStr, err)
  27. ctx.JSON(nil, ecode.RequestErr)
  28. return
  29. }
  30. if mid <= 0 {
  31. log.Error("register error mid (%d)", mid)
  32. ctx.JSON(nil, ecode.RequestErr)
  33. return
  34. }
  35. from, err := strconv.Atoi(fromStr)
  36. if err != nil {
  37. log.Error("strconv.ParseInt(%s) error(%v)", fromStr, err)
  38. ctx.JSON(nil, ecode.RequestErr)
  39. return
  40. }
  41. isAuthor, err := strconv.Atoi(isAuthorStr)
  42. if err != nil {
  43. log.Error("strconv.ParseInt(%s) error(%v)", isAuthorStr, err)
  44. ctx.JSON(nil, ecode.RequestErr)
  45. return
  46. }
  47. result := 0
  48. row, err := Svc.Edit(ctx, mid, isAuthor, uint8(from))
  49. if err != nil {
  50. ctx.JSON(nil, err)
  51. return
  52. }
  53. if row > 0 {
  54. result = 1
  55. }
  56. ctx.JSON(map[string]interface{}{
  57. "result": result,
  58. }, nil)
  59. }
  60. func info(ctx *bm.Context) {
  61. param := ctx.Request.Form
  62. midStr := param.Get("mid")
  63. fromStr := param.Get("from")
  64. // check params
  65. mid, err := strconv.ParseInt(midStr, 10, 64)
  66. if err != nil {
  67. log.Error("strconv.ParseInt(%s) error(%v)", midStr, err)
  68. ctx.JSON(nil, ecode.RequestErr)
  69. return
  70. }
  71. if mid <= 0 {
  72. log.Error("info error mid (%d)", mid)
  73. ctx.JSON(nil, ecode.RequestErr)
  74. return
  75. }
  76. from, err := strconv.Atoi(fromStr)
  77. if err != nil {
  78. log.Error("strconv.ParseInt(%s) error(%v)", fromStr, err)
  79. ctx.JSON(nil, ecode.RequestErr)
  80. return
  81. }
  82. isAuthor, err := Svc.Info(ctx, mid, uint8(from))
  83. if err != nil {
  84. ctx.JSON(nil, err)
  85. return
  86. }
  87. ctx.JSON(map[string]interface{}{
  88. "is_author": isAuthor,
  89. }, nil)
  90. }
  91. func all(ctx *bm.Context) {
  92. param := ctx.Request.Form
  93. midStr := param.Get("mid")
  94. ip := metadata.String(ctx, metadata.RemoteIP)
  95. // check params
  96. mid, err := strconv.ParseInt(midStr, 10, 64)
  97. if err != nil {
  98. log.Error("strconv.ParseInt(%s) error(%v)", midStr, err)
  99. ctx.JSON(nil, ecode.RequestErr)
  100. return
  101. }
  102. if mid <= 0 {
  103. log.Error("up all error mid (%d)", mid)
  104. ctx.JSON(nil, ecode.RequestErr)
  105. return
  106. }
  107. identifyAll, err := Svc.IdentifyAll(ctx, mid, ip)
  108. if err != nil {
  109. ctx.JSON(nil, err)
  110. return
  111. }
  112. ctx.JSON(map[string]interface{}{
  113. "identify": identifyAll,
  114. }, nil)
  115. }
  116. // flows get specialUps list
  117. func specialUps(c *bm.Context) {
  118. var res interface{}
  119. params := c.Request.Form
  120. groupStr := params.Get("group_id")
  121. var err error
  122. // check params
  123. //default all groups
  124. groupID := int64(0)
  125. if groupStr != "" {
  126. groupID, err = strconv.ParseInt(groupStr, 10, 64)
  127. if err != nil {
  128. log.Error("strconv.ParseInt(groupStr(%s)) error(%v)", groupStr, err)
  129. err = ecode.RequestErr
  130. c.JSON(res, err)
  131. return
  132. }
  133. }
  134. res = Svc.UpsByGroup(c, groupID)
  135. c.JSON(res, err)
  136. }
  137. func specialDel(c *bm.Context) {
  138. var res interface{}
  139. var err error
  140. var r = new(struct {
  141. Ids string `form:"ids" validate:"required"`
  142. })
  143. if err = c.Bind(r); err != nil {
  144. log.Error("params error, err=%v", err)
  145. err = ecode.RequestErr
  146. c.JSON(res, err)
  147. return
  148. }
  149. var idstr = strings.Split(r.Ids, ",")
  150. for _, s := range idstr {
  151. var affectedRow int64
  152. var id, e = strconv.ParseInt(s, 10, 64)
  153. err = e
  154. if err != nil {
  155. log.Error("id is not integer, id=%s", s)
  156. continue
  157. }
  158. affectedRow, err = Svc.SpecialDel(c, id)
  159. log.Info("delete special up, id=%d, affected=%d, err=%v", id, affectedRow, err)
  160. }
  161. c.JSON(res, err)
  162. }
  163. func specialAdd(c *bm.Context) {
  164. var res interface{}
  165. var err error
  166. var errMsg string
  167. var r = new(struct {
  168. GroupIds string `form:"group_ids" validate:"required"` // 支持多个group id,用,分隔
  169. MidStr string `form:"mids" validate:"required"` // 支持多个mid,用,分隔
  170. Note string `form:"note" default:""`
  171. })
  172. switch {
  173. default:
  174. if err = c.Bind(r); err != nil {
  175. log.Error("params error, err=%v", err)
  176. err = ecode.RequestErr
  177. errMsg = "params error"
  178. break
  179. }
  180. var groupIds = strings.Split(r.GroupIds, ",")
  181. // 检查是否有特殊权限
  182. for _, groupIDStr := range groupIds {
  183. var groupID, _ = strconv.ParseInt(groupIDStr, 10, 64)
  184. var e = Svc.SpecialGroupPermit(c, groupID)
  185. if e != nil {
  186. err = e
  187. break
  188. }
  189. }
  190. var midStrArray = strings.Split(r.MidStr, ",")
  191. if len(midStrArray) == 0 {
  192. log.Error("params error, no mid got, mid=%s", r.MidStr)
  193. err = ecode.RequestErr
  194. errMsg = "params error, no mid got"
  195. break
  196. }
  197. var mids []int64
  198. for _, v := range midStrArray {
  199. mid, e := strconv.ParseInt(v, 10, 64)
  200. if e != nil {
  201. continue
  202. }
  203. mids = append(mids, int64(mid))
  204. }
  205. if len(mids) == 0 {
  206. log.Error("params error, wrong mid got, mid=%s", r.MidStr)
  207. err = ecode.RequestErr
  208. errMsg = "params error, wrong mid got"
  209. break
  210. }
  211. uidtemp, ok := c.Get("uid")
  212. var uid int64
  213. if ok {
  214. uid = uidtemp.(int64)
  215. }
  216. if len(groupIds) == 0 {
  217. log.Error("params error, no group id got, groupid=%s", r.GroupIds)
  218. err = ecode.RequestErr
  219. errMsg = "params error, wrong mid got"
  220. break
  221. }
  222. var uname, _ = bmGetStringOrDefault(c, "username", "unkown")
  223. for _, groupIDStr := range groupIds {
  224. var groupID, err2 = strconv.ParseInt(groupIDStr, 10, 64)
  225. if err2 != nil {
  226. log.Warn("group id convert fail, group id=%s", groupIDStr)
  227. continue
  228. }
  229. var data = &model.UpSpecial{
  230. GroupID: groupID,
  231. Note: r.Note,
  232. UID: uid,
  233. }
  234. go func() {
  235. const step = 100
  236. for start := 0; start < len(mids); start += step {
  237. var end = start + step
  238. if end > len(mids) {
  239. end = len(mids)
  240. }
  241. _, err = Svc.SpecialAdd(context.Background(), uname, data, mids[start:end]...)
  242. if err != nil {
  243. log.Error("add special fail, mid=%s, err=%v", r.MidStr, err)
  244. errMsg = "server error"
  245. return
  246. }
  247. log.Info("add special, id=%v, group id=%d", mids, groupID)
  248. }
  249. }()
  250. }
  251. }
  252. if err != nil {
  253. service.BmHTTPErrorWithMsg(c, err, errMsg)
  254. } else {
  255. c.JSON(res, err)
  256. }
  257. }
  258. func specialEdit(c *bm.Context) {
  259. var res interface{}
  260. var err error
  261. var errMsg string
  262. var r = new(struct {
  263. GroupID int64 `form:"group_id" validate:"required"`
  264. Mid int64 `form:"mid"`
  265. ID int64 `form:"id" validate:"required"`
  266. Note string `form:"note" default:""`
  267. })
  268. switch {
  269. default:
  270. if err = c.Bind(r); err != nil {
  271. log.Error("request argument bind fail, err=%v", err)
  272. errMsg = fmt.Sprintf("wrong argument, %s", err.Error())
  273. err = ecode.RequestErr
  274. break
  275. }
  276. // 检查是否有特殊权限
  277. err = Svc.SpecialGroupPermit(c, r.GroupID)
  278. if err != nil {
  279. break
  280. }
  281. uidtemp, ok := c.Get("uid")
  282. var uid int64
  283. if ok {
  284. uid = uidtemp.(int64)
  285. }
  286. var data = &model.UpSpecial{
  287. GroupID: r.GroupID,
  288. Note: r.Note,
  289. UID: uid,
  290. ID: r.ID,
  291. }
  292. _, err = Svc.SpecialEdit(c, data, r.ID)
  293. if err != nil {
  294. log.Error("fail edit ups, err=%v, info=%v", err, data)
  295. errMsg = err.Error()
  296. break
  297. }
  298. log.Info("edit ups successful, info=%v", data)
  299. }
  300. if err != nil {
  301. service.BmHTTPErrorWithMsg(c, err, errMsg)
  302. } else {
  303. c.JSON(res, err)
  304. }
  305. }
  306. // 支持更多的条件类型
  307. func specialGet(c *bm.Context) {
  308. var data interface{}
  309. var err error
  310. var errMsg string
  311. var r = new(model.GetSpecialArg)
  312. var ups []*model.UpSpecialWithName
  313. switch {
  314. default:
  315. if err = c.Bind(r); err != nil {
  316. log.Error("request argument bind fail, err=%v", err)
  317. errMsg = fmt.Sprintf("wrong argument, %s", err.Error())
  318. err = ecode.RequestErr
  319. break
  320. }
  321. if r.Pn == 0 {
  322. r.Pn = 1
  323. }
  324. if r.Ps == 0 {
  325. r.Ps = 20
  326. }
  327. if r.Ps > 100 {
  328. r.Ps = 100
  329. }
  330. if r.Ps < 5 {
  331. r.Ps = 5
  332. }
  333. uidtemp, ok := c.Get("uid")
  334. var uid int64
  335. if ok {
  336. uid, _ = uidtemp.(int64)
  337. }
  338. var total int
  339. ups, total, err = Svc.SpecialGet(c, r)
  340. if err != nil {
  341. log.Error("fail to get special, err=%v, arg=%+v", err, r)
  342. errMsg = err.Error()
  343. err = ecode.ServerErr
  344. break
  345. }
  346. log.Info("get special successful, arg=%+v, result count=%d", len(ups), uid)
  347. data = model.UpsPage{
  348. Items: ups,
  349. Pager: &model.Pager{Num: r.Pn, Size: r.Ps, Total: total},
  350. }
  351. }
  352. if err != nil {
  353. service.BmHTTPErrorWithMsg(c, err, errMsg)
  354. } else {
  355. if r.Export == "csv" {
  356. c.Writer.Header().Set("Content-Type", "application/csv")
  357. c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment;filename=\"up_special_%s.csv\"", time.Now().Format(mysql.TimeFormat)))
  358. var buf = &bytes.Buffer{}
  359. var csvWriter = csv.NewWriter(buf)
  360. csvWriter.Write((&model.UpSpecialWithName{}).GetTitleFields())
  361. for _, v := range ups {
  362. csvWriter.Write(v.ToStringFields())
  363. }
  364. csvWriter.Flush()
  365. c.Writer.Write(buf.Bytes())
  366. } else {
  367. c.JSON(data, err)
  368. }
  369. }
  370. }
  371. func specialGetByMid(c *bm.Context) {
  372. var data interface{}
  373. var err error
  374. var errMsg string
  375. var r = new(model.GetSpecialByMidArg)
  376. switch {
  377. default:
  378. if err = c.Bind(r); err != nil {
  379. log.Error("request argument bind fail, err=%v", err)
  380. errMsg = fmt.Sprintf("wrong argument, %s", err.Error())
  381. err = ecode.RequestErr
  382. break
  383. }
  384. uidtemp, ok := c.Get("uid")
  385. var uid int64
  386. if ok {
  387. uid = uidtemp.(int64)
  388. }
  389. var ups []*model.UpSpecial
  390. ups, err = Svc.SpecialGetByMid(c, r)
  391. if err != nil {
  392. log.Error("fail to get special, err=%v, arg=%+v", err, r)
  393. errMsg = err.Error()
  394. err = ecode.ServerErr
  395. break
  396. }
  397. log.Info("get special successful, arg=%+v, result count=%d", len(ups), uid)
  398. data = ups
  399. }
  400. if err != nil {
  401. service.BmHTTPErrorWithMsg(c, err, errMsg)
  402. } else {
  403. c.JSON(data, err)
  404. }
  405. }
  406. func listUp(c *bm.Context) {
  407. var (
  408. mids []int64
  409. newLastID int64
  410. err error
  411. errMsg string
  412. arg = new(model.ListUpBaseArg)
  413. )
  414. switch {
  415. default:
  416. if err = c.Bind(arg); err != nil {
  417. log.Error("request argument bind fail, err=%v", err)
  418. errMsg = err.Error()
  419. err = ecode.RequestErr
  420. break
  421. }
  422. if !arg.Validate() {
  423. errMsg, err = "illegal argument", ecode.RequestErr
  424. break
  425. }
  426. if arg.Size == 0 {
  427. arg.Size = 100
  428. }
  429. mids, newLastID, err = Svc.ListUpBase(c, arg.Size, arg.LastID, arg.Activity)
  430. if err != nil {
  431. log.Error("fail to get special, err=%v, arg=%v", err, arg)
  432. errMsg = err.Error()
  433. err = ecode.ServerErr
  434. break
  435. }
  436. }
  437. if err != nil {
  438. service.BmHTTPErrorWithMsg(c, err, errMsg)
  439. return
  440. }
  441. c.JSON(map[string]interface{}{
  442. "result": mids,
  443. "last_id": newLastID,
  444. }, nil)
  445. }
  446. func active(ctx *bm.Context) {
  447. var (
  448. err error
  449. errMsg string
  450. req = new(model.UpInfoActiveReq)
  451. )
  452. if err = ctx.Bind(req); err != nil {
  453. log.Error("request param error: req: %+v, err: %+v", req, err)
  454. return
  455. }
  456. if req.Mid <= 0 {
  457. log.Error("param error mid: %d", req.Mid)
  458. ctx.JSON(nil, ecode.RequestErr)
  459. return
  460. }
  461. log.Info("req: %+v", req)
  462. res, err := Svc.GetUpInfoActive(ctx, req)
  463. if err != nil {
  464. log.Error("Svc.GetUpInfoActive error: %+v", err)
  465. service.BmHTTPErrorWithMsg(ctx, err, errMsg)
  466. return
  467. }
  468. ctx.JSON(res, nil)
  469. }
  470. func actives(ctx *bm.Context) {
  471. var (
  472. err error
  473. errMsg string
  474. req = new(model.UpsInfoActiveReq)
  475. )
  476. if err = ctx.Bind(req); err != nil {
  477. log.Error("request param error: req: %+v, err: %+v", req, err)
  478. return
  479. }
  480. if len(req.Mids) <= 0 {
  481. log.Error("param error mids: %+v", req.Mids)
  482. ctx.JSON(nil, ecode.RequestErr)
  483. return
  484. }
  485. log.Info("req: %+v", req)
  486. res, err := Svc.GetUpsInfoActive(ctx, req)
  487. if err != nil {
  488. log.Error("Svc.GetUpsInfoActive error: %+v", err)
  489. service.BmHTTPErrorWithMsg(ctx, err, errMsg)
  490. return
  491. }
  492. ctx.JSON(res, nil)
  493. }