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,54 @@
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",
"sobot_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/workflow/conf:go_default_library",
"//app/service/main/workflow/model/sobot:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"sobot.go",
],
importpath = "go-common/app/service/main/workflow/dao/sobot",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/workflow/conf:go_default_library",
"//app/service/main/workflow/model/sobot:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster: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,30 @@
package sobot
import (
"go-common/app/service/main/workflow/conf"
bm "go-common/library/net/http/blademaster"
)
// Dao it
type Dao struct {
c *conf.Config
ticketInfoURL string
ticketAddURL string
ticketModifyURL string
replyAddURL string
// sobot httpclient
httpSobot *bm.Client
}
// New Dao
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
httpSobot: bm.NewClient(c.HTTPClient.Sobot),
ticketInfoURL: c.Host.ServiceURI + _sobotTicketInfoURL,
ticketAddURL: c.Host.ServiceURI + _sobotAddTicketURL,
ticketModifyURL: c.Host.ServiceURI + _sobotTicketModifyURL,
replyAddURL: c.Host.ServiceURI + _sobotAddReplyURL,
}
return
}

View File

@@ -0,0 +1,36 @@
package sobot
import (
"flag"
"os"
"testing"
"go-common/app/service/main/workflow/conf"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.manager.workflow-service")
flag.Set("conf_token", "410a3a978eeda5cf44b66f193708c283")
flag.Set("tree_id", "6791")
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/workflow-service.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}

View File

@@ -0,0 +1,160 @@
package sobot
import (
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"go-common/library/log"
"net/http"
"net/url"
"strconv"
"strings"
"go-common/app/service/main/workflow/model/sobot"
)
const (
_sobotTicketModifyURL = "/ws/updateStatusBilibili/4"
_sobotAddTicketURL = "/ws/addCustomerTicketBilibili/4"
_sobotAddReplyURL = "/ws/addCustomerReplyInfoBilibili/4"
_sobotTicketInfoURL = "/ws/queryTicketReplyByCustomerListBilibili/4"
)
// SobotTicketInfo get ticket into
func (d *Dao) SobotTicketInfo(c context.Context, ticketID int32) (res json.RawMessage, err error) {
var (
req *http.Request
)
params := url.Values{}
params.Set("companyId", d.c.HTTPClient.Sobot.Secret)
params.Set("ticketId", strconv.Itoa(int(ticketID)))
sign := md5.Sum([]byte(fmt.Sprintf("%s%s%d", d.c.HTTPClient.Sobot.Secret, d.c.HTTPClient.Sobot.Key, ticketID)))
params.Set("sobotKey", hex.EncodeToString(sign[:]))
if req, err = http.NewRequest("GET", d.ticketInfoURL+"?"+params.Encode(), nil); err != nil {
log.Error("http.NewRequest(GET,%s) error(%v)", d.ticketInfoURL, err)
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err = d.httpSobot.Do(c, req, &res); err != nil {
log.Error("d.httpSobot.Do() error(%v)", err)
return
}
return
}
// SobotAddTicket add ticket to sobot
func (d *Dao) SobotAddTicket(c context.Context, tp *sobot.TicketParam) (err error) {
var (
req *http.Request
res struct {
RetCode string `json:"retCode"`
Item string `json:"item"`
}
)
params := url.Values{}
params.Set("fileStr", tp.FileStr)
params.Set("companyId", d.c.HTTPClient.Sobot.Secret)
params.Set("customerName", tp.CustomerName)
params.Set("customerQq", tp.CustomerQQ)
params.Set("customerNick", tp.CustomerNick)
params.Set("customerEmail", tp.CustomerEmail)
params.Set("customerPhone", tp.CustomerPhone)
params.Set("customerSource", strconv.Itoa(int(tp.CustomerSource)))
params.Set("ticketId", strconv.Itoa(int(tp.TicketID)))
params.Set("ticketTitle", tp.TicketTitle)
params.Set("ticketContent", tp.TicketContent)
params.Set("ticketLevel", strconv.Itoa(int(tp.TicketLevel)))
params.Set("ticketStatus", strconv.Itoa(int(tp.TicketStatus)))
params.Set("ticketFrom", strconv.Itoa(int(sobot.TicketFrom)))
sign := md5.Sum([]byte(fmt.Sprintf("%s%s%d", d.c.HTTPClient.Sobot.Secret, d.c.HTTPClient.Sobot.Key, tp.TicketID)))
params.Set("sobotKey", hex.EncodeToString(sign[:]))
if req, err = http.NewRequest("POST", d.ticketAddURL, strings.NewReader(params.Encode())); err != nil {
log.Error("http.NewRequest(POST,%s) error(%v)", d.ticketAddURL, err)
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err = d.httpSobot.Do(c, req, &res); err != nil {
log.Error("d.httpSobot.Do() error(%v)", err)
return
}
if res.RetCode != sobot.EcodeOK {
log.Error("d.httpSobot.Do() url(%s) ecode(%s)", d.ticketAddURL, res.RetCode)
err = errors.New(res.RetCode)
return
}
return
}
// SobotAddReply add reply to sobot
func (d *Dao) SobotAddReply(c context.Context, rp *sobot.ReplyParam) (err error) {
var (
req *http.Request
res struct {
RetCode string `json:"retCode"`
Item string `json:"item"`
}
)
params := url.Values{}
params.Set("companyId", d.c.HTTPClient.Sobot.Secret)
params.Set("customerEmail", rp.CustomerEmail)
params.Set("replyContent", rp.ReplyContent)
params.Set("ticketId", strconv.Itoa(int(rp.TicketID)))
params.Set("replyType", strconv.Itoa(int(rp.ReplyType)))
params.Set("startType", strconv.Itoa(int(rp.StartType)))
sign := md5.Sum([]byte(fmt.Sprintf("%s%s%d", d.c.HTTPClient.Sobot.Secret, d.c.HTTPClient.Sobot.Key, rp.TicketID)))
params.Set("sobotKey", hex.EncodeToString(sign[:]))
if req, err = http.NewRequest("POST", d.replyAddURL, strings.NewReader(params.Encode())); err != nil {
log.Error("http.NewRequest(POST,%s) error(%v)", d.replyAddURL, err)
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err = d.httpSobot.Do(c, req, &res); err != nil {
log.Error("d.httpSobot.Do() error(%v)", err)
return
}
if res.RetCode != sobot.EcodeOK {
log.Error("d.httpSobot.Do() url(%s) ecode(%s)", d.replyAddURL, res.RetCode)
err = errors.New(res.RetCode)
return
}
return
}
// SobotTicketModify modify ticket
func (d *Dao) SobotTicketModify(c context.Context, tp *sobot.TicketParam) (err error) {
// http
var (
req *http.Request
res struct {
RetCode string `json:"retCode"`
Item string `json:"item"`
}
)
params := url.Values{}
params.Set("companyId", d.c.HTTPClient.Sobot.Secret)
params.Set("customerEmail", tp.CustomerEmail)
params.Set("ticketId", strconv.Itoa(int(tp.TicketID)))
params.Set("ticketFrom", strconv.Itoa(int(sobot.TicketFrom)))
params.Set("ticketStatus", strconv.Itoa(int(tp.TicketStatus)))
params.Set("startType", strconv.Itoa(int(tp.StartType)))
sign := md5.Sum([]byte(fmt.Sprintf("%s%s%d", d.c.HTTPClient.Sobot.Secret, d.c.HTTPClient.Sobot.Key, tp.TicketID)))
params.Set("sobotKey", hex.EncodeToString(sign[:]))
if req, err = http.NewRequest("POST", d.ticketModifyURL, strings.NewReader(params.Encode())); err != nil {
log.Error("http.NewRequest(POST,%s) error(%v)", d.ticketModifyURL, err)
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err = d.httpSobot.Do(c, req, &res); err != nil {
log.Error("d.httpSobot.Do() error(%v)", err)
return
}
if res.RetCode != sobot.EcodeOK {
log.Error("d.httpSobot.Do() url(%s) ecode(%s)", d.ticketModifyURL, res.RetCode)
err = errors.New(res.RetCode)
return
}
return
}

View File

@@ -0,0 +1,78 @@
package sobot
import (
"context"
"testing"
"go-common/app/service/main/workflow/model/sobot"
"github.com/smartystreets/goconvey/convey"
)
func TestSobotSobotTicketInfo(t *testing.T) {
var (
c = context.Background()
ticketID = int32(0)
)
convey.Convey("SobotTicketInfo", t, func(ctx convey.C) {
res, err := d.SobotTicketInfo(c, ticketID)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
}
func TestSobotSobotAddTicket(t *testing.T) {
var (
c = context.Background()
tp = &sobot.TicketParam{
TicketTitle: "我是202工单",
TicketID: 202,
TicketContent: "233333333",
CustomerEmail: "1107691251@qq.com",
}
)
convey.Convey("SobotAddTicket", t, func(ctx convey.C) {
err := d.SobotAddTicket(c, tp)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err.Error(), convey.ShouldEqual, "2000101")
})
})
}
func TestSobotSobotAddReply(t *testing.T) {
var (
c = context.Background()
rp = &sobot.ReplyParam{
TicketID: 202,
ReplyContent: "reply_test",
CustomerEmail: "1107691251@qq.com",
StartType: 1,
ReplyType: 1,
}
)
convey.Convey("SobotAddReply", t, func(ctx convey.C) {
err := d.SobotAddReply(c, rp)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestSobotSobotTicketModify(t *testing.T) {
var (
c = context.Background()
tp = &sobot.TicketParam{
TicketID: 202,
CustomerEmail: "1107691251@qq.com",
StartType: 1,
}
)
convey.Convey("SobotTicketModify", t, func(ctx convey.C) {
err := d.SobotTicketModify(c, tp)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}