123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- package http
- import (
- "encoding/csv"
- "io/ioutil"
- "strconv"
- "strings"
- "go-common/app/admin/main/usersuit/model"
- "go-common/library/ecode"
- "go-common/library/log"
- bm "go-common/library/net/http/blademaster"
- "go-common/library/net/metadata"
- "go-common/library/xstr"
- )
- func pendantInfoList(c *bm.Context) {
- arg := new(model.ArgPendantGroupList)
- arg.PN, arg.PS = 1, 20
- if err := c.Bind(arg); err != nil {
- return
- }
- pis, pager, err := svc.PendantInfoList(c, arg)
- if err != nil {
- log.Error("svc.PendantInfoList(%+v) err(%v)", arg, err)
- return
- }
- if len(pis) == 0 {
- httpData(c, nil, pager)
- return
- }
- httpData(c, pis, pager)
- }
- func pendantInfoID(c *bm.Context) {
- arg := new(struct {
- PID int64 `form:"pid" validate:"required"`
- GID int64 `form:"gid" validate:"required"`
- })
- if err := c.Bind(arg); err != nil {
- return
- }
- pi, err := svc.PendantInfoID(c, arg.PID, arg.GID)
- if err != nil {
- log.Error("svc.PendantInfoID(%+v) err(%v)", arg, err)
- httpCode(c, err)
- return
- }
- httpData(c, pi, nil)
- }
- func pendantGroupID(c *bm.Context) {
- arg := new(struct {
- GID int64 `form:"gid" validate:"required"`
- })
- if err := c.Bind(arg); err != nil {
- return
- }
- pg, err := svc.PendantGroupID(c, arg.GID)
- if err != nil {
- log.Error("svc.PendantGroupID(%+v) err(%v)", arg, err)
- return
- }
- httpData(c, pg, nil)
- }
- func pendantGroupList(c *bm.Context) {
- arg := new(model.ArgPendantGroupList)
- arg.PN, arg.PS = 1, 20
- if err := c.Bind(arg); err != nil {
- return
- }
- pgs, pager, err := svc.PendantGroupList(c, arg)
- if err != nil {
- log.Error("svc.PendantGroupList(%+v) err(%v)", arg, err)
- httpCode(c, err)
- return
- }
- if len(pgs) == 0 {
- httpData(c, nil, pager)
- return
- }
- httpData(c, pgs, pager)
- }
- func pendantGroupAll(c *bm.Context) {
- pgs, err := svc.PendantGroupAll(c)
- if err != nil {
- log.Error("svc.PendantGroupAll() err(%v)", err)
- httpCode(c, err)
- return
- }
- httpData(c, pgs, nil)
- }
- func pendantInfoAllNoPage(c *bm.Context) {
- pis, err := svc.PendantInfoAllNoPage(c)
- if err != nil {
- log.Error("svc.pendantInfoAllNoPage() err(%v)", err)
- httpCode(c, err)
- return
- }
- httpData(c, pis, nil)
- }
- func addPendantInfo(c *bm.Context) {
- arg := new(model.ArgPendantInfo)
- if err := c.Bind(arg); err != nil {
- return
- }
- err := svc.AddPendantInfo(c, arg)
- if err != nil {
- log.Error("svc.AddPendantInfo(%+v) err(%v)", arg, err)
- httpCode(c, err)
- return
- }
- httpCode(c, nil)
- }
- func upPendantInfo(c *bm.Context) {
- arg := new(model.ArgPendantInfo)
- if err := c.Bind(arg); err != nil {
- return
- }
- err := svc.UpPendantInfo(c, arg)
- if err != nil {
- log.Error("svc.UpPendantInfo(%+v) err(%v)", arg, err)
- httpCode(c, err)
- return
- }
- httpCode(c, nil)
- }
- func upPendantGroupStatus(c *bm.Context) {
- arg := new(struct {
- GID int64 `form:"gid" validate:"required"`
- Status int8 `form:"status"`
- })
- if err := c.Bind(arg); err != nil {
- return
- }
- err := svc.UpPendantGroupStatus(c, arg.GID, arg.Status)
- if err != nil {
- log.Error("svc.UpPendantGroupStatus(%+v) err(%v)", arg, err)
- httpCode(c, err)
- return
- }
- httpCode(c, nil)
- }
- func upPendantInfoStatus(c *bm.Context) {
- arg := new(struct {
- PID int64 `form:"pid" validate:"required"`
- Status int8 `form:"status"`
- })
- if err := c.Bind(arg); err != nil {
- return
- }
- err := svc.UpPendantInfoStatus(c, arg.PID, arg.Status)
- if err != nil {
- log.Error("svc.UpPendantInfoStatus(%+v) err(%v)", arg, err)
- httpCode(c, err)
- return
- }
- httpCode(c, nil)
- }
- func addPendantGroup(c *bm.Context) {
- arg := new(model.ArgPendantGroup)
- if err := c.Bind(arg); err != nil {
- return
- }
- err := svc.AddPendantGroup(c, arg)
- if err != nil {
- log.Error("svc.AddPendantGroup(%+v) err(%v)", arg, err)
- httpCode(c, err)
- return
- }
- httpCode(c, nil)
- }
- func upPendantGroup(c *bm.Context) {
- arg := new(model.ArgPendantGroup)
- if err := c.Bind(arg); err != nil {
- return
- }
- err := svc.UpPendantGroup(c, arg)
- if err != nil {
- log.Error("svc.UpPendantGroup(%+v) err(%v)", arg, err)
- httpCode(c, err)
- return
- }
- httpCode(c, nil)
- }
- func pendantOrders(c *bm.Context) {
- arg := new(model.ArgPendantOrder)
- arg.PN, arg.PS = 1, 20
- if err := c.Bind(arg); err != nil {
- return
- }
- pos, pager, err := svc.PendantOrders(c, arg)
- if err != nil {
- log.Error("svc.PendantOrders(%+v) err(%v)", arg, err)
- httpCode(c, err)
- return
- }
- if len(pos) == 0 {
- httpData(c, nil, pager)
- return
- }
- httpData(c, pos, pager)
- }
- func equipPendant(c *bm.Context) {
- arg := new(struct {
- UID int64 `form:"uid" validate:"required"`
- PID int64 `form:"pid" validate:"required"`
- })
- if err := c.Bind(arg); err != nil {
- return
- }
- err := svc.EquipPendant(c, arg.UID, arg.PID)
- if err != nil {
- log.Error("svc.EquipPendant(%+v) err(%v)", arg, err)
- httpCode(c, err)
- return
- }
- httpCode(c, nil)
- }
- func userPendantPKG(c *bm.Context) {
- arg := new(struct {
- UID int64 `form:"uid" validate:"required"`
- })
- if err := c.Bind(arg); err != nil {
- return
- }
- pkg, equip, err := svc.PendantPKG(c, arg.UID)
- if err != nil {
- log.Error("svc.PendantPKG(%+v) err(%v)", arg, err)
- httpCode(c, err)
- return
- }
- httpData(c, map[string]interface{}{
- "pkg": pkg,
- "equip": equip,
- }, nil)
- }
- func userPKGDetails(c *bm.Context) {
- arg := new(struct {
- UID int64 `form:"uid" validate:"required"`
- PID int64 `form:"pid" validate:"required"`
- })
- if err := c.Bind(arg); err != nil {
- return
- }
- pkgs, err := svc.UserPKGDetails(c, arg.UID, arg.PID)
- if err != nil {
- log.Error("svc.UserPKGDetails(%+v) err(%v)", arg, err)
- httpCode(c, err)
- return
- }
- httpData(c, pkgs, nil)
- }
- func upPendantPKG(c *bm.Context) {
- arg := new(model.ArgPendantPKG)
- if err := c.Bind(arg); err != nil {
- return
- }
- msg := &model.SysMsg{IsMsg: arg.IsMsg, Type: arg.Type, Title: arg.Title, Content: arg.Content, RemoteIP: metadata.String(c, metadata.RemoteIP)}
- err := svc.UpPendantPKG(c, arg.UID, arg.PID, arg.Day, msg, arg.OID)
- if err != nil {
- log.Error("svc.UpPendantPKG(%+v) err(%v)", arg, err)
- httpCode(c, err)
- return
- }
- httpCode(c, nil)
- }
- // migrate old pedant info to new db.
- func mutliSend(c *bm.Context) {
- var (
- err error
- data []byte
- uids []int64
- )
- file, _, err := c.Request.FormFile("file")
- if err != nil {
- log.Error("upload err(%v)", err)
- httpCode(c, err)
- return
- }
- defer file.Close()
- data, err = ioutil.ReadAll(file)
- if err != nil {
- log.Error("ioutil.ReadAll err(%v)", err)
- return
- }
- r := csv.NewReader(strings.NewReader(string(data)))
- r.Comma = ','
- records, err := r.ReadAll()
- if err != nil {
- log.Error("r.ReadAll() err(%v)", err)
- }
- var (
- uid int64
- muids = make(map[int64]struct{}, len(records))
- )
- for _, v := range records {
- if v[0] == "" {
- continue
- }
- if uid, err = strconv.ParseInt(v[0], 10, 64); err != nil {
- log.Error("strconv.ParseInt err(%v)", err)
- continue
- }
- if _, ok := muids[uid]; ok {
- continue
- }
- muids[uid] = struct{}{}
- uids = append(uids, uid)
- }
- if len(uids) == 0 {
- log.Warn("uids is nothing to send pendant")
- httpCode(c, ecode.RequestErr)
- return
- }
- if len(uids) > 1000 {
- httpCode(c, ecode.PendantAboveSendMaxLimit)
- return
- }
- params := c.Request.Form
- pidStr := params.Get("pid")
- dayStr := params.Get("day")
- isMsgStr := params.Get("is_msg")
- title := params.Get("title")
- content := params.Get("content")
- operStr := params.Get("oper_id")
- pid, err := strconv.ParseInt(pidStr, 10, 64)
- if err != nil {
- httpCode(c, ecode.RequestErr)
- return
- }
- day, err := strconv.ParseInt(dayStr, 10, 64)
- if err != nil {
- httpCode(c, ecode.RequestErr)
- return
- }
- isMsg, err := strconv.ParseBool(isMsgStr)
- if err != nil {
- httpCode(c, ecode.RequestErr)
- return
- }
- oid, err := strconv.ParseInt(operStr, 10, 64)
- if err != nil {
- httpCode(c, ecode.RequestErr)
- return
- }
- msg := &model.SysMsg{IsMsg: isMsg, Type: model.MsgTypeCustom, Title: title, Content: content, RemoteIP: metadata.String(c, metadata.RemoteIP)}
- if err = svc.MutliSendPendant(c, uids, pid, day, msg, oid); err != nil {
- log.Error("svc.MutliSendPendant(%s,%d,%d,%v,%d) err(%v)", xstr.JoinInts(uids), pid, day, msg, oid, err)
- httpCode(c, err)
- return
- }
- httpCode(c, nil)
- }
- func pendantOperlog(c *bm.Context) {
- arg := new(struct {
- PN int `form:"pn"`
- PS int `form:"ps"`
- })
- arg.PN, arg.PS = 1, 20
- if err := c.Bind(arg); err != nil {
- return
- }
- opers, pager, err := svc.PendantOperlog(c, arg.PN, arg.PS)
- if err != nil {
- log.Error("svc.PendantOperlog(%+v) err(%v)", arg, err)
- httpCode(c, err)
- return
- }
- if len(opers) == 0 {
- httpData(c, nil, pager)
- return
- }
- httpData(c, opers, pager)
- }
|