pendant.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. package http
  2. import (
  3. "encoding/csv"
  4. "io/ioutil"
  5. "strconv"
  6. "strings"
  7. "go-common/app/admin/main/usersuit/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/metadata"
  12. "go-common/library/xstr"
  13. )
  14. func pendantInfoList(c *bm.Context) {
  15. arg := new(model.ArgPendantGroupList)
  16. arg.PN, arg.PS = 1, 20
  17. if err := c.Bind(arg); err != nil {
  18. return
  19. }
  20. pis, pager, err := svc.PendantInfoList(c, arg)
  21. if err != nil {
  22. log.Error("svc.PendantInfoList(%+v) err(%v)", arg, err)
  23. return
  24. }
  25. if len(pis) == 0 {
  26. httpData(c, nil, pager)
  27. return
  28. }
  29. httpData(c, pis, pager)
  30. }
  31. func pendantInfoID(c *bm.Context) {
  32. arg := new(struct {
  33. PID int64 `form:"pid" validate:"required"`
  34. GID int64 `form:"gid" validate:"required"`
  35. })
  36. if err := c.Bind(arg); err != nil {
  37. return
  38. }
  39. pi, err := svc.PendantInfoID(c, arg.PID, arg.GID)
  40. if err != nil {
  41. log.Error("svc.PendantInfoID(%+v) err(%v)", arg, err)
  42. httpCode(c, err)
  43. return
  44. }
  45. httpData(c, pi, nil)
  46. }
  47. func pendantGroupID(c *bm.Context) {
  48. arg := new(struct {
  49. GID int64 `form:"gid" validate:"required"`
  50. })
  51. if err := c.Bind(arg); err != nil {
  52. return
  53. }
  54. pg, err := svc.PendantGroupID(c, arg.GID)
  55. if err != nil {
  56. log.Error("svc.PendantGroupID(%+v) err(%v)", arg, err)
  57. return
  58. }
  59. httpData(c, pg, nil)
  60. }
  61. func pendantGroupList(c *bm.Context) {
  62. arg := new(model.ArgPendantGroupList)
  63. arg.PN, arg.PS = 1, 20
  64. if err := c.Bind(arg); err != nil {
  65. return
  66. }
  67. pgs, pager, err := svc.PendantGroupList(c, arg)
  68. if err != nil {
  69. log.Error("svc.PendantGroupList(%+v) err(%v)", arg, err)
  70. httpCode(c, err)
  71. return
  72. }
  73. if len(pgs) == 0 {
  74. httpData(c, nil, pager)
  75. return
  76. }
  77. httpData(c, pgs, pager)
  78. }
  79. func pendantGroupAll(c *bm.Context) {
  80. pgs, err := svc.PendantGroupAll(c)
  81. if err != nil {
  82. log.Error("svc.PendantGroupAll() err(%v)", err)
  83. httpCode(c, err)
  84. return
  85. }
  86. httpData(c, pgs, nil)
  87. }
  88. func pendantInfoAllNoPage(c *bm.Context) {
  89. pis, err := svc.PendantInfoAllNoPage(c)
  90. if err != nil {
  91. log.Error("svc.pendantInfoAllNoPage() err(%v)", err)
  92. httpCode(c, err)
  93. return
  94. }
  95. httpData(c, pis, nil)
  96. }
  97. func addPendantInfo(c *bm.Context) {
  98. arg := new(model.ArgPendantInfo)
  99. if err := c.Bind(arg); err != nil {
  100. return
  101. }
  102. err := svc.AddPendantInfo(c, arg)
  103. if err != nil {
  104. log.Error("svc.AddPendantInfo(%+v) err(%v)", arg, err)
  105. httpCode(c, err)
  106. return
  107. }
  108. httpCode(c, nil)
  109. }
  110. func upPendantInfo(c *bm.Context) {
  111. arg := new(model.ArgPendantInfo)
  112. if err := c.Bind(arg); err != nil {
  113. return
  114. }
  115. err := svc.UpPendantInfo(c, arg)
  116. if err != nil {
  117. log.Error("svc.UpPendantInfo(%+v) err(%v)", arg, err)
  118. httpCode(c, err)
  119. return
  120. }
  121. httpCode(c, nil)
  122. }
  123. func upPendantGroupStatus(c *bm.Context) {
  124. arg := new(struct {
  125. GID int64 `form:"gid" validate:"required"`
  126. Status int8 `form:"status"`
  127. })
  128. if err := c.Bind(arg); err != nil {
  129. return
  130. }
  131. err := svc.UpPendantGroupStatus(c, arg.GID, arg.Status)
  132. if err != nil {
  133. log.Error("svc.UpPendantGroupStatus(%+v) err(%v)", arg, err)
  134. httpCode(c, err)
  135. return
  136. }
  137. httpCode(c, nil)
  138. }
  139. func upPendantInfoStatus(c *bm.Context) {
  140. arg := new(struct {
  141. PID int64 `form:"pid" validate:"required"`
  142. Status int8 `form:"status"`
  143. })
  144. if err := c.Bind(arg); err != nil {
  145. return
  146. }
  147. err := svc.UpPendantInfoStatus(c, arg.PID, arg.Status)
  148. if err != nil {
  149. log.Error("svc.UpPendantInfoStatus(%+v) err(%v)", arg, err)
  150. httpCode(c, err)
  151. return
  152. }
  153. httpCode(c, nil)
  154. }
  155. func addPendantGroup(c *bm.Context) {
  156. arg := new(model.ArgPendantGroup)
  157. if err := c.Bind(arg); err != nil {
  158. return
  159. }
  160. err := svc.AddPendantGroup(c, arg)
  161. if err != nil {
  162. log.Error("svc.AddPendantGroup(%+v) err(%v)", arg, err)
  163. httpCode(c, err)
  164. return
  165. }
  166. httpCode(c, nil)
  167. }
  168. func upPendantGroup(c *bm.Context) {
  169. arg := new(model.ArgPendantGroup)
  170. if err := c.Bind(arg); err != nil {
  171. return
  172. }
  173. err := svc.UpPendantGroup(c, arg)
  174. if err != nil {
  175. log.Error("svc.UpPendantGroup(%+v) err(%v)", arg, err)
  176. httpCode(c, err)
  177. return
  178. }
  179. httpCode(c, nil)
  180. }
  181. func pendantOrders(c *bm.Context) {
  182. arg := new(model.ArgPendantOrder)
  183. arg.PN, arg.PS = 1, 20
  184. if err := c.Bind(arg); err != nil {
  185. return
  186. }
  187. pos, pager, err := svc.PendantOrders(c, arg)
  188. if err != nil {
  189. log.Error("svc.PendantOrders(%+v) err(%v)", arg, err)
  190. httpCode(c, err)
  191. return
  192. }
  193. if len(pos) == 0 {
  194. httpData(c, nil, pager)
  195. return
  196. }
  197. httpData(c, pos, pager)
  198. }
  199. func equipPendant(c *bm.Context) {
  200. arg := new(struct {
  201. UID int64 `form:"uid" validate:"required"`
  202. PID int64 `form:"pid" validate:"required"`
  203. })
  204. if err := c.Bind(arg); err != nil {
  205. return
  206. }
  207. err := svc.EquipPendant(c, arg.UID, arg.PID)
  208. if err != nil {
  209. log.Error("svc.EquipPendant(%+v) err(%v)", arg, err)
  210. httpCode(c, err)
  211. return
  212. }
  213. httpCode(c, nil)
  214. }
  215. func userPendantPKG(c *bm.Context) {
  216. arg := new(struct {
  217. UID int64 `form:"uid" validate:"required"`
  218. })
  219. if err := c.Bind(arg); err != nil {
  220. return
  221. }
  222. pkg, equip, err := svc.PendantPKG(c, arg.UID)
  223. if err != nil {
  224. log.Error("svc.PendantPKG(%+v) err(%v)", arg, err)
  225. httpCode(c, err)
  226. return
  227. }
  228. httpData(c, map[string]interface{}{
  229. "pkg": pkg,
  230. "equip": equip,
  231. }, nil)
  232. }
  233. func userPKGDetails(c *bm.Context) {
  234. arg := new(struct {
  235. UID int64 `form:"uid" validate:"required"`
  236. PID int64 `form:"pid" validate:"required"`
  237. })
  238. if err := c.Bind(arg); err != nil {
  239. return
  240. }
  241. pkgs, err := svc.UserPKGDetails(c, arg.UID, arg.PID)
  242. if err != nil {
  243. log.Error("svc.UserPKGDetails(%+v) err(%v)", arg, err)
  244. httpCode(c, err)
  245. return
  246. }
  247. httpData(c, pkgs, nil)
  248. }
  249. func upPendantPKG(c *bm.Context) {
  250. arg := new(model.ArgPendantPKG)
  251. if err := c.Bind(arg); err != nil {
  252. return
  253. }
  254. msg := &model.SysMsg{IsMsg: arg.IsMsg, Type: arg.Type, Title: arg.Title, Content: arg.Content, RemoteIP: metadata.String(c, metadata.RemoteIP)}
  255. err := svc.UpPendantPKG(c, arg.UID, arg.PID, arg.Day, msg, arg.OID)
  256. if err != nil {
  257. log.Error("svc.UpPendantPKG(%+v) err(%v)", arg, err)
  258. httpCode(c, err)
  259. return
  260. }
  261. httpCode(c, nil)
  262. }
  263. // migrate old pedant info to new db.
  264. func mutliSend(c *bm.Context) {
  265. var (
  266. err error
  267. data []byte
  268. uids []int64
  269. )
  270. file, _, err := c.Request.FormFile("file")
  271. if err != nil {
  272. log.Error("upload err(%v)", err)
  273. httpCode(c, err)
  274. return
  275. }
  276. defer file.Close()
  277. data, err = ioutil.ReadAll(file)
  278. if err != nil {
  279. log.Error("ioutil.ReadAll err(%v)", err)
  280. return
  281. }
  282. r := csv.NewReader(strings.NewReader(string(data)))
  283. r.Comma = ','
  284. records, err := r.ReadAll()
  285. if err != nil {
  286. log.Error("r.ReadAll() err(%v)", err)
  287. }
  288. var (
  289. uid int64
  290. muids = make(map[int64]struct{}, len(records))
  291. )
  292. for _, v := range records {
  293. if v[0] == "" {
  294. continue
  295. }
  296. if uid, err = strconv.ParseInt(v[0], 10, 64); err != nil {
  297. log.Error("strconv.ParseInt err(%v)", err)
  298. continue
  299. }
  300. if _, ok := muids[uid]; ok {
  301. continue
  302. }
  303. muids[uid] = struct{}{}
  304. uids = append(uids, uid)
  305. }
  306. if len(uids) == 0 {
  307. log.Warn("uids is nothing to send pendant")
  308. httpCode(c, ecode.RequestErr)
  309. return
  310. }
  311. if len(uids) > 1000 {
  312. httpCode(c, ecode.PendantAboveSendMaxLimit)
  313. return
  314. }
  315. params := c.Request.Form
  316. pidStr := params.Get("pid")
  317. dayStr := params.Get("day")
  318. isMsgStr := params.Get("is_msg")
  319. title := params.Get("title")
  320. content := params.Get("content")
  321. operStr := params.Get("oper_id")
  322. pid, err := strconv.ParseInt(pidStr, 10, 64)
  323. if err != nil {
  324. httpCode(c, ecode.RequestErr)
  325. return
  326. }
  327. day, err := strconv.ParseInt(dayStr, 10, 64)
  328. if err != nil {
  329. httpCode(c, ecode.RequestErr)
  330. return
  331. }
  332. isMsg, err := strconv.ParseBool(isMsgStr)
  333. if err != nil {
  334. httpCode(c, ecode.RequestErr)
  335. return
  336. }
  337. oid, err := strconv.ParseInt(operStr, 10, 64)
  338. if err != nil {
  339. httpCode(c, ecode.RequestErr)
  340. return
  341. }
  342. msg := &model.SysMsg{IsMsg: isMsg, Type: model.MsgTypeCustom, Title: title, Content: content, RemoteIP: metadata.String(c, metadata.RemoteIP)}
  343. if err = svc.MutliSendPendant(c, uids, pid, day, msg, oid); err != nil {
  344. log.Error("svc.MutliSendPendant(%s,%d,%d,%v,%d) err(%v)", xstr.JoinInts(uids), pid, day, msg, oid, err)
  345. httpCode(c, err)
  346. return
  347. }
  348. httpCode(c, nil)
  349. }
  350. func pendantOperlog(c *bm.Context) {
  351. arg := new(struct {
  352. PN int `form:"pn"`
  353. PS int `form:"ps"`
  354. })
  355. arg.PN, arg.PS = 1, 20
  356. if err := c.Bind(arg); err != nil {
  357. return
  358. }
  359. opers, pager, err := svc.PendantOperlog(c, arg.PN, arg.PS)
  360. if err != nil {
  361. log.Error("svc.PendantOperlog(%+v) err(%v)", arg, err)
  362. httpCode(c, err)
  363. return
  364. }
  365. if len(opers) == 0 {
  366. httpData(c, nil, pager)
  367. return
  368. }
  369. httpData(c, opers, pager)
  370. }