zjxpcyc 6 years ago
parent
commit
5ff3c1d750

+ 1
- 1
conf/sms.conf View File

@@ -4,4 +4,4 @@ method = POST
4 4
 contentType = "application/json;charset=UTF-8"
5 5
 
6 6
 [captcha]
7
-code = 0113
7
+message = {"code": "0113", "tel": "%s", "params": [%s]}

+ 19
- 19
controllers/course/course.go View File

@@ -8,7 +8,7 @@ import (
8 8
 
9 9
 // CourseController 商品
10 10
 type CourseController struct {
11
-	dao *course.CourseServ
11
+	serv *course.CourseServ
12 12
 	controllers.BaseController
13 13
 }
14 14
 
@@ -16,7 +16,7 @@ type CourseController struct {
16 16
 // @Title Constructor
17 17
 // @Description 初始化 Controller, 系统自动调用
18 18
 func (c *CourseController) Constructor() {
19
-	c.dao = course.NewCourseServ(c.Context)
19
+	c.serv = course.NewCourseServ(c.Context)
20 20
 }
21 21
 
22 22
 // GetCourseList 获取课程列表
@@ -30,7 +30,7 @@ func (c *CourseController) GetCourseList() {
30 30
 	pageSize, _ := c.GetInt("pagesize")
31 31
 	name := c.GetString("name")
32 32
 	typeid := c.GetString("typeid")
33
-	courses, err := c.dao.GetCourseList(caseids, name, typeid, page, pageSize)
33
+	courses, err := c.serv.GetCourseList(caseids, name, typeid, page, pageSize)
34 34
 	if err != nil {
35 35
 		c.ResponseError(err)
36 36
 	}
@@ -40,7 +40,7 @@ func (c *CourseController) GetCourseList() {
40 40
 // GetCourseByID 获取课程明细
41 41
 func (c *CourseController) GetCourseByID() {
42 42
 	courseid := c.GetString(":courseid")
43
-	info, err := c.dao.GetCourseByID(courseid)
43
+	info, err := c.serv.GetCourseByID(courseid)
44 44
 	if err != nil {
45 45
 		c.ResponseError(err)
46 46
 	}
@@ -54,7 +54,7 @@ func (c *CourseController) SaveCourse() {
54 54
 		c.ResponseError(err)
55 55
 	}
56 56
 	tagids := c.GetString("tagids")
57
-	newinfo, err := c.dao.SaveCourse(course, tagids)
57
+	newinfo, err := c.serv.SaveCourse(course, tagids)
58 58
 	if err != nil {
59 59
 		c.ResponseError(err)
60 60
 	}
@@ -64,7 +64,7 @@ func (c *CourseController) SaveCourse() {
64 64
 // DeleteCourse 删除课程
65 65
 func (c *CourseController) DeleteCourse() {
66 66
 	courseid := c.GetString(":courseid")
67
-	err := c.dao.DelCourse(courseid)
67
+	err := c.serv.DelCourse(courseid)
68 68
 	if err != nil {
69 69
 		c.ResponseError(err)
70 70
 	}
@@ -74,7 +74,7 @@ func (c *CourseController) DeleteCourse() {
74 74
 // CoursePublic 课程发布
75 75
 func (c *CourseController) CoursePublic() {
76 76
 	courseid := c.GetString(":courseid")
77
-	err := c.dao.CoursePublic(courseid)
77
+	err := c.serv.CoursePublic(courseid)
78 78
 	if err != nil {
79 79
 		c.ResponseError(err)
80 80
 	}
@@ -84,7 +84,7 @@ func (c *CourseController) CoursePublic() {
84 84
 // CourseUnPublic 课程取消发布
85 85
 func (c *CourseController) CourseUnPublic() {
86 86
 	courseid := c.GetString(":courseid")
87
-	err := c.dao.CourseUnPublic(courseid)
87
+	err := c.serv.CourseUnPublic(courseid)
88 88
 	if err != nil {
89 89
 		c.ResponseError(err)
90 90
 	}
@@ -94,7 +94,7 @@ func (c *CourseController) CourseUnPublic() {
94 94
 // GetCourseImgs 获取课程图片
95 95
 func (c *CourseController) GetCourseImgs() {
96 96
 	courseid := c.GetString(":courseid")
97
-	imgs, err := c.dao.GetCourseImgs(courseid)
97
+	imgs, err := c.serv.GetCourseImgs(courseid)
98 98
 	if err != nil {
99 99
 		c.ResponseError(err)
100 100
 	}
@@ -107,7 +107,7 @@ func (c *CourseController) SaveCourseImg() {
107 107
 	if err := c.ParseForm(&img); err != nil {
108 108
 		c.ResponseError(err)
109 109
 	}
110
-	newimg, err := c.dao.SaveCourseImg(img)
110
+	newimg, err := c.serv.SaveCourseImg(img)
111 111
 	if err != nil {
112 112
 		c.ResponseError(err)
113 113
 	}
@@ -117,7 +117,7 @@ func (c *CourseController) SaveCourseImg() {
117 117
 // DelCourseImg 删除课程图片
118 118
 func (c *CourseController) DelCourseImg() {
119 119
 	courseid := c.GetString(":courseid")
120
-	err := c.dao.DelCourseImg(courseid)
120
+	err := c.serv.DelCourseImg(courseid)
121 121
 	if err != nil {
122 122
 		c.ResponseError(err)
123 123
 	}
@@ -135,7 +135,7 @@ func (c *CourseController) GetCourseSchedule() {
135 135
 	date := c.GetString("date")
136 136
 	page, _ := c.GetInt("page")
137 137
 	pageSize, _ := c.GetInt("pagesize")
138
-	list, err := c.dao.GetCourseSchedule(name, caseids, date, page, pageSize)
138
+	list, err := c.serv.GetCourseSchedule(name, caseids, date, page, pageSize)
139 139
 	if err != nil {
140 140
 		c.ResponseError(err)
141 141
 	}
@@ -146,7 +146,7 @@ func (c *CourseController) GetCourseSchedule() {
146 146
 func (c *CourseController) GetDetails() {
147 147
 	caseid := c.GetString("caseid")
148 148
 	date := c.GetString("date")
149
-	list, err := c.dao.GetDetails(caseid, date)
149
+	list, err := c.serv.GetDetails(caseid, date)
150 150
 	if err != nil {
151 151
 		c.ResponseError(err)
152 152
 	}
@@ -156,7 +156,7 @@ func (c *CourseController) GetDetails() {
156 156
 // GetDetailByID 获取单个排课信息
157 157
 func (c *CourseController) GetDetailByID() {
158 158
 	detailid := c.GetString(":detailid")
159
-	detail, err := c.dao.GetDetailByID(detailid)
159
+	detail, err := c.serv.GetDetailByID(detailid)
160 160
 	if err != nil {
161 161
 		c.ResponseError(err)
162 162
 	}
@@ -169,7 +169,7 @@ func (c *CourseController) SaveDetail() {
169 169
 	if err := c.ParseForm(&detail); err != nil {
170 170
 		c.ResponseError(err)
171 171
 	}
172
-	newdetail, err := c.dao.SaveDetail(detail)
172
+	newdetail, err := c.serv.SaveDetail(detail)
173 173
 	if err != nil {
174 174
 		c.ResponseError(err)
175 175
 	}
@@ -179,7 +179,7 @@ func (c *CourseController) SaveDetail() {
179 179
 // DelCourseDetail 删除课程明细
180 180
 func (c *CourseController) DelCourseDetail() {
181 181
 	detailid := c.GetString(":detailid")
182
-	err := c.dao.DelCourseDetail(detailid)
182
+	err := c.serv.DelCourseDetail(detailid)
183 183
 	if err != nil {
184 184
 		c.ResponseError(err)
185 185
 	}
@@ -189,7 +189,7 @@ func (c *CourseController) DelCourseDetail() {
189 189
 // GetSelectCourseList 获取精选课程
190 190
 // func (c *CourseController) GetSelectCourseList() {
191 191
 // 	orgid := c.GetString("orgid")
192
-// 	courses, err := c.dao.GetSelectCourseList(orgid)
192
+// 	courses, err := c.serv.GetSelectCourseList(orgid)
193 193
 // 	if err != nil {
194 194
 // 		c.ResponseError(err)
195 195
 // 	}
@@ -201,13 +201,13 @@ func (c *CourseController) GetCourseByLocation() {
201 201
 	locationid := c.GetString("locationid")
202 202
 	orgid := c.GetString("orgid")
203 203
 	if locationid == "selected" {
204
-		courses, err := c.dao.GetSelectCourseList(orgid)
204
+		courses, err := c.serv.GetSelectCourseList(orgid)
205 205
 		if err != nil {
206 206
 			c.ResponseError(err)
207 207
 		}
208 208
 		c.ResponseJSON(courses)
209 209
 	}
210
-	courses, err := c.dao.GetCourseByLocation(orgid, locationid)
210
+	courses, err := c.serv.GetCourseByLocation(orgid, locationid)
211 211
 	if err != nil {
212 212
 		c.ResponseError(err)
213 213
 	}

+ 39
- 0
controllers/course/order.go View File

@@ -0,0 +1,39 @@
1
+package course
2
+
3
+import (
4
+	"encoding/json"
5
+	"errors"
6
+	"spaceofcheng/services/models/model"
7
+	"spaceofcheng/services/utils"
8
+)
9
+
10
+func (c *CourseController) PostOrder() {
11
+	// 订单主信息
12
+	info := c.GetString("info")
13
+	if info == "" {
14
+		c.ResponseError(errors.New("无有效订单信息"))
15
+	}
16
+
17
+	// 订单优惠券
18
+	coupon := c.GetString("coupon")
19
+
20
+	//
21
+	var orderInfo model.TaCourseOrders
22
+	var orderCoupon []model.TaCourseOrdersCoupon
23
+
24
+	if err := json.Unmarshal([]byte(info), &orderInfo); err != nil {
25
+		utils.LogError("下单转换JSON失败: " + err.Error())
26
+		c.ResponseError(errors.New("下单数据格式不正确"))
27
+	}
28
+
29
+	if err := json.Unmarshal([]byte(coupon), &orderCoupon); err != nil {
30
+		utils.LogError("下单优惠转换JSON失败: " + err.Error())
31
+		c.ResponseError(errors.New("优惠数据格式不正确"))
32
+	}
33
+
34
+	if err := c.serv.Orders(&orderInfo, orderCoupon); err != nil {
35
+		c.ResponseError(err)
36
+	}
37
+
38
+	c.ResponseJSON("ok")
39
+}

+ 28
- 4
models/constant.go View File

@@ -46,8 +46,10 @@ const (
46 46
 
47 47
 // 消费类型
48 48
 const (
49
-	CONSUME_POINTS   = "points"
50
-	CONSUME_MONEYCHG = "money-of-cheng"
49
+	CONSUME_INNER   = "sys user"
50
+	CONSUME_COUPON  = "coupon"
51
+	CONSUME_POINTS  = "points"
52
+	CONSUME_COINCHG = "cheng-coin"
51 53
 )
52 54
 
53 55
 // 消费方式
@@ -60,8 +62,10 @@ const (
60 62
 const (
61 63
 	// 充值
62 64
 	ACCSOURCE_RECHARGE = "recharge"
63
-	// 订单
64
-	ACCSOURCE_ORDERS = "orders"
65
+	// 商品订单
66
+	ACCSOURCE_GOODS_ORDERS = "goods-orders"
67
+	// 课程订单
68
+	ACCSOURCE_COURSE_ORDERS = "course-orders"
65 69
 	// 注册
66 70
 	ACCSOURCE_REGISTER = "register"
67 71
 	// 完善资料
@@ -83,3 +87,23 @@ const (
83 87
 	// 主管
84 88
 	USERTYPE_MANAGER = "manager"
85 89
 )
90
+
91
+// 课程获取方式
92
+const (
93
+	COURSE_GETBY_CARD    = "card"
94
+	COURSE_GETBY_COUPON  = "coupon"
95
+	COURSE_GETBY_COINCHG = "cheng-coin"
96
+)
97
+
98
+// 课程订单优惠券类型
99
+const (
100
+	COURSE_COUPON_CARD   = "card"
101
+	COURSE_COUPON_COUPON = "coupon"
102
+)
103
+
104
+// 核销类型
105
+const (
106
+	VERIFY_USEABLE = "useable"
107
+	VERIFY_USED    = "used"
108
+	VERIFY_LATE    = "late"
109
+)

+ 42
- 0
models/course/course.go View File

@@ -1,6 +1,7 @@
1 1
 package course
2 2
 
3 3
 import (
4
+	"errors"
4 5
 	"spaceofcheng/services/models"
5 6
 	"spaceofcheng/services/models/model"
6 7
 	"spaceofcheng/services/utils"
@@ -8,6 +9,8 @@ import (
8 9
 	"strings"
9 10
 	"time"
10 11
 
12
+	"github.com/yl10/kit/guid"
13
+
11 14
 	"github.com/go-xorm/xorm"
12 15
 )
13 16
 
@@ -374,3 +377,42 @@ func (m *CourseDAO) GetCourseByLocation(orgid, locationid string) ([]CourseDetai
374 377
 	err := m.db.Sql(sql).Find(&courses)
375 378
 	return courses, err
376 379
 }
380
+
381
+// GetCourseOfCustomer 获取客户课程
382
+func (m *CourseDAO) GetCourseOfCustomer(custID, courseID string) (*model.TaCustomerCourse, error) {
383
+	course := new(model.TaCustomerCourse)
384
+
385
+	if _, err := m.db.Where("course_id=?", courseID).And("customer_id=?", custID).Get(course); err != nil {
386
+		return nil, err
387
+	}
388
+
389
+	return course, nil
390
+}
391
+
392
+// SaveCourseOfCustomer 保存我的课程
393
+func (m *CourseDAO) SaveCourseOfCustomer(course *model.TaCustomerCourse, details []model.TaCustomerCourseDetail) error {
394
+	if course == nil || details == nil || len(details) == 0 {
395
+		return errors.New("没有有效的我的课程信息")
396
+	}
397
+
398
+	course.CustomerCourseId = guid.NewGUIDString()
399
+	course.Status = models.STATUS_NORMAL
400
+
401
+	for i, d := range details {
402
+		d.CustomerDetailId = guid.NewGUIDString()
403
+		d.CustomerCourseId = course.CustomerCourseId
404
+		d.Status = models.STATUS_NORMAL
405
+
406
+		details[i] = d
407
+	}
408
+
409
+	if _, err := m.db.Insert(&details); err != nil {
410
+		return err
411
+	}
412
+
413
+	if _, err := m.db.Insert(course); err != nil {
414
+		return err
415
+	}
416
+
417
+	return nil
418
+}

+ 15
- 13
models/course/order.go View File

@@ -1,22 +1,22 @@
1 1
 package course
2 2
 
3 3
 import (
4
-	"spaceofcheng/services/models"
5 4
 	"spaceofcheng/services/models/model"
6 5
 	"spaceofcheng/services/utils"
7
-	"strconv"
8 6
 	"strings"
9 7
 	"time"
8
+
9
+	"github.com/yl10/kit/guid"
10 10
 )
11 11
 
12 12
 // SaveCourseOrder 保存订单
13
-func (m *CourseDAO) SaveCourseOrder(order model.TaCourseOrders) (*model.TaCourseOrders, error) {
14
-	order.OrdersId = utils.GetGUID()
13
+func (m *CourseDAO) SaveCourseOrder(order *model.TaCourseOrders) error {
14
+	order.OrdersId = guid.NewGUIDString()
15 15
 	// 当前订单号的随机方式 = 时间 + 个人ID
16
-	order.OrdersNo = "Customer-" + time.Now().Local().Format("20060102150405") + "-" + strings.Join(utils.GUIID2IntString(order.CustomerId), "")
16
+	order.OrdersNo = "C-" + time.Now().Local().Format("20060102150405") + "-" + strings.Join(utils.GUIID2IntString(order.CustomerId), "")
17 17
 	order.CreateDate = time.Now()
18
-	_, err := m.db.Insert(&order)
19
-	return &order, err
18
+	_, err := m.db.Insert(order)
19
+	return err
20 20
 }
21 21
 
22 22
 // GetOrderByID 根据id获取订单明细
@@ -33,11 +33,13 @@ func (m *CourseDAO) GetOrderByID(ordersid string) (*model.TaCourseOrders, error)
33 33
 }
34 34
 
35 35
 // SaveCourseOrderDetail 保存订单明细
36
-func (m *CourseDAO) SaveCourseOrderDetail(courseid, ordersid string) error {
37
-	sql := `insert into (orders_detail_id,orders_id,course_id,course_detail_id,begin_date,end_date) select UUID(),'` + ordersid +
38
-		`',course_id,detail_id,begin_date,end_date from ta_course_detail where course_id='` + courseid + `' and status>` + strconv.Itoa(models.STATUS_DEL)
39
-	_, err := m.db.Insert(sql)
36
+func (m *CourseDAO) SaveCourseOrderDetail(details []model.TaCourseOrdersDetail) error {
37
+	for i, d := range details {
38
+		d.OrdersDetailId = guid.NewGUIDString()
39
+
40
+		details[i] = d
41
+	}
42
+
43
+	_, err := m.db.Insert(details)
40 44
 	return err
41 45
 }
42
-
43
-//

+ 1
- 0
models/course/types.go View File

@@ -0,0 +1 @@
1
+package course

+ 1
- 1
models/customer/account.go View File

@@ -57,7 +57,7 @@ func (m *CustomerDAO) InsertAccountRecords(rec *model.TaAccountChange) error {
57 57
 	amount, _ := strconv.ParseFloat(rec.Amount, 64)
58 58
 	if rec.ChangeType == models.CONSUME_POINTS {
59 59
 		points = amount
60
-	} else if rec.ChangeType == models.CONSUME_MONEYCHG {
60
+	} else if rec.ChangeType == models.CONSUME_COINCHG {
61 61
 		moenyCheng = amount
62 62
 	}
63 63
 

+ 1
- 1
models/goods/orders.go View File

@@ -41,7 +41,7 @@ func (m *GoodsDAO) SaveOrders(order *model.TaGoodsOrders) error {
41 41
 	order.CouponAmount = "0"
42 42
 
43 43
 	// 当前订单号的随机方式 = 时间 + 个人ID
44
-	order.OrdersNo = time.Now().Local().Format("20060102150405") + "-" + strings.Join(utils.GUIID2IntString(order.UserId), "")
44
+	order.OrdersNo = "G-" + time.Now().Local().Format("20060102150405") + "-" + strings.Join(utils.GUIID2IntString(order.UserId), "")
45 45
 
46 46
 	if _, err := m.db.Insert(order); err != nil {
47 47
 		utils.LogError("保存订单主记录失败: " + err.Error())

+ 1
- 1
models/model/ta_course.go View File

@@ -7,7 +7,7 @@ import (
7 7
 type TaCourse struct {
8 8
 	CourseId    string    `xorm:"not null pk VARCHAR(64)"`
9 9
 	CourseName  string    `xorm:"VARCHAR(50)"`
10
-	Price       float32   `xorm:"FLOAT(8,2)"`
10
+	Price       string    `xorm:"DECIMAL(8,2)"`
11 11
 	OrgId       string    `xorm:"VARCHAR(64)"`
12 12
 	CaseId      string    `xorm:"VARCHAR(64)"`
13 13
 	LocationId  string    `xorm:"VARCHAR(64)"`

+ 1
- 0
models/model/ta_course_orders.go View File

@@ -8,6 +8,7 @@ type TaCourseOrders struct {
8 8
 	OrdersId     string    `xorm:"not null pk VARCHAR(64)"`
9 9
 	OrdersNo     string    `xorm:"not null VARCHAR(32)"`
10 10
 	CustomerId   string    `xorm:"VARCHAR(64)"`
11
+	CustomerName string    `xorm:"VARCHAR(200)"`
11 12
 	OrgId        string    `xorm:"VARCHAR(64)"`
12 13
 	CaseId       string    `xorm:"VARCHAR(64)"`
13 14
 	CourseId     string    `xorm:"VARCHAR(64)"`

+ 1
- 0
models/model/ta_course_orders_coupon.go View File

@@ -7,6 +7,7 @@ import (
7 7
 type TaCourseOrdersCoupon struct {
8 8
 	OrdersCouponId string    `xorm:"not null pk VARCHAR(64)"`
9 9
 	OrdersId       string    `xorm:"VARCHAR(64)"`
10
+	CouponType     string    `xorm:"VARCHAR(20)"`
10 11
 	CouponId       string    `xorm:"VARCHAR(64)"`
11 12
 	CouponName     string    `xorm:"VARCHAR(50)"`
12 13
 	UsedAmount     string    `xorm:"DECIMAL(8,2)"`

+ 2
- 2
models/model/ta_customer.go View File

@@ -7,8 +7,8 @@ import (
7 7
 type TaCustomer struct {
8 8
 	CustomerId    string    `xorm:"not null pk VARCHAR(64)"`
9 9
 	OrgId         string    `xorm:"VARCHAR(64)"`
10
-	CustomerName  string    `xorm:"VARCHAR(50)"`
11
-	Name          string    `xorm:"VARCHAR(50)"`
10
+	CustomerName  string    `xorm:"VARCHAR(200)"`
11
+	Name          string    `xorm:"VARCHAR(200)"`
12 12
 	Phone         string    `xorm:"VARCHAR(100)"`
13 13
 	Sex           int       `xorm:"SMALLINT(6)"`
14 14
 	Headimgurl    string    `xorm:"TEXT"`

+ 1
- 1
models/model/ta_customer_account.go View File

@@ -7,7 +7,7 @@ import (
7 7
 type TaCustomerAccount struct {
8 8
 	AccountId    string    `xorm:"not null pk VARCHAR(64)"`
9 9
 	CustomerId   string    `xorm:"VARCHAR(64)"`
10
-	CustomerName string    `xorm:"VARCHAR(50)"`
10
+	CustomerName string    `xorm:"VARCHAR(200)"`
11 11
 	Amount       string    `xorm:"DECIMAL(8,2)"`
12 12
 	Points       string    `xorm:"DECIMAL(8,2)"`
13 13
 	Status       int       `xorm:"SMALLINT(6)"`

+ 1
- 6
models/model/ta_customer_course.go View File

@@ -11,15 +11,10 @@ type TaCustomerCourse struct {
11 11
 	OrgId            string    `xorm:"VARCHAR(64)"`
12 12
 	CaseId           string    `xorm:"VARCHAR(64)"`
13 13
 	CourseName       string    `xorm:"VARCHAR(50)"`
14
-	LocationId       string    `xorm:"VARCHAR(64)"`
15
-	LocationName     string    `xorm:"VARCHAR(64)"`
16
-	Price            float32   `xorm:"FLOAT(8,2)"`
14
+	Price            string    `xorm:"DECIMAL(8,2)"`
17 15
 	CourseNum        int       `xorm:"INT(11)"`
18 16
 	JoinNum          int       `xorm:"INT(11)"`
19 17
 	CreateDate       time.Time `xorm:"DATETIME"`
20
-	Address          string    `xorm:"VARCHAR(200)"`
21
-	CourseDate       string    `xorm:"VARCHAR(200)"`
22
-	Remark           string    `xorm:"TEXT"`
23 18
 	CourseObtaimType string    `xorm:"VARCHAR(32)"`
24 19
 	SourceId         string    `xorm:"VARCHAR(64)"`
25 20
 	Status           int       `xorm:"SMALLINT(6)"`

+ 1
- 1
models/model/ta_goods_orders.go View File

@@ -20,7 +20,7 @@ type TaGoodsOrders struct {
20 20
             coupon 优惠券抵用') VARCHAR(20)"`
21 21
 	UserType     string `xorm:"VARCHAR(20)"`
22 22
 	UserId       string `xorm:"VARCHAR(64)"`
23
-	UserName     string `xorm:"VARCHAR(50)"`
23
+	UserName     string `xorm:"VARCHAR(200)"`
24 24
 	OrdersNum    int    `xorm:"INT(11)"`
25 25
 	Remark       string `xorm:"TEXT"`
26 26
 	IsPay        int    `xorm:"TINYINT(1)"`

+ 7
- 1
service/course/course.go View File

@@ -4,7 +4,9 @@ import (
4 4
 	"spaceofcheng/services/models"
5 5
 	"spaceofcheng/services/models/cases"
6 6
 	"spaceofcheng/services/models/course"
7
+	"spaceofcheng/services/models/customer"
7 8
 	"spaceofcheng/services/models/model"
9
+	"spaceofcheng/services/models/system"
8 10
 	"spaceofcheng/services/service"
9 11
 	"spaceofcheng/services/utils"
10 12
 	"strings"
@@ -17,6 +19,8 @@ type CourseServ struct {
17 19
 	ctx     *utils.Context
18 20
 	dao     *course.CourseDAO
19 21
 	casedao *cases.CaseDAO
22
+	custDAO *customer.CustomerDAO
23
+	userDAO *system.UserDAO
20 24
 }
21 25
 
22 26
 // NewCourseServ 初始化
@@ -25,6 +29,8 @@ func NewCourseServ(ctx *utils.Context) *CourseServ {
25 29
 		ctx:     ctx,
26 30
 		dao:     course.NewCourseDAO(ctx),
27 31
 		casedao: cases.NewCaseDAO(ctx),
32
+		custDAO: customer.NewCustomerDAO(ctx),
33
+		userDAO: system.NewUserDAO(ctx),
28 34
 	}
29 35
 }
30 36
 
@@ -110,7 +116,7 @@ func (s *CourseServ) SaveCourse(course model.TaCourse, tagids string) (*model.Ta
110 116
 	if course.CaseId == "" {
111 117
 		return nil, utils.LogError("课程案场不允许为空!")
112 118
 	}
113
-	if course.Price == 0 {
119
+	if course.Price == "" || course.Price == "0" {
114 120
 		return nil, utils.LogError("课程价格不允许为0!")
115 121
 	}
116 122
 	if course.MinNum == 0 {

+ 283
- 2
service/course/order.go View File

@@ -1,12 +1,293 @@
1 1
 package course
2 2
 
3 3
 import (
4
+	"errors"
5
+	"spaceofcheng/services/models"
6
+	"spaceofcheng/services/models/course"
4 7
 	"spaceofcheng/services/models/model"
8
+	"spaceofcheng/services/utils"
9
+	"strconv"
10
+	"time"
11
+
12
+	"github.com/yl10/kit/guid"
5 13
 )
6 14
 
7
-func (s *CourseServ) Order(
8
-	couse *model.TaCourseOrders,
15
+// Orders 下单
16
+func (s *CourseServ) Orders(
17
+	couseOrder *model.TaCourseOrders,
9 18
 	coupons []model.TaCourseOrdersCoupon,
10 19
 ) error {
20
+	org := s.ctx.Get("org").(model.SysOrg)
21
+	cust := s.ctx.Get("customer").(model.TaCustomer)
22
+
23
+	// 校验人员
24
+	if cust.CustomerId != couseOrder.CustomerId {
25
+		return errors.New("下单人非当前人员")
26
+	}
27
+	if cust.UserId != "" {
28
+		// 内部人员
29
+		// 如果是内部人员购买或者兑换课程, 卡券或者城币就从内部人员自己的账户走
30
+		couseOrder.PayType = models.CONSUME_INNER
31
+	} else {
32
+		// 默认是卡券支付
33
+		couseOrder.PayType = models.CONSUME_COUPON
34
+	}
35
+
36
+	// 校验案场
37
+	caseID := couseOrder.CaseId
38
+	if err := utils.NewAuthEngine(s.ctx).CheckCase(caseID); err != nil {
39
+		return err
40
+	}
41
+
42
+	// 校验课程
43
+	course, err := s.validCourse(couseOrder)
44
+	if err != nil {
45
+		return err
46
+	}
47
+	if course.CourseDetail == nil || len(course.CourseDetail) > 0 {
48
+		return errors.New("定单课程尚未排课, 不能下单")
49
+	}
50
+
51
+	if coupons != nil && len(coupons) > 0 {
52
+		// 校验卡券, 同时进行卡券的注销或者已使用的更新操作
53
+		// TODO
54
+	}
55
+
56
+	// 课程价格, 即为订单价格
57
+	couseOrder.Price = course.Price
58
+	couseOrder.ActualAmount = course.Price
59
+	couseOrder.CourseName = course.CourseName
60
+	couseOrder.CourseNum = course.CourseNum
61
+	couseOrder.OrgId = org.OrgId
62
+	if cust.Name != "" {
63
+		couseOrder.CustomerName = cust.Name
64
+	} else {
65
+		couseOrder.CustomerName = cust.CustomerName
66
+	}
67
+
68
+	// 校验金额
69
+	if err := s.validBillCharges(couseOrder, coupons); err != nil {
70
+		return err
71
+	}
72
+
73
+	// 入库
74
+	if err := s.dao.SaveCourseOrder(couseOrder); err != nil {
75
+		utils.LogError("课程下单失败: " + err.Error())
76
+		return errors.New("下单失败, 请重试")
77
+	}
78
+
79
+	// 实际支付
80
+	actualAmount, _ := strconv.ParseFloat(couseOrder.ActualAmount, 64)
81
+
82
+	// 用户账户
83
+	if actualAmount > 0 {
84
+		couseOrder.PayType = models.CONSUME_COINCHG
85
+		account, err := s.custDAO.GetAccountByCust(cust.CustomerId)
86
+		if err != nil {
87
+			utils.LogError("查询用户账户信息出错: " + err.Error())
88
+			return errors.New("查询用户账户信息出错")
89
+		}
90
+		accMoney, _ := strconv.ParseFloat(account.Amount, 64)
91
+
92
+		if actualAmount > accMoney {
93
+			return errors.New("账户余额不足")
94
+		}
95
+	}
96
+
97
+	return nil
98
+}
99
+
100
+// validCourse 校验课程
101
+func (s *CourseServ) validCourse(order *model.TaCourseOrders) (*course.CourseDetail, error) {
102
+	if order.CourseId == "" {
103
+		return nil, errors.New("没有获取到下单课程")
104
+	}
105
+
106
+	custCourse, err := s.dao.GetCourseOfCustomer(order.CustomerId, order.CourseId)
107
+	if err != nil {
108
+		utils.LogError("校验用户课程失败: " + err.Error())
109
+		return nil, errors.New("校验用户课程失败")
110
+	}
111
+
112
+	if custCourse != nil && custCourse.CustomerCourseId != "" {
113
+		if custCourse.Status != models.STATUS_DEL {
114
+			return nil, errors.New("用户已经拥有该课程, 不需要再次下单")
115
+		}
116
+	}
117
+
118
+	// 课程
119
+	course, err := s.dao.GetCourseInfo(order.CourseId)
120
+	if err != nil {
121
+		utils.LogError("获取课程信息失败: " + err.Error())
122
+		return nil, errors.New("获取课程信息失败")
123
+	}
124
+
125
+	if course.Status != models.STATUS_NORMAL {
126
+		return nil, errors.New("课程状态不正确, 不能购买或者兑换")
127
+	}
128
+
129
+	return course, nil
130
+}
131
+
132
+// validCustomer 校验客户
133
+func (s *CourseServ) validCustomer(custID string) (*model.TaCustomer, error) {
134
+	if custID == "" {
135
+		return nil, errors.New("没有获取到下单人员")
136
+	}
137
+
138
+	cust, err := s.custDAO.GetCustomerByID(custID)
139
+	if err != nil {
140
+		utils.LogError("查询下单人出错: " + err.Error())
141
+		return nil, errors.New("验证下单人出错")
142
+	}
143
+
144
+	if cust.Status != models.STATUS_NORMAL {
145
+		return nil, errors.New("下单人状态不正确, 不能下单")
146
+	}
147
+
148
+	return cust, nil
149
+}
150
+
151
+// validBillCharges 校验各种金额, 数量等
152
+func (s *CourseServ) validBillCharges(
153
+	couseOrder *model.TaCourseOrders,
154
+	coupons []model.TaCourseOrdersCoupon,
155
+) error {
156
+	orderCost, _ := strconv.ParseFloat(couseOrder.Price, 64)
157
+	// 算法开始, 实际支付 == 订单金额
158
+	actualAmount := orderCost
159
+	// 优惠金额是 0
160
+	couponAmount := float64(0.0)
161
+
162
+	couseOrder.ActualAmount = strconv.FormatFloat(actualAmount, 'f', -1, 64)
163
+	couseOrder.CouponAmount = strconv.FormatFloat(couponAmount, 'f', -1, 64)
164
+
165
+	// 如果不是使用的卡券
166
+	if coupons == nil || len(coupons) == 0 {
167
+		return nil
168
+	}
169
+
170
+	// 目前的业务是, 购买课程只能使用一张卡券
171
+	// 这里使用的是数组, 相当于做一个拓展
172
+	for _, coupon := range coupons {
173
+		// TODO 从卡券字典中获取价格
174
+		// 目前是前端传过来的
175
+		dedCharge, err := strconv.ParseFloat(coupon.UsedAmount, 64)
176
+		if err != nil {
177
+			utils.LogError("获取卡券抵用金额失败: " + err.Error())
178
+			return errors.New("获取卡券抵用金额错误")
179
+		}
180
+
181
+		if dedCharge < 0 {
182
+			return errors.New("卡券抵用金额不能为负值")
183
+		}
184
+
185
+		couponAmount += dedCharge
186
+	}
187
+
188
+	// 如果优惠券金额 > 订单金额
189
+	if couponAmount > orderCost {
190
+		couponAmount = orderCost
191
+		actualAmount = float64(0)
192
+	} else {
193
+		actualAmount = orderCost - couponAmount
194
+	}
195
+
196
+	couseOrder.ActualAmount = strconv.FormatFloat(actualAmount, 'f', -1, 64)
197
+	couseOrder.CouponAmount = strconv.FormatFloat(couponAmount, 'f', -1, 64)
198
+	return nil
199
+}
200
+
201
+// saveCustomerPayRec 保存账户消费流水
202
+func (s *CourseServ) saveCustomerPayRec(account *model.TaCustomerAccount, info *model.TaCourseOrders) error {
203
+	accBill := new(model.TaAccountChange)
204
+	accBill.AccountId = account.AccountId
205
+	accBill.Amount = info.ActualAmount
206
+	accBill.CaseId = info.CaseId
207
+	accBill.ChangeSource = models.ACCSOURCE_COURSE_ORDERS
208
+	accBill.ChangeType = models.CONSUME_COINCHG
209
+	accBill.CustomerId = info.CustomerId
210
+	accBill.CustomerName = info.CustomerName
211
+	accBill.FloatType = models.ACCOUNT_SPENT
212
+	accBill.SourceId = info.OrdersId
213
+	accBill.SourceName = ""
214
+
215
+	return s.custDAO.InsertAccountRecords(accBill)
216
+}
217
+
218
+// SaveOrder 保存订单明细
219
+func (s *CourseServ) SaveOrder(order *model.TaCourseOrders, coupons []model.TaCourseOrdersCoupon, course *course.CourseDetail) error {
220
+	// 订单信息
221
+	if err := s.dao.SaveCourseOrder(order); err != nil {
222
+		utils.LogError("课程下单失败: " + err.Error())
223
+		return errors.New("下单失败, 请重试")
224
+	}
225
+
226
+	// 默认城币购买
227
+	courseObtaimType := models.COURSE_GETBY_COINCHG
228
+	srcID := ""
229
+	if coupons != nil && len(coupons) > 0 {
230
+		for _, c := range coupons {
231
+			if c.CouponType == models.COURSE_COUPON_CARD {
232
+				courseObtaimType = models.COURSE_GETBY_CARD
233
+			} else if c.CouponType == models.COURSE_COUPON_COUPON {
234
+				courseObtaimType = models.COURSE_GETBY_COUPON
235
+			}
236
+
237
+			srcID = c.CouponId
238
+		}
239
+	}
240
+
241
+	// 我的课程信息
242
+	custCourse := model.TaCustomerCourse{
243
+		CourseId:         order.CourseId,
244
+		CustomerId:       order.CustomerId,
245
+		OrgId:            order.OrgId,
246
+		CaseId:           order.CaseId,
247
+		CourseName:       course.CourseName,
248
+		Price:            course.Price,
249
+		CourseNum:        course.CourseNum,
250
+		JoinNum:          0,
251
+		CreateDate:       time.Now().Local(),
252
+		CourseObtaimType: courseObtaimType,
253
+		SourceId:         srcID,
254
+		IsDone:           models.BOOL_FALSE,
255
+	}
256
+
257
+	details := make([]model.TaCourseOrdersDetail, 0)
258
+	custCourseDetails := make([]model.TaCustomerCourseDetail, 0)
259
+
260
+	for _, subCourse := range course.CourseDetail {
261
+		d1 := model.TaCourseOrdersDetail{
262
+			OrdersDetailId: guid.NewGUIDString(),
263
+			OrdersId:       order.OrdersId,
264
+			CourseId:       course.CourseId,
265
+			CourseDetailId: subCourse.DetailId,
266
+			BeginDate:      subCourse.BeginDate,
267
+			EndDate:        subCourse.EndDate,
268
+		}
269
+		details = append(details, d1)
270
+
271
+		d2 := model.TaCustomerCourseDetail{
272
+			CourseId:     course.CourseId,
273
+			DetailId:     subCourse.DetailId,
274
+			StartDate:    subCourse.BeginDate,
275
+			EndDate:      subCourse.EndDate,
276
+			VerifyStatus: models.VERIFY_USEABLE,
277
+			CaseId:       order.CaseId,
278
+		}
279
+		custCourseDetails = append(custCourseDetails, d2)
280
+	}
281
+
282
+	if err := s.dao.SaveCourseOfCustomer(&custCourse, custCourseDetails); err != nil {
283
+		utils.LogError("插入我的课程失败: " + err.Error())
284
+		return errors.New("写入我的课程失败")
285
+	}
286
+
287
+	if err := s.dao.SaveCourseOrderDetail(details); err != nil {
288
+		utils.LogError("插入课程订单详情: " + err.Error())
289
+		return errors.New("写入课程订单详情失败")
290
+	}
291
+
11 292
 	return nil
12 293
 }

+ 22
- 6
service/goods/orders.go View File

@@ -2,6 +2,7 @@ package goods
2 2
 
3 3
 import (
4 4
 	"errors"
5
+	"math"
5 6
 	"spaceofcheng/services/models"
6 7
 	"spaceofcheng/services/models/goods"
7 8
 	"spaceofcheng/services/models/model"
@@ -36,6 +37,16 @@ func (s *GoodsServ) Orders(
36 37
 		return err
37 38
 	}
38 39
 
40
+	// 内部人员, 可以直接购买
41
+	if info.PayType == models.CONSUME_INNER {
42
+
43
+		// TODO
44
+		// 通知后端有新订单 - websocket
45
+		// utils.SendMessage()
46
+
47
+		return nil
48
+	}
49
+
39 50
 	account, err := s.custDAO.GetAccountByCust(info.UserId)
40 51
 	if err != nil {
41 52
 		utils.LogError("查询用户账户信息出错: " + err.Error())
@@ -44,10 +55,12 @@ func (s *GoodsServ) Orders(
44 55
 
45 56
 	// 如果是使用优惠券
46 57
 	if coupons != nil && len(coupons) > 0 {
58
+		info.PayType = models.CONSUME_COUPON
47 59
 		// TODO
48 60
 		// 校验优惠券相关
49 61
 	} else {
50 62
 		// 如果是使用城币
63
+		info.PayType = models.CONSUME_COINCHG
51 64
 		accMoney, _ := strconv.ParseFloat(account.Amount, 64)
52 65
 		payMoney, _ := strconv.ParseFloat(info.ActualAmount, 64)
53 66
 
@@ -68,7 +81,7 @@ func (s *GoodsServ) Orders(
68 81
 	}
69 82
 
70 83
 	// 如果是城币, 则插入用户账户消费记录
71
-	if info.PayType == models.CONSUME_MONEYCHG {
84
+	if info.PayType == models.CONSUME_COINCHG {
72 85
 		if err := s.saveCustomerPayRec(account, info); err != nil {
73 86
 			return err
74 87
 		}
@@ -87,8 +100,8 @@ func (s *GoodsServ) saveCustomerPayRec(account *model.TaCustomerAccount, info *m
87 100
 	accBill.AccountId = account.AccountId
88 101
 	accBill.Amount = info.ActualAmount
89 102
 	accBill.CaseId = info.CaseId
90
-	accBill.ChangeSource = models.ACCSOURCE_ORDERS
91
-	accBill.ChangeType = models.CONSUME_MONEYCHG
103
+	accBill.ChangeSource = models.ACCSOURCE_GOODS_ORDERS
104
+	accBill.ChangeType = models.CONSUME_COINCHG
92 105
 	accBill.CustomerId = info.UserId
93 106
 	accBill.CustomerName = info.UserName
94 107
 	accBill.FloatType = models.ACCOUNT_SPENT
@@ -154,6 +167,9 @@ func (s *GoodsServ) validOrdersInfo(info *model.TaGoodsOrders) error {
154 167
 
155 168
 	// 如果是管理人员
156 169
 	if cust.UserId != "" {
170
+		// 内部人员
171
+		info.PayType = models.CONSUME_INNER
172
+
157 173
 		// 查找用户类型
158 174
 		userWithTypes, err := s.userDAO.GetUserWithTypeByID(cust.UserId)
159 175
 		if err != nil {
@@ -205,7 +221,7 @@ func (s *GoodsServ) validBillCharges(info *model.TaGoodsOrders, details []model.
205 221
 
206 222
 	caseID := info.CaseId
207 223
 
208
-	orgAmount := float64(0.0)
224
+	goodsAmount := float64(0.0)
209 225
 	for _, item := range details {
210 226
 		goods, err := s.dao.GetGoodsWithPriceByID(item.GoodsId, item.SpecId, caseID)
211 227
 		if err != nil {
@@ -221,13 +237,13 @@ func (s *GoodsServ) validBillCharges(info *model.TaGoodsOrders, details []model.
221 237
 		// 商品暂时无库存
222 238
 
223 239
 		price, _ := strconv.ParseFloat(item.Price, 64)
224
-		orgAmount += price * float64(item.Number)
240
+		goodsAmount += price * float64(item.Number)
225 241
 	}
226 242
 
227 243
 	// 校验总金额
228 244
 	orderAmount, _ := strconv.ParseFloat(info.Amount, 64)
229 245
 
230
-	if orgAmount != orderAmount {
246
+	if math.Abs(orderAmount-goodsAmount) > 0.00001 {
231 247
 		return errors.New("商品金额计算错误")
232 248
 	}
233 249
 

+ 2
- 4
utils/sms.go View File

@@ -47,17 +47,15 @@ func SendSMS(smsType, tel string, messages ...string) {
47 47
 		}
48 48
 
49 49
 		// 接口内容
50
-		paramsTpl := `{"code": "%s", "tel": "%s", "params": [%s]}`
50
+		messageTpl := defaultSMS.setting.String(smsType + "::message")
51 51
 		// 发送消息
52 52
 		message := strings.Join(messages, `","`)
53 53
 		if message != "" {
54 54
 			message = fmt.Sprintf(`"%s"`, message)
55 55
 		}
56 56
 
57
-		code := defaultSMS.setting.String(smsType + "::code")
58
-
59 57
 		// 接口 data
60
-		data := bytes.NewBuffer([]byte(fmt.Sprintf(paramsTpl, code, tel, message)))
58
+		data := bytes.NewBuffer([]byte(fmt.Sprintf(messageTpl, tel, message)))
61 59
 
62 60
 		// 暂时只支持 post 发送数据
63 61
 		if defaultSMS.method == http.MethodPost {