Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"blocked_test.go",
"case_test.go",
"extra_func_test.go",
"jury_test.go",
"reply_test.go",
"service_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/credit/conf:go_default_library",
"//app/job/main/credit/model:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"blocked.go",
"case.go",
"credit.go",
"extra_func.go",
"jury.go",
"kpi.go",
"reply.go",
"service.go",
],
importpath = "go-common/app/job/main/credit/service",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/credit/conf:go_default_library",
"//app/job/main/credit/dao:go_default_library",
"//app/job/main/credit/model:go_default_library",
"//app/service/main/archive/api/gorpc:go_default_library",
"//app/service/main/archive/model/archive:go_default_library",
"//app/service/main/member/api/gorpc:go_default_library",
"//app/service/main/member/model/block:go_default_library",
"//library/log:go_default_library",
"//library/queue/databus:go_default_library",
"//library/time:go_default_library",
"//library/xstr:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,106 @@
package service
import (
"context"
"encoding/json"
"strconv"
"time"
"go-common/app/job/main/credit/model"
blkmdl "go-common/app/service/main/member/model/block"
"go-common/library/log"
xtime "go-common/library/time"
)
// InvalidJury invalid juryer.
func (s *Service) InvalidJury(c context.Context, nwMsg []byte, oldMsg []byte) (err error) {
mr := &model.BlockedInfo{}
if err = json.Unmarshal(nwMsg, mr); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
return
}
if err = s.dao.InvalidJury(c, model.JuryBlocked, mr.UID); err != nil {
log.Error("s.dao.InvalidJury(%d %d) error(%v)", model.JuryBlocked, mr.UID, err)
}
return
}
// UnBlockAccount unblock account.
func (s *Service) UnBlockAccount(c context.Context, nwMsg []byte, oldMsg []byte) (err error) {
nMR := &model.BlockedInfo{}
if err = json.Unmarshal(nwMsg, nMR); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
return
}
oMR := &model.BlockedInfo{}
if err = json.Unmarshal(oldMsg, oMR); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(oldMsg), err)
return
}
if int8(oMR.Status) != model.StatusOpen {
return
}
if int8(nMR.Status) != model.StatusClose {
return
}
var id int64
if id, err = s.dao.BlockedInfoID(c, nMR.UID); err != nil {
log.Error("s.dao.BlockedInfoID(%+v) error(%v)", oMR, err)
return
}
if id != nMR.ID {
log.Warn("databus id(%d) do uid(%d) unblocked info(%d) not right!", nMR.ID, nMR.UID, id)
return
}
if err = s.dao.UnBlockAccount(c, oMR); err != nil {
log.Error("s.dao.UnBlockAccount(%+v) error(%v)", oMR, err)
}
return
}
// CheckBlock check user block state
func (s *Service) CheckBlock(c context.Context, mid int64) (ok bool, err error) {
var block *blkmdl.RPCResInfo
if block, err = s.memRPC.BlockInfo(c, &blkmdl.RPCArgInfo{MID: mid}); err != nil {
log.Error("s.memRPC.BlockInfo(%d) error(%+v)", err)
return
}
status := int8(block.BlockStatus)
if status == model.BlockStatusOn {
log.Warn("mid(%d) in blocked", mid)
return
}
if status == model.BlockStatusForever {
log.Warn("mid(%d) in blocked forever", mid)
return
}
ok = true
return
}
// NotifyBlockAnswer notify block answer status
func (s *Service) NotifyBlockAnswer(c context.Context, nwMsg []byte) (err error) {
mr := &model.BlockLabourAnswerLog{}
if err = json.Unmarshal(nwMsg, mr); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
return
}
if mr.Score < 100 {
log.Warn("mid(%d) answer score(%d) lt 100", mr.Score, mr.Score)
return
}
var ts time.Time
if ts, err = time.ParseInLocation(model.TimeFormatSec, mr.CTime, time.Local); err != nil {
log.Error("time.ParseInLocation(%s) error(%v)", mr.CTime, err)
return
}
key := strconv.FormatInt(mr.MID, 10)
msg := &model.LabourAnswer{MID: mr.MID, MTime: xtime.Time(ts.Unix())}
if err = s.labourSub.Send(c, key, msg); err != nil {
log.Error("PubLabour.Pub(%s, %+v) error (%v)", key, msg, err)
}
if err = s.dao.DelAnswerStateCache(c, mr.MID); err != nil {
log.Error("DelAnswerStateCache(%d) error (%v)", mr.MID, err)
}
return
}

View File

@@ -0,0 +1,20 @@
package service
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_CheckBlock(t *testing.T) {
var (
c = context.TODO()
)
mid := int64(304)
Convey("should return err be nil", t, func() {
ok, err := s.CheckBlock(c, mid)
So(err, ShouldBeNil)
So(ok, ShouldNotBeNil)
})
}

View File

@@ -0,0 +1,265 @@
package service
import (
"context"
"encoding/json"
"time"
"go-common/app/job/main/credit/model"
"go-common/library/log"
xtime "go-common/library/time"
"go-common/library/xstr"
)
// loadCase load grant case to list.
func (s *Service) loadCase() (err error) {
c := context.TODO()
if s.c.Judge.CaseLoadSwitch != model.StateCaseLoadOpen {
log.Warn("case load switch off!")
return
}
count, err := s.dao.TotalGrantCase(c)
if err != nil {
log.Error("s.dao.CaseGrantCount error(%v)", err)
return
}
loadCount := s.c.Judge.CaseLoadMax - count
if loadCount <= 0 {
log.Info("load case full caseLoadMax(%d) listCount(%d)", s.c.Judge.CaseLoadMax, count)
return
}
log.Info("need load count(%d) on already que len(%d) and max(%d)", loadCount, count, s.c.Judge.CaseLoadMax)
mcases, err := s.dao.Grantcase(c, loadCount)
if err != nil {
log.Error("s.dao.Grantcase(%d) error(%v)", loadCount, err)
return
}
if len(mcases) == 0 {
log.Warn("granting case is zero!")
return
}
var cids []int64
for cid := range mcases {
cids = append(cids, cid)
}
now := time.Now()
stime := xtime.Time(now.Unix())
etime := xtime.Time(now.Add(time.Duration(s.c.Judge.CaseGiveHours) * time.Hour).Unix())
if err = s.dao.UpGrantCase(c, cids, stime, etime); err != nil {
log.Error("s.dao.UpGrantCase(%s) error(%v)", xstr.JoinInts(cids), err)
return
}
for _, v := range mcases {
v.Stime = stime
v.Etime = etime
}
if err = s.dao.SetGrantCase(c, mcases); err != nil {
log.Error("s.dao.SetMIDCaseGrant(%s) error(%v)", xstr.JoinInts(cids), err)
}
log.Info("load cases(%s) to queue on start_time(%v) and end_time(%v)", xstr.JoinInts(cids), stime, etime)
return
}
// DealCaseApplyReason deal with case apply reason.
func (s *Service) DealCaseApplyReason(c context.Context, nwMsg []byte) (err error) {
mr := &model.CaseApplyModifyLog{}
if err = json.Unmarshal(nwMsg, mr); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
return
}
var (
aReasons []int64
cas []*model.CaseApplyModifyLog
)
if aReasons, err = s.dao.CaseApplyReasons(c, mr.CID); err != nil {
log.Error("s.dao.CaseApplyReasons(%d) error(%v)", mr.CID, err)
return
}
if len(aReasons) == 0 {
log.Warn("apply reason(%s) is nil", xstr.JoinInts(aReasons))
return
}
if cas, err = s.dao.CaseApplyReasonNum(c, mr.CID, aReasons); err != nil {
log.Error("s.dao.CaseApplyReasonNum(%d,%s) error(%v)", mr.CID, xstr.JoinInts(aReasons), err)
return
}
var (
max int
reason int8
)
for _, v := range cas {
if v.Num > max {
max = v.Num
reason = v.AReason
}
}
standard := int(s.c.Judge.CaseVoteMax * int64(s.c.Judge.CaseVoteMaxPercent) / 100)
if max < standard {
log.Warn("apply reason num(%d) not enough standard(%d)", max, standard)
return
}
if reason == mr.OReason {
log.Warn("max reason(%d) eq orgin reason(%d)", reason, mr.OReason)
return
}
var effect int64
if effect, err = s.dao.UpBlockedCaseReason(c, mr.CID, reason); err != nil {
log.Error("s.dao.UpBlockedCaseReason(%d,%d) error(%v)", mr.CID, reason, err)
return
}
if effect <= 0 {
log.Warn("update case_id(%d) and reason(%d) notIdempotent", mr.CID, reason)
return
}
if model.ReasonToFreeze(reason) {
if err = s.dao.UpBlockedCaseStatus(c, mr.CID, model.CaseStatusFreeze); err != nil {
log.Error("s.dao.UpBlockedCaseStatus(%d,%d) error(%v)", mr.CID, model.CaseStatusFreeze, err)
return
}
}
if err = s.dao.AddBlockedCaseModifyLog(c, mr.CID, mr.AType, mr.OReason, mr.AReason); err != nil {
log.Error("s.dao.AddBlockedCaseModifyLog(%d,%d,%d,%d) error(%v)", mr.CID, mr.AType, mr.OReason, mr.AReason, err)
}
return
}
// GrantCase push case in list.
func (s *Service) GrantCase(c context.Context, nwMsg []byte, oldMsg []byte) (err error) {
mr := &model.Case{}
if err = json.Unmarshal(nwMsg, mr); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
return
}
if mr.Status != model.CaseStatusGranting {
return
}
stime, err := time.ParseInLocation(time.RFC3339, mr.Stime, time.Local)
if err != nil {
stime, err = time.ParseInLocation("2006-01-02 15:04:05", mr.Stime, time.Local)
if err != nil {
log.Error("time.ParseInLocation(%s) error(%v)", mr.Stime, err)
return
}
err = nil
}
etime, err := time.ParseInLocation(time.RFC3339, mr.Etime, time.Local)
if err != nil {
etime, err = time.ParseInLocation("2006-01-02 15:04:05", mr.Etime, time.Local)
if err != nil {
log.Error("time.ParseInLocation(%s) error(%v)", mr.Etime, err)
return
}
err = nil
}
simCase := &model.SimCase{
ID: mr.ID,
Mid: mr.Mid,
VoteRule: mr.Agree,
VoteBreak: mr.Against,
VoteDelete: mr.VoteDelete,
CaseType: mr.CaseType,
Stime: xtime.Time(stime.Unix()),
Etime: xtime.Time(etime.Unix()),
}
mcases := make(map[int64]*model.SimCase)
mcases[mr.ID] = simCase
if err = s.dao.SetGrantCase(c, mcases); err != nil {
log.Error("s.dao.SetMIDCaseGrant(%+v) error(%v)", mr, err)
}
return
}
// DelGrantCase del case in list.
func (s *Service) DelGrantCase(c context.Context, nwMsg []byte, oldMsg []byte) (err error) {
mr := &model.Case{}
if err = json.Unmarshal(nwMsg, mr); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
return
}
if mr.Status == model.CaseStatusDealing && mr.CaseType == model.JudeCaseTypePrivate {
return
}
if mr.Status == model.CaseStatusGranting || mr.Status == model.CaseStatusDealed || mr.Status == model.CaseStatusRestart {
return
}
cids := []int64{mr.ID}
// 删除冻结和停止发放中的cid
if err = s.dao.DelGrantCase(c, cids); err != nil {
log.Error("s.dao.SetMIDCaseGrant(%d) error(%v)", mr.ID, err)
}
log.Info("cid(%d) status(%d) remove hash list on start_time(%s) and end_time(%s)", mr.ID, mr.Status, mr.Stime, mr.Etime)
return
}
// DelCaseInfoCache del case info cache.
func (s *Service) DelCaseInfoCache(c context.Context, nwMsg []byte) (err error) {
mr := &model.Case{}
if err = json.Unmarshal(nwMsg, mr); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
return
}
if err = s.dao.DelCaseInfoCache(c, mr.ID); err != nil {
log.Error("s.dao.DelCaseInfoCache(%d) error(%v)", mr.ID, err)
return
}
log.Info("cid(%d) del case_info cache", mr.ID)
return
}
// DelVoteCaseCache del vote case cache.
func (s *Service) DelVoteCaseCache(c context.Context, nwMsg []byte) (err error) {
mr := &model.BLogCaseVote{}
if err = json.Unmarshal(nwMsg, mr); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
return
}
if err = s.dao.DelVoteCaseCache(c, mr.MID, mr.CID); err != nil {
log.Error("s.dao.DelCaseInfoCache(%d,%d) error(%v)", mr.MID, mr.CID, err)
return
}
log.Info("mid(%d) cid(%d) del vote_case cache", mr.MID, mr.CID)
if err = s.dao.DelJuryInfoCache(c, mr.MID); err != nil {
log.Error("s.dao.DelJuryInfoCache(%d) error(%v)", mr.MID, err)
}
log.Info("mid(%d) del jury cache", mr.MID, mr.CID)
if err = s.dao.DelCaseVoteTopCache(c, mr.MID); err != nil {
log.Error("s.dao.DelCaseVoteTopCache(%d) error(%v)", mr.MID, err)
}
log.Info("mid(%d) del vote top cache", mr.MID)
return
}
// loadDealWrongCase deal wrong case status in cache .
func (s *Service) loadDealWrongCase() (err error) {
var (
cids, wcids []int64
mcids map[int64]int8
c = context.TODO()
)
if cids, err = s.dao.GrantCases(c); err != nil {
log.Error("s.dao.GrantCases error(%+v)", err)
return
}
if len(cids) == 0 {
log.Warn("deal wrong (granting case is zero!)")
return
}
if mcids, err = s.dao.CasesStatus(c, cids); err != nil {
log.Error("s.dao.CasesStatus(%s) error(%+v)", xstr.JoinInts(cids), err)
return
}
for cid, status := range mcids {
if status != model.CaseStatusGranting {
wcids = append(wcids, cid)
}
}
if len(wcids) == 0 {
return
}
if err = s.dao.DelGrantCase(c, wcids); err != nil {
log.Error("s.dao.DelGrantCase(%s) error(%+v)", xstr.JoinInts(wcids), err)
return
}
log.Info("load del wrong cases(%s) from queue", xstr.JoinInts(wcids))
return
}

View File

@@ -0,0 +1,59 @@
package service
import (
"context"
"encoding/json"
"testing"
"go-common/app/job/main/credit/model"
. "github.com/smartystreets/goconvey/convey"
)
func Test_DelCaseInfoCache(t *testing.T) {
var (
c = context.TODO()
mr = &model.Case{}
)
mr.ID = 304
mr.Status = model.CaseStatusDealing
mr.JudgeType = model.JudgeTypeViolate
mr.OriginTitle = "304"
mr.Mid = 1
mr.OriginURL = "http:304"
mr.OriginContent = "304cont"
mr.OriginType = 4
mr.Operator = "lgs"
mr.Status = 4
mr.ReasonType = 3
mr.RelationID = "10741-4052410"
mr.PunishResult = 2
bb, _ := json.Marshal(mr)
Convey("should return err be nil", t, func() {
err := s.DelCaseInfoCache(c, bb)
So(err, ShouldBeNil)
})
}
func Test_UpdateVoteCount(t *testing.T) {
var (
c = context.TODO()
mr = &model.Case{}
)
mr.ID = 304
mr.Status = model.CaseStatusDealing
mr.JudgeType = model.JudgeTypeViolate
mr.OriginTitle = "304"
mr.Mid = 1
mr.OriginURL = "http:304"
mr.OriginContent = "304cont"
mr.OriginType = 4
mr.Operator = "lgs"
mr.Status = 4
mr.ReasonType = 3
mr.RelationID = "10741-4052410"
mr.PunishResult = 2
Convey("should return err be nil", t, func() {
s.UpdateVoteCount(c, mr)
})
}

View File

@@ -0,0 +1,96 @@
package service
import (
"context"
"encoding/json"
"time"
"go-common/app/job/main/credit/model"
"go-common/library/log"
)
const (
_blockedJuryTable = "blocked_jury"
_blockedInfoTable = "blocked_info"
_blockedCaseTable = "blocked_case"
_voteOpinion = "blocked_opinion"
_blockedKpiTable = "blocked_kpi"
_blockedPublishTable = "blocked_publish"
_blockedVoteCaseTable = "blocked_case_vote"
_blockedCaseApplyLogTable = "blocked_case_apply_log"
_blockedLabourAnswerLog = "blocked_labour_answer_log"
_retry = 3
_retrySleep = time.Second * 1
)
func (s *Service) creditConsumer() {
var err error
for res := range s.credbSub.Messages() {
mu := &model.Message{}
if err = json.Unmarshal(res.Value, mu); err != nil {
log.Error("credit-job,json.Unmarshal (%v) error(%v)", string(res.Value), err)
continue
}
for i := 0; ; i++ {
err = s.dealCredit(mu)
if err != nil {
log.Error("s.flush error(%v)", err)
time.Sleep(_retrySleep)
if i > _retry && s.c.Env == "prod" {
s.dao.Sms(context.TODO(), s.c.Sms.Phone, s.c.Sms.Token, "credit-job update cache fail for 5 times")
i = 0
}
continue
}
break
}
if err = res.Commit(); err != nil {
log.Error("databus.Commit err(%v)", err)
}
log.Info("subproc key:%v,topic: %v, part:%v offset:%v,message %s,", res.Key, res.Topic, res.Partition, res.Offset, res.Value)
}
}
// dealAction deal databus action
func (s *Service) dealCredit(mu *model.Message) (err error) {
switch mu.Table {
case _blockedCaseTable:
if mu.Action == "insert" {
s.RegReply(context.TODO(), mu.Table, mu.New, mu.Old)
}
err = s.Judge(context.TODO(), mu.New, mu.Old)
s.GrantCase(context.TODO(), mu.New, mu.Old)
s.DelGrantCase(context.TODO(), mu.New, mu.Old)
s.DelCaseInfoCache(context.TODO(), mu.New)
case _blockedInfoTable:
if mu.Action == "insert" {
s.RegReply(context.TODO(), mu.Table, mu.New, mu.Old)
s.InvalidJury(context.TODO(), mu.New, mu.Old)
}
if mu.Action == "update" {
s.UnBlockAccount(context.TODO(), mu.New, mu.Old)
}
s.UpdateCache(context.TODO(), mu.New)
case _blockedKpiTable:
if mu.Action == "insert" {
s.KPIReward(context.TODO(), mu.New, mu.Old)
}
case _voteOpinion:
s.DeleteIdx(context.TODO(), mu.New)
case _blockedPublishTable:
s.RegReply(context.TODO(), mu.Table, mu.New, mu.Old)
case _blockedVoteCaseTable:
s.DelVoteCaseCache(context.TODO(), mu.New)
case _blockedLabourAnswerLog:
if mu.Action == "insert" {
s.NotifyBlockAnswer(context.TODO(), mu.New)
}
case _blockedCaseApplyLogTable:
if mu.Action == "insert" {
s.DealCaseApplyReason(context.TODO(), mu.New)
}
case _blockedJuryTable:
s.DelJuryInfoCache(context.TODO(), mu.New)
}
return
}

View File

@@ -0,0 +1,154 @@
package service
import (
"context"
"encoding/json"
"strings"
"time"
"go-common/app/job/main/credit/model"
"go-common/library/log"
)
// UpdateVoteCount update user vote count.
func (s *Service) UpdateVoteCount(c context.Context, mr *model.Case) {
cvs, err := s.dao.CaseVotesCID(c, mr.ID)
if err != nil {
log.Error("s.dao.CaseVotesCID(%d)", mr.ID)
return
}
for _, cv := range cvs {
switch {
case mr.JudgeType == model.JudgeTypeViolate:
if cv.Vote == model.VoteTypeDelete || cv.Vote == model.VoteTypeViolate {
if err = s.dao.UpdateVoteRight(c, cv.MID); err != nil {
log.Error("s.dao.UpdateVoteRight(%d)", cv.MID)
}
} else if cv.Vote == model.VoteTypeLegal {
if err = s.dao.UpdateVoteTotal(c, cv.MID); err != nil {
log.Error("s.dao.UpdateVoteTotal(%d)", cv.MID)
}
}
case mr.JudgeType == model.JudgeTypeLegal:
if cv.Vote == model.VoteTypeLegal {
if err = s.dao.UpdateVoteRight(c, cv.MID); err != nil {
log.Error("s.dao.UpdateVoteRight(%d)", cv.MID)
}
} else if cv.Vote == model.VoteTypeViolate || cv.Vote == model.VoteTypeDelete {
if err = s.dao.UpdateVoteTotal(c, cv.MID); err != nil {
log.Error("s.dao.UpdateVoteTotal(%d)", cv.MID)
}
}
}
time.Sleep(100 * time.Millisecond)
}
}
// UpdateCache update blocked info cache.
func (s *Service) UpdateCache(c context.Context, nwMsg []byte) (err error) {
mr := &model.BlockedInfo{}
if err = json.Unmarshal(nwMsg, mr); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
return
}
if mr.PublishStatus == int64(model.PublishClose) || mr.Status == int64(model.StatusClose) {
s.dao.DelBlockedInfoIdx(c, mr)
} else {
s.dao.AddBlockInfoIdx(c, mr)
}
return
}
// DelOrigin delorigin content.
func (s *Service) DelOrigin(c context.Context, cs *model.Case) {
switch int8(cs.OriginType) {
case model.OriginReply:
if cs.RelationID != "" {
args := strings.Split(cs.RelationID, "-")
if len(args) != 3 {
return
}
s.dao.DelReply(c, args[0], args[1], args[2])
}
case model.OriginTag:
if cs.RelationID != "" {
args := strings.Split(cs.RelationID, "-")
if len(args) != 2 {
return
}
s.dao.DelTag(c, args[0], args[1])
}
case model.OriginDM:
if cs.RelationID != "" {
args := strings.Split(cs.RelationID, "-")
if len(args) != 4 {
return
}
s.dao.ReportDM(c, args[2], args[1], model.DMNotifyDel)
}
}
}
// DeleteIdx del cache idx.
func (s *Service) DeleteIdx(c context.Context, nwMsg []byte) (err error) {
var opinion *model.Opinion
if err = json.Unmarshal(nwMsg, &opinion); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
return
}
s.dao.DelOpinionCache(c, opinion.Vid)
s.dao.DelCaseIdx(c, opinion.Cid)
s.dao.DelVoteIdx(c, opinion.Cid)
return
}
// jugeBlockedUser
func (s *Service) jugeBlockedUser(c context.Context, mid int64, timeStr string, bType int8) (ok bool, count int64, err error) {
var ts, nts time.Time
if ts, err = time.ParseInLocation(model.TimeFormatSec, timeStr, time.Local); err != nil {
log.Error("time.ParseInLocation(%s) error(%v)", timeStr, err)
return
}
switch bType {
case model.DealTimeTypeNone:
nts = ts
case model.DealTimeTypeDay:
nts = ts.AddDate(0, 0, -1)
case model.DealTimeTypeYear:
nts = ts.AddDate(-1, 0, 0)
}
if count, err = s.dao.CountBlocked(c, mid, nts); err != nil {
log.Error("s.dao.CountBlocked(%d,%s) error(%v)", mid, nts, err)
return
}
if count == 0 {
ok = true
}
return
}
// DelJuryInfoCache .
func (s *Service) DelJuryInfoCache(c context.Context, nwMsg []byte) (err error) {
mr := &model.Jury{}
if err = json.Unmarshal(nwMsg, mr); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
return
}
if err = s.dao.DelJuryInfoCache(c, mr.Mid); err != nil {
log.Error("s.dao.DelJuryInfoCache(%d) error(%v)", mr.Mid, err)
}
return
}
// DelCaseVoteTopCache .
func (s *Service) DelCaseVoteTopCache(c context.Context, nwMsg []byte) (err error) {
mr := &model.CaseVote{}
if err = json.Unmarshal(nwMsg, mr); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
return
}
if err = s.dao.DelCaseVoteTopCache(c, mr.MID); err != nil {
log.Error("s.dao.DelCaseVoteTopCache(%d) error(%v)", mr.MID, err)
}
return
}

View File

@@ -0,0 +1,50 @@
package service
import (
"context"
"encoding/json"
"testing"
"go-common/app/job/main/credit/model"
. "github.com/smartystreets/goconvey/convey"
)
func Test_DelOrigin(t *testing.T) {
var (
c = context.TODO()
mr = &model.Case{}
)
mr.OriginType = int64(model.OriginTag)
mr.RelationID = "57-4052400"
Convey("should return err be nil", t, func() {
s.DelOrigin(c, mr)
})
}
func Test_DelOrigin2(t *testing.T) {
var (
c = context.TODO()
mr = &model.Case{}
)
mr.OriginType = int64(model.OriginReply)
mr.RelationID = "111002543-3-400"
Convey("should return err be nil", t, func() {
s.DelOrigin(c, mr)
})
}
func TestServiceDelJuryInfoCache(t *testing.T) {
var (
c = context.TODO()
mr = &model.Jury{}
)
mr.ID = 304
mr.Status = model.CaseStatusDealing
mr.Mid = 1
Convey("should return err be nil", t, func() {
b, _ := json.Marshal(&mr)
err := s.DelJuryInfoCache(c, b)
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,193 @@
package service
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"go-common/app/job/main/credit/model"
"go-common/library/log"
)
const (
_msgTitle = "风纪委员任期结束"
_msgContext = "您的风纪委员资格已到期,任职期间的总结报告已生成。感谢您对社区工作的大力支持!#{点击查看}{" + "\"http://www.bilibili.com/judgement/\"" + "}"
_appealTitle = "账号违规处理通知"
_appealContent = `抱歉,你的账号因"在%s中%s",现已进行%s处理账号解封需要满足以下两个条件:1.账号封禁时间已满。2.完成解封答题( #{点击进入解封答题}{"http://www.bilibili.com/blackroom/releaseexame.html"} 全部完成后解封。封禁期间将无法投稿、发送及回复消息无法发布评论、弹幕无法对他人评论进行回复、赞踩操作无法进行投币、编辑标签、添加关注、添加收藏操作。如对处罚有异议可在7日内进行申诉
#{点击申诉}{"http://www.bilibili.com/judgement/appeal?bid=%d"} 。请遵守社区规范,共同维护良好的社区氛围!`
)
// Judge judge case.
func (s *Service) Judge(c context.Context, nwMsg []byte, oldMsg []byte) (err error) {
var (
sum int64
mr = &model.Case{}
bc model.Case
judge int64
status int64
yratio int64
nratio int64
judgeRadio = s.c.Judge.JudgeRadio
voteMin = s.c.Judge.CaseVoteMin
)
if err = json.Unmarshal(nwMsg, mr); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
return
}
if mr.CaseType == model.JudeCaseTypePublic {
return
}
if mr.Status != model.CaseStatusDealing {
return
}
if bc, err = s.dao.CaseByID(c, mr.ID); err != nil {
log.Error("s.dao.CaseByID error(%v)", err)
return
}
if bc.Status == model.CaseStatusDealed || bc.Status == model.CaseStatusUndealed {
return
}
sum = mr.Against + mr.Agree + mr.VoteDelete
if voteMin <= 0 {
log.Error("CaseVoteMin(%d) error(%v)", voteMin, err)
return
}
if sum < voteMin {
status = model.CaseStatusUndealed
judge = model.JudgeTypeUndeal
s.dao.UpdateCase(c, status, judge, mr.ID)
return
}
yratio = mr.Agree * 100 / sum
nratio = (mr.Against + mr.VoteDelete) * 100 / sum
if judgeRadio <= 50 {
log.Error("CaseJudgeRadio(%d) error(%v)", judgeRadio, err)
return
}
if yratio >= judgeRadio {
status = model.CaseStatusDealed
judge = model.JudgeTypeLegal
} else if nratio >= judgeRadio {
status = model.CaseStatusDealed
judge = model.JudgeTypeViolate
} else {
status = model.CaseStatusUndealed
judge = model.JudgeTypeUndeal
}
s.dao.UpdateCase(c, status, judge, mr.ID)
mr.Status = status
mr.JudgeType = judge
if status == model.CaseStatusDealed {
s.BlockUser(c, mr)
s.UpdateVoteCount(c, mr)
s.dao.DelGrantCase(c, []int64{mr.ID})
} else {
if mr.OriginType == int64(model.OriginDM) {
if mr.RelationID != "" {
args := strings.Split(mr.RelationID, "-")
if len(args) != 4 {
return
}
s.dao.ReportDM(c, args[2], args[1], model.DMNotifyNotDel)
}
}
}
return
}
// BlockUser add user block.
func (s *Service) BlockUser(c context.Context, mr *model.Case) (err error) {
if mr.JudgeType == model.JudgeTypeViolate {
s.DelOrigin(c, mr)
if mr.Against <= mr.VoteDelete+mr.Agree {
err = s.dealMoralCase(c, mr)
return
}
var (
ok bool
punishType int64
)
forever, days := mr.BlockDays()
if forever != model.InBlockedForever {
punishType = int64(model.PunishTypeBlock)
} else {
punishType = int64(model.PunishTypeForever)
}
r := &model.BlockedInfo{
UID: mr.Mid,
PunishType: punishType,
BlockedType: model.PunishJury,
OperatorName: mr.Operator,
CaseID: mr.ID,
Origin: mr.Origin,
OPID: mr.OPID,
BlockedForever: forever,
BlockedDays: days,
}
r.OriginContentModify = r.OriginContent
if ok, err = s.CheckBlock(c, mr.Mid); err != nil || !ok {
return
}
if mr.BusinessTime != model.DefaultTime {
ok, _, err = s.jugeBlockedUser(c, mr.Mid, mr.BusinessTime, model.DealTimeTypeNone)
if err != nil {
log.Error("s.jugeBlockedUser(%d,%s,%d) error(%v)", mr.Mid, mr.BusinessTime, model.DealTimeTypeNone, err)
return
}
} else {
ok, _, err = s.jugeBlockedUser(c, mr.Mid, mr.Ctime, model.DealTimeTypeDay)
if err != nil {
log.Error("s.jugeBlockedUser(%d,%s,%d) error(%v)", mr.Mid, mr.Ctime, model.DealTimeTypeDay, err)
return
}
}
if ok {
var id int64
id, err = s.dao.AddBlockInfo(c, r, time.Now())
if err != nil {
log.Error("s.dao.AddBlockInfo error(%v)", err)
return
}
if err = s.dao.BlockAccount(c, r); err != nil {
log.Error("s.dao.BlockAccount(%+v) error(%v)", r, err)
return
}
s.dao.SendMsg(c, mr.Mid, _appealTitle, fmt.Sprintf(_appealContent, model.OriginTypeDesc(int8(mr.OriginType)), model.ReasonTypeDesc(int8(mr.ReasonType)), model.BlockedDayDesc(int8(mr.BlockedDay)), id))
}
} else if mr.JudgeType == model.JudgeTypeLegal {
if mr.JudgeType == int64(model.OriginDM) {
s.dao.UpdatePunishResult(c, mr.ID, model.BlockNone)
if mr.RelationID != "" {
args := strings.Split(mr.RelationID, "-")
if len(args) != 4 {
return
}
s.dao.ReportDM(c, args[2], args[1], model.DMNotifyNotDel)
}
}
}
return
}
func (s *Service) dealMoralCase(c context.Context, mr *model.Case) (err error) {
if err = s.dao.UpdatePunishResult(c, mr.ID, model.BlockOnlyDel); err != nil {
log.Error("UpdatePunishResult error(%v)", err)
return
}
title, content := model.OriginMsgContent(mr.OriginTitle, mr.OriginURL, mr.OriginContent, int8(mr.OriginType))
for i := 0; i <= 5; i++ {
if err := s.dao.AddMoral(c, mr.Mid, model.DefealtMoralVal, model.OrginMoralType(int8(mr.OriginType)), model.BUSSINESS, model.ReasonTypeDesc(int8(mr.ReasonType)), model.MoralRemark, ""); err != nil {
continue
}
break
}
for i := 0; i <= 5; i++ {
if err := s.dao.SendMsg(c, mr.Mid, title, content); err != nil {
continue
}
break
}
return
}

View File

@@ -0,0 +1,83 @@
package service
import (
"context"
"encoding/json"
"testing"
"go-common/app/job/main/credit/model"
. "github.com/smartystreets/goconvey/convey"
)
func Test_BlockUser(t *testing.T) {
var (
c = context.TODO()
mr = &model.Case{}
)
mr.ID = 304
mr.JudgeType = model.JudgeTypeViolate
mr.OriginTitle = "304"
mr.Mid = 1
mr.OriginURL = "http:304"
mr.OriginContent = "304cont"
mr.OriginType = 4
mr.Operator = "lgs"
mr.Status = 4
mr.ReasonType = 3
mr.RelationID = "10741-4052410"
mr.PunishResult = 2
Convey("should return err be nil", t, func() {
err := s.BlockUser(c, mr)
So(err, ShouldBeNil)
})
}
func Test_Judge(t *testing.T) {
var (
c = context.TODO()
mr = &model.Case{}
)
mr.ID = 304
mr.Status = model.CaseStatusDealing
mr.JudgeType = model.JudgeTypeViolate
mr.OriginTitle = "304"
mr.Mid = 1
mr.OriginURL = "http:304"
mr.OriginContent = "304cont"
mr.OriginType = 4
mr.Operator = "lgs"
mr.Status = 4
mr.ReasonType = 3
mr.RelationID = "10741-4052410"
mr.PunishResult = 2
Convey("should return err be nil", t, func() {
b, _ := json.Marshal(&mr)
err := s.Judge(c, b, b)
So(err, ShouldBeNil)
})
}
func Test_dealMoralCase(t *testing.T) {
var (
c = context.TODO()
mr = &model.Case{}
)
mr.ID = 304
mr.Status = model.CaseStatusDealing
mr.JudgeType = model.JudgeTypeViolate
mr.OriginTitle = "304"
mr.Mid = 1
mr.OriginURL = "http:304"
mr.OriginContent = "304cont"
mr.OriginType = 4
mr.Operator = "lgs"
mr.Status = 4
mr.ReasonType = 3
mr.RelationID = "10741-4052410"
mr.PunishResult = 2
Convey("dealMoralCase", t, func() {
err := s.dealMoralCase(c, mr)
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,70 @@
package service
import (
"context"
"encoding/json"
"time"
"go-common/app/job/main/credit/model"
"go-common/library/log"
)
// KPIReward send reward to user by kpi info.
func (s *Service) KPIReward(c context.Context, nwMsg []byte, oldMsg []byte) (err error) {
var (
mr = &model.Kpi{}
res model.Kpi
nameplentID int64
coins float64
)
if err = json.Unmarshal(nwMsg, mr); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
return
}
if res, err = s.dao.KPIInfo(c, mr.ID); err != nil {
log.Error("json.KPIInf(%d) error(%v)", mr.ID, err)
return
}
if res.HandlerStatus == 1 {
return
}
s.dao.SendMsg(c, mr.Mid, _msgTitle, _msgContext)
coins = model.KpiCoinsRate(mr.Rate)
if coins > 0 {
s.dao.AddMoney(c, mr.Mid, coins, model.KPICoinsReason)
}
s.dao.UpdateKPIHandlerStatus(c, mr.ID)
if pend, ok := model.LevelPendantByKPI(int8(mr.Rate)); ok {
expired := time.Now().AddDate(0, 0, 30)
if err = s.dao.UpdateJuryExpired(c, mr.Mid, expired); err != nil {
return
}
if len(pend) > 0 {
if err = s.dao.SendPendant(c, mr.Mid, pend, model.KPIDefealtPendSendDays); err != nil {
log.Error("s.dao.SendPendant err(%v)", err)
}
if err == nil {
s.dao.UpdateKPIPendentStatus(c, mr.ID)
}
if mr.Mid%50 == 1 {
time.Sleep(time.Second)
}
}
}
num, err := s.dao.CountKPIRate(c, mr.Mid)
if err != nil {
log.Error("s.dao.CountKPIRate(mid:%d) err(%v)", mr.Mid, err)
return
}
nameplentID = model.KpiPlateIDRateTimes(num)
if nameplentID != 0 {
for i := 0; i <= 5; i++ {
if err = s.dao.SendMedal(c, mr.Mid, nameplentID); err != nil {
log.Error("s.dao.SendMedal(mid:%d nameNameplatid:%d) err(%v)", mr.Mid, nameplentID, err)
continue
}
break
}
}
return
}

View File

@@ -0,0 +1,198 @@
package service
import (
"context"
"encoding/json"
"fmt"
"time"
"go-common/app/job/main/credit/model"
"go-common/app/service/main/archive/model/archive"
"go-common/library/log"
)
func (s *Service) replyAllConsumer() {
defer s.wg.Done()
var (
msgs = s.replyAllSub.Messages()
err error
c = context.TODO()
)
for {
msg, ok := <-msgs
if !ok {
log.Error("s.replyAllSub.Message closed")
return
}
msg.Commit()
m := &model.Reply{}
if err = json.Unmarshal(msg.Value, m); err != nil {
log.Error("json.Unmarshal(%v) error(%v)", string(msg.Value), err)
continue
}
switch m.Action {
case model.RouteReplyReport:
err = s.addReplyReport(c, m)
default:
log.Warn("replyAllConsumer unknown message action(%s)", m.Action)
}
if err != nil {
log.Error("replyMessage key(%s) value(%s) partition(%d) offset(%d) commit error(%v)", msg.Key, msg.Value, msg.Partition, msg.Offset, err)
continue
}
log.Info("replyMessage key(%s) value(%s) partition(%d) offset(%d) commit", msg.Key, msg.Value, msg.Partition, msg.Offset)
}
}
func (s *Service) addReplyReport(c context.Context, m *model.Reply) (err error) {
if m.Reply == nil || m.Subject == nil || m.Report == nil {
log.Warn("reply content(%+v) empty!", m)
return
}
if m.Report.State == model.ReportStateAddJuge {
log.Warn("rpid(%d) state(%d) is in juge", m.Report.RPID, m.Report.State)
return
}
if m.Report.Type != model.SubTypeArchive {
log.Warn("m.Report.Type(%d) model.SubTypeArchive(%d)", m.Report.Type, model.SubTypeArchive)
return
}
var ca *model.AutoCaseConf
if ca, err = s.dao.AutoCaseConf(c, model.OriginReply); err != nil {
log.Error("s.dao.AutoCaseConf(%d) error(%v)", model.OriginReply, err)
return
}
if ca == nil {
log.Warn("otype(%d) auto acse conf is not set !", model.OriginReply)
return
}
replyReportReason := model.BlockedReasonTypeByReply(m.Report.Reason)
if _, ok := ca.Reasons[replyReportReason]; !ok {
log.Warn("m.Report.Reason(%d) not int ca.Reasons(%+v)", m.Report.Reason, ca.Reasons)
return
}
if m.Report.Score < ca.ReportScore {
log.Warn("m.Report.Score(%d) ca.ReportScore(%d)", m.Report.Score, ca.ReportScore)
return
}
if m.Reply.Like > int64(ca.Likes) {
log.Warn("m.Reply.Like(%d) ca.Likes(%d)", m.Reply.Like, ca.Likes)
return
}
var disCount, total int64
if disCount, err = s.dao.CountCaseMID(c, m.Reply.MID, model.OriginReply); err != nil {
log.Error("s.dao.CountBlocked(%d) err(%v)", m.Reply.MID, err)
return
}
if disCount > 0 {
log.Warn("rpid(%d) mid(%d) report in 24 hours", m.Report.RPID, m.Reply.MID)
return
}
if total, err = s.dao.CountBlocked(c, m.Reply.MID, time.Now().AddDate(-1, 0, 0)); err != nil {
log.Error("s.dao.CountBlocked(%d) err(%v)", m.Reply.MID, err)
return
}
punishResult, blockedDay := model.PunishResultDays(total)
mc := &model.Case{
Mid: m.Reply.MID,
Status: model.CaseStatusGrantStop,
RelationID: fmt.Sprintf("%d-%d-%d", m.Report.RPID, m.Report.Type, m.Report.OID),
PunishResult: punishResult,
BlockedDay: blockedDay,
OPID: model.AutoOPID,
BCtime: m.Reply.CTime,
}
mc.Origin = model.Origin{
OriginTitle: s.replyOriginTitle(c, m.Report.OID, m.Report.Type),
OriginContent: m.Reply.Content.Message,
OriginType: int64(model.OriginReply),
OriginURL: s.replyOriginURL(m.Report.RPID, m.Report.OID, m.Report.Type),
ReasonType: int64(replyReportReason),
}
var count int64
if count, err = s.dao.CaseRelationIDCount(c, model.OriginReply, mc.RelationID); err != nil {
log.Error("ss.dao.CaseRelationIDCount(%d,%s) err(%v)", model.OriginReply, mc.RelationID, err)
return
}
if count > 0 {
log.Warn("rpid(%d) state(%d) is alreadly juge", m.Report.RPID, m.Report.State)
return
}
var need bool
if need, err = s.dao.CheckFilter(c, "credit", m.Reply.Content.Message, ""); err != nil {
log.Error("s.dao.CheckFilter(%s,%s) error(%v)", "credit", m.Reply.Content.Message, err)
return
}
if need {
log.Warn("reply(%d) message(%s) is filter", m.Report.RPID, m.Reply.Content.Message)
return
}
if err = s.dao.AddBlockedCase(c, mc); err != nil {
log.Error("s.dao.AddBlockedCase(%+v) err(%v)", mc, err)
return
}
s.dao.UpReplyState(c, m.Report.OID, m.Report.RPID, m.Report.Type, model.ReportStateAddJuge)
s.dao.UpAppealState(c, model.AppealBusinessID, m.Report.OID, m.Report.RPID)
return
}
func (s *Service) replyOriginTitle(c context.Context, oid int64, oType int8) (title string) {
switch oType {
case model.SubTypeArchive:
arg := &archive.ArgAid2{Aid: oid}
arc, err := s.arcRPC.Archive3(c, arg)
if err != nil {
log.Error("s.arcRPC.Archive3(%v) error(%v)", arg, err)
return
}
title = arc.Title
return
}
return
}
func (s *Service) replyOriginURL(rpid, oid int64, oType int8) string {
switch oType {
case model.SubTypeArchive:
return fmt.Sprintf(model.ReplyOriginURL, oid, rpid)
}
return ""
}
// RegReply regist reply subject.
func (s *Service) RegReply(c context.Context, table string, nwMsg []byte, oldMsg []byte) (err error) {
var (
replyType int8
replyID int64
)
switch table {
case _blockedCaseTable:
mr := &model.Case{}
if err = json.Unmarshal(nwMsg, mr); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
return
}
replyType = model.ReplyCase
replyID = mr.ID
case _blockedInfoTable:
mr := &model.BlockedInfo{}
if err = json.Unmarshal(nwMsg, mr); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
return
}
replyType = model.ReplyBlocked
replyID = mr.ID
case _blockedPublishTable:
mr := &model.Publish{}
if err = json.Unmarshal(nwMsg, mr); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
return
}
if mr.PStatus != model.PublishOpen {
return
}
replyType = model.ReplyPublish
replyID = mr.ID
}
return s.dao.RegReply(c, replyID, replyType)
}

View File

@@ -0,0 +1,40 @@
package service
import (
"context"
"testing"
"go-common/app/job/main/credit/model"
. "github.com/smartystreets/goconvey/convey"
)
func Test_addReplyReport(t *testing.T) {
var (
c = context.TODO()
mr = &model.Reply{
Subject: &model.ReplySubject{
OID: 2222,
},
Reply: &model.ReplyMain{
Like: 1,
MID: 27515628,
Content: &struct {
Message string `json:"message"`
}{Message: "xxxx"},
},
Report: &model.ReplyReport{
State: model.ReportStateNew,
Type: model.SubTypeArchive,
Reason: model.ReplyReasonGarbageAds,
Score: 2,
RPID: 1111,
OID: 2222,
},
}
)
Convey("should return err be nil", t, func() {
err := s.addReplyReport(c, mr)
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,135 @@
package service
import (
"context"
"encoding/json"
"strconv"
"sync"
"time"
"go-common/app/job/main/credit/conf"
"go-common/app/job/main/credit/dao"
archive "go-common/app/service/main/archive/api/gorpc"
memrpc "go-common/app/service/main/member/api/gorpc"
"go-common/library/log"
"go-common/library/queue/databus"
)
// Service struct of service.
type Service struct {
c *conf.Config
dao *dao.Dao
credbSub *databus.Databus
replyAllSub *databus.Databus
labourSub *databus.Databus
arcRPC *archive.Service2
memRPC *memrpc.Service
// wait group
wg sync.WaitGroup
}
// New create service instance and return.
func New(c *conf.Config) (s *Service) {
s = &Service{
c: c,
dao: dao.New(c),
credbSub: databus.New(c.DataBus.CreditDBSub),
replyAllSub: databus.New(c.DataBus.ReplyAllSub),
labourSub: databus.New(c.DataBus.LabourSub),
arcRPC: archive.New2(c.RPCClient.Archive),
memRPC: memrpc.New(c.RPCClient.Member),
}
s.loadConf()
s.loadCase()
s.loadDealWrongCase()
s.wg.Add(1)
go s.replyAllConsumer()
go s.creditConsumer()
go s.loadConfproc()
return
}
// Ping check service health.
func (s *Service) Ping(c context.Context) error {
return s.dao.Ping(c)
}
func (s *Service) loadConfproc() {
for {
time.Sleep(time.Duration(s.c.Judge.ConfTimer))
s.loadConf()
s.loadCase()
s.loadDealWrongCase()
}
}
func (s *Service) loadConf() {
m, err := s.dao.LoadConf(context.TODO())
if err != nil {
log.Error("loadConf error(%v)", err)
return
}
if s.c.Judge.CaseGiveHours, err = strconv.ParseInt(m["case_give_hours"], 10, 64); err != nil {
log.Error("loadConf CaseGiveHours error(%v)", err)
}
if s.c.Judge.CaseCheckTime, err = strconv.ParseInt(m["case_check_hours"], 10, 64); err != nil {
log.Error("loadConf CaseCheckTime error(%v)", err)
}
if s.c.Judge.JuryRatio, err = strconv.ParseInt(m["jury_vote_radio"], 10, 64); err != nil {
log.Error("loadConf JuryRatio error(%v)", err)
}
if s.c.Judge.JudgeRadio, err = strconv.ParseInt(m["case_judge_radio"], 10, 64); err != nil {
log.Error("loadConf JudgeRadio error(%v)", err)
}
if s.c.Judge.CaseVoteMin, err = strconv.ParseInt(m["case_vote_min"], 10, 64); err != nil {
log.Error("loadConf CaseVoteMin error(%v)", err)
}
if s.c.Judge.CaseObtainMax, err = strconv.ParseInt(m["case_obtain_max"], 10, 64); err != nil {
log.Error("loadConf CaseObtainMax error(%v)", err)
}
if s.c.Judge.CaseVoteMax, err = strconv.ParseInt(m["case_vote_max"], 10, 64); err != nil {
log.Error("loadConf CaseVoteMax error(%v)", err)
}
if s.c.Judge.JuryApplyMax, err = strconv.ParseInt(m["jury_apply_max"], 10, 64); err != nil {
log.Error("loadConf JuryApplyMax error(%v)", err)
}
if s.c.Judge.CaseLoadMax, err = strconv.Atoi(m["case_load_max"]); err != nil {
log.Error("loadConf CaseLoadMax error(%v)", err)
}
var caseLoadSwitch int64
if caseLoadSwitch, err = strconv.ParseInt(m["case_load_switch"], 10, 64); err != nil {
log.Error("loadConf CaseLoadSwitch error(%v)", err)
}
s.c.Judge.CaseLoadSwitch = int8(caseLoadSwitch)
if s.c.Judge.CaseVoteMaxPercent, err = strconv.Atoi(m["case_vote_max_percent"]); err != nil {
log.Error("loadConf CaseVoteMaxPercent error(%v)", err)
}
if _, ok := m["vote_num"]; !ok {
s.c.Judge.VoteNum.RateS = 1
s.c.Judge.VoteNum.RateA = 1
s.c.Judge.VoteNum.RateB = 1
s.c.Judge.VoteNum.RateC = 1
s.c.Judge.VoteNum.RateD = 1
return
}
if err = json.Unmarshal([]byte(m["vote_num"]), &s.c.Judge.VoteNum); err != nil {
log.Error("loadConf vote_num error(%v)", err)
}
}
// Close kafka consumer close.
func (s *Service) Close() {
if s == nil {
return
}
if s.dao != nil {
s.dao.Close()
}
if s.credbSub != nil {
s.credbSub.Close()
}
if s.replyAllSub != nil {
s.replyAllSub.Close()
}
s.wg.Wait()
}

View File

@@ -0,0 +1,31 @@
package service
import (
"flag"
"path/filepath"
"testing"
"time"
"go-common/app/job/main/credit/conf"
. "github.com/smartystreets/goconvey/convey"
)
var (
s *Service
)
func init() {
dir, _ := filepath.Abs("../cmd/convey-test.toml")
flag.Set("conf", dir)
conf.Init()
s = New(conf.Conf)
time.Sleep(time.Second)
}
func TestService_loadCase(t *testing.T) {
Convey("should return err be nil", t, func() {
s.loadConf()
s.loadCase()
})
}