zjxpcyc 6 gadus atpakaļ
vecāks
revīzija
366402f5ae

+ 2
- 2
conf/db.conf Parādīt failu

@@ -11,10 +11,10 @@ db_addr      = localhost
11 11
 db_port      = 3306
12 12
 
13 13
 ; 用户名
14
-username     = h5-template
14
+username     = spaceofcheng
15 15
 
16 16
 ; 密码
17
-password     = h5-template
17
+password     = spaceofcheng
18 18
 
19 19
 ; 数据库名或者schema
20 20
 database     = spaceofcheng

+ 2
- 0
log/common.log Parādīt failu

@@ -18,3 +18,5 @@
18 18
 2018/08/21 13:39:24 [E] 您没有该案场的权限!
19 19
 2018/08/21 13:41:04 [E] 用户没有设置默认案场
20 20
 2018/08/21 13:41:04 [E] 您没有该案场的权限!
21
+2018/08/21 14:26:22 [E] 用户登录失败: dial tcp 192.168.0.62:3306: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
22
+2018/08/21 14:27:06 [E] 用户登录失败: dial tcp 192.168.0.62:3306: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

+ 35
- 0
models/constant.go Parādīt failu

@@ -34,3 +34,38 @@ const (
34 34
 	USERMAP_USER     = "user"
35 35
 	USERMAP_CUSTOMER = "customer"
36 36
 )
37
+
38
+// 人员禁止内容
39
+const (
40
+	// 禁止下单
41
+	FORBID_ORDER = "order"
42
+
43
+	// 禁止发卡, 发券
44
+	FORBID_COUPON = "coupon"
45
+)
46
+
47
+// 消费类型
48
+const (
49
+	CONSUME_POINTS   = "points"
50
+	CONSUME_MONEYCHG = "money-of-cheng"
51
+)
52
+
53
+// 消费方式
54
+const (
55
+	ACCOUNT_INCOME = "+"
56
+	ACCOUNT_SPENT  = "-"
57
+)
58
+
59
+// 账户消费来源
60
+const (
61
+	// 充值
62
+	ACCSOURCE_RECHARGE = "recharge"
63
+	// 订单
64
+	ACCSOURCE_ORDERS = "orders"
65
+	// 注册
66
+	ACCSOURCE_REGISTER = "register"
67
+	// 完善资料
68
+	ACCSOURCE_COMPLETE_PROFILE = "complete_profile"
69
+	// 推荐
70
+	ACCSOURCE_RECOMMEND = "recommend"
71
+)

+ 123
- 0
models/customer/account.go Parādīt failu

@@ -0,0 +1,123 @@
1
+package customer
2
+
3
+import (
4
+	"errors"
5
+	"spaceofcheng/services/models"
6
+	"spaceofcheng/services/models/model"
7
+	"spaceofcheng/services/utils"
8
+	"strconv"
9
+	"time"
10
+
11
+	"github.com/yl10/kit/guid"
12
+)
13
+
14
+// SaveAccount 保存账户信息
15
+func (m *CustomerDAO) SaveAccount(account *model.TaCustomerAccount) error {
16
+	if account.CustomerId == "" {
17
+		return errors.New("账户未关联人员")
18
+	}
19
+
20
+	account.Status = models.STATUS_NORMAL
21
+	account.CreateDate = time.Now().Local()
22
+	account.AccountId = guid.NewGUIDString()
23
+
24
+	_, err := m.db.Insert(account)
25
+	return err
26
+}
27
+
28
+// InsertAccountRecords 新增账户消费记录
29
+func (m *CustomerDAO) InsertAccountRecords(rec *model.TaAccountChange) error {
30
+	if rec.AccountId == "" {
31
+		return errors.New("无法确定消费账户")
32
+	}
33
+
34
+	if rec.CaseId == "" {
35
+		return errors.New("无法确定消费案场")
36
+	}
37
+
38
+	acc, err := m.GetAccountByID(rec.AccountId)
39
+	if err != nil {
40
+		utils.LogError("获取人员账户失败: " + err.Error())
41
+		return errors.New("找不到账户消费记录")
42
+	}
43
+
44
+	if acc.Status != models.STATUS_NORMAL {
45
+		return errors.New("人员账户状态不正常")
46
+	}
47
+
48
+	// 补充部分字段
49
+	rec.DetailId = guid.NewGUIDString()
50
+	rec.CreateDate = time.Now().Local()
51
+	rec.Status = models.STATUS_NORMAL
52
+	rec.OrgId = acc.OrgId
53
+
54
+	// 消费内容
55
+	points := float64(0.0)
56
+	moenyCheng := float64(0.0)
57
+	amount, _ := strconv.ParseFloat(rec.Amount, 64)
58
+	if rec.ChangeType == models.CONSUME_POINTS {
59
+		points = amount
60
+	} else if rec.ChangeType == models.CONSUME_MONEYCHG {
61
+		moenyCheng = amount
62
+	}
63
+
64
+	// 反向更新账户表
65
+	accAmount, _ := strconv.ParseFloat(acc.Amount, 64)
66
+	accPoints, _ := strconv.ParseFloat(acc.Points, 64)
67
+	accPayedAmount, _ := strconv.ParseFloat(acc.PayedAmount, 64)
68
+	accPayedPoints, _ := strconv.ParseFloat(acc.PayedPoints, 64)
69
+
70
+	if rec.FloatType == models.ACCOUNT_SPENT {
71
+		accPayedAmount += moenyCheng
72
+		accPayedPoints += points
73
+
74
+		accAmount -= moenyCheng
75
+		accPoints -= points
76
+	}
77
+
78
+	// 账户不能为负
79
+	if accAmount < 0 || accPoints < 0 {
80
+		return errors.New("账户不足, 不能消费")
81
+	}
82
+
83
+	acc.Amount = strconv.FormatFloat(accAmount, 'f', -1, 64)
84
+	acc.Points = strconv.FormatFloat(accPoints, 'f', -1, 64)
85
+	acc.PayedAmount = strconv.FormatFloat(accPayedAmount, 'f', -1, 64)
86
+	acc.PayedPoints = strconv.FormatFloat(accPayedPoints, 'f', -1, 64)
87
+
88
+	if _, err := m.db.Insert(rec); err != nil {
89
+		utils.LogError("插入消费记录失败: " + err.Error())
90
+		return errors.New("记录消费记录失败")
91
+	}
92
+
93
+	if _, err := m.db.Where("account_id=?", acc.AccountId).
94
+		Cols([]string{"amount", "points", "payed_amount", "payed_points"}...).
95
+		Update(acc); err != nil {
96
+		utils.LogError("更新账户信息失败: " + err.Error())
97
+		return errors.New("更新消费信息失败")
98
+	}
99
+
100
+	return nil
101
+}
102
+
103
+// GetAccountByID 获取账户信息
104
+func (m *CustomerDAO) GetAccountByID(id string) (*model.TaCustomerAccount, error) {
105
+	acc := new(model.TaCustomerAccount)
106
+
107
+	if _, err := m.db.Where("account_id=?", id).Get(acc); err != nil {
108
+		return nil, err
109
+	}
110
+
111
+	return acc, nil
112
+}
113
+
114
+// GetAccountByCust 获取账户信息
115
+func (m *CustomerDAO) GetAccountByCust(id string) (*model.TaCustomerAccount, error) {
116
+	acc := new(model.TaCustomerAccount)
117
+
118
+	if _, err := m.db.Where("customer_id=?", id).Get(acc); err != nil {
119
+		return nil, err
120
+	}
121
+
122
+	return acc, nil
123
+}

+ 22
- 0
models/customer/customer.go Parādīt failu

@@ -201,3 +201,25 @@ func (m *CustomerDAO) SaveCustomer(cust *model.TaCustomer) error {
201 201
 	_, err := m.db.Insert(cust)
202 202
 	return err
203 203
 }
204
+
205
+// GetCustomerByID 依据ID获取客户
206
+func (m *CustomerDAO) GetCustomerByID(id string) (*model.TaCustomer, error) {
207
+	cust := new(model.TaCustomer)
208
+
209
+	if _, err := m.db.Where("customer_id=?", id).Get(cust); err != nil {
210
+		return nil, err
211
+	}
212
+
213
+	return cust, nil
214
+}
215
+
216
+// GetForbidUserByUserID 查询禁用人员
217
+func (m *CustomerDAO) GetForbidUserByUserID(userID string) ([]model.TaForbidUser, error) {
218
+	var fbUsers []model.TaForbidUser
219
+
220
+	if err := m.db.Where("user_id=?", userID).Find(&fbUsers); err != nil {
221
+		return nil, err
222
+	}
223
+
224
+	return fbUsers, nil
225
+}

+ 22
- 0
models/goods/goods.go Parādīt failu

@@ -63,6 +63,28 @@ func (m *GoodsDAO) GetGoodsByID(id, caseID string) (*model.TaGoods, error) {
63 63
 	return goods, nil
64 64
 }
65 65
 
66
+// GetGoodsWithPriceByID 依据ID 获取商品
67
+func (m *GoodsDAO) GetGoodsWithPriceByID(id, spec, caseID string) (*GoodsWithPrice, error) {
68
+	goods := new(GoodsWithPrice)
69
+	query := `
70
+		SELECT *
71
+		FROM
72
+			ta_goods t
73
+		JOIN ta_goods_spec s ON t.goods_id = s.goods_id
74
+		WHERE
75
+			t.goods_id = ?
76
+		AND t.case_id = ?
77
+		AND s.spec_id = ?
78
+		AND t.status = ?
79
+	`
80
+
81
+	if _, err := m.db.SQL(query, id, caseID, spec, models.STATUS_NORMAL).Get(goods); err != nil {
82
+		return nil, err
83
+	}
84
+
85
+	return goods, nil
86
+}
87
+
66 88
 // UpdateGoods 更新商品
67 89
 func (m *GoodsDAO) UpdateGoods(goods *model.TaGoods, cols []string) error {
68 90
 	if _, err := m.db.Where("goods_id=?", goods.GoodsId).Cols(cols...).Update(goods); err != nil {

+ 64
- 0
models/goods/orders.go Parādīt failu

@@ -5,6 +5,7 @@ import (
5 5
 	"spaceofcheng/services/models"
6 6
 	"spaceofcheng/services/models/model"
7 7
 	"spaceofcheng/services/utils"
8
+	"strconv"
8 9
 	"strings"
9 10
 	"time"
10 11
 
@@ -31,10 +32,73 @@ func (m *GoodsDAO) SaveOrders(order *model.TaGoodsOrders) error {
31 32
 	order.OrgId = caseInfo.OrgId
32 33
 	order.CreateDate = time.Now().Local()
33 34
 	order.Status = models.STATUS_NORMAL
35
+	order.IsPay = models.BOOL_TRUE // 默认已支付
36
+	order.CouponAmount = "0"
34 37
 
35 38
 	// 当前订单号的随机方式 = 时间 + 个人ID
36 39
 	order.OrdersNo = time.Now().Local().Format("20060102150405") + "-" + strings.Join(utils.GUIID2IntString(order.UserId), "")
37 40
 
41
+	if _, err := m.db.Insert(order); err != nil {
42
+		utils.LogError("保存订单主记录失败: " + err.Error())
43
+
44
+		return errors.New("保存订单主记录失败")
45
+	}
46
+	return nil
47
+}
48
+
49
+// SaveOrdersDetail 保存订单明细
50
+func (m *GoodsDAO) SaveOrdersDetail(list []model.TaGoodsOrdersDetail, ordersID string) error {
51
+	for inx := range list {
52
+		list[inx].OrdersId = ordersID
53
+	}
54
+
55
+	if _, err := m.db.Insert(list); err != nil {
56
+		utils.LogError("保存订单明细失败: " + err.Error())
57
+
58
+		return errors.New("保存订单明细失败")
59
+	}
60
+
61
+	return nil
62
+}
63
+
64
+// SaveOrdersCoupon 保存订单优惠券
65
+func (m *GoodsDAO) SaveOrdersCoupon(coupons []model.TaGoodsOrdersCoupon, order *model.TaGoodsOrders) error {
66
+	if order.OrdersId == "" {
67
+		return errors.New("内部错误, 订单事务顺序出错")
68
+	}
69
+
70
+	var couponAmount float64 = 0
71
+
72
+	// 补充优惠券表字段
73
+	for inx := range coupons {
74
+		coupons[inx].OrdersCouponId = guid.NewGUIDString()
75
+		coupons[inx].OrdersId = order.OrdersId
76
+		coupons[inx].OrdersNo = order.OrdersNo
77
+		coupons[inx].OrgId = order.OrgId
78
+		coupons[inx].Status = models.STATUS_NORMAL
79
+		coupons[inx].CreateDate = time.Now().Local()
80
+
81
+		userAmount, _ := strconv.ParseFloat(coupons[inx].UsedAmount, 64)
82
+		couponAmount += userAmount
83
+	}
84
+
85
+	// 反向更新 订单主表
86
+	totalAmount, _ := strconv.ParseFloat(order.Amount, 64)
87
+
88
+	order.CouponAmount = strconv.FormatFloat(couponAmount, 'f', -1, 32)
89
+	order.ActualAmount = strconv.FormatFloat((totalAmount - couponAmount), 'f', -1, 32)
90
+
91
+	if _, err := m.db.Insert(&coupons); err != nil {
92
+		return err
93
+	}
94
+
95
+	if _, err := m.db.
96
+		Where("orders_id=?", order.OrdersId).
97
+		Cols([]string{"coupon_amount", "actual_amount"}...).
98
+		Update(&order); err != nil {
99
+		return err
100
+	}
101
+
38 102
 	return nil
39 103
 }
40 104
 

+ 6
- 0
models/goods/types.go Parādīt failu

@@ -15,3 +15,9 @@ type GoodsWithSpec struct {
15 15
 	model.TaGoods
16 16
 	Specs []SpecWithPrice
17 17
 }
18
+
19
+// GoodsWithPrice 含规格商品
20
+type GoodsWithPrice struct {
21
+	model.TaGoods     `xorm."extends"`
22
+	model.TaGoodsSpec `xorm."extends"`
23
+}

+ 3
- 3
models/model/ta_account_change.go Parādīt failu

@@ -7,15 +7,15 @@ import (
7 7
 type TaAccountChange struct {
8 8
 	DetailId     string    `xorm:"not null pk VARCHAR(64)"`
9 9
 	AccountId    string    `xorm:"VARCHAR(64)"`
10
-	UserId       string    `xorm:"VARCHAR(64)"`
11
-	UserName     string    `xorm:"VARCHAR(50)"`
10
+	CustomerId   string    `xorm:"VARCHAR(64)"`
11
+	CustomerName string    `xorm:"VARCHAR(50)"`
12 12
 	OrgId        string    `xorm:"VARCHAR(64)"`
13 13
 	CaseId       string    `xorm:"VARCHAR(64)"`
14 14
 	ChangeType   string    `xorm:"comment('integral 积分 amount 城币') VARCHAR(20)"`
15 15
 	ChangeSource string    `xorm:"comment('vip vip卡充值 orders 订单消费 register 注册 perfect 完善资料 recommend 推荐') VARCHAR(20)"`
16 16
 	SourceId     string    `xorm:"VARCHAR(64)"`
17 17
 	SourceName   string    `xorm:"VARCHAR(255)"`
18
-	Amount       float32   `xorm:"FLOAT(8,2)"`
18
+	Amount       string    `xorm:"DECIMAL(8,2)"`
19 19
 	FloatType    string    `xorm:"comment('+ -') VARCHAR(5)"`
20 20
 	CreateDate   time.Time `xorm:"DATETIME"`
21 21
 	CreateUser   string    `xorm:"VARCHAR(64)"`

+ 0
- 28
models/model/ta_coupon_card2.go Parādīt failu

@@ -1,28 +0,0 @@
1
-package model
2
-
3
-import (
4
-	"time"
5
-)
6
-
7
-type TaCouponCard2 struct {
8
-	CardId           string    `xorm:"not null pk VARCHAR(64)"`
9
-	CardTypeId       string    `xorm:"VARCHAR(64)"`
10
-	SendType         int       `xorm:"comment('0案场 1渠道') SMALLINT(6)"`
11
-	VideoUrl         string    `xorm:"TEXT"`
12
-	CoverUrl         string    `xorm:"TEXT"`
13
-	Price            float32   `xorm:"FLOAT(64)"`
14
-	StartDate        time.Time `xorm:"DATETIME"`
15
-	EndDate          time.Time `xorm:"DATETIME"`
16
-	ShareDescription string    `xorm:"VARCHAR(255)"`
17
-	RuleDescription  string    `xorm:"VARCHAR(400)"`
18
-	UseDescription   string    `xorm:"TEXT"`
19
-	TotalCount       int       `xorm:"INT(11)"`
20
-	SentCount        int       `xorm:"INT(11)"`
21
-	UsedCount        int       `xorm:"INT(11)"`
22
-	Status           int       `xorm:"comment('0未发布 1发布 2过期') SMALLINT(6)"`
23
-	CaseId           string    `xorm:"VARCHAR(64)"`
24
-	OrgId            string    `xorm:"VARCHAR(64)"`
25
-	CreateDate       time.Time `xorm:"DATETIME"`
26
-	CreateUser       string    `xorm:"VARCHAR(64)"`
27
-	IsOver           int       `xorm:"TINYINT(1)"`
28
-}

models/model/ta_coupon_card_target2.go → models/model/ta_coupon_target.go Parādīt failu

@@ -1,7 +1,7 @@
1 1
 package model
2 2
 
3
-type TaCouponCardTarget2 struct {
3
+type TaCouponTarget struct {
4 4
 	TargetId   string `xorm:"not null pk VARCHAR(64)"`
5
-	CardId     string `xorm:"VARCHAR(64)"`
5
+	CouponId   string `xorm:"VARCHAR(64)"`
6 6
 	TargetName string `xorm:"VARCHAR(255)"`
7 7
 }

+ 10
- 10
models/model/ta_customer_account.go Parādīt failu

@@ -5,14 +5,14 @@ import (
5 5
 )
6 6
 
7 7
 type TaCustomerAccount struct {
8
-	AccountId     string    `xorm:"not null pk VARCHAR(64)"`
9
-	UserId        string    `xorm:"VARCHAR(64)"`
10
-	UserName      string    `xorm:"VARCHAR(50)"`
11
-	Amount        float32   `xorm:"FLOAT(8,2)"`
12
-	Integral      float32   `xorm:"FLOAT(8,2)"`
13
-	Status        int       `xorm:"SMALLINT(6)"`
14
-	OrgId         string    `xorm:"VARCHAR(64)"`
15
-	CreateDate    time.Time `xorm:"DATETIME"`
16
-	PayedAmount   float32   `xorm:"FLOAT(8,2)"`
17
-	PayedIntegral float32   `xorm:"FLOAT(8,2)"`
8
+	AccountId    string    `xorm:"not null pk VARCHAR(64)"`
9
+	CustomerId   string    `xorm:"VARCHAR(64)"`
10
+	CustomerName string    `xorm:"VARCHAR(50)"`
11
+	Amount       string    `xorm:"DECIMAL(8,2)"`
12
+	Points       string    `xorm:"DECIMAL(8,2)"`
13
+	Status       int       `xorm:"SMALLINT(6)"`
14
+	OrgId        string    `xorm:"VARCHAR(64)"`
15
+	CreateDate   time.Time `xorm:"DATETIME"`
16
+	PayedAmount  string    `xorm:"DECIMAL(8,2)"`
17
+	PayedPoints  string    `xorm:"DECIMAL(8,2)"`
18 18
 }

+ 16
- 0
models/model/ta_forbid_user.go Parādīt failu

@@ -0,0 +1,16 @@
1
+package model
2
+
3
+import (
4
+	"time"
5
+)
6
+
7
+type TaForbidUser struct {
8
+	ForbidId   string    `xorm:"not null pk VARCHAR(64)"`
9
+	UserId     string    `xorm:"VARCHAR(64)"`
10
+	UserType   string    `xorm:"VARCHAR(64)"`
11
+	ForbidType string    `xorm:"VARCHAR(20)"`
12
+	BeginDate  time.Time `xorm:"DATETIME"`
13
+	EndDate    time.Time `xorm:"DATETIME"`
14
+	Status     int       `xorm:"SMALLINT(6)"`
15
+	CreateDate time.Time `xorm:"DATETIME"`
16
+}

+ 20
- 21
models/model/ta_goods_orders.go Parādīt failu

@@ -5,25 +5,24 @@ import (
5 5
 )
6 6
 
7 7
 type TaGoodsOrders struct {
8
-	OrdersId   string    `xorm:"not null pk VARCHAR(64)"`
9
-	OrdersNo   string    `xorm:"VARCHAR(32)"`
10
-	OrgId      string    `xorm:"VARCHAR(64)"`
11
-	CaseId     string    `xorm:"VARCHAR(64)"`
12
-	AreaId     string    `xorm:"VARCHAR(64)"`
13
-	AreaName   string    `xorm:"VARCHAR(50)"`
14
-	TableId    string    `xorm:"VARCHAR(64)"`
15
-	TableNo    string    `xorm:"VARCHAR(50)"`
16
-	Amount     string    `xorm:"DECIMAL(8,2)"`
17
-	CreateDate time.Time `xorm:"DATETIME"`
18
-	Status     int       `xorm:"SMALLINT(6)"`
19
-	PayType    string    `xorm:"comment('vip VIP卡城币抵用
20
-            coupon 优惠券抵用') VARCHAR(20)"`
21
-	UserType     string `xorm:"VARCHAR(20)"`
22
-	UserId       string `xorm:"VARCHAR(64)"`
23
-	UserName     string `xorm:"VARCHAR(50)"`
24
-	OrdersNum    int    `xorm:"INT(11)"`
25
-	Remark       string `xorm:"TEXT"`
26
-	IsPay        int    `xorm:"TINYINT(1)"`
27
-	ActualAmount string `xorm:"DECIMAL(8,2)"`
28
-	CouponAmount string `xorm:"DECIMAL(8,2)"`
8
+	OrdersId     string    `xorm:"not null pk VARCHAR(64)"`
9
+	OrdersNo     string    `xorm:"VARCHAR(32)"`
10
+	OrgId        string    `xorm:"VARCHAR(64)"`
11
+	CaseId       string    `xorm:"VARCHAR(64)"`
12
+	AreaId       string    `xorm:"VARCHAR(64)"`
13
+	AreaName     string    `xorm:"VARCHAR(50)"`
14
+	TableId      string    `xorm:"VARCHAR(64)"`
15
+	TableNo      string    `xorm:"VARCHAR(50)"`
16
+	Amount       string    `xorm:"DECIMAL(8,2)"`
17
+	CreateDate   time.Time `xorm:"DATETIME"`
18
+	Status       int       `xorm:"SMALLINT(6)"`
19
+	PayType      string    `xorm:"comment('vip VIP卡城币抵用 coupon 优惠券抵用') VARCHAR(20)"`
20
+	UserType     string    `xorm:"VARCHAR(20)"`
21
+	UserId       string    `xorm:"VARCHAR(64)"`
22
+	UserName     string    `xorm:"VARCHAR(50)"`
23
+	OrdersNum    int       `xorm:"INT(11)"`
24
+	Remark       string    `xorm:"TEXT"`
25
+	IsPay        int       `xorm:"TINYINT(1)"`
26
+	ActualAmount string    `xorm:"DECIMAL(8,2)"`
27
+	CouponAmount string    `xorm:"DECIMAL(8,2)"`
29 28
 }

+ 1
- 1
models/model/ta_goods_orders_coupon.go Parādīt failu

@@ -13,7 +13,7 @@ type TaGoodsOrdersCoupon struct {
13 13
 	OrgId          string    `xorm:"VARCHAR(64)"`
14 14
 	GoodsId        string    `xorm:"VARCHAR(64)"`
15 15
 	GoodsName      string    `xorm:"VARCHAR(50)"`
16
-	UsedAmount     float32   `xorm:"FLOAT(8,2)"`
16
+	UsedAmount     string    `xorm:"DECIMAL(8,2)"`
17 17
 	Status         int       `xorm:"SMALLINT(6)"`
18 18
 	CreateDate     time.Time `xorm:"DATETIME"`
19 19
 }

+ 3
- 3
models/model/ta_goods_spec.go Parādīt failu

@@ -1,7 +1,7 @@
1 1
 package model
2 2
 
3 3
 type TaGoodsSpec struct {
4
-	SpecId     string  `xorm:"not null pk VARCHAR(64)"`
5
-	GoodsId    string  `xorm:"not null pk VARCHAR(64)"`
6
-	GoodsPrice float32 `xorm:"FLOAT(8,2)"`
4
+	SpecId     string `xorm:"not null pk VARCHAR(64)"`
5
+	GoodsId    string `xorm:"not null pk VARCHAR(64)"`
6
+	GoodsPrice string `xorm:"DECIMAL(8,2)"`
7 7
 }

+ 0
- 14
models/model/ta_stop_user.go Parādīt failu

@@ -1,14 +0,0 @@
1
-package model
2
-
3
-import (
4
-	"time"
5
-)
6
-
7
-type TaStopUser struct {
8
-	StopId    string    `xorm:"not null pk VARCHAR(64)"`
9
-	UserId    string    `xorm:"VARCHAR(64)"`
10
-	UserType  string    `xorm:"VARCHAR(64)"`
11
-	StopType  int       `xorm:"SMALLINT(6)"`
12
-	BeginDate time.Time `xorm:"DATETIME"`
13
-	EndDate   time.Time `xorm:"DATETIME"`
14
-}

+ 14
- 1
service/customer/customer.go Parādīt failu

@@ -246,7 +246,20 @@ func (s *CustomerServ) SaveNewCustomer(wxInfo map[string]interface{}, caseID, ar
246 246
 
247 247
 	if err := s.dao.SaveCustomer(&cust); err != nil {
248 248
 		utils.LogError("更新客户信息失败: " + err.Error())
249
-		beego.Error(err)
249
+		return nil, errors.New("更新客户信息失败")
250
+	}
251
+
252
+	account := new(model.TaCustomerAccount)
253
+	account.CustomerId = cust.CustomerId
254
+	account.CustomerName = cust.CustomerName
255
+	account.OrgId = cust.OrgId
256
+	account.Amount = "0"
257
+	account.Points = "0"
258
+	account.PayedAmount = "0"
259
+	account.PayedPoints = "0"
260
+
261
+	if err := s.dao.SaveAccount(account); err != nil {
262
+		utils.LogError("插入账户信息失败: " + err.Error())
250 263
 		return nil, errors.New("更新客户信息失败")
251 264
 	}
252 265
 

+ 10
- 4
service/goods/goods.go Parādīt failu

@@ -1,20 +1,26 @@
1 1
 package goods
2 2
 
3 3
 import (
4
+	"spaceofcheng/services/models/cases"
5
+	"spaceofcheng/services/models/customer"
4 6
 	"spaceofcheng/services/models/goods"
5 7
 	"spaceofcheng/services/utils"
6 8
 )
7 9
 
8 10
 // GoodsServ 系统处理
9 11
 type GoodsServ struct {
10
-	ctx *utils.Context
11
-	dao *goods.GoodsDAO
12
+	ctx     *utils.Context
13
+	dao     *goods.GoodsDAO
14
+	caseDAO *cases.CaseDAO
15
+	custDAO *customer.CustomerDAO
12 16
 }
13 17
 
14 18
 // NewGoodsServ 初始化
15 19
 func NewGoodsServ(ctx *utils.Context) *GoodsServ {
16 20
 	return &GoodsServ{
17
-		ctx: ctx,
18
-		dao: goods.NewGoodsDAO(ctx),
21
+		ctx:     ctx,
22
+		dao:     goods.NewGoodsDAO(ctx),
23
+		caseDAO: cases.NewCaseDAO(ctx),
24
+		custDAO: customer.NewCustomerDAO(ctx),
19 25
 	}
20 26
 }

+ 204
- 0
service/goods/orders.go Parādīt failu

@@ -0,0 +1,204 @@
1
+package goods
2
+
3
+import (
4
+	"errors"
5
+	"spaceofcheng/services/models"
6
+	"spaceofcheng/services/models/model"
7
+	"spaceofcheng/services/utils"
8
+	"strconv"
9
+)
10
+
11
+// Orders 下单
12
+func (s *GoodsServ) Orders(
13
+	info *model.TaGoodsOrders,
14
+	details []model.TaGoodsOrdersDetail,
15
+	coupons []model.TaGoodsOrdersCoupon) error {
16
+
17
+	// 校验下单内容
18
+	if err := s.validOrdersInfo(info); err != nil {
19
+		return err
20
+	}
21
+
22
+	// 校验各种金额
23
+	if err := s.validBillCharges(info, details); err != nil {
24
+		return err
25
+	}
26
+
27
+	// 保存主订单
28
+	if err := s.dao.SaveOrders(info); err != nil {
29
+		return err
30
+	}
31
+
32
+	// 保存订单明细
33
+	if err := s.dao.SaveOrdersDetail(details, info.OrdersId); err != nil {
34
+		return err
35
+	}
36
+
37
+	account, err := s.custDAO.GetAccountByCust(info.UserId)
38
+	if err != nil {
39
+		utils.LogError("查询用户账户信息出错: " + err.Error())
40
+		return errors.New("查询用户账户信息出错")
41
+	}
42
+
43
+	// 如果是使用优惠券
44
+	if coupons != nil && len(coupons) > 0 {
45
+		// TODO
46
+		// 校验优惠券相关
47
+	} else {
48
+		// 如果是使用城币
49
+		accMoney, _ := strconv.ParseFloat(account.Amount, 64)
50
+		payMoney, _ := strconv.ParseFloat(info.ActualAmount, 64)
51
+
52
+		if accMoney < payMoney {
53
+			return errors.New("账户余额不足")
54
+		}
55
+	}
56
+
57
+	// 保存优惠券使用记录
58
+	if coupons != nil && len(coupons) > 0 {
59
+		if err := s.dao.SaveOrdersCoupon(coupons, info); err != nil {
60
+			utils.LogError("保存优惠信息出错: " + err.Error())
61
+			return errors.New("保存优惠信息出错")
62
+		}
63
+
64
+		// TODO
65
+		// 核销优惠券
66
+	}
67
+
68
+	// 如果是城币, 则插入用户账户消费记录
69
+	if info.PayType == models.CONSUME_MONEYCHG {
70
+		if err := s.saveCustomerPayRec(account, info); err != nil {
71
+			return err
72
+		}
73
+	}
74
+
75
+	return nil
76
+}
77
+
78
+// saveCustomerPayRec 保存账户消费流水
79
+func (s *GoodsServ) saveCustomerPayRec(account *model.TaCustomerAccount, info *model.TaGoodsOrders) error {
80
+	accBill := new(model.TaAccountChange)
81
+	accBill.AccountId = account.AccountId
82
+	accBill.Amount = info.ActualAmount
83
+	accBill.CaseId = info.CaseId
84
+	accBill.ChangeSource = models.ACCSOURCE_ORDERS
85
+	accBill.ChangeType = models.CONSUME_MONEYCHG
86
+	accBill.CustomerId = info.UserId
87
+	accBill.CustomerName = info.UserName
88
+	accBill.FloatType = models.ACCOUNT_SPENT
89
+	accBill.SourceId = info.OrdersId
90
+	accBill.SourceName = ""
91
+
92
+	return s.custDAO.InsertAccountRecords(accBill)
93
+}
94
+
95
+// validOrdersInfo 校验下单内容
96
+// 没有对金额, 数量的关系进行校验
97
+func (s *GoodsServ) validOrdersInfo(info *model.TaGoodsOrders) error {
98
+
99
+	caseID := info.CaseId
100
+	if caseID == "" {
101
+		return errors.New("未指定消费案场")
102
+	}
103
+
104
+	areaID := info.AreaId
105
+	if areaID == "" {
106
+		return errors.New("未指定消费区域")
107
+	}
108
+
109
+	tableID := info.TableId
110
+	if tableID == "" {
111
+		return errors.New("未指定消费桌位")
112
+	}
113
+
114
+	custID := info.UserId
115
+	if custID == "" {
116
+		return errors.New("未指定下单人员")
117
+	}
118
+
119
+	// 验证区域
120
+	caseArea, err := s.caseDAO.GetCaseAreaByID(areaID)
121
+	if err != nil {
122
+		utils.LogError("查询案场区域出错: " + err.Error())
123
+		return errors.New("验证案场区域出错")
124
+	}
125
+
126
+	info.AreaName = caseArea.AreaName
127
+	info.OrgId = caseArea.OrgId
128
+
129
+	// 验证桌位
130
+	table, err := s.caseDAO.GetCaseTableByID(tableID)
131
+	if err != nil {
132
+		utils.LogError("查询案场桌位出错: " + err.Error())
133
+		return errors.New("验证案场桌位出错")
134
+	}
135
+
136
+	info.TableNo = table.TableNo
137
+
138
+	// 验证人员
139
+	cust, err := s.custDAO.GetCustomerByID(custID)
140
+	if err != nil {
141
+		utils.LogError("查询下单人出错: " + err.Error())
142
+		return errors.New("验证下单人出错")
143
+	}
144
+
145
+	if cust.Status != models.STATUS_NORMAL {
146
+		return errors.New("下单人状态不正确, 不能下单")
147
+	}
148
+
149
+	// 如果是管理人员
150
+	if cust.UserId != "" {
151
+		fibUsers, err := s.custDAO.GetForbidUserByUserID(cust.UserId)
152
+		if err != nil {
153
+			utils.LogError("查询下单人是否被禁出错: " + err.Error())
154
+			return errors.New("验证下单人出错")
155
+		}
156
+
157
+		if fibUsers != nil && len(fibUsers) > 0 {
158
+			for _, u := range fibUsers {
159
+				if u.ForbidType == models.FORBID_ORDER && u.Status == models.STATUS_NORMAL {
160
+					return errors.New("当前下单人被禁止点单")
161
+				}
162
+			}
163
+		}
164
+	}
165
+
166
+	return nil
167
+}
168
+
169
+// validBillCharges 验证下单各种金额
170
+func (s *GoodsServ) validBillCharges(info *model.TaGoodsOrders, details []model.TaGoodsOrdersDetail) error {
171
+	if details == nil || len(details) == 0 {
172
+		return errors.New("没有发现下单明细")
173
+	}
174
+
175
+	caseID := info.CaseId
176
+
177
+	orgAmount := float64(0.0)
178
+	for _, item := range details {
179
+		goods, err := s.dao.GetGoodsWithPriceByID(item.GoodsId, item.SpecId, caseID)
180
+		if err != nil {
181
+			utils.LogError("查询商品详情失败: " + err.Error())
182
+			return errors.New("校验商品信息失败")
183
+		}
184
+
185
+		if goods.GoodsPrice != item.Price {
186
+			return errors.New("商品金额计算错误")
187
+		}
188
+
189
+		// TODO 校验库存
190
+		// 商品暂时无库存
191
+
192
+		price, _ := strconv.ParseFloat(item.Price, 64)
193
+		orgAmount += price * float64(item.Number)
194
+	}
195
+
196
+	// 校验总金额
197
+	orderAmount, _ := strconv.ParseFloat(info.Amount, 64)
198
+
199
+	if orgAmount != orderAmount {
200
+		return errors.New("商品金额计算错误")
201
+	}
202
+
203
+	return nil
204
+}