123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- package http
- import (
- "context"
- "fmt"
- "time"
- "go-common/app/admin/main/credit/model"
- "go-common/app/admin/main/credit/model/blocked"
- "go-common/library/ecode"
- "go-common/library/log"
- bm "go-common/library/net/http/blademaster"
- xtime "go-common/library/time"
- "go-common/library/xstr"
- )
- // infos get info list.
- func infos(c *bm.Context) {
- v := new(blocked.ArgBlockedSearch)
- err := c.Bind(v)
- if err != nil {
- return
- }
- info, pager, err := creSvc.Infos(c, v)
- if err != nil {
- log.Error("creSvc.Infos(%+v) error(%v)", v, err)
- httpCode(c, err)
- return
- }
- if len(info) == 0 {
- httpData(c, nil, pager)
- return
- }
- httpData(c, info, pager)
- }
- // infoByID get info by id.
- func infoByID(c *bm.Context) {
- var err error
- v := new(struct {
- ID int64 `form:"id" validate:"required"`
- })
- if err = c.Bind(v); err != nil {
- return
- }
- item := &blocked.Info{}
- if err = creSvc.ReadDB.Where("id = ?", v.ID).Find(item).Error; err != nil {
- if err != ecode.NothingFound {
- log.Error("creSvc.infoByID error(%v)", err)
- httpCode(c, err)
- return
- }
- httpData(c, nil, nil)
- return
- }
- if item != nil {
- info, err := creSvc.UserInfo(c, item.UID)
- if err != nil {
- log.Error("creSvc.UserInfo error(%v)", err)
- err = nil
- }
- if info != nil {
- item.UName = info.Info.Name
- }
- item.ReasonTypeDesc = blocked.ReasonTypeDesc(item.ReasonType)
- item.BlockedDaysDesc = blocked.BDaysDesc(item.BlockedDays, item.MoralNum, item.PunishType, item.BlockedForever)
- item.PublishStatusDesc = blocked.PStatusDesc[item.PublishStatus]
- item.OriginTypeDesc = blocked.OriginTypeDesc[item.OriginType]
- item.BlockedTypeDesc = blocked.BTypeDesc[item.BlockedType]
- }
- httpData(c, item, nil)
- }
- // upInfo update info.
- func upInfo(c *bm.Context) {
- var err error
- v := new(blocked.ArgUpInfo)
- if err = c.Bind(v); err != nil {
- return
- }
- item := &blocked.Info{}
- if err = creSvc.ReadDB.Where("id = ?", v.ID).Find(item).Error; err != nil {
- log.Error("http.blcokedInfoByID(%d) error(%v)", v.ID, err)
- httpCode(c, err)
- return
- }
- var data map[string]interface{}
- switch {
- case item.PublishStatus == blocked.StatusClose && v.Status == blocked.StatusOpen:
- data = map[string]interface{}{
- "origin_content_modify": v.Content,
- "publish_status": v.Status,
- "oper_id": v.OID,
- "publish_time": xtime.Time(time.Now().Unix()),
- }
- default:
- data = map[string]interface{}{
- "origin_content_modify": v.Content,
- "publish_status": v.Status,
- "oper_id": v.OID,
- }
- }
- if err = creSvc.DB.Model(&blocked.Info{}).Where("id = ?", v.ID).Updates(data).Error; err != nil {
- log.Error("s.dao.UpInfo(%v) error(%v)", v, err)
- httpCode(c, err)
- return
- }
- log.Info("business_upInfo:%+v", v)
- var multiple []interface{}
- single := map[string]interface{}{
- "id": item.ID,
- "oper_id": v.OID,
- "status": item.Status,
- "publish_status": v.Status,
- }
- multiple = append(multiple, single)
- creSvc.AddNotify(func() {
- creSvc.Search.SearchUpdate(context.TODO(), blocked.BusinessBlockedInfo, blocked.TableBlockedInfo, multiple)
- })
- httpCode(c, nil)
- }
- func delInfo(c *bm.Context) {
- var err error
- v := new(struct {
- IDS []int64 `form:"ids,split" validate:"min=1,max=100"`
- OID int64 `form:"op_id" validate:"required"`
- Status int8 `form:"status" default:"1"`
- })
- if err = c.Bind(v); err != nil {
- return
- }
- items := []*blocked.Info{}
- if err = creSvc.ReadDB.Where("id IN (?)", v.IDS).Find(&items).Error; err != nil {
- log.Error("http.blcokedInfoByIDs(%s) error(%v)", xstr.JoinInts(v.IDS), err)
- httpCode(c, err)
- return
- }
- if err = creSvc.DB.Model(&blocked.Info{}).Where("id IN (?)", v.IDS).Updates(map[string]interface{}{"status": v.Status, "oper_id": v.OID}).Error; err != nil {
- log.Error("s.dao.delInfo(%v) error(%v)", v, err)
- httpCode(c, err)
- return
- }
- log.Info("business_delInfo:%+v", v)
- var multiple []interface{}
- for _, item := range items {
- single := map[string]interface{}{
- "id": item.ID,
- "oper_id": v.OID,
- "status": v.Status,
- "publish_status": item.PublishStatus,
- }
- multiple = append(multiple, single)
- }
- creSvc.AddNotify(func() {
- creSvc.Search.SearchUpdate(context.TODO(), blocked.BusinessBlockedInfo, blocked.TableBlockedInfo, multiple)
- })
- httpCode(c, nil)
- }
- // upInfoStatus update info publish_status.
- func upInfoStatus(c *bm.Context) {
- var err error
- v := new(blocked.ArgUpStatus)
- if err = c.Bind(v); err != nil {
- return
- }
- items := []*blocked.Info{}
- if err = creSvc.ReadDB.Where("id IN (?)", v.IDS).Find(&items).Error; err != nil {
- log.Error("http.blcokedInfoByIDs(%s) error(%v)", xstr.JoinInts(v.IDS), err)
- httpCode(c, err)
- return
- }
- if err = creSvc.DB.Model(&blocked.Info{}).Where("id IN (?)", v.IDS).Updates(
- map[string]interface{}{
- "oper_id": v.OID,
- "publish_status": v.Status,
- "publish_time": xtime.Time(time.Now().Unix()),
- }).Error; err != nil {
- log.Error("creSvc.upInfoStatus(%v) error(%v)", v, err)
- httpCode(c, err)
- return
- }
- log.Info("business_upInfoStatus:%+v", v)
- var multiple []interface{}
- for _, item := range items {
- single := map[string]interface{}{
- "id": item.ID,
- "oper_id": v.OID,
- "status": item.Status,
- "publish_status": v.Status,
- }
- multiple = append(multiple, single)
- }
- creSvc.AddNotify(func() {
- creSvc.Search.SearchUpdate(context.TODO(), blocked.BusinessBlockedInfo, blocked.TableBlockedInfo, multiple)
- })
- httpCode(c, nil)
- }
- // infosEx export info data.
- func infosEx(c *bm.Context) {
- v := new(blocked.ArgBlockedSearch)
- if err := c.Bind(v); err != nil {
- return
- }
- list, err := creSvc.InfosEx(c, v)
- if err != nil {
- log.Error("creSvc.InfosEx error(%v)", err)
- httpCode(c, err)
- return
- }
- re, err := blocked.DealInfo(list)
- if err != nil {
- log.Error("blocked.DealInfo error(%v)", err)
- httpCode(c, err)
- return
- }
- outBuf(c, creSvc.FormatCSV(re), fmt.Sprintf("%s-%s", time.Now().Format(model.TimeFormatDay), "blocked-info"))
- }
- func publishs(c *bm.Context) {
- v := new(blocked.ArgPublishSearch)
- if err := c.Bind(v); err != nil {
- return
- }
- publish, pager, err := creSvc.Publishs(c, v)
- if err != nil {
- log.Error("creSvc.Publishs(%+v) error(%v)", v, err)
- httpCode(c, err)
- return
- }
- if len(publish) == 0 {
- httpData(c, nil, pager)
- return
- }
- httpData(c, publish, pager)
- }
- // publishByID get publish by id.
- func publishByID(c *bm.Context) {
- var err error
- v := new(struct {
- ID int64 `form:"id" validate:"required"`
- })
- if err = c.Bind(v); err != nil {
- return
- }
- item := &blocked.Publish{}
- if err = creSvc.ReadDB.Where("id = ?", v.ID).Find(item).Error; err != nil {
- if err != ecode.NothingFound {
- log.Error("creSvc.publishByID error(%v)", err)
- httpCode(c, err)
- return
- }
- httpData(c, nil, nil)
- return
- }
- if item != nil {
- item.PublishTypeDesc = blocked.PTypeDesc[item.Type]
- item.PublishStatusDesc = blocked.PStatusDesc[item.PublishStatus]
- item.StickStatusDesc = blocked.SStatusDesc[item.StickStatus]
- }
- httpData(c, item, nil)
- }
- func addPublish(c *bm.Context) {
- var err error
- v := new(blocked.ArgPublish)
- if err = c.Bind(v); err != nil {
- return
- }
- publish := &blocked.Publish{
- Title: v.Title,
- SubTitle: v.SubTitle,
- PublishStatus: v.PublishStatus,
- StickStatus: v.StickStatus,
- Content: v.Content,
- URL: v.URL,
- Type: v.PType,
- OPID: v.OID,
- ShowTime: xtime.Time(time.Now().Unix()),
- }
- if v.ShowTime != "" {
- var tm time.Time
- tm, err = time.ParseInLocation(model.TimeFormatSec, v.ShowTime, time.Local)
- if err == nil {
- publish.ShowTime = xtime.Time(tm.Unix())
- }
- }
- if err = creSvc.DB.Create(publish).Error; err != nil {
- log.Error("creSvc.AddPublish(%+v) error(%v)", v, err)
- httpCode(c, err)
- return
- }
- log.Info("business_addPublish:%+v", v)
- httpCode(c, nil)
- }
- func upPublish(c *bm.Context) {
- var err error
- v := new(blocked.ArgPublish)
- if err = c.Bind(v); err != nil {
- return
- }
- item := &blocked.Publish{}
- if err = creSvc.ReadDB.Where("id = ?", v.ID).Find(item).Error; err != nil {
- log.Error("http.publishByID(%d) error(%v)", v.ID, err)
- httpCode(c, err)
- return
- }
- var showTime xtime.Time
- tm, err := time.ParseInLocation(model.TimeFormatSec, v.ShowTime, time.Local)
- if err == nil {
- showTime = xtime.Time(tm.Unix())
- }
- if err = creSvc.DB.Model(&blocked.Publish{}).Where("id = ?", v.ID).
- Updates(
- map[string]interface{}{
- "title": v.Title,
- "sub_title": v.SubTitle,
- "publish_status": v.PublishStatus,
- "stick_status": v.StickStatus,
- "content": v.Content,
- "url": v.URL,
- "ptype": v.PType,
- "show_time": showTime,
- "oper_id": v.OID,
- }).Error; err != nil {
- log.Error("creSvc.UpPublish(%+v) error(%v)", v, err)
- httpCode(c, err)
- return
- }
- log.Info("business_upPublish:%+v", v)
- var multiple []interface{}
- single := map[string]interface{}{
- "id": v.ID,
- "title": v.Title,
- "sub_title": v.SubTitle,
- "oper_id": v.OID,
- "ptype": v.PType,
- "show_time": showTime.Time().Format(model.TimeFormatSec),
- "status": item.Status,
- }
- multiple = append(multiple, single)
- creSvc.AddNotify(func() {
- creSvc.Search.SearchUpdate(context.TODO(), blocked.BusinessBlockedPublish, blocked.TableBlockedPublish, multiple)
- })
- httpCode(c, nil)
- }
- func delPublish(c *bm.Context) {
- var err error
- v := new(struct {
- IDS []int64 `form:"ids,split" validate:"required"`
- Status int8 `form:"status" default:"1"`
- OID int64 `form:"op_id" validate:"required"`
- })
- if err = c.Bind(v); err != nil {
- return
- }
- if err = creSvc.DB.Model(&blocked.Publish{}).Where(v.IDS).Updates(map[string]interface{}{"status": v.Status, "oper_id": v.OID}).Error; err != nil {
- httpCode(c, err)
- return
- }
- log.Info("business_delPublish:%+v", v)
- var multiple []interface{}
- for _, id := range v.IDS {
- single := map[string]interface{}{
- "id": id,
- "oper_id": v.OID,
- "status": v.Status,
- }
- multiple = append(multiple, single)
- }
- creSvc.AddNotify(func() {
- creSvc.Search.SearchUpdate(context.TODO(), blocked.BusinessBlockedPublish, blocked.TableBlockedPublish, multiple)
- })
- httpCode(c, nil)
- }
- func notices(c *bm.Context) {
- var (
- err error
- order = "id"
- sort = "DESC"
- )
- v := new(struct {
- PN int `form:"pn" default:"1"`
- PS int `form:"ps" default:"20"`
- })
- if err = c.Bind(v); err != nil {
- return
- }
- count := int(0)
- items := []*blocked.Notice{}
- pager := &blocked.Pager{
- Total: count,
- PN: v.PN,
- PS: v.PS,
- Order: order,
- Sort: sort,
- }
- if err = creSvc.ReadDB.Offset((v.PN - 1) * v.PS).Limit(v.PS).Order(fmt.Sprintf("%s %s", order, sort)).Find(&items).Error; err != nil {
- if err != ecode.NothingFound {
- log.Error("creSvc.notices error(%v)", err)
- httpData(c, nil, nil)
- return
- }
- httpData(c, nil, pager)
- return
- }
- if err = creSvc.ReadDB.Model(&blocked.Notice{}).Count(&count).Error; err != nil {
- if err != ecode.NothingFound {
- log.Error("creSvc.notices count error(%v)", err)
- httpData(c, nil, nil)
- return
- }
- httpData(c, nil, pager)
- return
- }
- pager.Total = count
- for _, v := range items {
- v.StatusDesc = blocked.NoticeStateDesc[v.Status]
- v.OPName = creSvc.Managers[v.OperID]
- }
- httpData(c, items, pager)
- }
- func addNotice(c *bm.Context) {
- var err error
- v := new(struct {
- Content string `form:"content" validate:"required"`
- URL string `form:"url" validate:"required"`
- OID int64 `form:"op_id" validate:"required"`
- })
- if err = c.Bind(v); err != nil {
- return
- }
- if err = creSvc.DB.Model(&blocked.Notice{}).Create(&blocked.Notice{Content: v.Content, URL: v.URL, OperID: v.OID}).Error; err != nil {
- log.Error("creSvc.addNotice(%v) error(%v)", v, err)
- httpCode(c, err)
- return
- }
- log.Info("business_addNotice:%+v", v)
- httpCode(c, nil)
- }
- func upNoticeStatus(c *bm.Context) {
- var err error
- v := new(struct {
- ID int `form:"id" validate:"required"`
- Status int8 `form:"status" validate:"min=0,max=1"`
- OID int `form:"op_id" validate:"required"`
- })
- if err = c.Bind(v); err != nil {
- return
- }
- if err = creSvc.DB.Model(&blocked.Notice{}).Where("id=?", v.ID).Updates(map[string]interface{}{"status": v.Status, "oper_id": v.OID}).Error; err != nil {
- log.Error("creSvc.upNoticeStatus(%v) error(%v)", v, err)
- httpCode(c, err)
- return
- }
- log.Info("business_upNoticeStatus:%+v", v)
- httpCode(c, nil)
- }
|