wangfei 6 年之前
父節點
當前提交
650420f638
共有 8 個文件被更改,包括 202 次插入64 次删除
  1. 7
    7
      controllers/course/order.go
  2. 37
    0
      log/common.log
  3. 3
    5
      models/coupon/coupon.go
  4. 16
    0
      models/course/order.go
  5. 5
    3
      models/goods/orders.go
  6. 13
    10
      service/course/course.go
  7. 92
    30
      service/course/order.go
  8. 29
    9
      service/goods/orders.go

+ 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
 

+ 37
- 0
log/common.log 查看文件

@@ -3307,3 +3307,40 @@
3307 3307
 2018/09/14 10:05:36 [I] 请求远程接口: %!(EXTRA string=https://api.weixin.qq.com/cgi-bin/token?appid=wx7320287e057bbeee&grant_type=client_credential&secret=6e844dc2ade439545c47569ba1707673)
3308 3308
 2018/09/14 10:05:36 [I] 远程请求结果:%!(EXTRA string={"access_token":"13_pJwR0iIuLqwQgKOYyUBsFc2YTHtD04oNF4fMTDOK_0a0ux8dFcrUxbrj7FucyzO-QImo6gezgyZGtSYQyzwacpDEWSUHnBWd_Xgv-J_L9GLyNtUouMYhHdCBUWr95iD6CENDbrS3k1nZai9MFVDbAEAVLT","expires_in":7200})
3309 3309
 2018/09/14 10:05:36 [I] 接口返回结果: %!(EXTRA map[string]interface {}=map[access_token:13_pJwR0iIuLqwQgKOYyUBsFc2YTHtD04oNF4fMTDOK_0a0ux8dFcrUxbrj7FucyzO-QImo6gezgyZGtSYQyzwacpDEWSUHnBWd_Xgv-J_L9GLyNtUouMYhHdCBUWr95iD6CENDbrS3k1nZai9MFVDbAEAVLT expires_in:7200])
3310
+2018/09/14 10:21:38 [E] 保存优惠信息出错: Error 1062: Duplicate entry '' for key 'PRIMARY'
3311
+2018/09/14 10:22:42 [E] 保存优惠信息出错: Error 1062: Duplicate entry '' for key 'PRIMARY'
3312
+2018/09/14 11:18:43 [E] 用户没有设置默认案场
3313
+2018/09/14 11:18:43 [E] 用户没有设置默认案场
3314
+2018/09/14 11:18:46 [E] 用户没有设置默认案场
3315
+2018/09/14 11:18:46 [E] 用户没有设置默认案场
3316
+2018/09/14 11:18:52 [E] 用户没有设置默认案场
3317
+2018/09/14 11:18:56 [E] 用户没有设置默认案场
3318
+2018/09/14 11:18:58 [E] 用户没有设置默认案场
3319
+2018/09/14 11:18:59 [E] 用户没有设置默认案场
3320
+2018/09/14 11:18:59 [E] 用户没有设置默认案场
3321
+2018/09/14 11:19:08 [E] 用户没有设置默认案场
3322
+2018/09/14 11:19:12 [E] 用户没有设置默认案场
3323
+2018/09/14 11:19:13 [E] 用户没有设置默认案场
3324
+2018/09/14 11:19:14 [E] 用户没有设置默认案场
3325
+2018/09/14 11:19:14 [E] 用户没有设置默认案场
3326
+2018/09/14 11:20:19 [E] 用户没有设置默认案场
3327
+2018/09/14 11:20:22 [E] 用户没有设置默认案场
3328
+2018/09/14 11:20:26 [E] 用户没有设置默认案场
3329
+2018/09/14 11:20:28 [E] 用户没有设置默认案场
3330
+2018/09/14 11:20:59 [E] 用户没有设置默认案场
3331
+2018/09/14 11:21:13 [E] 用户没有设置默认案场
3332
+2018/09/14 11:21:41 [E] 用户没有设置默认案场
3333
+2018/09/14 11:21:45 [E] 用户没有设置默认案场
3334
+2018/09/14 11:21:50 [E] 用户没有设置默认案场
3335
+2018/09/14 11:21:50 [E] 用户没有设置默认案场
3336
+2018/09/14 11:22:49 [E] 用户没有设置默认案场
3337
+2018/09/14 11:22:55 [E] 用户没有设置默认案场
3338
+2018/09/14 11:23:08 [E] 用户没有设置默认案场
3339
+2018/09/14 11:23:08 [E] 用户没有设置默认案场
3340
+2018/09/14 11:24:44 [E] 用户没有设置默认案场
3341
+2018/09/14 11:25:29 [E] 用户没有设置默认案场
3342
+2018/09/14 11:25:38 [E] 用户没有设置默认案场
3343
+2018/09/14 11:25:38 [E] 用户没有设置默认案场
3344
+2018/09/14 13:46:17 [E] 下单优惠转换JSON失败: unexpected end of JSON input
3345
+2018/09/14 13:49:40 [E] 下单优惠转换JSON失败: unexpected end of JSON input
3346
+2018/09/14 13:52:47 [E] 查询优惠券信息失败: a pointer to a pointer is not allowed

+ 3
- 5
models/coupon/coupon.go 查看文件

@@ -426,13 +426,11 @@ func (m *CouponDAO) VerifyCustomerCoupon(id string) error {
426 426
 	var customerCoupon = model.TaCustomerCoupon{
427 427
 		CustomerCouponId: id,
428 428
 		UseDate:          time.Now(),
429
+		Status:           models.STATUS_READY,
429 430
 	}
430 431
 	var cols = []string{
431
-		"area_name",
432
-		"area_icon",
433
-		"area_icon_white",
434
-		"order",
435
-		"case_id",
432
+		"use_date",
433
+		"status",
436 434
 	}
437 435
 	_, err := m.db.Cols(cols...).Where("customer_coupon_id=?", id).Update(customerCoupon)
438 436
 	return err

+ 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
+}

+ 5
- 3
models/goods/orders.go 查看文件

@@ -73,12 +73,14 @@ 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
-	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 {
82 84
 		return err
83 85
 	}
84 86
 

+ 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
 

+ 92
- 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,90 @@ 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
+		} else {
104
+			for _, target := range coupon.Targets {
105
+				if target.TargetId == course.CourseId {
106
+					isdy = true
107
+				}
108
+			}
109
+		}
110
+		if !isdy {
111
+			return errors.New("优惠券不可抵用该课程")
112
+		}
113
+		var ordersCoupon = model.TaCourseOrdersCoupon{
114
+			OrdersId:   couseOrder.OrdersId,
115
+			CouponId:   coupon.CouponId,
116
+			CouponName: coupon.CouponName,
117
+			UsedAmount: coupon.Price,
118
+		}
119
+		if err := s.dao.SaveOrdersCoupon(&ordersCoupon, couseOrder); err != nil {
120
+			utils.LogError("保存优惠信息出错: " + err.Error())
121
+			return errors.New("保存优惠信息出错")
122
+		}
123
+		couseOrder.CouponAmount = coupon.Price
124
+
125
+		// 优惠券核销
126
+		err = s.couponDAO.VerifyCustomerCoupon(customercouponid)
127
+		if err != nil {
128
+			utils.LogError("优惠券核销出错: " + err.Error())
129
+			return errors.New("优惠券核销出错")
130
+		}
131
+	} else {
132
+		couseOrder.PayType = models.CONSUME_COINCHG
133
+	}
134
+
135
+	couponAmount, _ := strconv.ParseFloat(couseOrder.CouponAmount, 64)
136
+	payMoney, _ := strconv.ParseFloat(couseOrder.Price, 64)
137
+	payMoney = payMoney - couponAmount
138
+
139
+	couseOrder.ActualAmount = strconv.FormatFloat(payMoney, 'f', -1, 64)
140
+
141
+	// 校验金额
142
+	// if err := s.validBillCharges(couseOrder, coupons); err != nil {
143
+	// 	return err
144
+	// }
76 145
 
77 146
 	// 实际支付
78
-	actualAmount, _ := strconv.ParseFloat(couseOrder.ActualAmount, 64)
147
+	actualAmount := payMoney
79 148
 
80 149
 	// 用户账户  -- 内部人员也可以购买
81 150
 	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 151
 
90 152
 		if actualAmount > accMoney {
91 153
 			return errors.New("账户余额不足")
@@ -99,7 +161,7 @@ func (s *CourseServ) Orders(
99 161
 	}
100 162
 
101 163
 	// 入库
102
-	if err := s.SaveOrder(couseOrder, coupons, course); err != nil {
164
+	if err := s.SaveOrder(couseOrder, course); err != nil {
103 165
 		utils.LogError("课程下单失败: " + err.Error())
104 166
 		return errors.New("下单失败, 请重试")
105 167
 	}
@@ -226,7 +288,7 @@ func (s *CourseServ) saveCustomerPayRec(account *model.TaCustomerAccount, info *
226 288
 }
227 289
 
228 290
 // SaveOrder 保存订单明细
229
-func (s *CourseServ) SaveOrder(order *model.TaCourseOrders, coupons []model.TaCourseOrdersCoupon, course *course.CourseDetail) error {
291
+func (s *CourseServ) SaveOrder(order *model.TaCourseOrders, course *course.CourseDetail) error {
230 292
 	// 订单信息
231 293
 	if err := s.dao.SaveCourseOrder(order); err != nil {
232 294
 		utils.LogError("课程下单失败: " + err.Error())
@@ -234,16 +296,16 @@ func (s *CourseServ) SaveOrder(order *model.TaCourseOrders, coupons []model.TaCo
234 296
 	}
235 297
 
236 298
 	// 默认城币购买
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
-	}
299
+	// courseObtaimType := models.COURSE_GETBY_COINCHG
300
+	// if coupons != nil && len(coupons) > 0 {
301
+	// 	for _, c := range coupons {
302
+	// 		if c.CouponType == models.COURSE_COUPON_CARD {
303
+	// 			courseObtaimType = models.COURSE_GETBY_CARD
304
+	// 		} else if c.CouponType == models.COURSE_COUPON_COUPON {
305
+	// 			courseObtaimType = models.COURSE_GETBY_COUPON
306
+	// 		}
307
+	// 	}
308
+	// }
247 309
 
248 310
 	// 我的课程信息
249 311
 	custCourse := model.TaCustomerCourse{
@@ -257,7 +319,7 @@ func (s *CourseServ) SaveOrder(order *model.TaCourseOrders, coupons []model.TaCo
257 319
 		CourseNum:        course.CourseNum,
258 320
 		JoinNum:          0,
259 321
 		CreateDate:       time.Now().Local(),
260
-		CourseObtaimType: courseObtaimType,
322
+		CourseObtaimType: order.PayType,
261 323
 		SourceId:         order.OrdersId,
262 324
 		IsDone:           models.BOOL_FALSE,
263 325
 	}

+ 29
- 9
service/goods/orders.go 查看文件

@@ -57,26 +57,46 @@ func (s *GoodsServ) Orders(
57 57
 		// 如果是使用优惠券
58 58
 		if customercouponid != "" {
59 59
 			info.PayType = models.CONSUME_COUPON
60
+
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
+
60 74
 			// 根据id获取优惠券信息
61 75
 			coupon, err := s.couponDAO.GetCouponInfoByCustomerCouponID(customercouponid)
62 76
 			if err != nil {
63 77
 				utils.LogError("查询优惠券信息失败: " + err.Error())
64 78
 				return errors.New("查询优惠券信息失败")
65 79
 			}
66
-			// 优惠券校验
67
-			// s.couponDAO.GetCustomerCouponByID
68 80
 			// 遍历优惠券可抵用的商品
69 81
 			var dyGoods = model.TaGoodsOrdersDetail{
70 82
 				Price:   "0.0",
71 83
 				GoodsId: "",
72 84
 			}
73 85
 			for _, detail := range details {
74
-				for _, target := range coupon.Targets {
75
-					if target.TargetId == detail.GoodsId {
76
-						dyprice, _ := strconv.ParseFloat(dyGoods.Price, 64)
77
-						detailprice, _ := strconv.ParseFloat(detail.Price, 64)
78
-						if dyprice < detailprice {
79
-							dyGoods = detail
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
+							}
80 100
 						}
81 101
 					}
82 102
 				}
@@ -107,7 +127,7 @@ func (s *GoodsServ) Orders(
107 127
 					return errors.New("优惠券核销出错")
108 128
 				}
109 129
 			} else {
110
-				info.CouponAmount = "0.0"
130
+				return errors.New("优惠券不可抵用商品")
111 131
 			}
112 132
 		} else {
113 133
 			// 如果是使用城币