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,61 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"scoresection_test.go",
"up_base_info_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/upcredit/conf:go_default_library",
"//app/service/main/upcredit/model/calculator:go_default_library",
"//vendor/github.com/go-sql-driver/mysql:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"scoresection.go",
"up_base_info.go",
],
importmap = "go-common/app/service/main/upcredit/dao/upcrmdao",
importpath = "go-common/app/service/main/upcredit/dao/upcrmdao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/upcredit/conf:go_default_library",
"//app/service/main/upcredit/model/calculator:go_default_library",
"//app/service/main/upcredit/model/upcrmmodel:go_default_library",
"//library/log:go_default_library",
"//library/time:go_default_library",
"//vendor/github.com/jinzhu/gorm:go_default_library",
"//vendor/github.com/siddontang/go-mysql/mysql: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,98 @@
package upcrmdao
import (
"github.com/jinzhu/gorm"
"go-common/app/service/main/upcredit/conf"
"context"
"fmt"
"go-common/app/service/main/upcredit/model/upcrmmodel"
"go-common/library/log"
)
//Dao upcrm dao
type Dao struct {
conf *conf.Config
crmdb *gorm.DB
}
//New create
func New(c *conf.Config) *Dao {
var d = &Dao{
conf: c,
}
crmdb, err := gorm.Open("mysql", c.DB.Upcrm.DSN)
if crmdb == nil {
log.Error("connect to db fail, err=%v", err)
return nil
}
d.crmdb = crmdb
crmdb.SingularTable(true)
d.crmdb.LogMode(c.IsTest)
return d
}
//Close close
func (d *Dao) Close() {
if d.crmdb != nil {
d.crmdb.Close()
}
}
//AddLog add log
func (d *Dao) AddLog(arg *upcrmmodel.ArgCreditLogAdd) error {
var creditLog = &upcrmmodel.CreditLog{}
creditLog.CopyFrom(arg)
return d.crmdb.Create(creditLog).Error
}
//AddCreditScore add score
func (d *Dao) AddCreditScore(creditScore *upcrmmodel.UpScoreHistory) error {
return d.crmdb.Create(creditScore).Error
}
//AddOrUpdateCreditScore update score
func (d *Dao) AddOrUpdateCreditScore(creditScore *upcrmmodel.UpScoreHistory) (err error) {
var tablename = creditScore.TableName()
var insertSQL = fmt.Sprintf("insert into %s (mid, score_type, score, generate_date, ctime) values (?,?,?,?,?) "+
"on duplicate key update score=?", tablename)
err = d.crmdb.Exec(
insertSQL,
creditScore.Mid, creditScore.ScoreType, creditScore.Score, creditScore.GenerateDate, creditScore.CTime,
creditScore.Score).Error
if err != nil {
log.Error("add credit score fail, mid=%d, err=%+v", creditScore.Mid, err)
}
return
}
//GetCreditScore get score
func (d *Dao) GetCreditScore(c context.Context, arg *upcrmmodel.GetScoreParam) (results []*upcrmmodel.UpScoreHistory, err error) {
var mod = upcrmmodel.UpScoreHistory{
Mid: arg.Mid,
}
err = d.crmdb.Table(mod.TableName()).Select("score, generate_date").
Where("mid=? AND generate_date>=? AND generate_date<=? AND score_type=?", arg.Mid, arg.FromDate, arg.ToDate, arg.ScoreType).
Find(&results).Error
if err != nil {
log.Error("get score history fail, arg=%+v, err=%+v", arg, err)
}
return
}
//GetCreditLog get log
func (d *Dao) GetCreditLog(c context.Context, arg *upcrmmodel.ArgGetLogHistory) (results []*upcrmmodel.SimpleCreditLogWithContent, err error) {
var mod = upcrmmodel.SimpleCreditLogWithContent{}
mod.Mid = arg.Mid
err = d.crmdb.Table(mod.TableName()).Select("type, op_type, reason, business_type, ctime, content, oid").
//Limit(arg.Limit).
//Offset(arg.Offset).
Where("mid=? AND ctime>=? AND ctime<=?", arg.Mid, arg.FromDate, arg.ToDate).
Order("ctime").
Find(&results).Error
if err != nil {
log.Error("get log history fail, arg=%+v, err=%+v", arg, err)
}
return
}

View File

@@ -0,0 +1,50 @@
package upcrmdao
import (
"flag"
"github.com/go-sql-driver/mysql"
"go-common/app/service/main/upcredit/conf"
"os"
"testing"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.archive.upcredit-service")
flag.Set("conf_token", "e85677843358d6c5dcd7246e6e3fc2de")
flag.Set("tree_id", "33287")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
} else {
flag.Set("conf", "../../cmd/upcredit-service.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}
func IgnoreErr(err error) error {
if err == nil {
return err
}
var e, _ = err.(*mysql.MySQLError)
if e != nil {
switch e.Number {
case 1062:
return nil
}
}
return err
}

View File

@@ -0,0 +1,30 @@
package upcrmdao
import (
"go-common/app/service/main/upcredit/model/calculator"
"go-common/app/service/main/upcredit/model/upcrmmodel"
xtime "go-common/library/time"
"time"
)
//InsertScoreSection insert score section
func (d *Dao) InsertScoreSection(statis calculator.OverAllStatistic, scoreType int, date time.Time) error {
var history upcrmmodel.ScoreSectionHistory
history.Section0 = statis.GetScore(0)
history.Section1 = statis.GetScore(1)
history.Section2 = statis.GetScore(2)
history.Section3 = statis.GetScore(3)
history.Section4 = statis.GetScore(4)
history.Section5 = statis.GetScore(5)
history.Section6 = statis.GetScore(6)
history.Section7 = statis.GetScore(7)
history.Section8 = statis.GetScore(8)
history.Section9 = statis.GetScore(9)
history.ScoreType = scoreType
history.GenerateDate = xtime.Time(date.Unix())
var now = time.Now().Unix()
history.CTime = xtime.Time(now)
history.MTime = xtime.Time(now)
// insert or update
return d.crmdb.Create(&history).Error
}

View File

@@ -0,0 +1,24 @@
package upcrmdao
import (
"go-common/app/service/main/upcredit/model/calculator"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestUpcrmdaoInsertScoreSection(t *testing.T) {
var (
statis calculator.OverAllStatistic
scoreType = int(0)
date = time.Now()
)
convey.Convey("InsertScoreSection", t, func(ctx convey.C) {
err := d.InsertScoreSection(statis, scoreType, date)
err = IgnoreErr(err)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,99 @@
package upcrmdao
import (
"fmt"
"github.com/siddontang/go-mysql/mysql"
"go-common/app/service/main/upcredit/model/upcrmmodel"
"go-common/library/log"
xtime "go-common/library/time"
"strings"
"time"
)
const (
//TimeFmtMysql mysql time format
TimeFmtMysql = mysql.TimeFormat
//TimeFmtDate with only date
TimeFmtDate = "2006-01-02"
)
//UpQualityInfo struct
type UpQualityInfo struct {
Mid int64 `json:"mid"`
QualityValue int `json:"quality_value"`
PrValue int `json:"pr_value"`
Cdate string `json:"cdate"` // 产生时间 "2018-01-01"
}
// AsPrScore copy to db struct
func (u *UpQualityInfo) AsPrScore() (history *upcrmmodel.UpScoreHistory) {
if u == nil {
return &upcrmmodel.UpScoreHistory{}
}
history = &upcrmmodel.UpScoreHistory{
Mid: u.Mid,
ScoreType: upcrmmodel.ScoreTypePr,
Score: u.PrValue,
}
var date, _ = time.Parse(TimeFmtDate, u.Cdate)
history.GenerateDate = xtime.Time(date.Unix())
return
}
// AsQualityScore copy to db struct
func (u *UpQualityInfo) AsQualityScore() (history *upcrmmodel.UpScoreHistory) {
if u == nil {
return &upcrmmodel.UpScoreHistory{}
}
history = &upcrmmodel.UpScoreHistory{
Mid: u.Mid,
ScoreType: upcrmmodel.ScoreTypeQuality,
Score: u.QualityValue,
}
var date, _ = time.Parse(TimeFmtDate, u.Cdate)
history.GenerateDate = xtime.Time(date.Unix())
return
}
//UpdateCreditScore update score
func (d *Dao) UpdateCreditScore(score int, mid int64) (affectRow int64, err error) {
var db = d.crmdb.Model(upcrmmodel.UpBaseInfo{}).Where("mid = ? and business_type = 1", mid).Update("credit_score", score)
return db.RowsAffected, db.Error
}
//UpdateQualityAndPrScore update score
func (d *Dao) UpdateQualityAndPrScore(prScore int, qualityScore int, mid int64) (affectRow int64, err error) {
var db = d.crmdb.Model(upcrmmodel.UpBaseInfo{}).Where("mid = ? and business_type = 1", mid).Update(map[string]int{"pr_score": prScore, "quality_score": qualityScore})
return db.RowsAffected, db.Error
}
//InsertScoreHistory insert into score history
func (d *Dao) InsertScoreHistory(info *UpQualityInfo) (affectRow int64, err error) {
var qualityScoreSt = info.AsQualityScore()
err = d.crmdb.Save(qualityScoreSt).Error
if err != nil {
log.Error("insert quality score error, err=%+v", err)
}
var prScore = info.AsPrScore()
err = d.crmdb.Save(prScore).Error
if err != nil {
log.Error("insert pr score error, err=%+v", err)
}
return
}
//InsertBatchScoreHistory insert batch sql
func (d *Dao) InsertBatchScoreHistory(infoList []*UpQualityInfo, tablenum int) (affectRow int64, err error) {
var batchSQL = fmt.Sprintf("insert into up_scores_history_%02d (mid, score_type, score, generate_date) values ", tablenum)
var valueString []string
var valueArgs []interface{}
for _, info := range infoList {
valueString = append(valueString, "(?,?,?,?),(?,?,?,?)")
valueArgs = append(valueArgs, info.Mid, upcrmmodel.ScoreTypePr, info.PrValue, info.Cdate)
valueArgs = append(valueArgs, info.Mid, upcrmmodel.ScoreTypeQuality, info.QualityValue, info.Cdate)
}
var db = d.crmdb.Exec(batchSQL+strings.Join(valueString, ","), valueArgs...)
affectRow = db.RowsAffected
err = db.Error
return
}

View File

@@ -0,0 +1,88 @@
package upcrmdao
import (
"testing"
"github.com/smartystreets/goconvey/convey"
"time"
)
func TestUpcrmdaoAsPrScore(t *testing.T) {
convey.Convey("AsPrScore", t, func(ctx convey.C) {
var info *UpQualityInfo
history := info.AsPrScore()
ctx.Convey("Then history should not be nil.", func(ctx convey.C) {
ctx.So(history, convey.ShouldNotBeNil)
})
})
}
func TestUpcrmdaoAsQualityScore(t *testing.T) {
convey.Convey("AsQualityScore", t, func(ctx convey.C) {
var info *UpQualityInfo
history := info.AsQualityScore()
ctx.Convey("Then history should not be nil.", func(ctx convey.C) {
ctx.So(history, convey.ShouldNotBeNil)
})
})
}
func TestUpcrmdaoUpdateCreditScore(t *testing.T) {
var (
score = int(0)
mid = int64(0)
)
convey.Convey("UpdateCreditScore", t, func(ctx convey.C) {
affectRow, err := d.UpdateCreditScore(score, mid)
err = IgnoreErr(err)
ctx.Convey("Then err should be nil.affectRow should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affectRow, convey.ShouldNotBeNil)
})
})
}
func TestUpcrmdaoUpdateQualityAndPrScore(t *testing.T) {
var (
prScore = int(0)
qualityScore = int(0)
mid = int64(0)
)
convey.Convey("UpdateQualityAndPrScore", t, func(ctx convey.C) {
affectRow, err := d.UpdateQualityAndPrScore(prScore, qualityScore, mid)
err = IgnoreErr(err)
ctx.Convey("Then err should be nil.affectRow should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affectRow, convey.ShouldNotBeNil)
})
})
}
func TestUpcrmdaoInsertScoreHistory(t *testing.T) {
var (
info = &UpQualityInfo{}
)
convey.Convey("InsertScoreHistory", t, func(ctx convey.C) {
affectRow, err := d.InsertScoreHistory(info)
err = IgnoreErr(err)
ctx.Convey("Then err should be nil.affectRow should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affectRow, convey.ShouldNotBeNil)
})
})
}
func TestUpcrmdaoInsertBatchScoreHistory(t *testing.T) {
var (
infoList = []*UpQualityInfo{{Mid: 100, Cdate: time.Now().Format(TimeFmtDate)}}
tablenum = int(0)
)
convey.Convey("InsertBatchScoreHistory", t, func(ctx convey.C) {
affectRow, err := d.InsertBatchScoreHistory(infoList, tablenum)
err = IgnoreErr(err)
ctx.Convey("Then err should be nil.affectRow should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affectRow, convey.ShouldNotBeNil)
})
})
}