소스 검색

Merge branch 'dev' of http://git.ycjcjy.com/SpaceOfCheng/services into dev

胡轶钦 6 년 전
부모
커밋
333dfa09e3

+ 2
- 1
.gitignore 파일 보기

@@ -1,4 +1,5 @@
1 1
 *.exe
2 2
 *.exe~
3 3
 *.test
4
-*.log
4
+*.log
5
+common.log

+ 1
- 1
conf/app.conf 파일 보기

@@ -1,6 +1,6 @@
1 1
 appname = services
2 2
 httpport = 8080
3
-runmode = prod
3
+runmode = dev
4 4
 autorender = false
5 5
 copyrequestbody = true
6 6
 EnableDocs = true

+ 2
- 2
conf/db.conf 파일 보기

@@ -5,8 +5,8 @@ db_type      = mysql
5 5
 con_protocol = tcp
6 6
 
7 7
 ; 数据库地址,可以使用IP
8
-db_addr      = 192.168.0.122
9
-# db_addr      = localhost
8
+# db_addr      = 192.168.0.122
9
+db_addr      = localhost
10 10
 
11 11
 ; 端口
12 12
 db_port      = 3306

+ 4
- 15
controllers/auth.go 파일 보기

@@ -13,10 +13,10 @@ import (
13 13
 // 4、结束后,设置过期并从 Context 中删除
14 14
 // 5、生成新的 token, 并放入 Context 中
15 15
 func (c *BaseController) authenticate() {
16
-	serv := service.NewSysServ(c.Context)
16
+	c.serv = service.NewSysServ(c.Context)
17 17
 
18 18
 	// 鉴权 - 并初始化上下文
19
-	res := serv.AuthAndInitCtx(c.Ctx)
19
+	res := c.serv.AuthAndInitCtx(c.Ctx)
20 20
 
21 21
 	if res != nil {
22 22
 		code := http.StatusOK
@@ -32,26 +32,16 @@ func (c *BaseController) authenticate() {
32 32
 				data = res["message"].(map[string]interface{})
33 33
 			}
34 34
 
35
-			// 设置旧 token 过期
36
-			c.SetTokenExipre()
37
-
38 35
 			c.ResponseData(data, err, code)
39 36
 		}
40 37
 	}
41
-
42
-	// 设置旧 token 过期
43
-	c.SetTokenExipre()
44
-
45
-	// 生成新 token
46
-	c.CreateNewToken()
47 38
 }
48 39
 
49 40
 // SetTokenExipre 设置 token 过期
50 41
 func (c *BaseController) SetTokenExipre() {
51 42
 	token := c.Context.Get("token")
52 43
 	if token != nil {
53
-		serv := service.NewSysServ(c.Context)
54
-		serv.UpdateTokenExpire(token.(string))
44
+		c.serv.UpdateTokenExpire(token.(string))
55 45
 	}
56 46
 
57 47
 	c.Context.Set("token", "")
@@ -59,6 +49,5 @@ func (c *BaseController) SetTokenExipre() {
59 49
 
60 50
 // CreateNewToken 新 token
61 51
 func (c *BaseController) CreateNewToken() {
62
-	serv := service.NewSysServ(c.Context)
63
-	c.Context.Set("token", serv.NewToken())
52
+	c.Context.Set("token", c.serv.NewToken())
64 53
 }

+ 13
- 7
controllers/base.go 파일 보기

@@ -3,6 +3,7 @@ package controllers
3 3
 import (
4 4
 	"net/http"
5 5
 	"spaceofcheng/services/models/model"
6
+	"spaceofcheng/services/service"
6 7
 	"spaceofcheng/services/utils"
7 8
 
8 9
 	"github.com/astaxie/beego"
@@ -15,6 +16,7 @@ type BaseController struct {
15 16
 	Context  *utils.Context
16 17
 	Configer map[string]config.Configer
17 18
 	RunMode  string
19
+	serv     *service.SysServ
18 20
 }
19 21
 
20 22
 //Prepare 继承beego的
@@ -49,7 +51,6 @@ func (c *BaseController) ResponseError(err error, code ...int) {
49 51
 // ResponseData 自定义的JSON返回
50 52
 func (c *BaseController) ResponseData(data interface{}, msg interface{}, code int) {
51 53
 	status := code
52
-	c.destroyContext(status < http.StatusMultipleChoices)
53 54
 
54 55
 	sendMessage := ""
55 56
 	switch msgVal := msg.(type) {
@@ -64,14 +65,19 @@ func (c *BaseController) ResponseData(data interface{}, msg interface{}, code in
64 65
 	c.Data["json"] = JSONMessage{status, sendMessage, data}
65 66
 	c.Ctx.Output.Header("Access-Control-Expose-Headers", utils.TokenHeader)
66 67
 
67
-	token := c.Context.Get("token")
68
-	if token != nil {
69
-		tk := token.(string)
70
-		if tk != "" {
71
-			c.Ctx.Output.Header(utils.TokenHeader, utils.TokenSchema+" "+tk)
72
-		}
68
+	if status == http.StatusOK {
69
+		// 设置旧 token 过期
70
+		c.SetTokenExipre()
71
+
72
+		// 生成新 token
73
+		c.CreateNewToken()
74
+
75
+		token := c.Context.Get("token").(string)
76
+		c.Ctx.Output.Header(utils.TokenHeader, utils.TokenSchema+" "+token)
73 77
 	}
74 78
 
79
+	c.destroyContext(status < http.StatusMultipleChoices)
80
+
75 81
 	c.crosPolicy()
76 82
 	c.ServeJSON()
77 83
 	c.StopRun()

+ 16
- 6
controllers/card/card.go 파일 보기

@@ -86,12 +86,6 @@ func (c *CardController) GetCardByID() {
86 86
 	})
87 87
 }
88 88
 
89
-// GetMyCard 获取我的卡券
90
-// 微信端
91
-func (c *CardController) GetMyCard() {
92
-
93
-}
94
-
95 89
 // SaveCard 保存卡
96 90
 func (c *CardController) SaveCard() {
97 91
 	jsnStr := c.GetString("info")
@@ -172,3 +166,19 @@ func (c *CardController) GiveCard() {
172 166
 
173 167
 	c.ResponseJSON("ok")
174 168
 }
169
+
170
+// GetCardWithCustomer 获取卡及领取信息
171
+func (c *CardController) GetCardWithCustomer() {
172
+	cardid := c.GetString(":id")
173
+	cardDetail, err := c.serv.GetCardWithCustomer(cardid)
174
+	if err != nil {
175
+		c.ResponseError(err)
176
+	}
177
+
178
+	c.ResponseJSON(cardDetail)
179
+}
180
+
181
+// GetCardByCustomer 获取我的卡信息
182
+func (c *CardController) GetCardByCustomer() {
183
+	c.ResponseJSON("OK")
184
+}

+ 10
- 0
controllers/coupon/coupon.go 파일 보기

@@ -189,3 +189,13 @@ func (c *CouponController) GetCouponWithCustomer() {
189 189
 
190 190
 	c.ResponseJSON(couponDetail)
191 191
 }
192
+
193
+// GetCouponByCustomer 获取我的优惠券信息
194
+func (c *CouponController) GetCouponByCustomer() {
195
+	coupons, err := c.serv.GetCouponByCustomer()
196
+	if err != nil {
197
+		c.ResponseError(err)
198
+	}
199
+
200
+	c.ResponseJSON(coupons)
201
+}

+ 7
- 7
controllers/course/order.go 파일 보기

@@ -16,21 +16,21 @@ func (c *CourseController) PostOrder() {
16 16
 	}
17 17
 
18 18
 	// 订单优惠券
19
-	coupons := c.GetString("coupons")
19
+	customercouponid := c.GetString("customercouponid")
20 20
 
21 21
 	//
22 22
 	var orderInfo model.TaCourseOrders
23
-	var orderCoupon []model.TaCourseOrdersCoupon
23
+	//	var orderCoupon []model.TaCourseOrdersCoupon
24 24
 
25 25
 	if err := json.Unmarshal([]byte(info), &orderInfo); err != nil {
26 26
 		utils.LogError("下单转换JSON失败: " + err.Error())
27 27
 		c.ResponseError(errors.New("下单数据格式不正确"))
28 28
 	}
29 29
 
30
-	if err := json.Unmarshal([]byte(coupons), &orderCoupon); err != nil {
31
-		utils.LogError("下单优惠转换JSON失败: " + err.Error())
32
-		c.ResponseError(errors.New("优惠数据格式不正确"))
33
-	}
30
+	// if err := json.Unmarshal([]byte(coupons), &orderCoupon); err != nil {
31
+	// 	utils.LogError("下单优惠转换JSON失败: " + err.Error())
32
+	// 	c.ResponseError(errors.New("优惠数据格式不正确"))
33
+	// }
34 34
 	cust := c.Context.Get("customer").(model.TaCustomer)
35 35
 
36 36
 	if cust.Phone == "" {
@@ -40,7 +40,7 @@ func (c *CourseController) PostOrder() {
40 40
 		)
41 41
 	}
42 42
 
43
-	if err := c.serv.Orders(&orderInfo, orderCoupon); err != nil {
43
+	if err := c.serv.Orders(&orderInfo, customercouponid); err != nil {
44 44
 		c.ResponseError(err)
45 45
 	}
46 46
 

+ 2
- 2
controllers/file.go 파일 보기

@@ -50,9 +50,9 @@ func (c *BaseController) uploadFileToOSS(field string) (string, error) {
50 50
 }
51 51
 
52 52
 // SaveToExcel 保存文件到 excel
53
-func (c *BaseController) SaveToExcel(fn string, data interface{}, f func(interface{}) []interface{}) {
53
+func (c *BaseController) SaveToExcel(fn string, excel *utils.TinyXLSXEngine) {
54 54
 	var buf bytes.Buffer
55
-	if err := utils.NewTinyXLSXEngine().SetTransFunc(f).SetData(data).Write(&buf); err != nil {
55
+	if err := excel.Write(&buf); err != nil {
56 56
 		utils.LogError("写 xlsx buffer 失败: " + err.Error())
57 57
 		c.ResponseError(errors.New("生成 excel 异常, 请重试"))
58 58
 	}

+ 6
- 8
controllers/goods/order.go 파일 보기

@@ -102,12 +102,10 @@ func (c *GoodsController) PostOrder() {
102 102
 	}
103 103
 
104 104
 	// 订单优惠券
105
-	coupon := c.GetString("coupon")
106
-
105
+	customercouponid := c.GetString("customercouponid")
107 106
 	//
108 107
 	var orderInfo model.TaGoodsOrders
109 108
 	var orderDetail []model.TaGoodsOrdersDetail
110
-	var orderCoupon []model.TaGoodsOrdersCoupon
111 109
 
112 110
 	if err := json.Unmarshal([]byte(info), &orderInfo); err != nil {
113 111
 		utils.LogError("下单转换JSON失败: " + err.Error())
@@ -119,10 +117,10 @@ func (c *GoodsController) PostOrder() {
119 117
 		c.ResponseError(errors.New("详情数据格式不正确"))
120 118
 	}
121 119
 
122
-	if err := json.Unmarshal([]byte(coupon), &orderCoupon); err != nil {
123
-		utils.LogError("下单优惠转换JSON失败: " + err.Error())
124
-		c.ResponseError(errors.New("优惠数据格式不正确"))
125
-	}
120
+	// if err := json.Unmarshal([]byte(coupon), &orderCoupon); err != nil {
121
+	// 	utils.LogError("下单优惠转换JSON失败: " + err.Error())
122
+	// 	c.ResponseError(errors.New("优惠数据格式不正确"))
123
+	// }
126 124
 	cust := c.Context.Get("customer").(model.TaCustomer)
127 125
 
128 126
 	if cust.Phone == "" {
@@ -132,7 +130,7 @@ func (c *GoodsController) PostOrder() {
132 130
 		)
133 131
 	}
134 132
 
135
-	if err := c.serv.Orders(&orderInfo, orderDetail, orderCoupon); err != nil {
133
+	if err := c.serv.Orders(&orderInfo, orderDetail, customercouponid); err != nil {
136 134
 		c.ResponseError(err)
137 135
 	}
138 136
 

+ 4
- 5
controllers/user/user.go 파일 보기

@@ -144,7 +144,8 @@ func (c *UserController) GetUserCustomer() {
144 144
 	page, _ := c.GetInt("page")
145 145
 	pageSize, _ := c.GetInt("pagesize")
146 146
 	isrecommend := c.GetString("isrecommend")
147
-	customers, err := c.dao.GetUserCustomer(customer.UserId, isrecommend, page, pageSize)
147
+	key := c.GetString("key")
148
+	customers, err := c.dao.GetUserCustomer(customer.UserId, isrecommend, key, page, pageSize)
148 149
 	if err != nil {
149 150
 		c.ResponseError(err)
150 151
 	}
@@ -182,9 +183,8 @@ func (c *UserController) SignIn() {
182 183
 		)
183 184
 	}
184 185
 
185
-	// 成功之后, 生成新 token
186
+	// 成功之后, 设置用户
186 187
 	c.Context.Set("user", *user)
187
-	c.CreateNewToken()
188 188
 
189 189
 	if token == "" && doRemember != 0 {
190 190
 		var err error
@@ -206,8 +206,7 @@ func (c *UserController) SignIn() {
206 206
 
207 207
 // SignOut 用户登出
208 208
 func (c *UserController) SignOut() {
209
-	c.SetTokenExipre()
210
-	c.ResponseJSON("ok")
209
+	c.Context.Set("user", nil)
211 210
 }
212 211
 
213 212
 // GetEnvVars 获取当前用户相关

+ 284
- 1898
log/common.log
파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
파일 보기


+ 28
- 0
models/card/card.go 파일 보기

@@ -274,3 +274,31 @@ func (m *CardDAO) GetCustomerCardByUser(caseid, userid string) ([]model.TaCustom
274 274
 	err := m.db.Where("case_id=?", caseid).And("sales_id=?", userid).And("status>?", models.STATUS_DEL).Find(&customerCards)
275 275
 	return customerCards, err
276 276
 }
277
+
278
+// GetCardWithCustomer 获取优惠券明细
279
+func (m *CardDAO) GetCardWithCustomer(cardid string) (*CaseUsableCard, error) {
280
+	var card = new(CaseUsableCard)
281
+	sql := `select * from ta_card where coupin_id=?`
282
+	_, err := m.db.Sql(sql, cardid).Get(&card)
283
+	if err != nil {
284
+		return nil, err
285
+	}
286
+	if card == nil || card.CardId == "" {
287
+		return nil, errors.New("无优惠券信息")
288
+	}
289
+	var customerCopons []model.TaCustomerCard
290
+	sql = `select * from ta_customer_card where card_id = ? and status>?`
291
+	err = m.db.Sql(sql, card, models.STATUS_DEL).Find(&customerCopons)
292
+	if err != nil {
293
+		return nil, err
294
+	}
295
+	card.CustomerCard = customerCopons
296
+	return card, nil
297
+}
298
+
299
+// GetCardByCustomer 获取我的体验卡
300
+func (m *CardDAO) GetCardByCustomer(orgid, customerid string) ([]model.TaCustomerCard, error) {
301
+	var cards []model.TaCustomerCard
302
+	err := m.db.Where("status>?", models.STATUS_DEL).And("customer_id=?", customerid).And("org_id=?", orgid).Find(&cards)
303
+	return cards, err
304
+}

+ 100
- 1
models/coupon/coupon.go 파일 보기

@@ -89,6 +89,35 @@ func (m *CouponDAO) GetCouponInfoByID(id string) (*CouponInfo, error) {
89 89
 	return cp, nil
90 90
 }
91 91
 
92
+// GetCouponInfoByCustomerCouponID 获取优惠券详情
93
+func (m *CouponDAO) GetCouponInfoByCustomerCouponID(id string) (*CouponInfo, error) {
94
+	cp := new(CouponInfo)
95
+
96
+	query := `
97
+		SELECT
98
+			a.*
99
+		FROM
100
+			ta_coupon a 
101
+			inner join ta_customer_coupon b 
102
+			on a.coupon_id = b.coupon_id
103
+		WHERE
104
+			b.customer_coupon_id = ?
105
+	`
106
+
107
+	if _, err := m.db.SQL(query, id).Get(cp); err != nil {
108
+		return nil, err
109
+	}
110
+	// 关联目标
111
+	var targes []model.TaCouponTarget
112
+	if err := m.db.Where("coupon_id=?", cp.CouponId).Find(&targes); err != nil {
113
+		return nil, err
114
+	}
115
+
116
+	cp.Targets = targes
117
+
118
+	return cp, nil
119
+}
120
+
92 121
 // GetCouponList 获取优惠券列表
93 122
 func (m *CouponDAO) GetCouponList(filters []string, limit []int) ([]model.TaCoupon, int64, error) {
94 123
 	var cps []model.TaCoupon
@@ -317,7 +346,7 @@ func (m *CouponDAO) GetCustomerCouponByUser(caseid, userid string) ([]model.TaCu
317 346
 }
318 347
 
319 348
 // GetCouponWithCustomer 获取优惠券明细
320
-func (m CouponDAO) GetCouponWithCustomer(couponid string) (*CaseCouponDetail, error) {
349
+func (m *CouponDAO) GetCouponWithCustomer(couponid string) (*CaseCouponDetail, error) {
321 350
 	var coupon = new(CaseCouponDetail)
322 351
 	sql := `select * from ta_coupon where coupin_id=?`
323 352
 	_, err := m.db.Sql(sql, couponid).Get(&coupon)
@@ -336,3 +365,73 @@ func (m CouponDAO) GetCouponWithCustomer(couponid string) (*CaseCouponDetail, er
336 365
 	coupon.CustomerCoupon = customerCopons
337 366
 	return coupon, nil
338 367
 }
368
+
369
+// GetCouponByCustomer 获取我的优惠券信息
370
+func (m *CouponDAO) GetCouponByCustomer(orgid, customerid string) ([]CustomerCoupon, error) {
371
+	var coupons []CustomerCoupon
372
+	sql := `select * from ta_customer_coupon where status>? and customer_id=? and org_id=?`
373
+	err := m.db.Sql(sql, models.STATUS_DEL, customerid, orgid).Find(&coupons)
374
+	// err := m.db.Where("status>?", models.STATUS_DEL).And("customer_id=?", customerid).And("org_id=?", orgid).Find(&coupons)
375
+	for inx, coupon := range coupons {
376
+		info, err := m.GetCouponInfoByID(coupon.CouponId)
377
+		if err != nil {
378
+			return nil, err
379
+		}
380
+		coupons[inx].Coupon = info
381
+	}
382
+	return coupons, err
383
+}
384
+
385
+// GetCustomerCouponByID 获取我的优惠券想想
386
+func (m *CouponDAO) GetCustomerCouponByID(id string) (*model.TaCustomerCoupon, error) {
387
+	if id == "" {
388
+		return nil, errors.New("无优惠券信息")
389
+	}
390
+	var customerCoupon = new(model.TaCustomerCoupon)
391
+	_, err := m.db.Where("customer_coupon_id=?", id).Get(customerCoupon)
392
+	return customerCoupon, err
393
+}
394
+
395
+// GetCouponWithTarget 获取优惠券与关联信息
396
+func (m *CouponDAO) GetCouponWithTarget(ids string) ([]CouponInfo, error) {
397
+	var cp []CouponInfo
398
+
399
+	query := `
400
+		SELECT
401
+			*
402
+		FROM
403
+			ta_coupon
404
+		WHERE
405
+			coupon_id in ('` + strings.Replace(ids, ",", "','", -1) + `')
406
+	`
407
+
408
+	if err := m.db.SQL(query).Find(&cp); err != nil {
409
+		return nil, err
410
+	}
411
+
412
+	for i, c := range cp {
413
+		// 关联目标
414
+		var targes []model.TaCouponTarget
415
+		if err := m.db.Where("coupon_id=?", c.CouponId).Find(&targes); err != nil {
416
+			return nil, err
417
+		}
418
+		cp[i].Targets = targes
419
+	}
420
+
421
+	return cp, nil
422
+}
423
+
424
+// VerifyCustomerCoupon 核销客户优惠券
425
+func (m *CouponDAO) VerifyCustomerCoupon(id string) error {
426
+	var customerCoupon = model.TaCustomerCoupon{
427
+		CustomerCouponId: id,
428
+		UseDate:          time.Now(),
429
+		Status:           models.STATUS_READY,
430
+	}
431
+	var cols = []string{
432
+		"use_date",
433
+		"status",
434
+	}
435
+	_, err := m.db.Cols(cols...).Where("customer_coupon_id=?", id).Update(customerCoupon)
436
+	return err
437
+}

+ 6
- 0
models/coupon/types.go 파일 보기

@@ -24,3 +24,9 @@ type CaseCouponDetail struct {
24 24
 	model.TaCoupon `xorm:"extends"`
25 25
 	CustomerCoupon []model.TaCustomerCoupon
26 26
 }
27
+
28
+// CustomerCoupon 用户优惠券
29
+type CustomerCoupon struct {
30
+	model.TaCustomerCoupon `xorm:"extends"`
31
+	Coupon                 *CouponInfo
32
+}

+ 16
- 0
models/course/order.go 파일 보기

@@ -1,6 +1,8 @@
1 1
 package course
2 2
 
3 3
 import (
4
+	"errors"
5
+	"spaceofcheng/services/models"
4 6
 	"spaceofcheng/services/models/model"
5 7
 	"spaceofcheng/services/utils"
6 8
 	"strings"
@@ -48,3 +50,17 @@ func (m *CourseDAO) SaveCourseOrderDetail(details []model.TaCourseOrdersDetail)
48 50
 	_, err := m.db.Insert(details)
49 51
 	return err
50 52
 }
53
+
54
+// SaveOrdersCoupon 保存订单优惠券
55
+func (m *CourseDAO) SaveOrdersCoupon(coupon *model.TaCourseOrdersCoupon, order *model.TaCourseOrders) error {
56
+	if order.OrdersId == "" {
57
+		return errors.New("内部错误, 订单事务顺序出错")
58
+	}
59
+	coupon.OrdersCouponId = utils.GetGUID()
60
+	coupon.Status = models.STATUS_NORMAL
61
+	coupon.CreateDate = time.Now().Local()
62
+	if _, err := m.db.Insert(coupon); err != nil {
63
+		return err
64
+	}
65
+	return nil
66
+}

+ 36
- 30
models/goods/orders.go 파일 보기

@@ -73,42 +73,48 @@ func (m *GoodsDAO) SaveOrdersDetail(list []model.TaGoodsOrdersDetail, ordersID s
73 73
 }
74 74
 
75 75
 // SaveOrdersCoupon 保存订单优惠券
76
-func (m *GoodsDAO) SaveOrdersCoupon(coupons []model.TaGoodsOrdersCoupon, order *model.TaGoodsOrders) error {
76
+func (m *GoodsDAO) SaveOrdersCoupon(coupon *model.TaGoodsOrdersCoupon, order *model.TaGoodsOrders) error {
77 77
 	if order.OrdersId == "" {
78 78
 		return errors.New("内部错误, 订单事务顺序出错")
79 79
 	}
80
-
81
-	var couponAmount float64 = 0
82
-
83
-	// 补充优惠券表字段
84
-	for inx := range coupons {
85
-		coupons[inx].OrdersCouponId = guid.NewGUIDString()
86
-		coupons[inx].OrdersId = order.OrdersId
87
-		coupons[inx].OrdersNo = order.OrdersNo
88
-		coupons[inx].OrgId = order.OrgId
89
-		coupons[inx].Status = models.STATUS_NORMAL
90
-		coupons[inx].CreateDate = time.Now().Local()
91
-
92
-		userAmount, _ := strconv.ParseFloat(coupons[inx].UsedAmount, 64)
93
-		couponAmount += userAmount
94
-	}
95
-
96
-	// 反向更新 订单主表
97
-	totalAmount, _ := strconv.ParseFloat(order.Amount, 64)
98
-
99
-	order.CouponAmount = strconv.FormatFloat(couponAmount, 'f', -1, 32)
100
-	order.ActualAmount = strconv.FormatFloat((totalAmount - couponAmount), 'f', -1, 32)
101
-
102
-	if _, err := m.db.Insert(&coupons); err != nil {
80
+	coupon.OrdersCouponId = utils.GetGUID()
81
+	coupon.Status = models.STATUS_NORMAL
82
+	coupon.CreateDate = time.Now().Local()
83
+	if _, err := m.db.Insert(coupon); err != nil {
103 84
 		return err
104 85
 	}
105 86
 
106
-	if _, err := m.db.
107
-		Where("orders_id=?", order.OrdersId).
108
-		Cols([]string{"coupon_amount", "actual_amount"}...).
109
-		Update(order); err != nil {
110
-		return err
111
-	}
87
+	// var couponAmount float64 = 0
88
+
89
+	// // 补充优惠券表字段
90
+	// for inx := range coupons {
91
+	// 	coupons[inx].OrdersCouponId = guid.NewGUIDString()
92
+	// 	coupons[inx].OrdersId = order.OrdersId
93
+	// 	coupons[inx].OrdersNo = order.OrdersNo
94
+	// 	coupons[inx].OrgId = order.OrgId
95
+	// 	coupons[inx].Status = models.STATUS_NORMAL
96
+	// 	coupons[inx].CreateDate = time.Now().Local()
97
+
98
+	// 	userAmount, _ := strconv.ParseFloat(coupons[inx].UsedAmount, 64)
99
+	// 	couponAmount += userAmount
100
+	// }
101
+
102
+	// // 反向更新 订单主表
103
+	// totalAmount, _ := strconv.ParseFloat(order.Amount, 64)
104
+
105
+	// order.CouponAmount = strconv.FormatFloat(couponAmount, 'f', -1, 32)
106
+	// order.ActualAmount = strconv.FormatFloat((totalAmount - couponAmount), 'f', -1, 32)
107
+
108
+	// if _, err := m.db.Insert(&coupons); err != nil {
109
+	// 	return err
110
+	// }
111
+
112
+	// if _, err := m.db.
113
+	// 	Where("orders_id=?", order.OrdersId).
114
+	// 	Cols([]string{"coupon_amount", "actual_amount"}...).
115
+	// 	Update(order); err != nil {
116
+	// 	return err
117
+	// }
112 118
 
113 119
 	return nil
114 120
 }

+ 4
- 1
models/system/user.go 파일 보기

@@ -340,7 +340,7 @@ func (m *UserDAO) UpdateCustomerHeadImg(customerid, username, imgurl string) err
340 340
 }
341 341
 
342 342
 // GetUserCustomer 获取我的推荐客户
343
-func (m *UserDAO) GetUserCustomer(userid, isrecommend string, page int, pageSize int) ([]model.TaCustomer, error) {
343
+func (m *UserDAO) GetUserCustomer(userid, isrecommend, key string, page int, pageSize int) ([]model.TaCustomer, error) {
344 344
 	var customers []model.TaCustomer
345 345
 	sql := `select * from ta_customer where recommend_id='` + userid + `'`
346 346
 	if isrecommend == "" {
@@ -350,6 +350,9 @@ func (m *UserDAO) GetUserCustomer(userid, isrecommend string, page int, pageSize
350 350
 			select customer_id from ta_customer_card where sales_id='` + userid + `'
351 351
 		)`
352 352
 	}
353
+	if key != "" {
354
+		sql += ` and (customer_name like '%` + key + `%' or phone like '%` + key + `%' or name like '%` + key + `%')`
355
+	}
353 356
 	offset := (page - 1) * pageSize
354 357
 	sql += ` order by create_date desc LIMIT ` + strconv.Itoa(pageSize) + ` OFFSET ` + strconv.Itoa(offset)
355 358
 	err := m.db.Sql(sql).Find(&customers)

+ 1
- 0
routers/common.go 파일 보기

@@ -269,6 +269,7 @@ func getCommonRoutes() beego.LinkNamespace {
269 269
 
270 270
 		// 签到
271 271
 		beego.NSRouter("/case/signin", &cases.SigninController{}, "get:GetSigninWhere"),
272
+		beego.NSRouter("/case/signin", &cases.SigninController{}, "post:AddSignin"),
272 273
 
273 274
 		// websocket
274 275
 		beego.NSRouter("/websocket/:grps/:id", &controllers.BaseController{}, "get:Ws"),

+ 12
- 0
routers/wechat.go 파일 보기

@@ -1,6 +1,8 @@
1 1
 package routers
2 2
 
3 3
 import (
4
+	"spaceofcheng/services/controllers/card"
5
+	"spaceofcheng/services/controllers/coupon"
4 6
 	"spaceofcheng/services/controllers/course"
5 7
 	"spaceofcheng/services/controllers/customer"
6 8
 	"spaceofcheng/services/controllers/goods"
@@ -17,6 +19,8 @@ func getWechatRoutes() beego.LinkNamespace {
17 19
 		// 会员
18 20
 		beego.NSRouter("/customer", &customer.CustomerController{}, "get:GetCustWXByID"),
19 21
 		beego.NSRouter("/customer/user", &user.UserController{}, "get:GetUserCustomer"),
22
+		beego.NSRouter("/customer/coupon", &coupon.CouponController{}, "get:GetCouponByCustomer"),
23
+		beego.NSRouter("/customer/card", &card.CardController{}, "get:GetCardByCustomer"),
20 24
 
21 25
 		// 下单
22 26
 		beego.NSRouter("/order/goods", &goods.GoodsController{}, "post:PostOrder"),
@@ -45,5 +49,13 @@ func getWechatRoutes() beego.LinkNamespace {
45 49
 		// 用户
46 50
 		beego.NSRouter("/user/:type", &user.UserController{}, "get:GetCaseUserByType"),
47 51
 		beego.NSRouter("/user/detail/:id", &user.UserController{}, "get:GetCaseUserByID"),
52
+
53
+		// 优惠券
54
+		beego.NSRouter("/coupon/:id", &coupon.CouponController{}, "get:GetCouponByID"),
55
+		beego.NSRouter("/coupon/detail/:id", &coupon.CouponController{}, "get:GetCouponWithCustomer"),
56
+
57
+		// 卡
58
+		beego.NSRouter("/card/:id", &card.CardController{}, "get:GetCardByID"),
59
+		beego.NSRouter("/card/detail/:id", &card.CardController{}, "get:GetCardWithCustomer"),
48 60
 	)
49 61
 }

+ 25
- 0
service/card/card.go 파일 보기

@@ -274,3 +274,28 @@ func (s *CardServ) GetCardByIDWithCheck(cardID string) (*card.CardInfo, error) {
274 274
 
275 275
 	return card, nil
276 276
 }
277
+
278
+// GetCardWithCustomer 获取优惠券明细
279
+func (s *CardServ) GetCardWithCustomer(cardid string) (*card.CaseUsableCard, error) {
280
+	if cardid == "" {
281
+		return nil, errors.New("没有卡信息!")
282
+	}
283
+	cardDetail, err := s.dao.GetCardDetail(cardid)
284
+	if err != nil {
285
+		utils.LogError("获取优惠券失败: " + err.Error())
286
+		return nil, errors.New("获取优惠券失败")
287
+	}
288
+	return cardDetail, nil
289
+}
290
+
291
+// GetCardByCustomer 获取我的优惠券
292
+func (s *CardServ) GetCardByCustomer() ([]model.TaCustomerCard, error) {
293
+	org := s.ctx.Get("org").(model.SysOrg)
294
+	customer := s.ctx.Get("customer").(model.TaCustomer)
295
+	cards, err := s.dao.GetCardByCustomer(org.OrgId, customer.CustomerId)
296
+	if err != nil {
297
+		utils.LogError("获取体验卡失败: " + err.Error())
298
+		return nil, errors.New("获取体验卡失败")
299
+	}
300
+	return cards, nil
301
+}

+ 13
- 1
service/coupon/coupon.go 파일 보기

@@ -364,7 +364,7 @@ func (s *CouponServ) GetCouponBySendType(sendtype string) ([]model.TaCoupon, err
364 364
 	coupon, err := s.dao.GetCouponBySendType(caseIDs, sendtype)
365 365
 	if err != nil {
366 366
 		utils.LogError("获取优惠券失败: " + err.Error())
367
-		return nil, errors.New("校验优惠券失败")
367
+		return nil, errors.New("获取优惠券失败")
368 368
 	}
369 369
 	return coupon, nil
370 370
 }
@@ -381,3 +381,15 @@ func (s *CouponServ) GetCouponWithCustomer(couponid string) (*coupon.CaseCouponD
381 381
 	}
382 382
 	return couponDetail, nil
383 383
 }
384
+
385
+// GetCouponByCustomer 获取我的优惠券
386
+func (s *CouponServ) GetCouponByCustomer() ([]coupon.CustomerCoupon, error) {
387
+	org := s.ctx.Get("org").(model.SysOrg)
388
+	customer := s.ctx.Get("customer").(model.TaCustomer)
389
+	coupons, err := s.dao.GetCouponByCustomer(org.OrgId, customer.CustomerId)
390
+	if err != nil {
391
+		utils.LogError("获取优惠券失败: " + err.Error())
392
+		return nil, errors.New("获取优惠券失败")
393
+	}
394
+	return coupons, nil
395
+}

+ 13
- 10
service/course/course.go 파일 보기

@@ -4,6 +4,7 @@ import (
4 4
 	"errors"
5 5
 	"spaceofcheng/services/models"
6 6
 	"spaceofcheng/services/models/cases"
7
+	"spaceofcheng/services/models/coupon"
7 8
 	"spaceofcheng/services/models/course"
8 9
 	"spaceofcheng/services/models/customer"
9 10
 	"spaceofcheng/services/models/model"
@@ -15,21 +16,23 @@ import (
15 16
 
16 17
 // CourseServ 系统处理
17 18
 type CourseServ struct {
18
-	ctx     *utils.Context
19
-	dao     *course.CourseDAO
20
-	casedao *cases.CaseDAO
21
-	custDAO *customer.CustomerDAO
22
-	userDAO *system.UserDAO
19
+	ctx       *utils.Context
20
+	dao       *course.CourseDAO
21
+	casedao   *cases.CaseDAO
22
+	custDAO   *customer.CustomerDAO
23
+	userDAO   *system.UserDAO
24
+	couponDAO *coupon.CouponDAO
23 25
 }
24 26
 
25 27
 // NewCourseServ 初始化
26 28
 func NewCourseServ(ctx *utils.Context) *CourseServ {
27 29
 	return &CourseServ{
28
-		ctx:     ctx,
29
-		dao:     course.NewCourseDAO(ctx),
30
-		casedao: cases.NewCaseDAO(ctx),
31
-		custDAO: customer.NewCustomerDAO(ctx),
32
-		userDAO: system.NewUserDAO(ctx),
30
+		ctx:       ctx,
31
+		dao:       course.NewCourseDAO(ctx),
32
+		casedao:   cases.NewCaseDAO(ctx),
33
+		custDAO:   customer.NewCustomerDAO(ctx),
34
+		userDAO:   system.NewUserDAO(ctx),
35
+		couponDAO: coupon.NewCouponDAO(ctx),
33 36
 	}
34 37
 }
35 38
 

+ 93
- 30
service/course/order.go 파일 보기

@@ -15,7 +15,7 @@ import (
15 15
 // Orders 下单
16 16
 func (s *CourseServ) Orders(
17 17
 	couseOrder *model.TaCourseOrders,
18
-	coupons []model.TaCourseOrdersCoupon,
18
+	customercouponid string,
19 19
 ) error {
20 20
 	org := s.ctx.Get("org").(model.SysOrg)
21 21
 	cust := s.ctx.Get("customer").(model.TaCustomer)
@@ -47,11 +47,6 @@ func (s *CourseServ) Orders(
47 47
 		return errors.New("定单课程尚未排课, 不能下单")
48 48
 	}
49 49
 
50
-	if coupons != nil && len(coupons) > 0 {
51
-		// 校验卡券, 同时进行卡券的注销或者已使用的更新操作
52
-		// TODO
53
-	}
54
-
55 50
 	couseOrder.OrdersId = guid.NewGUIDString()
56 51
 	couseOrder.Price = course.Price // 课程价格, 即为订单价格
57 52
 	couseOrder.ActualAmount = course.Price
@@ -69,23 +64,91 @@ func (s *CourseServ) Orders(
69 64
 		couseOrder.CustomerName = cust.CustomerName
70 65
 	}
71 66
 
72
-	// 校验金额
73
-	if err := s.validBillCharges(couseOrder, coupons); err != nil {
74
-		return err
67
+	account, err := s.custDAO.GetAccountByCust(cust.CustomerId)
68
+	if err != nil {
69
+		utils.LogError("查询用户账户信息出错: " + err.Error())
70
+		return errors.New("查询用户账户信息出错")
75 71
 	}
72
+	accMoney, _ := strconv.ParseFloat(account.Amount, 64)
73
+
74
+	if customercouponid != "" {
75
+		// 校验卡券, 同时进行卡券的注销或者已使用的更新操作
76
+		// TODO
77
+		couseOrder.PayType = models.CONSUME_COUPON
78
+
79
+		// 优惠券校验
80
+		customerCoupon, err := s.couponDAO.GetCustomerCouponByID(customercouponid)
81
+		if err != nil {
82
+			utils.LogError("查询优惠券信息失败: " + err.Error())
83
+			return errors.New("查询优惠券信息失败")
84
+		}
85
+		if customerCoupon == nil || customerCoupon.CustomerCouponId == "" {
86
+			return errors.New("优惠券无效!")
87
+		}
88
+		if customerCoupon.Status != models.STATUS_NORMAL || !customerCoupon.UseDate.IsZero() {
89
+			return errors.New("优惠券已被使用!请重新选择优惠券!")
90
+		}
91
+
92
+		// 根据id获取优惠券信息
93
+		coupon, err := s.couponDAO.GetCouponInfoByCustomerCouponID(customercouponid)
94
+		if err != nil {
95
+			utils.LogError("查询优惠券信息失败: " + err.Error())
96
+			return errors.New("查询优惠券信息失败")
97
+		}
98
+
99
+		// 判断优惠券是否可以抵用商品
100
+		var isdy = false
101
+		if coupon.IsAll == 1 {
102
+			isdy = true
103
+			couseOrder.CouponAmount = couseOrder.Price
104
+		} else {
105
+			for _, target := range coupon.Targets {
106
+				if target.TargetId == course.CourseId {
107
+					isdy = true
108
+				}
109
+			}
110
+			couseOrder.CouponAmount = coupon.Price
111
+		}
112
+		if !isdy {
113
+			return errors.New("优惠券不可抵用该课程")
114
+		}
115
+		var ordersCoupon = model.TaCourseOrdersCoupon{
116
+			OrdersId:   couseOrder.OrdersId,
117
+			CouponId:   coupon.CouponId,
118
+			CouponName: coupon.CouponName,
119
+			UsedAmount: couseOrder.CouponAmount,
120
+		}
121
+		if err := s.dao.SaveOrdersCoupon(&ordersCoupon, couseOrder); err != nil {
122
+			utils.LogError("保存优惠信息出错: " + err.Error())
123
+			return errors.New("保存优惠信息出错")
124
+		}
125
+
126
+		// 优惠券核销
127
+		err = s.couponDAO.VerifyCustomerCoupon(customercouponid)
128
+		if err != nil {
129
+			utils.LogError("优惠券核销出错: " + err.Error())
130
+			return errors.New("优惠券核销出错")
131
+		}
132
+	} else {
133
+		couseOrder.PayType = models.CONSUME_COINCHG
134
+	}
135
+
136
+	couponAmount, _ := strconv.ParseFloat(couseOrder.CouponAmount, 64)
137
+	payMoney, _ := strconv.ParseFloat(couseOrder.Price, 64)
138
+	payMoney = payMoney - couponAmount
139
+
140
+	couseOrder.ActualAmount = strconv.FormatFloat(payMoney, 'f', -1, 64)
141
+
142
+	// 校验金额
143
+	// if err := s.validBillCharges(couseOrder, coupons); err != nil {
144
+	// 	return err
145
+	// }
76 146
 
77 147
 	// 实际支付
78
-	actualAmount, _ := strconv.ParseFloat(couseOrder.ActualAmount, 64)
148
+	actualAmount := payMoney
79 149
 
80 150
 	// 用户账户  -- 内部人员也可以购买
81 151
 	if actualAmount > 0 {
82
-		couseOrder.PayType = models.CONSUME_COINCHG
83
-		account, err := s.custDAO.GetAccountByCust(cust.CustomerId)
84
-		if err != nil {
85
-			utils.LogError("查询用户账户信息出错: " + err.Error())
86
-			return errors.New("查询用户账户信息出错")
87
-		}
88
-		accMoney, _ := strconv.ParseFloat(account.Amount, 64)
89 152
 
90 153
 		if actualAmount > accMoney {
91 154
 			return errors.New("账户余额不足")
@@ -99,7 +162,7 @@ func (s *CourseServ) Orders(
99 162
 	}
100 163
 
101 164
 	// 入库
102
-	if err := s.SaveOrder(couseOrder, coupons, course); err != nil {
165
+	if err := s.SaveOrder(couseOrder, course); err != nil {
103 166
 		utils.LogError("课程下单失败: " + err.Error())
104 167
 		return errors.New("下单失败, 请重试")
105 168
 	}
@@ -226,7 +289,7 @@ func (s *CourseServ) saveCustomerPayRec(account *model.TaCustomerAccount, info *
226 289
 }
227 290
 
228 291
 // SaveOrder 保存订单明细
229
-func (s *CourseServ) SaveOrder(order *model.TaCourseOrders, coupons []model.TaCourseOrdersCoupon, course *course.CourseDetail) error {
292
+func (s *CourseServ) SaveOrder(order *model.TaCourseOrders, course *course.CourseDetail) error {
230 293
 	// 订单信息
231 294
 	if err := s.dao.SaveCourseOrder(order); err != nil {
232 295
 		utils.LogError("课程下单失败: " + err.Error())
@@ -234,16 +297,16 @@ func (s *CourseServ) SaveOrder(order *model.TaCourseOrders, coupons []model.TaCo
234 297
 	}
235 298
 
236 299
 	// 默认城币购买
237
-	courseObtaimType := models.COURSE_GETBY_COINCHG
238
-	if coupons != nil && len(coupons) > 0 {
239
-		for _, c := range coupons {
240
-			if c.CouponType == models.COURSE_COUPON_CARD {
241
-				courseObtaimType = models.COURSE_GETBY_CARD
242
-			} else if c.CouponType == models.COURSE_COUPON_COUPON {
243
-				courseObtaimType = models.COURSE_GETBY_COUPON
244
-			}
245
-		}
246
-	}
300
+	// courseObtaimType := models.COURSE_GETBY_COINCHG
301
+	// if coupons != nil && len(coupons) > 0 {
302
+	// 	for _, c := range coupons {
303
+	// 		if c.CouponType == models.COURSE_COUPON_CARD {
304
+	// 			courseObtaimType = models.COURSE_GETBY_CARD
305
+	// 		} else if c.CouponType == models.COURSE_COUPON_COUPON {
306
+	// 			courseObtaimType = models.COURSE_GETBY_COUPON
307
+	// 		}
308
+	// 	}
309
+	// }
247 310
 
248 311
 	// 我的课程信息
249 312
 	custCourse := model.TaCustomerCourse{
@@ -257,7 +320,7 @@ func (s *CourseServ) SaveOrder(order *model.TaCourseOrders, coupons []model.TaCo
257 320
 		CourseNum:        course.CourseNum,
258 321
 		JoinNum:          0,
259 322
 		CreateDate:       time.Now().Local(),
260
-		CourseObtaimType: courseObtaimType,
323
+		CourseObtaimType: order.PayType,
261 324
 		SourceId:         order.OrdersId,
262 325
 		IsDone:           models.BOOL_FALSE,
263 326
 	}

+ 13
- 10
service/goods/goods.go 파일 보기

@@ -4,6 +4,7 @@ import (
4 4
 	"errors"
5 5
 	"spaceofcheng/services/models"
6 6
 	"spaceofcheng/services/models/cases"
7
+	"spaceofcheng/services/models/coupon"
7 8
 	"spaceofcheng/services/models/customer"
8 9
 	"spaceofcheng/services/models/goods"
9 10
 	"spaceofcheng/services/models/model"
@@ -14,21 +15,23 @@ import (
14 15
 
15 16
 // GoodsServ 系统处理
16 17
 type GoodsServ struct {
17
-	ctx     *utils.Context
18
-	dao     *goods.GoodsDAO
19
-	caseDAO *cases.CaseDAO
20
-	custDAO *customer.CustomerDAO
21
-	userDAO *system.UserDAO
18
+	ctx       *utils.Context
19
+	dao       *goods.GoodsDAO
20
+	caseDAO   *cases.CaseDAO
21
+	custDAO   *customer.CustomerDAO
22
+	userDAO   *system.UserDAO
23
+	couponDAO *coupon.CouponDAO
22 24
 }
23 25
 
24 26
 // NewGoodsServ 初始化
25 27
 func NewGoodsServ(ctx *utils.Context) *GoodsServ {
26 28
 	return &GoodsServ{
27
-		ctx:     ctx,
28
-		dao:     goods.NewGoodsDAO(ctx),
29
-		caseDAO: cases.NewCaseDAO(ctx),
30
-		custDAO: customer.NewCustomerDAO(ctx),
31
-		userDAO: system.NewUserDAO(ctx),
29
+		ctx:       ctx,
30
+		dao:       goods.NewGoodsDAO(ctx),
31
+		caseDAO:   cases.NewCaseDAO(ctx),
32
+		custDAO:   customer.NewCustomerDAO(ctx),
33
+		userDAO:   system.NewUserDAO(ctx),
34
+		couponDAO: coupon.NewCouponDAO(ctx),
32 35
 	}
33 36
 }
34 37
 

+ 91
- 23
service/goods/orders.go 파일 보기

@@ -20,7 +20,7 @@ import (
20 20
 func (s *GoodsServ) Orders(
21 21
 	info *model.TaGoodsOrders,
22 22
 	details []model.TaGoodsOrdersDetail,
23
-	coupons []model.TaGoodsOrdersCoupon) error {
23
+	customercouponid string) error {
24 24
 	// org := s.ctx.Get("org").(model.SysOrg)
25 25
 	cust := s.ctx.Get("customer").(model.TaCustomer)
26 26
 
@@ -48,7 +48,6 @@ func (s *GoodsServ) Orders(
48 48
 		// TODO
49 49
 	} else {
50 50
 		// 普通客户
51
-
52 51
 		account, err := s.custDAO.GetAccountByCust(info.UserId)
53 52
 		if err != nil {
54 53
 			utils.LogError("查询用户账户信息出错: " + err.Error())
@@ -56,39 +55,108 @@ func (s *GoodsServ) Orders(
56 55
 		}
57 56
 
58 57
 		// 如果是使用优惠券
59
-		if coupons != nil && len(coupons) > 0 {
58
+		if customercouponid != "" {
60 59
 			info.PayType = models.CONSUME_COUPON
61 60
 
62
-			// TODO
63
-			// 校验优惠券相关
64
-			// 优惠券默认抵消全部金额
65
-			info.ActualAmount = "0.0"
61
+			// 优惠券校验
62
+			customerCoupon, err := s.couponDAO.GetCustomerCouponByID(customercouponid)
63
+			if err != nil {
64
+				utils.LogError("查询优惠券信息失败: " + err.Error())
65
+				return errors.New("查询优惠券信息失败")
66
+			}
67
+			if customerCoupon == nil || customerCoupon.CustomerCouponId == "" {
68
+				return errors.New("优惠券无效!")
69
+			}
70
+			if customerCoupon.Status != models.STATUS_NORMAL || !customerCoupon.UseDate.IsZero() {
71
+				return errors.New("优惠券已被使用!请重新选择优惠券!")
72
+			}
73
+
74
+			// 根据id获取优惠券信息
75
+			coupon, err := s.couponDAO.GetCouponInfoByCustomerCouponID(customercouponid)
76
+			if err != nil {
77
+				utils.LogError("查询优惠券信息失败: " + err.Error())
78
+				return errors.New("查询优惠券信息失败")
79
+			}
80
+			// 遍历优惠券可抵用的商品
81
+			var dyGoods = model.TaGoodsOrdersDetail{
82
+				Price:   "0.0",
83
+				GoodsId: "",
84
+			}
85
+			for _, detail := range details {
86
+				if coupon.IsAll == 1 {
87
+					dyprice, _ := strconv.ParseFloat(dyGoods.Price, 64)
88
+					detailprice, _ := strconv.ParseFloat(detail.Price, 64)
89
+					if dyprice < detailprice {
90
+						dyGoods = detail
91
+					}
92
+				} else {
93
+					for _, target := range coupon.Targets {
94
+						if target.TargetId == detail.GoodsId {
95
+							dyprice, _ := strconv.ParseFloat(dyGoods.Price, 64)
96
+							detailprice, _ := strconv.ParseFloat(detail.Price, 64)
97
+							if dyprice < detailprice {
98
+								dyGoods = detail
99
+							}
100
+						}
101
+					}
102
+				}
103
+			}
104
+			if dyGoods.GoodsId != "" {
105
+				// 优惠券可以抵用商品
106
+				var ordersCoupon = model.TaGoodsOrdersCoupon{
107
+					OrdersId:   info.OrdersId,
108
+					OrdersNo:   info.OrdersNo,
109
+					CouponId:   coupon.CouponId,
110
+					CouponName: coupon.CouponName,
111
+					OrgId:      coupon.OrgId,
112
+					GoodsId:    dyGoods.GoodsId,
113
+					GoodsName:  dyGoods.GoodsName,
114
+					UsedAmount: dyGoods.Price,
115
+				}
116
+				if err := s.dao.SaveOrdersCoupon(&ordersCoupon, info); err != nil {
117
+					utils.LogError("保存优惠信息出错: " + err.Error())
118
+					return errors.New("保存优惠信息出错")
119
+				}
120
+				info.CouponAmount = dyGoods.Price
121
+				// info.ActualAmount = "0.0"
122
+
123
+				// 优惠券核销
124
+				err := s.couponDAO.VerifyCustomerCoupon(customercouponid)
125
+				if err != nil {
126
+					utils.LogError("优惠券核销出错: " + err.Error())
127
+					return errors.New("优惠券核销出错")
128
+				}
129
+			} else {
130
+				return errors.New("优惠券不可抵用商品")
131
+			}
66 132
 		} else {
67 133
 			// 如果是使用城币
68 134
 			info.PayType = models.CONSUME_COINCHG
69
-			accMoney, _ := strconv.ParseFloat(account.Amount, 64)
70
-			payMoney, _ := strconv.ParseFloat(info.Amount, 64)
71
-
72
-			if accMoney < payMoney {
73
-				return errors.New("账户余额不足")
74
-			}
135
+		}
75 136
 
76
-			info.ActualAmount = strconv.FormatFloat(payMoney, 'f', -1, 64)
137
+		accMoney, _ := strconv.ParseFloat(account.Amount, 64)
138
+		couponAmount, _ := strconv.ParseFloat(info.CouponAmount, 64)
139
+		payMoney, _ := strconv.ParseFloat(info.Amount, 64)
140
+		payMoney = payMoney - couponAmount
141
+		if accMoney < payMoney {
142
+			return errors.New("账户余额不足")
77 143
 		}
78 144
 
145
+		info.ActualAmount = strconv.FormatFloat(payMoney, 'f', -1, 64)
146
+
79 147
 		// 保存优惠券使用记录
80
-		if coupons != nil && len(coupons) > 0 {
81
-			if err := s.dao.SaveOrdersCoupon(coupons, info); err != nil {
82
-				utils.LogError("保存优惠信息出错: " + err.Error())
83
-				return errors.New("保存优惠信息出错")
84
-			}
148
+		// if coupons != nil && len(coupons) > 0 {
149
+		// 	if err := s.dao.SaveOrdersCoupon(coupons, info); err != nil {
150
+		// 		utils.LogError("保存优惠信息出错: " + err.Error())
151
+		// 		return errors.New("保存优惠信息出错")
152
+		// 	}
85 153
 
86
-			// TODO
87
-			// 核销优惠券
88
-		}
154
+		// TODO
155
+		// 核销优惠券
156
+		// }
89 157
 
90 158
 		// 如果是城币, 则插入用户账户消费记录
91
-		if info.PayType == models.CONSUME_COINCHG {
159
+		if payMoney > 0 {
92 160
 			if err := s.saveCustomerPayRec(account, info); err != nil {
93 161
 				return err
94 162
 			}

+ 56
- 35
service/statistics/dashboard.go 파일 보기

@@ -1,37 +1,58 @@
1 1
 package statistics
2 2
 
3
-// import (
4
-// 	"spaceofcheng/services/models/model"
5
-// 	"spaceofcheng/services/utils"
6
-// 	"strings"
7
-// )
8
-
9
-// // StaDashboard 控制板统计
10
-// func (s *StatisticsServ) StaDashboard() error {
11
-// 	user := s.ctx.Get("user").(model.SysUser)
12
-
13
-// }
14
-
15
-// // distinctDashSettings 去重 dashboard 配置
16
-// func (s *StatisticsServ) distinctDashSettings(settings []model.TaDashboardSetting) []string {
17
-// 	comps := make([]string, 0)
18
-
19
-// 	if settings == nil || len(settings) == 0 {
20
-// 		return comps
21
-// 	}
22
-
23
-// 	for _, s := range settings {
24
-// 		comps = append(comps, s.CompList)
25
-// 	}
26
-
27
-// 	allComps := strings.Split(strings.Join(comps, ","), ",")
28
-
29
-// 	res := make([]string, 0)
30
-// 	for _, cmp := range allComps {
31
-// 		if utils.StrSliceIndexOf(res, cmp) == -1 {
32
-// 			res = append(res, cmp)
33
-// 		}
34
-// 	}
35
-
36
-// 	return res
37
-// }
3
+import (
4
+	"spaceofcheng/services/models/model"
5
+	"spaceofcheng/services/utils"
6
+	"strings"
7
+)
8
+
9
+// StaDashboard 控制板统计
10
+func (s *StatisticsServ) StaDashboard() ([]map[string]interface{}, error) {
11
+	// user := s.ctx.Get("user").(model.SysUser)
12
+	// userTypes, err := s.dao.GetUserTypes(user.UserId)
13
+	// if err != nil {
14
+	// 	utils.LogError("获取用户类型失败: " + err.Error())
15
+	// 	return nil, errors.New("获取用户类型失败")
16
+	// }
17
+
18
+	// tps := make([]string, 0)
19
+	// if userTypes != nil {
20
+	// 	for _, tp := range userTypes {
21
+	// 		tps = append(tps, tp.TypeId)
22
+	// 	}
23
+	// }
24
+
25
+	// settings, err := s.dao.GetDashboardSetting(user.UserId, tps)
26
+	// if err != nil {
27
+	// 	utils.LogError("获取用户 Dashboard 配置失败: " + err.Error())
28
+	// 	return nil, errors.New("获取用户 Dashboard 配置失败")
29
+	// }
30
+
31
+	// comps := s.distinctDashSettings(settings)
32
+
33
+	return nil, nil
34
+}
35
+
36
+// distinctDashSettings 去重 dashboard 配置
37
+func (s *StatisticsServ) distinctDashSettings(settings []model.TaDashboardSetting) []string {
38
+	comps := make([]string, 0)
39
+
40
+	if settings == nil || len(settings) == 0 {
41
+		return comps
42
+	}
43
+
44
+	for _, s := range settings {
45
+		comps = append(comps, s.CompList)
46
+	}
47
+
48
+	allComps := strings.Split(strings.Join(comps, ","), ",")
49
+
50
+	res := make([]string, 0)
51
+	for _, cmp := range allComps {
52
+		if utils.StrSliceIndexOf(res, cmp) == -1 {
53
+			res = append(res, cmp)
54
+		}
55
+	}
56
+
57
+	return res
58
+}

+ 8
- 1
service/statistics/setting.go 파일 보기

@@ -1,6 +1,13 @@
1 1
 package statistics
2 2
 
3
+const (
4
+	STA_TOTAL_CUSTOMER = "total-customer"
5
+	// STA_TOTAL_CUSTOMER = "total-customer"
6
+	// STA_TOTAL_CUSTOMER = "total-customer"
7
+	// STA_TOTAL_CUSTOMER = "total-customer"
8
+)
9
+
3 10
 // StaticMapFrontComponent 每种统计映射的前端组件名称
4 11
 var StaticMapFrontComponent = map[string]string{
5
-	"": "",
12
+	"total": "",
6 13
 }

+ 63
- 75
service/sys.go 파일 보기

@@ -8,7 +8,6 @@ import (
8 8
 	"spaceofcheng/services/models/customer"
9 9
 	"spaceofcheng/services/models/model"
10 10
 	"spaceofcheng/services/utils"
11
-	"strconv"
12 11
 	"strings"
13 12
 	"time"
14 13
 
@@ -58,42 +57,15 @@ func (s *SysServ) AuthAndInitCtx(gctx *context.Context) map[string]interface{} {
58 57
 	// 客户端类型
59 58
 	// 通过 UA 判断
60 59
 	clientType := utils.GetClientType(gctx.Request)
61
-	if clientType == utils.ClientWechat {
62
-		// 初始化微信配置
63
-		if err := s.initWechatClient(s.org.OrgId); err != nil {
64
-			utils.LogError("初始化微信客户端失败: " + err.Error())
65
-
66
-			return map[string]interface{}{
67
-				"code":  http.StatusInternalServerError,
68
-				"error": errors.New("初始化微信客户端失败"),
69
-			}
70
-		}
71
-	}
72
-
73
-	// 获取 token
74
-	token, err := s.getToken(gctx, clientType)
75
-	if err != nil {
76
-		message := make(map[string]interface{})
77
-		if clientType == utils.ClientWechat {
78
-			message["appid"] = utils.GetWxAppID(s.org.OrgId)
79
-		}
80
-
81
-		// token 报错一律视为需要重新登录
82
-		return map[string]interface{}{
83
-			"code":    http.StatusUnauthorized,
84
-			"error":   err,
85
-			"message": message,
86
-		}
87
-	}
88 60
 
89 61
 	// pc 管理端
90 62
 	if clientType == utils.ClientAdmin {
91
-		return s.authPCAdmin(gctx, token)
63
+		return s.authPCAdmin(gctx)
92 64
 	}
93 65
 
94 66
 	// wechat 端
95 67
 	if clientType == utils.ClientWechat {
96
-		return s.authWechat(gctx, token)
68
+		return s.authWechat(gctx)
97 69
 	}
98 70
 
99 71
 	return map[string]interface{}{
@@ -150,11 +122,21 @@ func (s *SysServ) NewToken() string {
150 122
 
151 123
 // authPCAdmin
152 124
 // 管理端 API 校验
153
-func (s *SysServ) authPCAdmin(gctx *context.Context, token *utils.JWTToken) map[string]interface{} {
125
+func (s *SysServ) authPCAdmin(gctx *context.Context) map[string]interface{} {
154 126
 	if !s.needAuth(gctx) {
155 127
 		return nil
156 128
 	}
157 129
 
130
+	// 获取 token
131
+	token, err := s.getToken(gctx)
132
+	if err != nil {
133
+		// token 报错一律视为需要重新登录
134
+		return map[string]interface{}{
135
+			"code":  http.StatusUnauthorized,
136
+			"error": err,
137
+		}
138
+	}
139
+
158 140
 	if token.ID == "" || token.Guest == true {
159 141
 		return map[string]interface{}{
160 142
 			"code":  http.StatusUnauthorized,
@@ -172,20 +154,42 @@ func (s *SysServ) authPCAdmin(gctx *context.Context, token *utils.JWTToken) map[
172 154
 	return nil
173 155
 }
174 156
 
175
-func (s *SysServ) authWechat(gctx *context.Context, token *utils.JWTToken) map[string]interface{} {
176
-	// 微信 code
177
-	code := gctx.Input.Query("code")
178
-
157
+func (s *SysServ) authWechat(gctx *context.Context) map[string]interface{} {
179 158
 	var wxUser *utils.WechatUser
180 159
 	var openID string
181 160
 
182
-	// 未登录 或 未验证
183
-	if token.ID == "" {
184
-		// 微信用户信息
185
-		var err error
186
-		wxUser, err = s.wechartSignIn(gctx, code)
161
+	if beego.BConfig.RunMode == "dev" {
162
+		openID = "OPENID"
163
+	} else {
164
+		// 初始化微信配置
165
+		if err := s.initWechatClient(s.org.OrgId); err != nil {
166
+			utils.LogError("初始化微信客户端失败: " + err.Error())
167
+
168
+			return map[string]interface{}{
169
+				"code":  http.StatusInternalServerError,
170
+				"error": errors.New("初始化微信客户端失败"),
171
+			}
172
+		}
173
+
174
+		// 微信 code
175
+		code := gctx.Input.Query("code")
176
+
177
+		// 获取 token
178
+		token, err := s.getToken(gctx)
187 179
 		if err != nil {
188
-			if strings.Index(err.Error(), strconv.Itoa(http.StatusUnauthorized)) > -1 {
180
+			// token 报错一律视为需要重新登录
181
+			return map[string]interface{}{
182
+				"code":  http.StatusUnauthorized,
183
+				"error": err,
184
+				"message": map[string]interface{}{
185
+					"appid": utils.GetWxAppID(s.org.OrgId),
186
+				},
187
+			}
188
+		}
189
+
190
+		// 未登录 或 未验证
191
+		if token.ID == "" {
192
+			if code == "" {
189 193
 				return map[string]interface{}{
190 194
 					"code":  http.StatusUnauthorized,
191 195
 					"error": errors.New("请授权微信用户登录"),
@@ -195,22 +199,27 @@ func (s *SysServ) authWechat(gctx *context.Context, token *utils.JWTToken) map[s
195 199
 				}
196 200
 			}
197 201
 
198
-			return map[string]interface{}{
199
-				"code":  http.StatusInternalServerError,
200
-				"error": err,
202
+			// 微信用户信息
203
+			var err error
204
+			wxUser, err = s.wechartSignIn(gctx, code)
205
+			if err != nil {
206
+				return map[string]interface{}{
207
+					"code":  http.StatusInternalServerError,
208
+					"error": err,
209
+				}
201 210
 			}
202
-		}
203 211
 
204
-		if wxUser == nil {
205
-			return map[string]interface{}{
206
-				"code":  http.StatusInternalServerError,
207
-				"error": errors.New("请先关注公众号"),
212
+			if wxUser == nil {
213
+				return map[string]interface{}{
214
+					"code":  http.StatusInternalServerError,
215
+					"error": errors.New("请先关注公众号"),
216
+				}
208 217
 			}
209
-		}
210 218
 
211
-		openID = wxUser.OpenID
212
-	} else {
213
-		openID = token.ID
219
+			openID = wxUser.OpenID
220
+		} else {
221
+			openID = token.ID
222
+		}
214 223
 	}
215 224
 
216 225
 	// 查询数据库是否存在已有映射
@@ -310,16 +319,6 @@ func (s *SysServ) authWechat(gctx *context.Context, token *utils.JWTToken) map[s
310 319
 
311 320
 // wechartSignIn 使用 code 微信登录
312 321
 func (s *SysServ) wechartSignIn(gctx *context.Context, code string) (*utils.WechatUser, error) {
313
-	if beego.BConfig.RunMode == "dev" {
314
-		return &utils.WechatUser{
315
-			OpenID: "OPENID",
316
-		}, nil
317
-	}
318
-
319
-	if code == "" {
320
-		return nil, errors.New(strconv.Itoa(http.StatusUnauthorized))
321
-	}
322
-
323 322
 	// 获取 微信信息
324 323
 	// 可能出现的情况是 openid 获取到了, 但是详情没有获取到
325 324
 	wxUserMap, err := utils.WxClientFor(s.org.OrgId).GetUserInfo(code)
@@ -334,18 +333,7 @@ func (s *SysServ) wechartSignIn(gctx *context.Context, code string) (*utils.Wech
334 333
 	return utils.MapToWechatUser(wxUserMap), nil
335 334
 }
336 335
 
337
-func (s *SysServ) getToken(gctx *context.Context, clientType string) (*utils.JWTToken, error) {
338
-	if beego.BConfig.RunMode == "dev" {
339
-		id := "0"
340
-		if clientType == utils.ClientWechat {
341
-			id = "OPENID"
342
-		}
343
-
344
-		return &utils.JWTToken{
345
-			ID: id,
346
-		}, nil
347
-	}
348
-
336
+func (s *SysServ) getToken(gctx *context.Context) (*utils.JWTToken, error) {
349 337
 	tokenRaw := gctx.Input.Header(utils.TokenHeader)
350 338
 	if tokenRaw == "" {
351 339
 		return new(utils.JWTToken), nil

+ 2
- 2
service/user.go 파일 보기

@@ -379,14 +379,14 @@ func (s *UserServ) SaveUserRole(userid, roleids string) error {
379 379
 }
380 380
 
381 381
 // GetUserCustomer 获取我的推荐客户
382
-func (s *UserServ) GetUserCustomer(userid, isrecommend string, page int, pageSize int) ([]model.TaCustomer, error) {
382
+func (s *UserServ) GetUserCustomer(userid, isrecommend, key string, page int, pageSize int) ([]model.TaCustomer, error) {
383 383
 	if pageSize == 0 {
384 384
 		pageSize = PAGENUM
385 385
 	}
386 386
 	if page == 0 {
387 387
 		page = 1
388 388
 	}
389
-	customers, err := s.dao.GetUserCustomer(userid, isrecommend, page, pageSize)
389
+	customers, err := s.dao.GetUserCustomer(userid, isrecommend, key, page, pageSize)
390 390
 	return customers, err
391 391
 }
392 392
 

+ 16
- 39
utils/excel.go 파일 보기

@@ -1,8 +1,8 @@
1 1
 package utils
2 2
 
3 3
 import (
4
+	"errors"
4 5
 	"io"
5
-	"reflect"
6 6
 
7 7
 	"github.com/tealeg/xlsx"
8 8
 )
@@ -10,56 +10,33 @@ import (
10 10
 // TinyXLSXEngine 简版 xlsx 处理器
11 11
 type TinyXLSXEngine struct {
12 12
 	xlsxFile *xlsx.File
13
-	toSlice  func(interface{}) []interface{}
13
+	sheet    *xlsx.Sheet
14 14
 }
15 15
 
16 16
 // NewTinyXLSXEngine init
17
-func NewTinyXLSXEngine() *TinyXLSXEngine {
18
-	return &TinyXLSXEngine{}
19
-}
20
-
21
-// SetTransFunc 设置转换函数
22
-func (t *TinyXLSXEngine) SetTransFunc(f func(interface{}) []interface{}) *TinyXLSXEngine {
23
-	t.toSlice = f
24
-	return t
25
-}
26
-
27
-// SetData 设置数据
28
-func (t *TinyXLSXEngine) SetData(data interface{}) *TinyXLSXEngine {
17
+func NewTinyXLSXEngine() (*TinyXLSXEngine, error) {
29 18
 	file := xlsx.NewFile()
30
-	t.xlsxFile = file
31 19
 
32 20
 	sheet, err := file.AddSheet("sheet1")
33 21
 	if err != nil {
34 22
 		LogError("创建 xlsx sheet 失败: " + err.Error())
35
-		return t
36
-	}
37
-
38
-	if t.toSlice == nil {
39
-		t.toSlice = func(dt interface{}) []interface{} {
40
-			return nil
41
-		}
23
+		return nil, errors.New("创建 xlsx sheet 失败")
42 24
 	}
43 25
 
44
-	v := reflect.ValueOf(data)
45
-	if v.Kind() == reflect.Ptr {
46
-		v.Elem()
47
-	}
48
-
49
-	if v.Kind() == reflect.Slice && v.IsValid() {
50
-		l := v.Len()
51
-		for i := 0; i < l; i++ {
52
-			rowData := v.Index(i).Interface()
53
-			cols := t.toSlice(rowData)
26
+	return &TinyXLSXEngine{
27
+		xlsxFile: file,
28
+		sheet:    sheet,
29
+	}, nil
30
+}
54 31
 
55
-			row := sheet.AddRow()
56
-			for _, col := range cols {
57
-				row.AddCell().SetValue(col)
58
-			}
59
-		}
60
-	}
32
+// InsertRow 新增行
33
+func (t *TinyXLSXEngine) InsertRow() *xlsx.Row {
34
+	return t.sheet.AddRow()
35
+}
61 36
 
62
-	return t
37
+// SetCell 新增数据
38
+func (t *TinyXLSXEngine) SetCell(row *xlsx.Row, v interface{}) {
39
+	row.AddCell().SetValue(v)
63 40
 }
64 41
 
65 42
 // Write 写数据