作者 唐旭辉

更新

  1 +package subscriber
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
  5 +)
  6 +
  7 +//订单数据修改触发的订阅事件
  8 +type OrderLogSubscriber struct {
  9 + transactionContext *transaction.TransactionContext
  10 +}
  11 +
  12 +func (subscriber OrderLogSubscriber) AddLog() error {
  13 + return nil
  14 +}
@@ -222,6 +222,7 @@ func (PartnerInfoService *PartnerInfoService) UpdatePartnerInfo(cmd *command.Upd @@ -222,6 +222,7 @@ func (PartnerInfoService *PartnerInfoService) UpdatePartnerInfo(cmd *command.Upd
222 }); err != nil { 222 }); err != nil {
223 return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) 223 return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
224 } 224 }
  225 + //TODO 修改为本地消息订阅
225 err = businessBonusSrv.EnableOrDisable(partnerInfo.Partner.Id) 226 err = businessBonusSrv.EnableOrDisable(partnerInfo.Partner.Id)
226 if err != nil { 227 if err != nil {
227 e := fmt.Sprintf("更新业务分红(partner_id=%d)数据失败:%s", partnerInfo.Partner.Id, err) 228 e := fmt.Sprintf("更新业务分红(partner_id=%d)数据失败:%s", partnerInfo.Partner.Id, err)
@@ -41,12 +41,10 @@ type Company struct { @@ -41,12 +41,10 @@ type Company struct {
41 UpdateAt time.Time `json:"updateAt"` 41 UpdateAt time.Time `json:"updateAt"`
42 // 删除时间 42 // 删除时间
43 DeleteAt time.Time `json:"deleteAt"` 43 DeleteAt time.Time `json:"deleteAt"`
  44 + //模块授权
  45 + // Permission map[string]bool `json:"permission"`
44 } 46 }
45 47
46 -// func (c Company) StatusIsOk() bool {  
47 -// return c.Status == companyStatusUsable  
48 -// }  
49 -  
50 func (c Company) EnableIsOk() bool { 48 func (c Company) EnableIsOk() bool {
51 return c.Enable == CompanyEnableYes 49 return c.Enable == CompanyEnableYes
52 } 50 }
  1 +package event
  2 +
  3 +const (
  4 + ORDER_GOOD_MODIFY_EVENT string = "order-good-modify-event"
  5 +)
  6 +
  7 +//OrderGoodModify 事件:修改订单中货品的数据
  8 +type OrderGoodModify struct {
  9 + //订单id
  10 + OrderId int64
  11 + //订单id
  12 + GoodId int64
  13 + //货品名称
  14 + GoodName string
  15 + //管理员id
  16 + AdminId int64
  17 + //更改的订单基础数据
  18 + UpdateGoodData map[string]string
  19 +}
  20 +
  21 +//EventType 事件名称
  22 +func (m OrderGoodModify) EventType() string {
  23 + return ORDER_GOOD_MODIFY_EVENT
  24 +}
  1 +package event
  2 +
  3 +const (
  4 + PAY_ORDER_GOOD_BONUS_EVENT string = "pay-order-good-bonus-event"
  5 +)
  6 +
  7 +//PayOrderGoodBonus 事件:支付订单中货品的分红
  8 +type PayOrderGoodBonus struct {
  9 + //订单id
  10 + OrderId int64
  11 + //订单中的货品id
  12 + GoodId int64
  13 + //管理员id
  14 + AdminId int64
  15 +}
  16 +
  17 +func (p PayOrderGoodBonus) EventType() string {
  18 + return PAY_ORDER_GOOD_BONUS_EVENT
  19 +}
@@ -16,6 +16,7 @@ func NewBusinessBonusService(tcx *transaction.TransactionContext) *BusinessBonus @@ -16,6 +16,7 @@ func NewBusinessBonusService(tcx *transaction.TransactionContext) *BusinessBonus
16 transactionContext: tcx, 16 transactionContext: tcx,
17 } 17 }
18 } 18 }
  19 +
19 func (srv BusinessBonusService) EnableOrDisable(parntnerId int64) error { 20 func (srv BusinessBonusService) EnableOrDisable(parntnerId int64) error {
20 var ( 21 var (
21 bonusDao, _ = dao.NewBusinessBonusDao(srv.transactionContext) 22 bonusDao, _ = dao.NewBusinessBonusDao(srv.transactionContext)
  1 +package domainService
  2 +
  3 +import (
  4 + coreDomain "github.com/linmadan/egglib-go/core/domain"
  5 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain/event"
  6 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
  7 +)
  8 +
  9 +type OrderServices struct {
  10 + coreDomain.BaseEventPublisher
  11 + transactionContext *transaction.TransactionContext
  12 +}
  13 +
  14 +//UpdateOrderGoodData 修改订单中的货品数据
  15 +func (serve OrderServices) UpdateOrderGoodData(orderId int64, goodId int64, adminId int64, updateData map[string]string) error {
  16 + var err error
  17 + //TODO数据更新操作
  18 + //事件发布
  19 + modifyEvent := event.OrderGoodModify{
  20 + OrderId: orderId,
  21 + GoodId: goodId,
  22 + AdminId: adminId,
  23 + UpdateGoodData: updateData,
  24 + }
  25 + if err = serve.Publish(modifyEvent); err != nil {
  26 + return err
  27 + }
  28 + return nil
  29 +}
  30 +
  31 +//PayOrderGoodBonus 支付订单中货品的分红
  32 +func (serve OrderServices) PayOrderGoodBonus(orderId int64, goodId int64, adminId int64) error {
  33 + var err error
  34 + //TODO
  35 + payEvent := event.PayOrderGoodBonus{
  36 + OrderId: orderId,
  37 + GoodId: goodId,
  38 + AdminId: adminId,
  39 + }
  40 + if err = serve.Publish(payEvent); err != nil {
  41 + return err
  42 + }
  43 + return nil
  44 +}
@@ -68,13 +68,15 @@ func (c *PartnerInfoController) CreatePartnerInfo() { @@ -68,13 +68,15 @@ func (c *PartnerInfoController) CreatePartnerInfo() {
68 Status: param.State, 68 Status: param.State,
69 PartnerCategory: param.PartnerType, 69 PartnerCategory: param.PartnerType,
70 CooperateTime: cooperateTime, 70 CooperateTime: cooperateTime,
71 - Salesman: []domain.Salesman{ 71 + CompanyId: companyId,
  72 + }
  73 + if len(param.SalesmanName) > 0 || len(param.Phone) > 0 {
  74 + cmd.Salesman = []domain.Salesman{
72 domain.Salesman{ 75 domain.Salesman{
73 Name: param.SalesmanName, 76 Name: param.SalesmanName,
74 Telephone: param.Phone, 77 Telephone: param.Phone,
75 }, 78 },
76 - },  
77 - CompanyId: companyId, 79 + }
78 } 80 }
79 if len(param.Area) > 0 { 81 if len(param.Area) > 0 {
80 cmd.RegionInfo = &domain.RegionInfo{ 82 cmd.RegionInfo = &domain.RegionInfo{
1 -# httplib  
2 -httplib is an libs help you to curl remote url.  
3 -  
4 -# How to use?  
5 -  
6 -## GET  
7 -you can use Get to crawl data.  
8 -  
9 - import "github.com/astaxie/beego/httplib"  
10 -  
11 - str, err := httplib.Get("http://beego.me/").String()  
12 - if err != nil {  
13 - // error  
14 - }  
15 - fmt.Println(str)  
16 -  
17 -## POST  
18 -POST data to remote url  
19 -  
20 - req := httplib.Post("http://beego.me/")  
21 - req.Param("username","astaxie")  
22 - req.Param("password","123456")  
23 - str, err := req.String()  
24 - if err != nil {  
25 - // error  
26 - }  
27 - fmt.Println(str)  
28 -  
29 -## Set timeout  
30 -  
31 -The default timeout is `60` seconds, function prototype:  
32 -  
33 - SetTimeout(connectTimeout, readWriteTimeout time.Duration)  
34 -  
35 -Example:  
36 -  
37 - // GET  
38 - httplib.Get("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second)  
39 -  
40 - // POST  
41 - httplib.Post("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second)  
42 -  
43 -  
44 -## Debug  
45 -  
46 -If you want to debug the request info, set the debug on  
47 -  
48 - httplib.Get("http://beego.me/").Debug(true)  
49 -  
50 -## Set HTTP Basic Auth  
51 -  
52 - str, err := Get("http://beego.me/").SetBasicAuth("user", "passwd").String()  
53 - if err != nil {  
54 - // error  
55 - }  
56 - fmt.Println(str)  
57 -  
58 -## Set HTTPS  
59 -  
60 -If request url is https, You can set the client support TSL:  
61 -  
62 - httplib.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})  
63 -  
64 -More info about the `tls.Config` please visit http://golang.org/pkg/crypto/tls/#Config  
65 -  
66 -## Set HTTP Version  
67 -  
68 -some servers need to specify the protocol version of HTTP  
69 -  
70 - httplib.Get("http://beego.me/").SetProtocolVersion("HTTP/1.1")  
71 -  
72 -## Set Cookie  
73 -  
74 -some http request need setcookie. So set it like this:  
75 -  
76 - cookie := &http.Cookie{}  
77 - cookie.Name = "username"  
78 - cookie.Value = "astaxie"  
79 - httplib.Get("http://beego.me/").SetCookie(cookie)  
80 -  
81 -## Upload file  
82 -  
83 -httplib support mutil file upload, use `req.PostFile()`  
84 -  
85 - req := httplib.Post("http://beego.me/")  
86 - req.Param("username","astaxie")  
87 - req.PostFile("uploadfile1", "httplib.pdf")  
88 - str, err := req.String()  
89 - if err != nil {  
90 - // error  
91 - }  
92 - fmt.Println(str)  
93 -  
94 -  
95 -See godoc for further documentation and examples.  
96 -  
97 -* [godoc.org/github.com/astaxie/beego/httplib](https://godoc.org/github.com/astaxie/beego/httplib)  
1 -// Copyright 2014 beego Author. All Rights Reserved.  
2 -//  
3 -// Licensed under the Apache License, Version 2.0 (the "License");  
4 -// you may not use this file except in compliance with the License.  
5 -// You may obtain a copy of the License at  
6 -//  
7 -// http://www.apache.org/licenses/LICENSE-2.0  
8 -//  
9 -// Unless required by applicable law or agreed to in writing, software  
10 -// distributed under the License is distributed on an "AS IS" BASIS,  
11 -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
12 -// See the License for the specific language governing permissions and  
13 -// limitations under the License.  
14 -  
15 -// Package httplib is used as http.Client  
16 -// Usage:  
17 -//  
18 -// import "github.com/astaxie/beego/httplib"  
19 -//  
20 -// b := httplib.Post("http://beego.me/")  
21 -// b.Param("username","astaxie")  
22 -// b.Param("password","123456")  
23 -// b.PostFile("uploadfile1", "httplib.pdf")  
24 -// b.PostFile("uploadfile2", "httplib.txt")  
25 -// str, err := b.String()  
26 -// if err != nil {  
27 -// t.Fatal(err)  
28 -// }  
29 -// fmt.Println(str)  
30 -//  
31 -// more docs http://beego.me/docs/module/httplib.md  
32 -package httplib  
33 -  
34 -import (  
35 - "bytes"  
36 - "compress/gzip"  
37 - "crypto/tls"  
38 - "encoding/json"  
39 - "encoding/xml"  
40 - "io"  
41 - "io/ioutil"  
42 - "log"  
43 - "mime/multipart"  
44 - "net"  
45 - "net/http"  
46 - "net/http/cookiejar"  
47 - "net/http/httputil"  
48 - "net/url"  
49 - "os"  
50 - "path"  
51 - "strings"  
52 - "sync"  
53 - "time"  
54 -  
55 - "gopkg.in/yaml.v2"  
56 -)  
57 -  
58 -var defaultSetting = BeegoHTTPSettings{  
59 - UserAgent: "beegoServer",  
60 - ConnectTimeout: 60 * time.Second,  
61 - ReadWriteTimeout: 60 * time.Second,  
62 - Gzip: true,  
63 - DumpBody: true,  
64 -}  
65 -  
66 -var defaultCookieJar http.CookieJar  
67 -var settingMutex sync.Mutex  
68 -  
69 -// createDefaultCookie creates a global cookiejar to store cookies.  
70 -func createDefaultCookie() {  
71 - settingMutex.Lock()  
72 - defer settingMutex.Unlock()  
73 - defaultCookieJar, _ = cookiejar.New(nil)  
74 -}  
75 -  
76 -// SetDefaultSetting Overwrite default settings  
77 -func SetDefaultSetting(setting BeegoHTTPSettings) {  
78 - settingMutex.Lock()  
79 - defer settingMutex.Unlock()  
80 - defaultSetting = setting  
81 -}  
82 -  
83 -// NewBeegoRequest return *BeegoHttpRequest with specific method  
84 -func NewBeegoRequest(rawurl, method string) *BeegoHTTPRequest {  
85 - var resp http.Response  
86 - u, err := url.Parse(rawurl)  
87 - if err != nil {  
88 - log.Println("Httplib:", err)  
89 - }  
90 - req := http.Request{  
91 - URL: u,  
92 - Method: method,  
93 - Header: make(http.Header),  
94 - Proto: "HTTP/1.1",  
95 - ProtoMajor: 1,  
96 - ProtoMinor: 1,  
97 - }  
98 - return &BeegoHTTPRequest{  
99 - url: rawurl,  
100 - req: &req,  
101 - params: map[string][]string{},  
102 - files: map[string]string{},  
103 - setting: defaultSetting,  
104 - resp: &resp,  
105 - }  
106 -}  
107 -  
108 -// Get returns *BeegoHttpRequest with GET method.  
109 -func Get(url string) *BeegoHTTPRequest {  
110 - return NewBeegoRequest(url, "GET")  
111 -}  
112 -  
113 -// Post returns *BeegoHttpRequest with POST method.  
114 -func Post(url string) *BeegoHTTPRequest {  
115 - return NewBeegoRequest(url, "POST")  
116 -}  
117 -  
118 -// Put returns *BeegoHttpRequest with PUT method.  
119 -func Put(url string) *BeegoHTTPRequest {  
120 - return NewBeegoRequest(url, "PUT")  
121 -}  
122 -  
123 -// Delete returns *BeegoHttpRequest DELETE method.  
124 -func Delete(url string) *BeegoHTTPRequest {  
125 - return NewBeegoRequest(url, "DELETE")  
126 -}  
127 -  
128 -// Head returns *BeegoHttpRequest with HEAD method.  
129 -func Head(url string) *BeegoHTTPRequest {  
130 - return NewBeegoRequest(url, "HEAD")  
131 -}  
132 -  
133 -// BeegoHTTPSettings is the http.Client setting  
134 -type BeegoHTTPSettings struct {  
135 - ShowDebug bool  
136 - UserAgent string  
137 - ConnectTimeout time.Duration  
138 - ReadWriteTimeout time.Duration  
139 - TLSClientConfig *tls.Config  
140 - Proxy func(*http.Request) (*url.URL, error)  
141 - Transport http.RoundTripper  
142 - CheckRedirect func(req *http.Request, via []*http.Request) error  
143 - EnableCookie bool  
144 - Gzip bool  
145 - DumpBody bool  
146 - Retries int // if set to -1 means will retry forever  
147 -}  
148 -  
149 -// BeegoHTTPRequest provides more useful methods for requesting one url than http.Request.  
150 -type BeegoHTTPRequest struct {  
151 - url string  
152 - req *http.Request  
153 - params map[string][]string  
154 - files map[string]string  
155 - setting BeegoHTTPSettings  
156 - resp *http.Response  
157 - body []byte  
158 - dump []byte  
159 -}  
160 -  
161 -// GetRequest return the request object  
162 -func (b *BeegoHTTPRequest) GetRequest() *http.Request {  
163 - return b.req  
164 -}  
165 -  
166 -// Setting Change request settings  
167 -func (b *BeegoHTTPRequest) Setting(setting BeegoHTTPSettings) *BeegoHTTPRequest {  
168 - b.setting = setting  
169 - return b  
170 -}  
171 -  
172 -// SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.  
173 -func (b *BeegoHTTPRequest) SetBasicAuth(username, password string) *BeegoHTTPRequest {  
174 - b.req.SetBasicAuth(username, password)  
175 - return b  
176 -}  
177 -  
178 -// SetEnableCookie sets enable/disable cookiejar  
179 -func (b *BeegoHTTPRequest) SetEnableCookie(enable bool) *BeegoHTTPRequest {  
180 - b.setting.EnableCookie = enable  
181 - return b  
182 -}  
183 -  
184 -// SetUserAgent sets User-Agent header field  
185 -func (b *BeegoHTTPRequest) SetUserAgent(useragent string) *BeegoHTTPRequest {  
186 - b.setting.UserAgent = useragent  
187 - return b  
188 -}  
189 -  
190 -// Debug sets show debug or not when executing request.  
191 -func (b *BeegoHTTPRequest) Debug(isdebug bool) *BeegoHTTPRequest {  
192 - b.setting.ShowDebug = isdebug  
193 - return b  
194 -}  
195 -  
196 -// Retries sets Retries times.  
197 -// default is 0 means no retried.  
198 -// -1 means retried forever.  
199 -// others means retried times.  
200 -func (b *BeegoHTTPRequest) Retries(times int) *BeegoHTTPRequest {  
201 - b.setting.Retries = times  
202 - return b  
203 -}  
204 -  
205 -// DumpBody setting whether need to Dump the Body.  
206 -func (b *BeegoHTTPRequest) DumpBody(isdump bool) *BeegoHTTPRequest {  
207 - b.setting.DumpBody = isdump  
208 - return b  
209 -}  
210 -  
211 -// DumpRequest return the DumpRequest  
212 -func (b *BeegoHTTPRequest) DumpRequest() []byte {  
213 - return b.dump  
214 -}  
215 -  
216 -// SetTimeout sets connect time out and read-write time out for BeegoRequest.  
217 -func (b *BeegoHTTPRequest) SetTimeout(connectTimeout, readWriteTimeout time.Duration) *BeegoHTTPRequest {  
218 - b.setting.ConnectTimeout = connectTimeout  
219 - b.setting.ReadWriteTimeout = readWriteTimeout  
220 - return b  
221 -}  
222 -  
223 -// SetTLSClientConfig sets tls connection configurations if visiting https url.  
224 -func (b *BeegoHTTPRequest) SetTLSClientConfig(config *tls.Config) *BeegoHTTPRequest {  
225 - b.setting.TLSClientConfig = config  
226 - return b  
227 -}  
228 -  
229 -// Header add header item string in request.  
230 -func (b *BeegoHTTPRequest) Header(key, value string) *BeegoHTTPRequest {  
231 - b.req.Header.Set(key, value)  
232 - return b  
233 -}  
234 -  
235 -// SetHost set the request host  
236 -func (b *BeegoHTTPRequest) SetHost(host string) *BeegoHTTPRequest {  
237 - b.req.Host = host  
238 - return b  
239 -}  
240 -  
241 -// SetProtocolVersion Set the protocol version for incoming requests.  
242 -// Client requests always use HTTP/1.1.  
243 -func (b *BeegoHTTPRequest) SetProtocolVersion(vers string) *BeegoHTTPRequest {  
244 - if len(vers) == 0 {  
245 - vers = "HTTP/1.1"  
246 - }  
247 -  
248 - major, minor, ok := http.ParseHTTPVersion(vers)  
249 - if ok {  
250 - b.req.Proto = vers  
251 - b.req.ProtoMajor = major  
252 - b.req.ProtoMinor = minor  
253 - }  
254 -  
255 - return b  
256 -}  
257 -  
258 -// SetCookie add cookie into request.  
259 -func (b *BeegoHTTPRequest) SetCookie(cookie *http.Cookie) *BeegoHTTPRequest {  
260 - b.req.Header.Add("Cookie", cookie.String())  
261 - return b  
262 -}  
263 -  
264 -// SetTransport set the setting transport  
265 -func (b *BeegoHTTPRequest) SetTransport(transport http.RoundTripper) *BeegoHTTPRequest {  
266 - b.setting.Transport = transport  
267 - return b  
268 -}  
269 -  
270 -// SetProxy set the http proxy  
271 -// example:  
272 -//  
273 -// func(req *http.Request) (*url.URL, error) {  
274 -// u, _ := url.ParseRequestURI("http://127.0.0.1:8118")  
275 -// return u, nil  
276 -// }  
277 -func (b *BeegoHTTPRequest) SetProxy(proxy func(*http.Request) (*url.URL, error)) *BeegoHTTPRequest {  
278 - b.setting.Proxy = proxy  
279 - return b  
280 -}  
281 -  
282 -// SetCheckRedirect specifies the policy for handling redirects.  
283 -//  
284 -// If CheckRedirect is nil, the Client uses its default policy,  
285 -// which is to stop after 10 consecutive requests.  
286 -func (b *BeegoHTTPRequest) SetCheckRedirect(redirect func(req *http.Request, via []*http.Request) error) *BeegoHTTPRequest {  
287 - b.setting.CheckRedirect = redirect  
288 - return b  
289 -}  
290 -  
291 -// Param adds query param in to request.  
292 -// params build query string as ?key1=value1&key2=value2...  
293 -func (b *BeegoHTTPRequest) Param(key, value string) *BeegoHTTPRequest {  
294 - if param, ok := b.params[key]; ok {  
295 - b.params[key] = append(param, value)  
296 - } else {  
297 - b.params[key] = []string{value}  
298 - }  
299 - return b  
300 -}  
301 -  
302 -// PostFile add a post file to the request  
303 -func (b *BeegoHTTPRequest) PostFile(formname, filename string) *BeegoHTTPRequest {  
304 - b.files[formname] = filename  
305 - return b  
306 -}  
307 -  
308 -// Body adds request raw body.  
309 -// it supports string and []byte.  
310 -func (b *BeegoHTTPRequest) Body(data interface{}) *BeegoHTTPRequest {  
311 - switch t := data.(type) {  
312 - case string:  
313 - bf := bytes.NewBufferString(t)  
314 - b.req.Body = ioutil.NopCloser(bf)  
315 - b.req.ContentLength = int64(len(t))  
316 - case []byte:  
317 - bf := bytes.NewBuffer(t)  
318 - b.req.Body = ioutil.NopCloser(bf)  
319 - b.req.ContentLength = int64(len(t))  
320 - }  
321 - return b  
322 -}  
323 -  
324 -// XMLBody adds request raw body encoding by XML.  
325 -func (b *BeegoHTTPRequest) XMLBody(obj interface{}) (*BeegoHTTPRequest, error) {  
326 - if b.req.Body == nil && obj != nil {  
327 - byts, err := xml.Marshal(obj)  
328 - if err != nil {  
329 - return b, err  
330 - }  
331 - b.req.Body = ioutil.NopCloser(bytes.NewReader(byts))  
332 - b.req.ContentLength = int64(len(byts))  
333 - b.req.Header.Set("Content-Type", "application/xml")  
334 - }  
335 - return b, nil  
336 -}  
337 -  
338 -// YAMLBody adds request raw body encoding by YAML.  
339 -func (b *BeegoHTTPRequest) YAMLBody(obj interface{}) (*BeegoHTTPRequest, error) {  
340 - if b.req.Body == nil && obj != nil {  
341 - byts, err := yaml.Marshal(obj)  
342 - if err != nil {  
343 - return b, err  
344 - }  
345 - b.req.Body = ioutil.NopCloser(bytes.NewReader(byts))  
346 - b.req.ContentLength = int64(len(byts))  
347 - b.req.Header.Set("Content-Type", "application/x+yaml")  
348 - }  
349 - return b, nil  
350 -}  
351 -  
352 -// JSONBody adds request raw body encoding by JSON.  
353 -func (b *BeegoHTTPRequest) JSONBody(obj interface{}) (*BeegoHTTPRequest, error) {  
354 - if b.req.Body == nil && obj != nil {  
355 - byts, err := json.Marshal(obj)  
356 - if err != nil {  
357 - return b, err  
358 - }  
359 - b.req.Body = ioutil.NopCloser(bytes.NewReader(byts))  
360 - b.req.ContentLength = int64(len(byts))  
361 - b.req.Header.Set("Content-Type", "application/json")  
362 - }  
363 - return b, nil  
364 -}  
365 -  
366 -func (b *BeegoHTTPRequest) buildURL(paramBody string) {  
367 - // build GET url with query string  
368 - if b.req.Method == "GET" && len(paramBody) > 0 {  
369 - if strings.Contains(b.url, "?") {  
370 - b.url += "&" + paramBody  
371 - } else {  
372 - b.url = b.url + "?" + paramBody  
373 - }  
374 - return  
375 - }  
376 -  
377 - // build POST/PUT/PATCH url and body  
378 - if (b.req.Method == "POST" || b.req.Method == "PUT" || b.req.Method == "PATCH" || b.req.Method == "DELETE") && b.req.Body == nil {  
379 - // with files  
380 - if len(b.files) > 0 {  
381 - pr, pw := io.Pipe()  
382 - bodyWriter := multipart.NewWriter(pw)  
383 - go func() {  
384 - for formname, filename := range b.files {  
385 - fileWriter, err := bodyWriter.CreateFormFile(formname, filename)  
386 - if err != nil {  
387 - log.Println("Httplib:", err)  
388 - }  
389 - fh, err := os.Open(filename)  
390 - if err != nil {  
391 - log.Println("Httplib:", err)  
392 - }  
393 - //iocopy  
394 - _, err = io.Copy(fileWriter, fh)  
395 - fh.Close()  
396 - if err != nil {  
397 - log.Println("Httplib:", err)  
398 - }  
399 - }  
400 - for k, v := range b.params {  
401 - for _, vv := range v {  
402 - bodyWriter.WriteField(k, vv)  
403 - }  
404 - }  
405 - bodyWriter.Close()  
406 - pw.Close()  
407 - }()  
408 - b.Header("Content-Type", bodyWriter.FormDataContentType())  
409 - b.req.Body = ioutil.NopCloser(pr)  
410 - b.Header("Transfer-Encoding", "chunked")  
411 - return  
412 - }  
413 -  
414 - // with params  
415 - if len(paramBody) > 0 {  
416 - b.Header("Content-Type", "application/x-www-form-urlencoded")  
417 - b.Body(paramBody)  
418 - }  
419 - }  
420 -}  
421 -  
422 -func (b *BeegoHTTPRequest) getResponse() (*http.Response, error) {  
423 - if b.resp.StatusCode != 0 {  
424 - return b.resp, nil  
425 - }  
426 - resp, err := b.DoRequest()  
427 - if err != nil {  
428 - return nil, err  
429 - }  
430 - b.resp = resp  
431 - return resp, nil  
432 -}  
433 -  
434 -// DoRequest will do the client.Do  
435 -func (b *BeegoHTTPRequest) DoRequest() (resp *http.Response, err error) {  
436 - var paramBody string  
437 - if len(b.params) > 0 {  
438 - var buf bytes.Buffer  
439 - for k, v := range b.params {  
440 - for _, vv := range v {  
441 - buf.WriteString(url.QueryEscape(k))  
442 - buf.WriteByte('=')  
443 - buf.WriteString(url.QueryEscape(vv))  
444 - buf.WriteByte('&')  
445 - }  
446 - }  
447 - paramBody = buf.String()  
448 - paramBody = paramBody[0 : len(paramBody)-1]  
449 - }  
450 -  
451 - b.buildURL(paramBody)  
452 - urlParsed, err := url.Parse(b.url)  
453 - if err != nil {  
454 - return nil, err  
455 - }  
456 -  
457 - b.req.URL = urlParsed  
458 -  
459 - trans := b.setting.Transport  
460 -  
461 - if trans == nil {  
462 - // create default transport  
463 - trans = &http.Transport{  
464 - TLSClientConfig: b.setting.TLSClientConfig,  
465 - Proxy: b.setting.Proxy,  
466 - Dial: TimeoutDialer(b.setting.ConnectTimeout, b.setting.ReadWriteTimeout),  
467 - MaxIdleConnsPerHost: 100,  
468 - }  
469 - } else {  
470 - // if b.transport is *http.Transport then set the settings.  
471 - if t, ok := trans.(*http.Transport); ok {  
472 - if t.TLSClientConfig == nil {  
473 - t.TLSClientConfig = b.setting.TLSClientConfig  
474 - }  
475 - if t.Proxy == nil {  
476 - t.Proxy = b.setting.Proxy  
477 - }  
478 - if t.Dial == nil {  
479 - t.Dial = TimeoutDialer(b.setting.ConnectTimeout, b.setting.ReadWriteTimeout)  
480 - }  
481 - }  
482 - }  
483 -  
484 - var jar http.CookieJar  
485 - if b.setting.EnableCookie {  
486 - if defaultCookieJar == nil {  
487 - createDefaultCookie()  
488 - }  
489 - jar = defaultCookieJar  
490 - }  
491 -  
492 - client := &http.Client{  
493 - Transport: trans,  
494 - Jar: jar,  
495 - }  
496 -  
497 - if b.setting.UserAgent != "" && b.req.Header.Get("User-Agent") == "" {  
498 - b.req.Header.Set("User-Agent", b.setting.UserAgent)  
499 - }  
500 -  
501 - if b.setting.CheckRedirect != nil {  
502 - client.CheckRedirect = b.setting.CheckRedirect  
503 - }  
504 -  
505 - if b.setting.ShowDebug {  
506 - dump, err := httputil.DumpRequest(b.req, b.setting.DumpBody)  
507 - if err != nil {  
508 - log.Println(err.Error())  
509 - }  
510 - b.dump = dump  
511 - }  
512 - // retries default value is 0, it will run once.  
513 - // retries equal to -1, it will run forever until success  
514 - // retries is setted, it will retries fixed times.  
515 - for i := 0; b.setting.Retries == -1 || i <= b.setting.Retries; i++ {  
516 - resp, err = client.Do(b.req)  
517 - if err == nil {  
518 - break  
519 - }  
520 - }  
521 - return resp, err  
522 -}  
523 -  
524 -// String returns the body string in response.  
525 -// it calls Response inner.  
526 -func (b *BeegoHTTPRequest) String() (string, error) {  
527 - data, err := b.Bytes()  
528 - if err != nil {  
529 - return "", err  
530 - }  
531 -  
532 - return string(data), nil  
533 -}  
534 -  
535 -// Bytes returns the body []byte in response.  
536 -// it calls Response inner.  
537 -func (b *BeegoHTTPRequest) Bytes() ([]byte, error) {  
538 - if b.body != nil {  
539 - return b.body, nil  
540 - }  
541 - resp, err := b.getResponse()  
542 - if err != nil {  
543 - return nil, err  
544 - }  
545 - if resp.Body == nil {  
546 - return nil, nil  
547 - }  
548 - defer resp.Body.Close()  
549 - if b.setting.Gzip && resp.Header.Get("Content-Encoding") == "gzip" {  
550 - reader, err := gzip.NewReader(resp.Body)  
551 - if err != nil {  
552 - return nil, err  
553 - }  
554 - b.body, err = ioutil.ReadAll(reader)  
555 - return b.body, err  
556 - }  
557 - b.body, err = ioutil.ReadAll(resp.Body)  
558 - return b.body, err  
559 -}  
560 -  
561 -// ToFile saves the body data in response to one file.  
562 -// it calls Response inner.  
563 -func (b *BeegoHTTPRequest) ToFile(filename string) error {  
564 - resp, err := b.getResponse()  
565 - if err != nil {  
566 - return err  
567 - }  
568 - if resp.Body == nil {  
569 - return nil  
570 - }  
571 - defer resp.Body.Close()  
572 - err = pathExistAndMkdir(filename)  
573 - if err != nil {  
574 - return err  
575 - }  
576 - f, err := os.Create(filename)  
577 - if err != nil {  
578 - return err  
579 - }  
580 - defer f.Close()  
581 - _, err = io.Copy(f, resp.Body)  
582 - return err  
583 -}  
584 -  
585 -//Check that the file directory exists, there is no automatically created  
586 -func pathExistAndMkdir(filename string) (err error) {  
587 - filename = path.Dir(filename)  
588 - _, err = os.Stat(filename)  
589 - if err == nil {  
590 - return nil  
591 - }  
592 - if os.IsNotExist(err) {  
593 - err = os.MkdirAll(filename, os.ModePerm)  
594 - if err == nil {  
595 - return nil  
596 - }  
597 - }  
598 - return err  
599 -}  
600 -  
601 -// ToJSON returns the map that marshals from the body bytes as json in response .  
602 -// it calls Response inner.  
603 -func (b *BeegoHTTPRequest) ToJSON(v interface{}) error {  
604 - data, err := b.Bytes()  
605 - if err != nil {  
606 - return err  
607 - }  
608 - return json.Unmarshal(data, v)  
609 -}  
610 -  
611 -// ToXML returns the map that marshals from the body bytes as xml in response .  
612 -// it calls Response inner.  
613 -func (b *BeegoHTTPRequest) ToXML(v interface{}) error {  
614 - data, err := b.Bytes()  
615 - if err != nil {  
616 - return err  
617 - }  
618 - return xml.Unmarshal(data, v)  
619 -}  
620 -  
621 -// ToYAML returns the map that marshals from the body bytes as yaml in response .  
622 -// it calls Response inner.  
623 -func (b *BeegoHTTPRequest) ToYAML(v interface{}) error {  
624 - data, err := b.Bytes()  
625 - if err != nil {  
626 - return err  
627 - }  
628 - return yaml.Unmarshal(data, v)  
629 -}  
630 -  
631 -// Response executes request client gets response mannually.  
632 -func (b *BeegoHTTPRequest) Response() (*http.Response, error) {  
633 - return b.getResponse()  
634 -}  
635 -  
636 -// TimeoutDialer returns functions of connection dialer with timeout settings for http.Transport Dial field.  
637 -func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error) {  
638 - return func(netw, addr string) (net.Conn, error) {  
639 - conn, err := net.DialTimeout(netw, addr, cTimeout)  
640 - if err != nil {  
641 - return nil, err  
642 - }  
643 - err = conn.SetDeadline(time.Now().Add(rwTimeout))  
644 - return conn, err  
645 - }  
646 -}  
  1 +package domain
  2 +
  3 +type ConcurrencySafeEntity interface {
  4 + Entity
  5 + ConcurrencyVersion() int64
  6 +}
  1 +package domain
  2 +
  3 +import "time"
  4 +
  5 +type BaseEvent struct {
  6 + OccurredOn time.Time `json:"occurredOn"`
  7 +}
  8 +
  9 +type DomainEvent interface {
  10 + EventType() string
  11 +}
  1 +package domain
  2 +
  3 +import "errors"
  4 +
  5 +type DomainEventPublisher interface {
  6 + Reset() error
  7 + Subscribe(domainEventSubscriber DomainEventSubscriber) error
  8 + Publish(domainEvent DomainEvent) error
  9 +}
  10 +
  11 +type BaseEventPublisher struct {
  12 + isPublishing bool
  13 + subscribers []DomainEventSubscriber
  14 +}
  15 +
  16 +func (domainEventPublisher *BaseEventPublisher) Reset() error {
  17 + if domainEventPublisher.isPublishing {
  18 + return errors.New("domain event is publishing, don't reset")
  19 + }
  20 + domainEventPublisher.subscribers = []DomainEventSubscriber{}
  21 + return nil
  22 +}
  23 +
  24 +func (domainEventPublisher *BaseEventPublisher) Subscribe(domainEventSubscriber DomainEventSubscriber) error {
  25 + if domainEventPublisher.isPublishing {
  26 + return errors.New("domain event is publishing, don't subscribe")
  27 + }
  28 + domainEventPublisher.subscribers = append(domainEventPublisher.subscribers, domainEventSubscriber)
  29 + return nil
  30 +}
  31 +
  32 +func (domainEventPublisher *BaseEventPublisher) Publish(domainEvent DomainEvent) error {
  33 + if domainEventPublisher.isPublishing {
  34 + return errors.New("domain event is publishing, don't publish")
  35 + }
  36 + domainEventPublisher.isPublishing = true
  37 + for _, subscriber := range domainEventPublisher.subscribers {
  38 + for _, eventType := range subscriber.SubscribedToEventTypes() {
  39 + if eventType == domainEvent.EventType() {
  40 + if err := subscriber.HandleEvent(domainEvent); err != nil {
  41 + return err
  42 + }
  43 + }
  44 + }
  45 + }
  46 + return nil
  47 +}
  1 +package domain
  2 +
  3 +type DomainEventSubscriber interface {
  4 + HandleEvent(domainEvent DomainEvent) error
  5 + SubscribedToEventTypes() []string
  6 +}
  1 +package domain
  2 +
  3 +type Entity interface {
  4 + Identify() interface{}
  5 +}
@@ -13,7 +13,6 @@ github.com/astaxie/beego/config @@ -13,7 +13,6 @@ github.com/astaxie/beego/config
13 github.com/astaxie/beego/context 13 github.com/astaxie/beego/context
14 github.com/astaxie/beego/context/param 14 github.com/astaxie/beego/context/param
15 github.com/astaxie/beego/grace 15 github.com/astaxie/beego/grace
16 -github.com/astaxie/beego/httplib  
17 github.com/astaxie/beego/logs 16 github.com/astaxie/beego/logs
18 github.com/astaxie/beego/plugins/cors 17 github.com/astaxie/beego/plugins/cors
19 github.com/astaxie/beego/session 18 github.com/astaxie/beego/session
@@ -83,6 +82,7 @@ github.com/klauspost/compress/zlib @@ -83,6 +82,7 @@ github.com/klauspost/compress/zlib
83 # github.com/linmadan/egglib-go v0.0.0-20191217144343-ca4539f95bf9 82 # github.com/linmadan/egglib-go v0.0.0-20191217144343-ca4539f95bf9
84 ## explicit 83 ## explicit
85 github.com/linmadan/egglib-go/core/application 84 github.com/linmadan/egglib-go/core/application
  85 +github.com/linmadan/egglib-go/core/domain
86 github.com/linmadan/egglib-go/utils/snowflake 86 github.com/linmadan/egglib-go/utils/snowflake
87 # github.com/mattn/go-colorable v0.1.6 87 # github.com/mattn/go-colorable v0.1.6
88 ## explicit 88 ## explicit