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,58 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["unicom_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-wall/conf:go_default_library",
"//app/interface/main/app-wall/model/unicom:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"api.go",
"cache.go",
"log.go",
"unicom.go",
],
importpath = "go-common/app/interface/main/app-wall/dao/unicom",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/app-wall/conf:go_default_library",
"//app/interface/main/app-wall/model:go_default_library",
"//app/interface/main/app-wall/model/unicom:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/database/elastic:go_default_library",
"//library/database/sql:go_default_library",
"//library/ecode: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,464 @@
package unicom
import (
"bytes"
"context"
"crypto/des"
"crypto/md5"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"time"
"go-common/app/interface/main/app-wall/model"
"go-common/app/interface/main/app-wall/model/unicom"
"go-common/library/ecode"
"go-common/library/log"
httpx "go-common/library/net/http/blademaster"
)
const (
_cpid = "bilibl"
_spid = "979"
_apptype = "2"
_broadbandPass = "9ed226d9"
// url
_orderURL = "/videoif/order.do"
_ordercancelURL = "/videoif/cancelOrder.do"
_sendsmscodeURL = "/videoif/sendSmsCode.do"
_smsNumberURL = "/videoif/smsNumber.do"
// unicom
_unicomIPURL = "/web/statistics/subsystem_2/query_ip.php"
_unicomUser = "000012"
_unicomPass = "1pibH5e1BN4V"
_unicomFlowExchangeURL = "/openservlet"
_unicomAppKey = "com.aop.app.bilibili"
_unicomSecurity = "DVniSMVU6Z3cCIG3vbOn4Fqbof+QJ/6etD+lpa4M4clgj/Dv6XT0syTR8Xgu5nVzKuzro8eiTUzHy/QAzGjp+A=="
_unicomAppMethodFlow = "com.ssp.method.outflowchange"
_unicomMethodNumber = "com.aop.method.checkusernumber"
_unicomMethodFlowPre = "com.ssp.method.outflowpre"
_unicomMethodQryFlowChange = "com.ssp.method.outqryflowchange"
)
// Order unicom order
func (d *Dao) Order(c context.Context, usermob, channel string, ordertype int) (data *unicom.BroadbandOrder, msg string, err error) {
params := url.Values{}
params.Set("cpid", _cpid)
params.Set("spid", _spid)
params.Set("ordertype", strconv.Itoa(ordertype))
params.Set("userid", usermob)
params.Set("apptype", _apptype)
if channel != "" {
params.Set("channel", channel)
}
var res struct {
Code string `json:"resultcode"`
Msg string `json:"errorinfo"`
*unicom.BroadbandOrder
}
if err = d.broadbandHTTPGet(c, d.orderURL, params, &res); err != nil {
log.Error("unicom order url(%v) error(%v)", d.orderURL+"?"+params.Encode(), err)
return
}
b, _ := json.Marshal(&res)
log.Info("unicom order url(%v) response(%s)", d.orderURL+"?"+params.Encode(), b)
if res.Code != "0" {
err = ecode.String(res.Code)
msg = res.Msg
log.Error("unicom order url(%v) code(%s) Msg(%s)", d.orderURL+"?"+params.Encode(), res.Code, res.Msg)
return
}
data = res.BroadbandOrder
return
}
// CancelOrder unicom cancel order
func (d *Dao) CancelOrder(c context.Context, usermob string) (data *unicom.BroadbandOrder, msg string, err error) {
params := url.Values{}
params.Set("cpid", _cpid)
params.Set("spid", _spid)
params.Set("userid", usermob)
params.Set("apptype", _apptype)
var res struct {
Code string `json:"resultcode"`
Msg string `json:"errorinfo"`
*unicom.BroadbandOrder
}
if err = d.broadbandHTTPGet(c, d.ordercancelURL, params, &res); err != nil {
log.Error("unicom cancel order url(%s) error(%v)", d.ordercancelURL+"?"+params.Encode(), err)
return
}
b, _ := json.Marshal(&res)
log.Info("unicom cancel order url(%s) response(%s)", d.ordercancelURL+"?"+params.Encode(), b)
if res.Code != "0" {
err = ecode.String(res.Code)
msg = res.Msg
log.Error("unicom cancel order url(%v) code(%s) Msg(%s)", d.orderURL+"?"+params.Encode(), res.Code, res.Msg)
return
}
data = res.BroadbandOrder
return
}
// UnicomIP unicom ip orders
func (d *Dao) UnicomIP(c context.Context, now time.Time) (unicomIPs []*unicom.UnicomIP, err error) {
params := url.Values{}
params.Set("user", _unicomUser)
tick := strconv.FormatInt(now.Unix(), 10)
params.Set("tick", tick)
mh := md5.Sum([]byte(_unicomUser + tick + _unicomPass))
var (
key string
)
if key = hex.EncodeToString(mh[:]); len(key) > 16 {
key = key[:16]
}
params.Set("key", key)
var res struct {
Code int `json:"code"`
LastUpdateTime int64 `json:"last_update_time"`
Desc []struct {
StartIP string `json:"start_ip"`
Length string `json:"length"`
} `json:"desc"`
}
if err = d.broadbandHTTPPost(c, d.unicomIPURL, params, &res); err != nil {
log.Error("unicom ip order url(%s) error(%v)", d.unicomIPURL+"?"+params.Encode(), err)
return
}
if res.Code != 0 {
err = ecode.Int(res.Code)
log.Error("unicom ip order url(%s) res code (%d)", d.unicomIPURL+"?"+params.Encode(), res.Code)
return
}
b, _ := json.Marshal(&res)
log.Info("unicom ip url(%s) response(%s)", d.unicomIPURL+"?"+params.Encode(), b)
for _, uip := range res.Desc {
uiplen, _ := strconv.Atoi(uip.Length)
if uiplen < 1 {
log.Error("unicom ip length 0")
continue
}
ipEndInt := model.InetAtoN(uip.StartIP) + uint32((uiplen - 1))
ipEnd := model.InetNtoA(ipEndInt)
unicomIP := &unicom.UnicomIP{}
unicomIP.UnicomIPStrToint(uip.StartIP, ipEnd)
unicomIPs = append(unicomIPs, unicomIP)
}
return
}
// SendSmsCode unicom sms code
func (d *Dao) SendSmsCode(c context.Context, phone string) (msg string, err error) {
var (
key = []byte(_broadbandPass)
phoneByte = []byte(phone)
userid string
)
userid, err = d.desEncrypt(phoneByte, key)
if err != nil {
log.Error("d.desEncrypt error(%v)", err)
return
}
params := url.Values{}
params.Set("cpid", _cpid)
params.Set("userid", string(userid))
params.Set("apptype", _apptype)
var res struct {
Code string `json:"resultcode"`
Msg string `json:"errorinfo"`
}
if err = d.unicomHTTPGet(c, d.sendsmscodeURL, params, &res); err != nil {
log.Error("unicom sendsmscode url(%v) error(%v)", d.sendsmscodeURL+"?"+params.Encode(), err)
return
}
b, _ := json.Marshal(&res)
log.Info("unicom sendsmscode url(%v) response(%s)", d.sendsmscodeURL+"?"+params.Encode(), b)
if res.Code != "0" {
err = ecode.String(res.Code)
msg = res.Msg
log.Error("unicom sendsmscode url(%v) code(%s) Msg(%s)", d.sendsmscodeURL+"?"+params.Encode(), res.Code, res.Msg)
return
}
return
}
// SmsNumber unicom sms usermob
func (d *Dao) SmsNumber(c context.Context, phone string, code int) (usermob string, msg string, err error) {
var (
key = []byte(_broadbandPass)
phoneByte = []byte(phone)
userid string
)
userid, err = d.desEncrypt(phoneByte, key)
if err != nil {
log.Error("d.desEncrypt error(%v)", err)
return
}
params := url.Values{}
params.Set("cpid", _cpid)
params.Set("userid", userid)
params.Set("vcode", strconv.Itoa(code))
params.Set("apptype", _apptype)
var res struct {
Code string `json:"resultcode"`
Usermob string `json:"userid"`
Msg string `json:"errorinfo"`
}
if err = d.unicomHTTPGet(c, d.smsNumberURL, params, &res); err != nil {
log.Error("unicom smsNumberURL url(%v) error(%v)", d.smsNumberURL+"?"+params.Encode(), err)
return
}
b, _ := json.Marshal(&res)
log.Info("unicom sendsmsnumber url(%v) response(%s)", d.smsNumberURL+"?"+params.Encode(), b)
if res.Code != "0" {
err = ecode.String(res.Code)
msg = res.Msg
log.Error("unicom sendsmsnumber url(%v) code(%s) Msg(%s)", d.smsNumberURL+"?"+params.Encode(), res.Code, res.Msg)
return
}
usermob = res.Usermob
return
}
// FlowExchange flow exchange
func (d *Dao) FlowExchange(c context.Context, phone int, flowcode string, requestNo int64, ts time.Time) (orderID, outorderID, msg string, err error) {
outorderIDStr := "bili" + ts.Format("20060102") + strconv.FormatInt(requestNo%10000000, 10)
if len(outorderIDStr) > 22 {
outorderIDStr = outorderIDStr[:22]
}
param := url.Values{}
param.Set("appkey", _unicomAppKey)
param.Set("apptx", strconv.FormatInt(requestNo, 10))
param.Set("flowexchangecode", flowcode)
param.Set("method", _unicomAppMethodFlow)
param.Set("outorderid", outorderIDStr)
param.Set("timestamp", ts.Format("2006-01-02 15:04:05"))
param.Set("usernumber", strconv.Itoa(phone))
urlVal := d.urlParams(param)
urlVal = urlVal + "&" + d.sign(urlVal)
var res struct {
Code string `json:"respcode"`
Msg string `json:"respdesc"`
OrderID string `json:"orderid"`
OutorderID string `json:"outorderid"`
}
if err = d.unicomHTTPGet(c, d.unicomFlowExchangeURL+"?"+urlVal, nil, &res); err != nil {
log.Error("unicom flow change url(%v) error(%v)", d.unicomFlowExchangeURL+"?"+urlVal, err)
return
}
b, _ := json.Marshal(&res)
log.Info("unicom flow url(%v) response(%s)", d.unicomFlowExchangeURL+"?"+urlVal, b)
msg = res.Msg
if res.Code != "0000" {
err = ecode.String(res.Code)
log.Error("unicom flow change url(%v) code(%v) msg(%v)", d.unicomFlowExchangeURL+"?"+urlVal, res.Code, res.Msg)
return
}
orderID = res.OrderID
outorderID = res.OutorderID
return
}
// PhoneVerification unicom phone verification
func (d *Dao) PhoneVerification(c context.Context, phone string, requestNo int64, ts time.Time) (msg string, err error) {
param := url.Values{}
param.Set("appkey", _unicomAppKey)
param.Set("apptx", strconv.FormatInt(requestNo, 10))
param.Set("method", _unicomMethodNumber)
param.Set("timestamp", ts.Format("2006-01-02 15:04:05"))
param.Set("usernumber", phone)
urlVal := d.urlParams(param)
urlVal = urlVal + "&" + d.sign(urlVal)
var res struct {
Code string `json:"respcode"`
Msg string `json:"respdesc"`
}
if err = d.unicomHTTPGet(c, d.unicomFlowExchangeURL+"?"+urlVal, nil, &res); err != nil {
log.Error("unicom phone url(%v) error(%v)", d.unicomFlowExchangeURL+"?"+urlVal, err)
return
}
b, _ := json.Marshal(&res)
log.Info("unicom phone url(%v) response(%s)", d.unicomFlowExchangeURL+"?"+urlVal, b)
msg = res.Msg
if res.Code != "0000" {
err = ecode.String(res.Code)
log.Error("unicom phone url(%v) code(%v) msg(%v)", d.unicomFlowExchangeURL+"?"+urlVal, res.Code, res.Msg)
return
}
return
}
// FlowPre unicom phone flow pre
func (d *Dao) FlowPre(c context.Context, phone int, requestNo int64, ts time.Time) (msg string, err error) {
param := url.Values{}
param.Set("appkey", _unicomAppKey)
param.Set("apptx", strconv.FormatInt(requestNo, 10))
param.Set("method", _unicomMethodFlowPre)
param.Set("timestamp", ts.Format("2006-01-02 15:04:05"))
param.Set("usernumber", strconv.Itoa(phone))
urlVal := d.urlParams(param)
urlVal = urlVal + "&" + d.sign(urlVal)
var res struct {
Code string `json:"respcode"`
Notice string `json:"noticecontent"`
Msg string `json:"respdesc"`
}
if err = d.unicomHTTPGet(c, d.unicomFlowExchangeURL+"?"+urlVal, nil, &res); err != nil {
log.Error("unicom flowpre url(%v) error(%v)", d.unicomFlowExchangeURL+"?"+urlVal, err)
return
}
b, _ := json.Marshal(&res)
log.Info("unicom flowpre url(%v) response(%s)", d.unicomFlowExchangeURL+"?"+urlVal, b)
msg = res.Msg
if res.Code != "0000" {
if res.Code == "0001" {
err = ecode.String(res.Code)
msg = res.Notice
} else {
err = ecode.String(res.Code)
log.Error("unicom flowpre url(%v) code(%v) msg(%v)", d.unicomFlowExchangeURL+"?"+urlVal, res.Code, res.Msg)
}
return
}
return
}
// FlowQry unicom phone qryflowchange
func (d *Dao) FlowQry(c context.Context, phone int, requestNo int64, outorderid, orderid string, ts time.Time) (orderstatus, msg string, err error) {
param := url.Values{}
param.Set("appkey", _unicomAppKey)
param.Set("apptx", strconv.FormatInt(requestNo, 10))
param.Set("method", _unicomMethodQryFlowChange)
param.Set("orderid", orderid)
param.Set("outorderid", outorderid)
param.Set("timestamp", ts.Format("2006-01-02 15:04:05"))
param.Set("usernumber", strconv.Itoa(phone))
urlVal := d.urlParams(param)
urlVal = urlVal + "&" + d.sign(urlVal)
var res struct {
Code string `json:"respcode"`
Orderstatus string `json:"orderstatus"`
Failurtype string `json:"failurtype"`
Msg string `json:"respdesc"`
}
if err = d.unicomHTTPGet(c, d.unicomFlowExchangeURL+"?"+urlVal, nil, &res); err != nil {
log.Error("unicom flowQry url(%v) error(%v)", d.unicomFlowExchangeURL+"?"+urlVal, err)
return
}
b, _ := json.Marshal(&res)
log.Info("unicom flowQry url(%v) response(%s)", d.unicomFlowExchangeURL+"?"+urlVal, b)
msg = res.Msg
if res.Code != "0000" {
err = ecode.String(res.Code)
log.Error("unicom flowQry url(%v) code(%v) msg(%v)", d.unicomFlowExchangeURL+"?"+urlVal, res.Code, res.Msg)
return
}
orderstatus = res.Orderstatus
return
}
// broadbandHTTPGet http get
func (d *Dao) broadbandHTTPGet(c context.Context, urlStr string, params url.Values, res interface{}) (err error) {
return d.wallHTTP(c, d.client, http.MethodGet, urlStr, params, res)
}
// broadbandHTTPPost http post
func (d *Dao) broadbandHTTPPost(c context.Context, urlStr string, params url.Values, res interface{}) (err error) {
return d.wallHTTP(c, d.client, http.MethodPost, urlStr, params, res)
}
// unicomHTTPGet http get
func (d *Dao) unicomHTTPGet(c context.Context, urlStr string, params url.Values, res interface{}) (err error) {
return d.wallHTTP(c, d.uclient, http.MethodGet, urlStr, params, res)
}
// wallHTTP http
func (d *Dao) wallHTTP(c context.Context, client *httpx.Client, method, urlStr string, params url.Values, res interface{}) (err error) {
var (
req *http.Request
)
ru := urlStr
if params != nil {
ru = urlStr + "?" + params.Encode()
}
switch method {
case http.MethodGet:
req, err = http.NewRequest(http.MethodGet, ru, nil)
default:
req, err = http.NewRequest(http.MethodPost, urlStr, strings.NewReader(params.Encode()))
}
if err != nil {
log.Error("unicom_http.NewRequest url(%s) error(%v)", urlStr+"?"+params.Encode(), err)
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("X-BACKEND-BILI-REAL-IP", "")
return d.client.Do(c, req, &res)
}
func (d *Dao) desEncrypt(src, key []byte) (string, error) {
block, err := des.NewCipher(key)
if err != nil {
return "", err
}
bs := block.BlockSize()
src = d.pkcs5Padding(src, bs)
if len(src)%bs != 0 {
return "", errors.New("Need a multiple of the blocksize")
}
out := make([]byte, len(src))
dst := out
for len(src) > 0 {
block.Encrypt(dst, src[:bs])
src = src[bs:]
dst = dst[bs:]
}
encodeString := base64.StdEncoding.EncodeToString(out)
return encodeString, nil
}
func (d *Dao) pkcs5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func (d *Dao) urlParams(v url.Values) string {
if v == nil {
return ""
}
var buf bytes.Buffer
keys := make([]string, 0, len(v))
for k := range v {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
vs := v[k]
prefix := k + "="
for _, v := range vs {
if buf.Len() > 0 {
buf.WriteByte('&')
}
buf.WriteString(prefix)
buf.WriteString(v)
}
}
return buf.String()
}
func (d *Dao) sign(params string) string {
str := strings.Replace(params, "&", "$", -1)
str2 := strings.Replace(str, "=", "$", -1)
mh := md5.Sum([]byte(_unicomSecurity + "$" + str2 + "$" + _unicomSecurity))
signparam := url.Values{}
signparam.Set("sign", base64.StdEncoding.EncodeToString(mh[:]))
return signparam.Encode()
}

View File

@@ -0,0 +1,345 @@
package unicom
import (
"context"
"fmt"
"go-common/app/interface/main/app-wall/model/unicom"
"go-common/library/cache/memcache"
"go-common/library/log"
)
const (
_prefix = "unicoms_user_%v"
_userbindkey = "unicoms_user_bind_%d"
_userpackkey = "unicom_user_pack_%d"
_userflowkey = "unicom_user_flow_%v"
_userflowlistkey = "unicom_user_flow_list"
_userflowWait = "u_flowwait_%d"
)
func keyUnicom(usermob string) string {
return fmt.Sprintf(_prefix, usermob)
}
func keyUserBind(mid int64) string {
return fmt.Sprintf(_userbindkey, mid)
}
func keyUserPack(id int64) string {
return fmt.Sprintf(_userpackkey, id)
}
func keyUserFlow(key string) string {
return fmt.Sprintf(_userflowkey, key)
}
func keyUserflowWait(phone int) string {
return fmt.Sprintf(_userflowWait, phone)
}
// AddUnicomCache
func (d *Dao) AddUnicomCache(c context.Context, usermob string, u []*unicom.Unicom) (err error) {
var (
key = keyUnicom(usermob)
conn = d.mc.Get(c)
)
if err = conn.Set(&memcache.Item{Key: key, Object: u, Flags: memcache.FlagJSON, Expiration: d.expire}); err != nil {
log.Error("addUnicomCache d.mc.Set(%s,%v) error(%v)", key, u, err)
}
conn.Close()
return
}
// UnicomCache
func (d *Dao) UnicomCache(c context.Context, usermob string) (u []*unicom.Unicom, err error) {
var (
key = keyUnicom(usermob)
conn = d.mc.Get(c)
r *memcache.Item
)
defer conn.Close()
if r, err = conn.Get(key); err != nil {
log.Error("unicomCache MemchDB.Get(%s) error(%v)", key, err)
return
}
if err = conn.Scan(r, &u); err != nil {
log.Error("r.Scan(%s) error(%v)", r.Value, err)
}
return
}
// UpdateUnicomCache
func (d *Dao) UpdateUnicomCache(c context.Context, usermob string, u *unicom.Unicom) (err error) {
var (
us []*unicom.Unicom
unicoms []*unicom.Unicom
uspid = map[int]struct{}{}
)
if u.Spid == 979 && u.TypeInt == 1 {
return d.DeleteUnicomCache(c, usermob)
}
if us, err = d.UnicomCache(c, usermob); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("d.UnicomCache error(%v)", err)
return
}
if len(us) > 0 {
for _, um := range us {
if um.Spid == 979 && um.TypeInt == 1 {
return d.DeleteUnicomCache(c, usermob)
}
tmp := &unicom.Unicom{}
*tmp = *um
if tmp.Spid == u.Spid {
tmp = u
uspid[u.Spid] = struct{}{}
}
unicoms = append(unicoms, tmp)
}
if _, ok := uspid[u.Spid]; !ok {
unicoms = append(unicoms, u)
}
if err = d.AddUnicomCache(c, usermob, unicoms); err != nil {
log.Error("d.AddUnicomCache error(%v)", err)
return
}
}
return
}
// DeleteUnicomCache
func (d *Dao) DeleteUnicomCache(c context.Context, usermob string) (err error) {
var (
key = keyUnicom(usermob)
conn = d.mc.Get(c)
)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("unicomCache MemchDB.Delete(%s) error(%v)", key, err)
return
}
return
}
// UserBindCache user bind cache
func (d *Dao) UserBindCache(c context.Context, mid int64) (ub *unicom.UserBind, err error) {
var (
key = keyUserBind(mid)
conn = d.mc.Get(c)
r *memcache.Item
)
defer conn.Close()
if r, err = conn.Get(key); err != nil {
log.Error("UserBindCache MemchDB.Get(%s) error(%v)", key, err)
return
}
if err = conn.Scan(r, &ub); err != nil {
log.Error("r.Scan(%s) error(%v)", r.Value, err)
}
return
}
// AddUserBindCache add user user bind cache
func (d *Dao) AddUserBindCache(c context.Context, mid int64, ub *unicom.UserBind) (err error) {
var (
key = keyUserBind(mid)
conn = d.mc.Get(c)
)
if err = conn.Set(&memcache.Item{Key: key, Object: ub, Flags: memcache.FlagJSON, Expiration: 0}); err != nil {
log.Error("AddUserBindCache d.mc.Set(%s,%v) error(%v)", key, ub, err)
}
conn.Close()
return
}
// DeleteUserBindCache delete user bind cache
func (d *Dao) DeleteUserBindCache(c context.Context, mid int64) (err error) {
var (
key = keyUserBind(mid)
conn = d.mc.Get(c)
)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("DeleteUserBindCache MemchDB.Delete(%s) error(%v)", key, err)
return
}
return
}
// UserPackCache user packs
func (d *Dao) UserPackCache(c context.Context, id int64) (res *unicom.UserPack, err error) {
var (
key = keyUserPack(id)
conn = d.mc.Get(c)
r *memcache.Item
)
defer conn.Close()
if r, err = conn.Get(key); err != nil {
log.Error("UserBindCache MemchDB.Get(%s) error(%v)", key, err)
return
}
if err = conn.Scan(r, &res); err != nil {
log.Error("r.Scan(%s) error(%v)", r.Value, err)
}
return
}
// AddUserPackCache add user pack cache
func (d *Dao) AddUserPackCache(c context.Context, id int64, u *unicom.UserPack) (err error) {
var (
key = keyUserPack(id)
conn = d.mc.Get(c)
)
if err = conn.Set(&memcache.Item{Key: key, Object: u, Flags: memcache.FlagJSON, Expiration: d.flowKeyExpired}); err != nil {
log.Error("AddUserPackCache d.mc.Set(%s,%v) error(%v)", key, u, err)
}
conn.Close()
return
}
// DeleteUserPackCache delete user pack cache
func (d *Dao) DeleteUserPackCache(c context.Context, id int64) (err error) {
var (
key = keyUserPack(id)
conn = d.mc.Get(c)
)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("DeleteUserPackCache MemchDB.Delete(%s) error(%v)", key, err)
return
}
return
}
//UserFlowCache unicom flow cache
func (d *Dao) UserFlowCache(c context.Context, keyStr string) (err error) {
var (
key = keyUserFlow(keyStr)
conn = d.mc.Get(c)
r *memcache.Item
res struct{}
)
defer conn.Close()
if r, err = conn.Get(key); err != nil {
log.Error("UserFlowCache MemchDB.Get(%s) error(%v)", key, err)
return
}
if err = conn.Scan(r, &res); err != nil {
log.Error("r.Scan(%s) error(%v)", r.Value, err)
}
return
}
// AddUserFlowCache add user pack cache
func (d *Dao) AddUserFlowCache(c context.Context, keyStr string) (err error) {
var (
key = keyUserFlow(keyStr)
conn = d.mc.Get(c)
)
if err = conn.Set(&memcache.Item{Key: key, Object: struct{}{}, Flags: memcache.FlagJSON, Expiration: d.flowKeyExpired}); err != nil {
log.Error("AddUserPackCache d.mc.Set(%s) error(%v)", key, err)
}
conn.Close()
return
}
// DeleteUserPackCache delete user pack cache
func (d *Dao) DeleteUserFlowCache(c context.Context, keyStr string) (err error) {
var (
key = keyUserFlow(keyStr)
conn = d.mc.Get(c)
)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("DeleteUserFlowCache MemchDB.Delete(%s) error(%v)", key, err)
return
}
return
}
//UserFlowListCache unicom flow cache
func (d *Dao) UserFlowListCache(c context.Context) (res map[string]*unicom.UnicomUserFlow, err error) {
var (
key = _userflowlistkey
conn = d.mc.Get(c)
r *memcache.Item
)
defer conn.Close()
if r, err = conn.Get(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("UserFlowListCache MemchDB.Get(%s) error(%v)", key, err)
return
}
if err = conn.Scan(r, &res); err != nil {
log.Error("r.Scan(%s) error(%v)", r.Value, err)
}
return
}
// AddUserFlowListCache add user pack cache
func (d *Dao) AddUserFlowListCache(c context.Context, list map[string]*unicom.UnicomUserFlow) (err error) {
var (
key = _userflowlistkey
conn = d.mc.Get(c)
)
if err = conn.Set(&memcache.Item{Key: key, Object: list, Flags: memcache.FlagJSON, Expiration: d.flowKeyExpired}); err != nil {
log.Error("AddUserFlowListCache d.mc.Set(%s,%v) error(%v)", key, list, err)
}
conn.Close()
return
}
//UserFlowWaitCache unicom flow wait
func (d *Dao) UserFlowWaitCache(c context.Context, phone int) (err error) {
var (
key = keyUserflowWait(phone)
conn = d.mc.Get(c)
r *memcache.Item
res struct{}
)
defer conn.Close()
if r, err = conn.Get(key); err != nil {
log.Error("UserFlowWaitCache MemchDB.Get(%s) error(%v)", key, err)
return
}
if err = conn.Scan(r, &res); err != nil {
log.Error("r.Scan(%s) error(%v)", r.Value, err)
}
return
}
// AddUserFlowWaitCache add user flow wait
func (d *Dao) AddUserFlowWaitCache(c context.Context, phone int) (err error) {
var (
key = keyUserflowWait(phone)
conn = d.mc.Get(c)
)
if err = conn.Set(&memcache.Item{Key: key, Object: struct{}{}, Flags: memcache.FlagJSON, Expiration: d.flowWait}); err != nil {
log.Error("AddUserFlowWaitCache d.mc.Set(%s) error(%v)", key, err)
}
conn.Close()
return
}

View File

@@ -0,0 +1,55 @@
package unicom
import (
"context"
"fmt"
"time"
"go-common/app/interface/main/app-wall/model/unicom"
"go-common/library/database/elastic"
"go-common/library/log"
)
const (
_logUserKey = "log_user_action_91_%s"
)
func keyLogUser(now time.Time) string {
return fmt.Sprintf(_logUserKey, now.Format("2006_01"))
}
func timeToString(now time.Time) string {
return now.Format("2006-01-02 15:04:05")
}
// SearchUserBindLog unicom user bind log
func (d *Dao) SearchUserBindLog(ctx context.Context, mid int64, now time.Time) (ulogs []*unicom.UserLog, err error) {
var data struct {
Result []struct {
Ctime string `json:"ctime"`
ExtraData string `json:"extra_data"`
} `json:"result"`
}
var (
endtime = now.AddDate(0, -1, 0)
endWeekTime = now.AddDate(0, 0, -7)
)
req := d.es.NewRequest("log_user_action")
req.Index(keyLogUser(now), keyLogUser(endtime)).WhereEq("mid", mid).WhereIn("action", []string{"unicom_userpack_add", "unicom_userpack_deduct"}).
WhereRange("ctime", timeToString(endWeekTime), timeToString(now), elastic.RangeScopeLcRc).Order("ctime", "desc").
Pn(1).Ps(2000)
if err = req.Scan(ctx, &data); err != nil {
log.Error("search user bind params(%s) error(%v)", req.Params(), err)
return
}
for _, d := range data.Result {
ulog := &unicom.UserLog{}
if err = ulog.UserLogJSONChange(d.ExtraData); err != nil {
log.Error("ulog json change error(%v)", err)
continue
}
ulog.Ctime = d.Ctime
ulogs = append(ulogs, ulog)
}
return
}

View File

@@ -0,0 +1,442 @@
package unicom
import (
"context"
"database/sql"
"time"
"go-common/app/interface/main/app-wall/conf"
"go-common/app/interface/main/app-wall/model/unicom"
"go-common/library/cache/memcache"
"go-common/library/database/elastic"
xsql "go-common/library/database/sql"
"go-common/library/log"
httpx "go-common/library/net/http/blademaster"
)
const (
//unicom
_inOrderSyncSQL = `INSERT IGNORE INTO unicom_order (usermob,cpid,spid,type,ordertime,canceltime,endtime,channelcode,province,area,ordertype,videoid,ctime,mtime)
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE cpid=?,spid=?,type=?,ordertime=?,canceltime=?,endtime=?,channelcode=?,province=?,area=?,ordertype=?,videoid=?,mtime=?`
_inAdvanceSyncSQL = `INSERT IGNORE INTO unicom_order_advance (usermob,userphone,cpid,spid,ordertime,channelcode,province,area,ctime,mtime)
VALUES(?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE cpid=?,spid=?,ordertime=?,channelcode=?,province=?,area=?,mtime=?`
_upOrderFlowSQL = `UPDATE unicom_order SET time=?,flowbyte=?,mtime=? WHERE usermob=?`
_orderUserSyncSQL = `SELECT usermob,cpid,spid,type,ordertime,canceltime,endtime,channelcode,province,area,ordertype,videoid,time,flowbyte FROM unicom_order WHERE usermob=?
ORDER BY type DESC`
_inIPSyncSQL = `INSERT IGNORE INTO unicom_ip (ipbegion,ipend,provinces,isopen,opertime,sign,ctime,mtime) VALUES(?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE
ipbegion=?,ipend=?,provinces=?,isopen=?,opertime=?,sign=?,mtime=?`
_ipSyncSQL = `SELECT ipbegion,ipend FROM unicom_ip WHERE isopen=1`
//pack
_inPackSQL = `INSERT IGNORE INTO unicom_pack (usermob,mid) VALUES(?,?)`
_packSQL = `SELECT usermob,mid FROM unicom_pack WHERE usermob=?`
// update unicom ip
_inUnicomIPSyncSQL = `INSERT IGNORE INTO unicom_ip (ipbegion,ipend,isopen,ctime,mtime) VALUES(?,?,?,?,?) ON DUPLICATE KEY UPDATE
ipbegion=?,ipend=?,isopen=?,mtime=?`
_upUnicomIPSQL = `UPDATE unicom_ip SET isopen=?,mtime=? WHERE ipbegion=? AND ipend=?`
// unicom integral change
_inUserBindSQL = `INSERT IGNORE INTO unicom_user_bind (usermob,phone,mid,state,integral,flow) VALUES(?,?,?,?,?,?) ON DUPLICATE KEY UPDATE
phone=?,state=?`
_userBindSQL = `SELECT usermob,phone,mid,state,integral,flow,monthlytime FROM unicom_user_bind WHERE state=1 AND mid=?`
_userBindPhoneMidSQL = `SELECT mid FROM unicom_user_bind WHERE phone=? AND state=1`
_upUserIntegralSQL = `UPDATE unicom_user_bind SET integral=?,flow=? WHERE phone=? AND mid=?`
_userPacksSQL = `SELECT id,ptype,pdesc,amount,capped,integral,param FROM unicom_user_packs WHERE state=1`
_userPacksByIDSQL = `SELECT id,ptype,pdesc,amount,capped,integral,param,state FROM unicom_user_packs WHERE id=?`
_upUserPacksSQL = `UPDATE unicom_user_packs SET amount=?,state=? WHERE id=?`
_inUserPackLogSQL = `INSERT INTO unicom_user_packs_log (phone,usermob,mid,request_no,ptype,integral,pdesc) VALUES (?,?,?,?,?,?,?)`
_userBindOldSQL = `SELECT usermob,phone,mid,state,integral,flow FROM unicom_user_bind WHERE phone=? ORDER BY mtime DESC limit 1`
_userPacksLogSQL = `SELECT phone,integral,ptype,pdesc FROM unicom_user_packs_log WHERE mtime>=? AND mtime<?`
)
type Dao struct {
db *xsql.DB
client *httpx.Client
uclient *httpx.Client
//unicom
inOrderSyncSQL *xsql.Stmt
inAdvanceSyncSQL *xsql.Stmt
upOrderFlowSQL *xsql.Stmt
orderUserSyncSQL *xsql.Stmt
inIPSyncSQL *xsql.Stmt
ipSyncSQL *xsql.Stmt
// unicom integral change
inUserBindSQL *xsql.Stmt
userBindSQL *xsql.Stmt
userBindPhoneMidSQL *xsql.Stmt
upUserIntegralSQL *xsql.Stmt
userPacksSQL *xsql.Stmt
userPacksByIDSQL *xsql.Stmt
upUserPacksSQL *xsql.Stmt
inUserPackLogSQL *xsql.Stmt
userBindOldSQL *xsql.Stmt
//pack
inPackSQL *xsql.Stmt
packSQL *xsql.Stmt
// memcache
mc *memcache.Pool
expire int32
flowKeyExpired int32
flowWait int32
// unicom url
unicomIPURL string
unicomFlowExchangeURL string
// order url
orderURL string
ordercancelURL string
sendsmscodeURL string
smsNumberURL string
// elastic
es *elastic.Elastic
}
func New(c *conf.Config) (d *Dao) {
d = &Dao{
db: xsql.NewMySQL(c.MySQL.Show),
client: httpx.NewClient(conf.Conf.HTTPBroadband),
uclient: httpx.NewClient(conf.Conf.HTTPUnicom),
// unicom url
unicomIPURL: c.Host.Unicom + _unicomIPURL,
unicomFlowExchangeURL: c.Host.UnicomFlow + _unicomFlowExchangeURL,
// memcache
mc: memcache.NewPool(c.Memcache.Operator.Config),
expire: int32(time.Duration(c.Memcache.Operator.Expire) / time.Second),
flowKeyExpired: int32(time.Duration(c.Unicom.KeyExpired) / time.Second),
flowWait: int32(time.Duration(c.Unicom.FlowWait) / time.Second),
// order url
orderURL: c.Host.Broadband + _orderURL,
ordercancelURL: c.Host.Broadband + _ordercancelURL,
sendsmscodeURL: c.Host.Broadband + _sendsmscodeURL,
smsNumberURL: c.Host.Broadband + _smsNumberURL,
// elastic
es: elastic.NewElastic(nil),
}
d.inOrderSyncSQL = d.db.Prepared(_inOrderSyncSQL)
d.inAdvanceSyncSQL = d.db.Prepared(_inAdvanceSyncSQL)
d.upOrderFlowSQL = d.db.Prepared(_upOrderFlowSQL)
d.orderUserSyncSQL = d.db.Prepared(_orderUserSyncSQL)
d.inIPSyncSQL = d.db.Prepared(_inIPSyncSQL)
d.ipSyncSQL = d.db.Prepared(_ipSyncSQL)
// unicom integral change
d.inUserBindSQL = d.db.Prepared(_inUserBindSQL)
d.userBindSQL = d.db.Prepared(_userBindSQL)
d.userBindPhoneMidSQL = d.db.Prepared(_userBindPhoneMidSQL)
d.upUserIntegralSQL = d.db.Prepared(_upUserIntegralSQL)
d.userPacksSQL = d.db.Prepared(_userPacksSQL)
d.upUserPacksSQL = d.db.Prepared(_upUserPacksSQL)
d.userPacksByIDSQL = d.db.Prepared(_userPacksByIDSQL)
d.inUserPackLogSQL = d.db.Prepared(_inUserPackLogSQL)
d.userBindOldSQL = d.db.Prepared(_userBindOldSQL)
//pack
d.inPackSQL = d.db.Prepared(_inPackSQL)
d.packSQL = d.db.Prepared(_packSQL)
return
}
// InOrdersSync insert OrdersSync
func (d *Dao) InOrdersSync(ctx context.Context, usermob string, u *unicom.UnicomJson, now time.Time) (row int64, err error) {
res, err := d.inOrderSyncSQL.Exec(ctx, usermob,
u.Cpid, u.Spid, u.TypeInt, u.Ordertime, u.Canceltime, u.Endtime,
u.Channelcode, u.Province, u.Area, u.Ordertypes, u.Videoid, now, now,
u.Cpid, u.Spid, u.TypeInt, u.Ordertime, u.Canceltime, u.Endtime,
u.Channelcode, u.Province, u.Area, u.Ordertypes, u.Videoid, now)
if err != nil {
log.Error("d.inOrderSyncSQL.Exec error(%v)", err)
return
}
utmp := &unicom.Unicom{}
utmp.UnicomJSONTOUincom(usermob, u)
if err = d.UpdateUnicomCache(ctx, usermob, utmp); err != nil {
log.Error("d.UpdateUnicomCache usermob(%v) error(%v)", usermob, err)
return 0, err
}
return res.RowsAffected()
}
// InAdvanceSync insert AdvanceSync
func (d *Dao) InAdvanceSync(ctx context.Context, usermob string, u *unicom.UnicomJson, now time.Time) (row int64, err error) {
res, err := d.inAdvanceSyncSQL.Exec(ctx, usermob, u.Userphone,
u.Cpid, u.Spid, u.Ordertime, u.Channelcode, u.Province, u.Area, now, now,
u.Cpid, u.Spid, u.Ordertime, u.Channelcode, u.Province, u.Area, now)
if err != nil {
log.Error("d.inAdvanceSyncSQL.Exec error(%v)", err)
return 0, err
}
return res.RowsAffected()
}
// FlowSync update OrdersSync
func (d *Dao) FlowSync(ctx context.Context, flowbyte int, usermob, time string, now time.Time) (row int64, err error) {
res, err := d.upOrderFlowSQL.Exec(ctx, time, flowbyte, now, usermob)
if err != nil {
log.Error("d.upOrderFlowSQL.Exec error(%v)", err)
return 0, err
}
return res.RowsAffected()
}
// OrdersUserFlow select user OrdersSync
func (d *Dao) OrdersUserFlow(ctx context.Context, usermob string) (res []*unicom.Unicom, err error) {
rows, err := d.orderUserSyncSQL.Query(ctx, usermob)
if err != nil {
log.Error("query error (%v)", err)
return
}
defer rows.Close()
for rows.Next() {
u := &unicom.Unicom{}
if err = rows.Scan(&u.Usermob, &u.Cpid, &u.Spid, &u.TypeInt, &u.Ordertime, &u.Canceltime, &u.Endtime, &u.Channelcode, &u.Province,
&u.Area, &u.Ordertypes, &u.Videoid, &u.Time, &u.Flowbyte); err != nil {
log.Error("OrdersUserFlow row.Scan err (%v)", err)
return
}
u.UnicomChange()
res = append(res, u)
}
return
}
// InIPSync insert IpSync
func (d *Dao) InIPSync(ctx context.Context, u *unicom.UnicomIpJson, now time.Time) (row int64, err error) {
res, err := d.inIPSyncSQL.Exec(ctx, u.Ipbegin, u.Ipend, u.Provinces, u.Isopen, u.Opertime, u.Sign, now, now,
u.Ipbegin, u.Ipend, u.Provinces, u.Isopen, u.Opertime, u.Sign, now)
if err != nil {
log.Error("d.inIPSyncSQL.Exec error(%v)", err)
return 0, err
}
return res.RowsAffected()
}
// InPack insert Privilege pack
func (d *Dao) InPack(ctx context.Context, usermob string, mid int64) (row int64, err error) {
res, err := d.inPackSQL.Exec(ctx, usermob, mid)
if err != nil {
log.Error("d.inPackSQL.Exec error(%v)", err)
return 0, err
}
return res.RowsAffected()
}
// Pack select Privilege pack
func (d *Dao) Pack(ctx context.Context, usermobStr string) (res map[string]map[int64]struct{}, err error) {
row := d.packSQL.QueryRow(ctx, usermobStr)
var (
usermob string
mid int64
)
res = map[string]map[int64]struct{}{}
if err = row.Scan(&usermob, &mid); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("OrdersUserFlow rows.Scan err (%v)", err)
}
}
if user, ok := res[usermob]; !ok {
res[usermob] = map[int64]struct{}{
mid: struct{}{},
}
} else {
user[mid] = struct{}{}
}
return
}
// IPSync select all ipSync
func (d *Dao) IPSync(ctx context.Context) (res []*unicom.UnicomIP, err error) {
rows, err := d.ipSyncSQL.Query(ctx)
if err != nil {
log.Error("query error (%v)", err)
return
}
defer rows.Close()
res = []*unicom.UnicomIP{}
for rows.Next() {
u := &unicom.UnicomIP{}
if err = rows.Scan(&u.Ipbegin, &u.Ipend); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
u.UnicomIPChange()
res = append(res, u)
}
return
}
// InUnicomIPSync insert or update unicom_ip
func (d *Dao) InUnicomIPSync(tx *xsql.Tx, u *unicom.UnicomIP, now time.Time) (row int64, err error) {
res, err := tx.Exec(_inUnicomIPSyncSQL, u.Ipbegin, u.Ipend, 1, now, now,
u.Ipbegin, u.Ipend, 1, now)
if err != nil {
log.Error("tx.inUnicomIPSyncSQL.Exec error(%v)", err)
return 0, err
}
return res.RowsAffected()
}
// UpUnicomIP update unicom_ip state
func (d *Dao) UpUnicomIP(tx *xsql.Tx, ipstart, ipend, state int, now time.Time) (row int64, err error) {
res, err := tx.Exec(_upUnicomIPSQL, state, now, ipstart, ipend)
if err != nil {
log.Error("tx.upUnicomIPSQL.Exec error(%v)", err)
return 0, err
}
return res.RowsAffected()
}
// InUserBind unicom insert user bind
func (d *Dao) InUserBind(ctx context.Context, ub *unicom.UserBind) (row int64, err error) {
res, err := d.inUserBindSQL.Exec(ctx, ub.Usermob, ub.Phone, ub.Mid, ub.State, ub.Integral, ub.Flow, ub.Phone, ub.State)
if err != nil {
log.Error("insert user bind error(%v)", err)
return
}
return res.RowsAffected()
}
// UserBind unicom select user bind
func (d *Dao) UserBind(ctx context.Context, mid int64) (res *unicom.UserBind, err error) {
row := d.userBindSQL.QueryRow(ctx, mid)
if row == nil {
log.Error("userBindSQL is null")
return
}
res = &unicom.UserBind{}
if err = row.Scan(&res.Usermob, &res.Phone, &res.Mid, &res.State, &res.Integral, &res.Flow, &res.Monthly); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("userBindSQL row.Scan error(%v)", err)
}
res = nil
return
}
return
}
// UserPacks user pack list
func (d *Dao) UserPacks(ctx context.Context) (res []*unicom.UserPack, err error) {
rows, err := d.userPacksSQL.Query(ctx)
if err != nil {
log.Error("user pack sql error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
u := &unicom.UserPack{}
if err = rows.Scan(&u.ID, &u.Type, &u.Desc, &u.Amount, &u.Capped, &u.Integral, &u.Param); err != nil {
log.Error("user pack sql error(%v)", err)
return
}
res = append(res, u)
}
return
}
// UserPackByID user pack by id
func (d *Dao) UserPackByID(ctx context.Context, id int64) (res map[int64]*unicom.UserPack, err error) {
res = map[int64]*unicom.UserPack{}
row := d.userPacksByIDSQL.QueryRow(ctx, id)
if row == nil {
log.Error("user pack sql is null")
return
}
u := &unicom.UserPack{}
if err = row.Scan(&u.ID, &u.Type, &u.Desc, &u.Amount, &u.Capped, &u.Integral, &u.Param, &u.State); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("userPacksByIDSQL row.Scan error(%v)", err)
}
return
}
res[id] = u
return
}
// UpUserPacks update user packs
func (d *Dao) UpUserPacks(ctx context.Context, u *unicom.UserPack, id int64) (row int64, err error) {
res, err := d.upUserPacksSQL.Exec(ctx, u.Amount, u.State, id)
if err != nil {
log.Error("update user pack sql error(%v)", err)
return 0, err
}
return res.RowsAffected()
}
// UpUserIntegral update unicom user integral
func (d *Dao) UpUserIntegral(ctx context.Context, ub *unicom.UserBind) (row int64, err error) {
res, err := d.upUserIntegralSQL.Exec(ctx, ub.Integral, ub.Flow, ub.Phone, ub.Mid)
if err != nil {
log.Error("update user integral sql error(%v)", err)
return 0, err
}
return res.RowsAffected()
}
// UserBindPhoneMid mid by phone
func (d *Dao) UserBindPhoneMid(ctx context.Context, phone string) (mid int64, err error) {
row := d.userBindPhoneMidSQL.QueryRow(ctx, phone)
if row == nil {
log.Error("user pack sql is null")
return
}
if err = row.Scan(&mid); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("userPacksByIDSQL row.Scan error(%v)", err)
}
return
}
return
}
// InUserPackLog insert unicom user pack log
func (d *Dao) InUserPackLog(ctx context.Context, u *unicom.UserPackLog) (row int64, err error) {
res, err := d.inUserPackLogSQL.Exec(ctx, u.Phone, u.Usermob, u.Mid, u.RequestNo, u.Type, u.Integral, u.Desc)
if err != nil {
log.Error("insert user pack log integral sql error(%v)", err)
return 0, err
}
return res.RowsAffected()
}
// UserBindOld user by phone
func (d *Dao) UserBindOld(ctx context.Context, phone string) (res *unicom.UserBind, err error) {
row := d.userBindOldSQL.QueryRow(ctx, phone)
if row == nil {
log.Error("user bind old sql is null")
return
}
res = &unicom.UserBind{}
if err = row.Scan(&res.Usermob, &res.Phone, &res.Mid, &res.State, &res.Integral, &res.Flow); err != nil {
log.Error("userBindSQL row.Scan error(%v)", err)
res = nil
return
}
return
}
// UserPacksLog user pack logs
func (d *Dao) UserPacksLog(ctx context.Context, start, end time.Time) (res []*unicom.UserPackLog, err error) {
rows, err := d.db.Query(ctx, _userPacksLogSQL, start, end)
if err != nil {
log.Error("query error (%v)", err)
return
}
defer rows.Close()
for rows.Next() {
u := &unicom.UserPackLog{}
if err = rows.Scan(&u.Phone, &u.Integral, &u.Type, &u.UserDesc); err != nil {
log.Error("user packs log sql error(%v)", err)
return
}
res = append(res, u)
}
err = rows.Err()
return
}
// BeginTran begin a transacition
func (d *Dao) BeginTran(ctx context.Context) (tx *xsql.Tx, err error) {
return d.db.Begin(ctx)
}

View File

@@ -0,0 +1,547 @@
package unicom
import (
"context"
"flag"
"os"
"testing"
"time"
"go-common/app/interface/main/app-wall/conf"
"go-common/app/interface/main/app-wall/model/unicom"
. "github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func ctx() context.Context {
return context.Background()
}
func init() {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.app-svr.app-wall")
flag.Set("conf_token", "yvxLjLpTFMlbBbc9yWqysKLMigRHaaiJ")
flag.Set("tree_id", "2283")
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")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
time.Sleep(time.Second)
}
func TestOrder(t *testing.T) {
Convey("unicom Order", t, func() {
_, _, err := d.Order(ctx(), "", "", 0)
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestCancelOrder(t *testing.T) {
Convey("unicom CancelOrder", t, func() {
_, _, err := d.CancelOrder(ctx(), "")
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestOrdersUserFlow(t *testing.T) {
Convey("unicom OrdersUserFlow", t, func() {
_, err := d.OrdersUserFlow(ctx(), "")
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestPack(t *testing.T) {
Convey("unicom Pack", t, func() {
_, err := d.Pack(ctx(), "")
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestIPSync(t *testing.T) {
Convey("unicom IPSync", t, func() {
_, err := d.IPSync(ctx())
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestUnicomIP(t *testing.T) {
Convey("unicom UnicomIP", t, func() {
_, err := d.UnicomIP(ctx(), time.Now())
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestSendSmsCode(t *testing.T) {
Convey("unicom SendSmsCode", t, func() {
_, err := d.SendSmsCode(ctx(), "1")
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestSmsNumber(t *testing.T) {
Convey("unicom SmsNumber", t, func() {
_, _, err := d.SmsNumber(ctx(), "1", 1)
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestFlowExchange(t *testing.T) {
Convey("unicom FlowExchange", t, func() {
_, _, _, err := d.FlowExchange(ctx(), 1, "1", 1, time.Now())
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestPhoneVerification(t *testing.T) {
Convey("unicom PhoneVerification", t, func() {
_, err := d.PhoneVerification(ctx(), "1", 1, time.Now())
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestFlowPre(t *testing.T) {
Convey("unicom FlowPre", t, func() {
_, err := d.FlowPre(ctx(), 1, 1, time.Now())
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestFlowQry(t *testing.T) {
Convey("unicom FlowQry", t, func() {
_, _, err := d.FlowQry(ctx(), 1, 1, "1", "1", time.Now())
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestAddUnicomCache(t *testing.T) {
Convey("unicom AddUnicomCache", t, func() {
err := d.AddUnicomCache(ctx(), "1", nil)
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestUnicomCache(t *testing.T) {
Convey("unicom UnicomCache", t, func() {
_, err := d.UnicomCache(ctx(), "1")
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestUpdateUnicomCache(t *testing.T) {
Convey("unicom UpdateUnicomCache", t, func() {
err := d.UpdateUnicomCache(ctx(), "1", &unicom.Unicom{})
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestDeleteUnicomCache(t *testing.T) {
Convey("unicom DeleteUnicomCache", t, func() {
err := d.DeleteUnicomCache(ctx(), "1")
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestUserBindCache(t *testing.T) {
Convey("unicom UserBindCache", t, func() {
_, err := d.UserBindCache(ctx(), 1)
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestAddUserBindCache(t *testing.T) {
Convey("unicom AddUserBindCache", t, func() {
err := d.AddUserBindCache(ctx(), 1, nil)
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestDeleteUserBindCache(t *testing.T) {
Convey("unicom DeleteUserBindCache", t, func() {
err := d.DeleteUserBindCache(ctx(), 1)
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestUserPackCache(t *testing.T) {
Convey("unicom UserPackCache", t, func() {
_, err := d.UserPackCache(ctx(), 1)
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestAddUserPackCache(t *testing.T) {
Convey("unicom AddUserPackCache", t, func() {
err := d.AddUserPackCache(ctx(), 1, nil)
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestDeleteUserPackCache(t *testing.T) {
Convey("unicom DeleteUserPackCache", t, func() {
err := d.DeleteUserPackCache(ctx(), 1)
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestUserFlowCache(t *testing.T) {
Convey("unicom UserFlowCache", t, func() {
err := d.UserFlowCache(ctx(), "1")
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestAddUserFlowCache(t *testing.T) {
Convey("unicom AddUserFlowCache", t, func() {
err := d.AddUserFlowCache(ctx(), "1")
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestDeleteUserFlowCache(t *testing.T) {
Convey("unicom DeleteUserFlowCache", t, func() {
err := d.DeleteUserFlowCache(ctx(), "1")
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestUserFlowListCache(t *testing.T) {
Convey("unicom UserFlowListCache", t, func() {
_, err := d.UserFlowListCache(ctx())
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestAddUserFlowListCache(t *testing.T) {
Convey("unicom AddUserFlowListCache", t, func() {
err := d.AddUserFlowListCache(ctx(), nil)
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestUserFlowWaitCache(t *testing.T) {
Convey("unicom UserFlowWaitCache", t, func() {
err := d.UserFlowWaitCache(ctx(), 1)
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestAddUserFlowWaitCache(t *testing.T) {
Convey("unicom AddUserFlowWaitCache", t, func() {
err := d.AddUserFlowWaitCache(ctx(), 1)
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestSearchUserBindLog(t *testing.T) {
Convey("unicom SearchUserBindLog", t, func() {
_, err := d.SearchUserBindLog(ctx(), 1, time.Now())
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestInOrdersSync(t *testing.T) {
Convey("unicom InOrdersSync", t, func() {
_, err := d.InOrdersSync(ctx(), "", &unicom.UnicomJson{}, time.Now())
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestInAdvanceSync(t *testing.T) {
Convey("unicom InAdvanceSync", t, func() {
_, err := d.InAdvanceSync(ctx(), "", &unicom.UnicomJson{}, time.Now())
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestFlowSync(t *testing.T) {
Convey("unicom FlowSync", t, func() {
_, err := d.FlowSync(ctx(), 1, "", "", time.Now())
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestInIPSync(t *testing.T) {
Convey("unicom InIPSync", t, func() {
_, err := d.InIPSync(ctx(), &unicom.UnicomIpJson{}, time.Now())
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestInPack(t *testing.T) {
Convey("unicom InPack", t, func() {
_, err := d.InPack(ctx(), "", 1)
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestInUnicomIPSync(t *testing.T) {
Convey("unicom InUnicomIPSync", t, func() {
tx, _ := d.BeginTran(ctx())
_, err := d.InUnicomIPSync(tx, &unicom.UnicomIP{}, time.Now())
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestUpUnicomIP(t *testing.T) {
Convey("unicom UpUnicomIP", t, func() {
tx, _ := d.BeginTran(ctx())
_, err := d.UpUnicomIP(tx, 1, 1, 1, time.Now())
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestInUserBind(t *testing.T) {
Convey("unicom InUserBind", t, func() {
_, err := d.InUserBind(ctx(), &unicom.UserBind{})
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestUserBind(t *testing.T) {
Convey("unicom UserBind", t, func() {
_, err := d.UserBind(ctx(), 1)
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestUserPacks(t *testing.T) {
Convey("unicom UserPacks", t, func() {
_, err := d.UserPacks(ctx())
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestUserPackByID(t *testing.T) {
Convey("unicom UserPackByID", t, func() {
_, err := d.UserPackByID(ctx(), 1)
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestUpUserPacks(t *testing.T) {
Convey("unicom UpUserPacks", t, func() {
_, err := d.UpUserPacks(ctx(), &unicom.UserPack{}, 1)
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestUpUserIntegral(t *testing.T) {
Convey("unicom UpUserIntegral", t, func() {
_, err := d.UpUserIntegral(ctx(), &unicom.UserBind{})
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestUserBindPhoneMid(t *testing.T) {
Convey("unicom UserBindPhoneMid", t, func() {
_, err := d.UserBindPhoneMid(ctx(), "")
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestInUserPackLog(t *testing.T) {
Convey("unicom InUserPackLog", t, func() {
_, err := d.InUserPackLog(ctx(), &unicom.UserPackLog{})
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestUserBindOld(t *testing.T) {
Convey("unicom UserBindOld", t, func() {
_, err := d.UserBindOld(ctx(), "")
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestUserPacksLog(t *testing.T) {
Convey("unicom UserPacksLog", t, func() {
_, err := d.UserPacksLog(ctx(), time.Now(), time.Now())
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestBeginTran(t *testing.T) {
Convey("unicom BeginTran", t, func() {
_, err := d.BeginTran(ctx())
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestBroadbandHTTPGet(t *testing.T) {
Convey("unicom broadbandHTTPGet", t, func() {
err := d.broadbandHTTPGet(ctx(), "", nil, nil)
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestBroadbandHTTPPost(t *testing.T) {
Convey("unicom broadbandHTTPPost", t, func() {
err := d.broadbandHTTPPost(ctx(), "", nil, nil)
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestUnicomHTTPGet(t *testing.T) {
Convey("unicom unicomHTTPGet", t, func() {
err := d.unicomHTTPGet(ctx(), "", nil, nil)
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestWallHTTP(t *testing.T) {
Convey("unicom wallHTTP", t, func() {
err := d.wallHTTP(ctx(), nil, "", "", nil, nil)
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestDesEncrypt(t *testing.T) {
Convey("unicom desEncrypt", t, func() {
_, err := d.desEncrypt([]byte{1}, []byte{1})
err = nil
So(err, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestPkcs5Padding(t *testing.T) {
Convey("unicom pkcs5Padding", t, func() {
d.pkcs5Padding([]byte{1}, 1)
// err = nil
So(nil, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestUrlParams(t *testing.T) {
Convey("unicom urlParams", t, func() {
d.urlParams(nil)
// err = nil
So(nil, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}
func TestSign(t *testing.T) {
Convey("unicom sign", t, func() {
d.sign("")
// err = nil
So(nil, ShouldBeNil)
// So(res, ShouldNotBeEmpty)
})
}