123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844 |
- package http
- import (
- "context"
- "encoding/json"
- "fmt"
- "io/ioutil"
- "math"
- "sort"
- "strconv"
- "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"
- "go-common/library/net/metadata"
- xtime "go-common/library/time"
- "go-common/library/xstr"
- "github.com/jinzhu/gorm"
- )
- func users(c *bm.Context) {
- v := new(blocked.ArgJurySearch)
- if err := c.Bind(v); err != nil {
- return
- }
- list, pager, err := creSvc.Jurys(c, v)
- if err != nil {
- log.Error("creSvc.Jurys error(%v)", err)
- httpCode(c, err)
- return
- }
- if len(list) == 0 {
- httpData(c, nil, pager)
- return
- }
- httpData(c, list, pager)
- }
- func userAdd(c *bm.Context) {
- var err error
- v := new(blocked.ArgAddJurys)
- if err = c.Bind(v); err != nil {
- return
- }
- if err = creSvc.AddJury(c, v); err != nil {
- httpCode(c, err)
- return
- }
- log.Info("business_userAdd:%+v", v)
- httpCode(c, nil)
- }
- // userByID get user by id.
- func userByID(c *bm.Context) {
- var err error
- v := new(struct {
- MID int64 `form:"mid" validate:"required"`
- })
- if err = c.Bind(v); err != nil {
- return
- }
- item := &blocked.Jury{}
- if err = creSvc.ReadDB.Where("mid = ?", v.MID).Find(item).Error; err != nil {
- if err != ecode.NothingFound {
- log.Error("creSvc.userByID error(%v)", err)
- httpCode(c, err)
- return
- }
- httpData(c, nil, nil)
- return
- }
- if item != nil {
- if item.VoteTotal > 0 {
- item.VoteRadio = strconv.FormatFloat(float64(item.VoteRight)/float64(item.VoteTotal)*100, 'f', 2, 64)
- } else {
- item.VoteRadio = "-1"
- }
- item.StatusDesc = blocked.JuryerStatus[item.Status]
- item.BlackDesc = blocked.JuryerStyle[item.Black]
- }
- httpData(c, item, nil)
- }
- func upUserStatus(c *bm.Context) {
- v := new(struct {
- MIDS []int64 `form:"mids,split" validate:"min=1,max=100"`
- Status int8 `form:"status" validate:"min=1,max=2" default:"2"`
- OID int64 `form:"op_id" validate:"required"`
- })
- if err := c.Bind(v); err != nil {
- return
- }
- items := []*blocked.Jury{}
- if err := creSvc.ReadDB.Where("mid IN (?)", v.MIDS).Find(&items).Error; err != nil {
- log.Error("creSvc.juryByMIDs(%s) error(%v)", xstr.JoinInts(v.MIDS), err)
- httpCode(c, err)
- return
- }
- now := time.Now()
- if err := creSvc.DB.Model(&blocked.Jury{}).Where("mid IN(?)", v.MIDS).Updates(map[string]interface{}{"status": v.Status, "expired": now, "oper_id": v.OID}).Error; err != nil {
- httpCode(c, err)
- return
- }
- log.Info("business_upUserStatus:%+v", v)
- mids := make(map[int64]*blocked.Jury, len(v.MIDS))
- for _, v := range items {
- mids[v.UID] = v
- }
- var multiple []interface{}
- for _, mid := range v.MIDS {
- single := map[string]interface{}{
- "id": mids[mid].ID,
- "oper_id": v.OID,
- "status": v.Status,
- "expired": now.Format(model.TimeFormatSec),
- "black": mids[mid].Black,
- }
- multiple = append(multiple, single)
- }
- creSvc.AddNotify(func() {
- creSvc.Search.SearchUpdate(context.TODO(), blocked.BusinessBlockedJury, blocked.TableBlockedJury, multiple)
- })
- httpCode(c, nil)
- }
- func blackWhite(c *bm.Context) {
- var err error
- v := new(struct {
- MIDS []int64 `form:"mids,split" validate:"min=1,max=100"`
- Status int8 `form:"status" validate:"min=1,max=2"`
- Black int8 `form:"black"`
- ReMark string `form:"remark"`
- OID int64 `form:"op_id" validate:"required"`
- })
- if err = c.Bind(v); err != nil {
- return
- }
- items := []*blocked.Jury{}
- if err = creSvc.ReadDB.Where("mid IN (?)", v.MIDS).Find(&items).Error; err != nil {
- log.Error("creSvc.juryByMIDs(%s) error(%v)", xstr.JoinInts(v.MIDS), err)
- httpCode(c, err)
- return
- }
- mids := make(map[int64]*blocked.Jury, len(v.MIDS))
- for _, v := range items {
- mids[v.UID] = v
- }
- now := time.Now()
- var multiple []interface{}
- switch {
- case v.Black == blocked.JuryBlack && v.Status == blocked.JuryStatusOn:
- err = creSvc.DB.Model(&blocked.Jury{}).Where("mid IN(?)", v.MIDS).Updates(
- map[string]interface{}{
- "status": blocked.JuryStatusDown,
- "black": blocked.JuryBlack,
- "oper_id": v.OID,
- "expired": time.Now().Unix(),
- "remark": v.ReMark,
- }).Error
- for _, mid := range v.MIDS {
- single := map[string]interface{}{
- "id": mids[mid].ID,
- "oper_id": v.OID,
- "status": v.Status,
- "expired": now.Format(model.TimeFormatSec),
- "black": blocked.JuryBlack,
- }
- multiple = append(multiple, single)
- }
- case v.Black == blocked.JuryWhite && v.Status == blocked.JuryStatusOn:
- err = creSvc.DB.Model(&blocked.Jury{}).Where("mid IN(?)", v.MIDS).Updates(
- map[string]interface{}{
- "black": blocked.JuryWhite,
- "oper_id": v.OID,
- "remark": v.ReMark,
- }).Error
- for _, mid := range v.MIDS {
- single := map[string]interface{}{
- "id": mids[mid].ID,
- "oper_id": v.OID,
- "black": blocked.JuryWhite,
- }
- multiple = append(multiple, single)
- }
- case (v.Black == blocked.JuryBlack && v.Status == blocked.JuryStatusDown) || (v.Black == blocked.JuryWhite && v.Status == blocked.JuryStatusDown):
- err = creSvc.DB.Model(&blocked.Jury{}).Where("mid IN(?)", v.MIDS).Updates(
- map[string]interface{}{
- "black": blocked.JuryNormal,
- "oper_id": v.OID,
- "remark": v.ReMark,
- }).Error
- for _, mid := range v.MIDS {
- single := map[string]interface{}{
- "id": mids[mid].ID,
- "oper_id": v.OID,
- "black": blocked.JuryNormal,
- }
- multiple = append(multiple, single)
- }
- }
- if err != nil {
- log.Error("blackWhite creSvc.DB error(%v)", err)
- httpCode(c, err)
- return
- }
- log.Info("business_blackWhite:%+v", v)
- creSvc.AddNotify(func() {
- creSvc.Search.SearchUpdate(context.TODO(), blocked.BusinessBlockedJury, blocked.TableBlockedJury, multiple)
- })
- httpCode(c, nil)
- }
- func usersEx(c *bm.Context) {
- v := new(blocked.ArgJurySearch)
- if err := c.Bind(v); err != nil {
- return
- }
- list, err := creSvc.JurysEx(c, v)
- if err != nil {
- log.Error("creSvc.InfosEx error(%v)", err)
- httpCode(c, err)
- return
- }
- re, err := blocked.DealJury(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), "juryer"))
- }
- func cases(c *bm.Context) {
- v := new(blocked.ArgCaseSearch)
- if err := c.Bind(v); err != nil {
- return
- }
- list, pager, err := creSvc.Cases(c, v)
- if err != nil {
- log.Error("creSvc.Publishs(%+v) error(%v)", v, err)
- httpCode(c, err)
- return
- }
- if len(list) == 0 {
- httpData(c, nil, pager)
- return
- }
- httpData(c, list, pager)
- }
- // caseByID get case by id.
- func caseByID(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.Case{}
- if err = creSvc.ReadDB.Where("id = ?", v.ID).Find(item).Error; err != nil {
- if err != ecode.NothingFound {
- log.Error("creSvc.caseByID error(%v)", err)
- httpCode(c, err)
- return
- }
- httpData(c, nil, nil)
- return
- }
- if item != nil {
- item.CaseTypeDesc = blocked.CaseTypeDesc[item.CaseType]
- item.StatusDesc = blocked.StatusDesc[item.Status]
- item.OriginTypeDesc = blocked.OriginTypeDesc[item.OriginType]
- item.ReasonTypeDesc = blocked.ReasonTypeDesc(item.ReasonType)
- item.PunishDesc = blocked.PunishDesc[item.PunishResult]
- item.VoteTotal = blocked.VoteTotal(item.VoteRule, item.VoteBreak, item.VoteDelete)
- if item.VoteRule > 0 || item.VoteBreak > 0 || item.VoteDelete > 0 {
- item.BlockedPercent = blocked.BreakPercent(item.VoteRule, item.VoteBreak, item.VoteDelete)
- item.DeletePercent = blocked.DeletePercent(item.VoteRule, item.VoteBreak, item.VoteDelete)
- item.RulePercent = blocked.RulePercent(item.VoteRule, item.VoteBreak, item.VoteDelete)
- }
- }
- httpData(c, item, nil)
- }
- // reasons get all case reason.
- func reasons(c *bm.Context) {
- cr, err := creSvc.CaseReason(c)
- if err != nil {
- log.Error("creSvc.CaseReason error(%v)", err)
- httpCode(c, err)
- return
- }
- httpData(c, cr, nil)
- }
- func addCase(c *bm.Context) {
- var err error
- v := new(blocked.ArgCase)
- if err = c.Bind(v); err != nil {
- return
- }
- defTime, _ := time.ParseInLocation(model.TimeFormatSec, blocked.DefaultTime, time.Local)
- if err = creSvc.DB.Create(
- &blocked.Case{
- MID: v.UID,
- OriginType: v.Otype,
- ReasonType: v.ReasonType,
- PunishResult: v.PunishResult,
- BlockedDays: v.BlockedDays,
- OriginTitle: v.OriginTitle,
- OriginContent: v.OriginContent,
- OriginURL: v.OriginURL,
- OPID: v.OID,
- RelationID: v.RelationID,
- Status: blocked.CaseStatusGrantStop,
- StartTime: xtime.Time(defTime.Unix()),
- EndTime: xtime.Time(defTime.Unix()),
- }).Error; err != nil {
- log.Error("creSvc.addCase(%+v) error(%v)", v, err)
- httpCode(c, err)
- return
- }
- log.Info("business_addCase:%+v", v)
- httpCode(c, nil)
- }
- func upCase(c *bm.Context) {
- var err error
- v := new(blocked.ArgCase)
- if err = c.Bind(v); err != nil {
- return
- }
- item := &blocked.Case{}
- if err = creSvc.ReadDB.Where("id = ?", v.ID).Find(item).Error; err != nil {
- log.Error("creSvc.caseByID(%d) error(%v)", v.ID, err)
- httpCode(c, err)
- return
- }
- if err = creSvc.DB.Model(blocked.Case{}).Where("id = ?", v.ID).Updates(
- map[string]interface{}{
- "mid": v.UID,
- "origin_type": v.Otype,
- "reason_type": v.ReasonType,
- "punish_result": v.PunishResult,
- "blocked_days": v.BlockedDays,
- "origin_title": v.OriginTitle,
- "origin_content": v.OriginContent,
- "origin_url": v.OriginURL,
- "relation_id": v.RelationID,
- "oper_id": v.OID,
- }).Error; err != nil {
- log.Error("creSvc.upCase(%+v) error(%v)", v, err)
- httpCode(c, err)
- return
- }
- log.Info("business_upCase:%+v", v)
- var multiple []interface{}
- single := map[string]interface{}{
- "id": v.ID,
- "oper_id": v.OID,
- "origin_type": v.Otype,
- "mid": v.UID,
- "case_type": item.CaseType,
- }
- multiple = append(multiple, single)
- creSvc.AddNotify(func() {
- creSvc.Search.SearchUpdate(context.TODO(), blocked.BusinessBlockedCase, blocked.TableBlockedCase, multiple)
- })
- httpCode(c, nil)
- }
- func addCaseVote(c *bm.Context) {
- var err error
- v := new(struct {
- ID int64 `form:"id" validate:"required"`
- OPID int64 `form:"op_id" validate:"required"`
- VoteRule int64 `form:"vote_rule" default:"0"`
- VoteBreak int64 `form:"vote_break" default:"0"`
- VoteDelete int64 `form:"vote_delete" default:"0"`
- })
- if err = c.Bind(v); err != nil {
- return
- }
- item := &blocked.Case{}
- if err = creSvc.ReadDB.Where("id = ?", v.ID).Find(item).Error; err != nil {
- log.Error("creSvc.caseByID(%d) error(%v)", v.ID, err)
- httpCode(c, err)
- return
- }
- if (v.VoteBreak < 0 && (float64(item.VoteBreak)-math.Abs(float64(v.VoteBreak))) < 0) ||
- (v.VoteRule < 0 && (float64(item.VoteRule)-math.Abs(float64(v.VoteRule))) < 0) ||
- (v.VoteDelete < 0 && (float64(item.VoteDelete)-math.Abs(float64(v.VoteDelete))) < 0) {
- httpCode(c, ecode.RequestErr)
- return
- }
- if err = creSvc.DB.Model(&blocked.Case{}).Where("id = ?", v.ID).UpdateColumns(
- map[string]interface{}{
- "vote_rule": gorm.Expr("vote_rule + ?", v.VoteRule),
- "vote_break": gorm.Expr("vote_break + ?", v.VoteBreak),
- "vote_delete": gorm.Expr("vote_delete + ?", v.VoteDelete),
- "oper_id": v.OPID,
- }).Error; err != nil {
- log.Error("creSvc.addCaseVote(%+v) error(%v)", v, err)
- httpCode(c, err)
- return
- }
- log.Info("business_addCaseVote:%+v", v)
- httpCode(c, nil)
- }
- func upCaseStatus(c *bm.Context) {
- var err error
- v := new(blocked.ArgUpStatus)
- if err = c.Bind(v); err != nil {
- return
- }
- if err = creSvc.UpCaseStatus(c, v); err != nil {
- log.Error("creSvc.UpCaseStatus(%+v) error(%v)", v, err)
- httpCode(c, err)
- return
- }
- log.Info("business_upCaseStatus:%+v", v)
- httpCode(c, nil)
- }
- func addCaseType(c *bm.Context) {
- var err error
- v := new(struct {
- IDS []int64 `form:"ids,split" validate:"min=1,max=200"`
- Type int8 `form:"type" validate:"min=0,max=1"`
- OID int64 `form:"op_id" validate:"required"`
- })
- if err = c.Bind(v); err != nil {
- return
- }
- if err = creSvc.DB.Model(blocked.Case{}).Where("id IN(?)", v.IDS).Updates(
- map[string]interface{}{
- "case_type": v.Type,
- "oper_id": v.OID,
- }).Error; err != nil {
- log.Error("creSvc.addCaseType(%+v) error(%v)", v, err)
- httpCode(c, err)
- return
- }
- log.Info("business_addCaseType:%+v", v)
- var multiple []interface{}
- for _, id := range v.IDS {
- single := map[string]interface{}{
- "id": id,
- "oper_id": v.OID,
- "case_type": v.Type,
- }
- multiple = append(multiple, single)
- }
- creSvc.AddNotify(func() {
- creSvc.Search.SearchUpdate(context.TODO(), blocked.BusinessBlockedCase, blocked.TableBlockedCase, multiple)
- })
- httpCode(c, nil)
- }
- func opinions(c *bm.Context) {
- v := new(blocked.ArgOpinionSearch)
- if err := c.Bind(v); err != nil {
- return
- }
- list, pager, err := creSvc.Opinions(c, v)
- if err != nil {
- log.Error("creSvc.Opinions(%+v) error(%v)", v, err)
- httpCode(c, err)
- return
- }
- if len(list) == 0 {
- httpData(c, nil, pager)
- return
- }
- httpData(c, list, pager)
- }
- // opinionByID get opinion by id.
- func opinionByID(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.Opinion{}
- if err = creSvc.ReadDB.Where("id = ?", v.ID).Find(item).Error; err != nil {
- if err != ecode.NothingFound {
- log.Error("creSvc.opinionByID error(%v)", err)
- httpCode(c, err)
- return
- }
- httpData(c, nil, nil)
- return
- }
- if item != nil {
- item.AttrDesc = blocked.AttrDesc[item.Attr]
- item.VoteDesc = blocked.VoteDesc[item.Vote]
- item.VoteStateDesc = blocked.VoteStateDesc[item.State]
- }
- httpData(c, item, nil)
- }
- func delOpinions(c *bm.Context) {
- var err error
- v := new(struct {
- IDS []int64 `form:"ids,split" validate:"min=1,max=20"`
- OID int64 `form:"op_id" validate:"required"`
- Status int8 `form:"status" validate:"min=0,max=1" default:"1"`
- Send int8 `form:"send" validate:"min=0,max=1"`
- })
- if err = c.Bind(v); err != nil {
- return
- }
- v.IDS = model.ArrayUnique(v.IDS)
- if len(v.IDS) == 0 {
- return
- }
- if err = creSvc.DB.Model(&blocked.Opinion{}).Where("id IN (?)", v.IDS).Updates(map[string]interface{}{"state": v.Status, "oper_id": v.OID}).Error; err != nil {
- log.Error("creSvc.delOpinions(%+v) error(%v)", v, err)
- httpCode(c, err)
- return
- }
- log.Info("business_delOpinions:%+v", v)
- var multiple []interface{}
- for _, id := range v.IDS {
- single := map[string]interface{}{
- "id": id,
- "oper_id": v.OID,
- "state": v.Status,
- }
- multiple = append(multiple, single)
- }
- creSvc.AddNotify(func() {
- creSvc.Search.SearchUpdate(context.TODO(), blocked.BusinessBlockedOpinion, blocked.TableBlockedOpinion, multiple)
- })
- if v.Send == blocked.NeedSendMsg {
- items := []*blocked.Opinion{}
- if err = creSvc.ReadDB.Where("id IN (?)", v.IDS).Find(&items).Error; err != nil {
- log.Error("creSvc.opinionByIDs(%s) error(%v)", xstr.JoinInts(v.IDS), err)
- err = nil
- return
- }
- for _, v := range items {
- msg := &blocked.SysMsg{
- Type: blocked.MsgTypeDelOpinion,
- MID: v.MID,
- CID: v.CID,
- CaseContent: v.Content,
- RemoteIP: metadata.String(c, metadata.RemoteIP),
- }
- creSvc.MsgCh <- msg
- }
- }
- httpCode(c, nil)
- }
- func kpis(c *bm.Context) {
- var (
- err error
- start, end time.Time
- order = "id"
- sort = "DESC"
- )
- v := new(blocked.ArgKpiSearch)
- if err = c.Bind(v); err != nil {
- return
- }
- if v.Start == "" {
- if start, err = time.ParseInLocation(model.TimeFormatDay, time.Now().AddDate(0, -1, 0).Format(model.TimeFormatDay), time.Local); err != nil {
- log.Error("time.ParseInLocation(%s) error(%v)", time.Now().AddDate(0, -1, 0).Format(model.TimeFormatDay), err)
- }
- } else {
- if start, err = time.ParseInLocation(model.TimeFormatSec, v.Start, time.Local); err != nil {
- log.Error("time.ParseInLocation(%s) error(%v)", v.Start, err)
- }
- }
- if v.End == "" {
- if end, err = time.ParseInLocation(model.TimeFormatDay, time.Now().Format(model.TimeFormatDay), time.Local); err != nil {
- log.Error("time.ParseInLocation(%s) error(%v)", time.Now().Format(model.TimeFormatDay), err)
- }
- } else {
- if end, err = time.ParseInLocation(model.TimeFormatSec, v.End, time.Local); err != nil {
- log.Error("time.ParseInLocation(%s) error(%v)", v.End, err)
- }
- }
- count := int(0)
- items := []*blocked.KPI{}
- pager := &blocked.Pager{
- Total: count,
- PN: v.PN,
- PS: v.PS,
- Order: order,
- Sort: sort,
- }
- if v.UID > 0 {
- err = creSvc.ReadDB.Where("mid=? and day BETWEEN ? AND ? ", v.UID, start, end).Offset((v.PN - 1) * v.PS).Limit(v.PS).Order(fmt.Sprintf("%s %s", order, sort)).Find(&items).Error
- creSvc.ReadDB.Model(&blocked.KPI{}).Where("mid=? and day BETWEEN ? AND ? ", v.UID, start, end).Count(&count)
- } else {
- err = creSvc.ReadDB.Where("day BETWEEN ? AND ? ", start, end).Offset((v.PN - 1) * v.PS).Limit(v.PS).Order(fmt.Sprintf("%s %s", order, sort)).Find(&items).Error
- creSvc.ReadDB.Model(&blocked.KPI{}).Where("day BETWEEN ? AND ? ", start, end).Count(&count)
- }
- if err != nil {
- if err != ecode.NothingFound {
- log.Error("creSvc.kpis error(%v)", err)
- httpCode(c, err)
- return
- }
- httpData(c, nil, pager)
- return
- }
- pager.Total = count
- httpData(c, items, pager)
- }
- func kpiPoints(c *bm.Context) {
- v := new(blocked.ArgKpiPointSearch)
- if err := c.Bind(v); err != nil {
- return
- }
- list, pager, err := creSvc.KPIPoint(c, v)
- if err != nil {
- log.Error("creSvc.KPIPoint(%+v) error(%v)", v, err)
- httpCode(c, err)
- return
- }
- if len(list) == 0 {
- httpData(c, nil, pager)
- return
- }
- httpData(c, list, pager)
- }
- func kpisEx(c *bm.Context) {
- var (
- err error
- start, end time.Time
- items, list []*blocked.KPI
- )
- v := new(blocked.ArgKpiSearch)
- if err = c.Bind(v); err != nil {
- return
- }
- if v.Start == "" {
- if start, err = time.ParseInLocation(model.TimeFormatDay, time.Now().AddDate(0, -1, 0).Format(model.TimeFormatDay), time.Local); err != nil {
- log.Error("time.ParseInLocation(%s) error(%v)", time.Now().AddDate(0, -1, 0).Format(model.TimeFormatDay), err)
- }
- } else {
- if start, err = time.ParseInLocation(model.TimeFormatSec, v.Start, time.Local); err != nil {
- log.Error("time.ParseInLocation(%s) error(%v)", v.Start, err)
- }
- }
- if v.End == "" {
- if end, err = time.ParseInLocation(model.TimeFormatDay, time.Now().Format(model.TimeFormatDay), time.Local); err != nil {
- log.Error("time.ParseInLocation(%s) error(%v)", time.Now().Format(model.TimeFormatDay), err)
- }
- } else {
- if end, err = time.ParseInLocation(model.TimeFormatSec, v.End, time.Local); err != nil {
- log.Error("time.ParseInLocation(%s) error(%v)", v.End, err)
- }
- }
- ps, pn, loop, count := int64(500), int64(1), int64(0), int64(0)
- if v.UID > 0 {
- err = creSvc.ReadDB.Model(&blocked.KPI{}).Where("mid=? and day BETWEEN ? AND ?", v.UID, start, end).Count(&count).Error
- } else {
- err = creSvc.ReadDB.Model(&blocked.KPI{}).Where("day BETWEEN ? AND ?", start, end).Count(&count).Error
- }
- if err != nil {
- log.Error("creSvc.kpis count error(%v)", err)
- httpCode(c, err)
- return
- }
- if count < 1 {
- return
- }
- items = make([]*blocked.KPI, 0, count)
- list = make([]*blocked.KPI, 0, count)
- loop = (count / ps) + 1
- for pn = 1; pn <= loop; pn++ {
- if v.UID > 0 {
- err = creSvc.ReadDB.Where("mid=? and day BETWEEN ? AND ?", v.UID, start, end).Offset((pn - 1) * ps).Limit(ps).Order("id DESC").Find(&items).Error
- } else {
- err = creSvc.ReadDB.Where("day BETWEEN ? AND ?", start, end).Offset((pn - 1) * ps).Limit(ps).Order("id DESC").Find(&items).Error
- }
- if err != nil {
- log.Error("creSvc.kpis error(%v)", err)
- httpCode(c, err)
- return
- }
- list = append(list, items...)
- }
- sort.Slice(list, func(i int, j int) bool {
- return list[i].ID < list[j].ID
- })
- re, err := blocked.DealKPI(list)
- if err != nil {
- httpCode(c, err)
- return
- }
- outBuf(c, creSvc.FormatCSV(re), fmt.Sprintf("%s-%s", time.Now().Format(model.TimeFormatDay), "kpi"))
- }
- func caseConf(c *bm.Context) {
- cc, err := creSvc.CaseConf(c)
- if err != nil {
- log.Error("caseConfig error(%v)", err)
- httpCode(c, err)
- return
- }
- httpData(c, cc, nil)
- }
- func setCaseConf(c *bm.Context) {
- var err error
- v := new(blocked.ArgCaseConf)
- if err = c.Bind(v); err != nil {
- return
- }
- if err = creSvc.SetCaseConf(c, v); err != nil {
- log.Error("creSvc.SetCaseConf(%+v) error(%v)", v, err)
- httpCode(c, err)
- return
- }
- log.Info("business_setCaseConf:%+v", v)
- httpCode(c, nil)
- }
- func autoCaseConfig(c *bm.Context) {
- var err error
- v := new(struct {
- Platform int8 `form:"platform"`
- })
- if err = c.Bind(v); err != nil {
- return
- }
- item := &blocked.AutoCase{}
- if err = creSvc.ReadDB.Where("platform=?", v.Platform).Find(&item).Error; err != nil {
- if err != ecode.NothingFound {
- log.Error("http.autoCaseConfig error(%v)", err)
- httpCode(c, err)
- return
- }
- httpData(c, nil, nil)
- return
- }
- if item.ReasonStr != "" {
- if item.Reasons, err = xstr.SplitInts(item.ReasonStr); err != nil {
- log.Error("xstr.SplitInts(%s) err(%v)", item.ReasonStr, err)
- httpCode(c, err)
- return
- }
- }
- httpData(c, item, nil)
- }
- func setAutoCaseConfig(c *bm.Context) {
- var err error
- v := new(blocked.ArgAutoCaseConf)
- if err = c.Bind(v); err != nil {
- return
- }
- item := &blocked.AutoCase{}
- creSvc.ReadDB.Where("id = ?", v.ID).Find(item)
- var autoCase *blocked.AutoCase
- if item != nil && item.ID != 0 {
- autoCase = &blocked.AutoCase{
- ID: item.ID,
- Platform: item.Platform,
- Likes: v.Likes,
- ReasonStr: xstr.JoinInts(v.Reasons),
- ReportScore: v.ReportScore,
- OPID: v.OID,
- CTime: item.CTime,
- }
- } else {
- autoCase = &blocked.AutoCase{
- Platform: v.Platform,
- Likes: v.Likes,
- ReasonStr: xstr.JoinInts(v.Reasons),
- ReportScore: v.ReportScore,
- OPID: v.OID,
- }
- }
- if err = creSvc.DB.Save(&autoCase).Error; err != nil {
- log.Error("http.setAutoCaseConfig error(%v)", err)
- httpCode(c, err)
- return
- }
- log.Info("business_setAutoCaseConfig:%+v", v)
- httpCode(c, nil)
- }
- func votenumConf(c *bm.Context) {
- content, err := creSvc.VotenumConf(c)
- if err != nil {
- log.Error("caseConfig error(%v)", err)
- httpCode(c, err)
- return
- }
- vc := blocked.VoteNum{}
- if err = json.Unmarshal([]byte(content), &vc); err != nil {
- log.Error("votenumConf_Unmarshal err:%+v", err)
- httpCode(c, err)
- return
- }
- httpData(c, vc, nil)
- }
- func setVotenumConf(c *bm.Context) {
- var err error
- v := new(blocked.ArgVoteNum)
- if err = c.Bind(v); err != nil {
- return
- }
- if err = creSvc.SetVotenumConf(c, v); err != nil {
- log.Error("creSvc.setVotenumConf(%+v) error(%v)", v, err)
- httpCode(c, err)
- return
- }
- log.Info("business_SetVotenumConf:%+v", v)
- httpCode(c, nil)
- }
- func webHook(c *bm.Context) {
- body, err := ioutil.ReadAll(c.Request.Body)
- if err != nil {
- log.Error("read body fail error(%v)", err)
- httpCode(c, ecode.RequestErr)
- return
- }
- if err = creSvc.WebHook(c, body); err != nil {
- log.Error("creSvc.WebHook error(%v)", err)
- httpCode(c, err)
- return
- }
- httpCode(c, nil)
- }
|