Преглед на файлове

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

zjxpcyc преди 6 години
родител
ревизия
7ddee1e5fa

+ 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

+ 5
- 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,8 @@ func (c *CardController) GiveCard() {
172 166
 
173 167
 	c.ResponseJSON("ok")
174 168
 }
169
+
170
+// GetCardWithCustomer 获取卡及领取信息
171
+func (c *CardController) GetCardWithCustomer() {
172
+
173
+}

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

+ 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
 

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

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

+ 102
- 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,75 @@ 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
+	}
430
+	var cols = []string{
431
+		"area_name",
432
+		"area_icon",
433
+		"area_icon_white",
434
+		"order",
435
+		"case_id",
436
+	}
437
+	_, err := m.db.Cols(cols...).Where("customer_coupon_id=?", id).Update(customerCoupon)
438
+	return err
439
+}

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

+ 33
- 29
models/goods/orders.go Целия файл

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

+ 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 Целия файл

@@ -260,6 +260,7 @@ func getCommonRoutes() beego.LinkNamespace {
260 260
 
261 261
 		// 签到
262 262
 		beego.NSRouter("/case/signin", &cases.SigninController{}, "get:GetSigninWhere"),
263
+		beego.NSRouter("/case/signin", &cases.SigninController{}, "post:AddSignin"),
263 264
 
264 265
 		// websocket
265 266
 		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/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
 

+ 72
- 24
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,88 @@ 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
-
62
-			// TODO
63
-			// 校验优惠券相关
64
-			// 优惠券默认抵消全部金额
65
-			info.ActualAmount = "0.0"
60
+			// 根据id获取优惠券信息
61
+			coupon, err := s.couponDAO.GetCouponInfoByCustomerCouponID(customercouponid)
62
+			if err != nil {
63
+				utils.LogError("查询优惠券信息失败: " + err.Error())
64
+				return errors.New("查询优惠券信息失败")
65
+			}
66
+			// 优惠券校验
67
+			// s.couponDAO.GetCustomerCouponByID
68
+			// 遍历优惠券可抵用的商品
69
+			var dyGoods = model.TaGoodsOrdersDetail{
70
+				Price:   "0.0",
71
+				GoodsId: "",
72
+			}
73
+			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
80
+						}
81
+					}
82
+				}
83
+			}
84
+			if dyGoods.GoodsId != "" {
85
+				// 优惠券可以抵用商品
86
+				var ordersCoupon = model.TaGoodsOrdersCoupon{
87
+					OrdersId:   info.OrdersId,
88
+					OrdersNo:   info.OrdersNo,
89
+					CouponId:   coupon.CouponId,
90
+					CouponName: coupon.CouponName,
91
+					OrgId:      coupon.OrgId,
92
+					GoodsId:    dyGoods.GoodsId,
93
+					GoodsName:  dyGoods.GoodsName,
94
+					UsedAmount: dyGoods.Price,
95
+				}
96
+				if err := s.dao.SaveOrdersCoupon(&ordersCoupon, info); err != nil {
97
+					utils.LogError("保存优惠信息出错: " + err.Error())
98
+					return errors.New("保存优惠信息出错")
99
+				}
100
+				info.CouponAmount = dyGoods.Price
101
+				// info.ActualAmount = "0.0"
102
+
103
+				// 优惠券核销
104
+				err := s.couponDAO.VerifyCustomerCoupon(customercouponid)
105
+				if err != nil {
106
+					utils.LogError("优惠券核销出错: " + err.Error())
107
+					return errors.New("优惠券核销出错")
108
+				}
109
+			} else {
110
+				info.CouponAmount = "0.0"
111
+			}
66 112
 		} else {
67 113
 			// 如果是使用城币
68 114
 			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
-			}
115
+		}
75 116
 
76
-			info.ActualAmount = strconv.FormatFloat(payMoney, 'f', -1, 64)
117
+		accMoney, _ := strconv.ParseFloat(account.Amount, 64)
118
+		couponAmount, _ := strconv.ParseFloat(info.CouponAmount, 64)
119
+		payMoney, _ := strconv.ParseFloat(info.Amount, 64)
120
+		payMoney = payMoney - couponAmount
121
+		if accMoney < payMoney {
122
+			return errors.New("账户余额不足")
77 123
 		}
78 124
 
125
+		info.ActualAmount = strconv.FormatFloat(payMoney, 'f', -1, 64)
126
+
79 127
 		// 保存优惠券使用记录
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
-			}
128
+		// if coupons != nil && len(coupons) > 0 {
129
+		// 	if err := s.dao.SaveOrdersCoupon(coupons, info); err != nil {
130
+		// 		utils.LogError("保存优惠信息出错: " + err.Error())
131
+		// 		return errors.New("保存优惠信息出错")
132
+		// 	}
85 133
 
86
-			// TODO
87
-			// 核销优惠券
88
-		}
134
+		// TODO
135
+		// 核销优惠券
136
+		// }
89 137
 
90 138
 		// 如果是城币, 则插入用户账户消费记录
91
-		if info.PayType == models.CONSUME_COINCHG {
139
+		if payMoney > 0 {
92 140
 			if err := s.saveCustomerPayRec(account, info); err != nil {
93 141
 				return err
94 142
 			}

+ 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