Skip to content

Commit cf7bbd3

Browse files
committed
增加重置商品库存接口
1 parent 67e77b3 commit cf7bbd3

File tree

7 files changed

+94
-90
lines changed

7 files changed

+94
-90
lines changed

controller/product/inventory.go

+21-88
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ package product
22

33
import (
44
"fmt"
5-
"strings"
6-
"strconv"
7-
"unicode/utf8"
85
"wemall/model"
9-
"wemall/config"
106
"gopkg.in/kataras/iris.v6"
117
)
128

@@ -40,13 +36,12 @@ func combinationPropValue(productID uint, properties []model.Property) []model.I
4036
return inventories
4137
}
4238

43-
// test 添加商品属性值
44-
func test(ctx *iris.Context) {
45-
var productID uint
46-
var propertyValue model.PropertyValue
39+
// ResetInventory 复位库存
40+
func ResetInventory(ctx *iris.Context) {
41+
var product model.Product
4742

48-
if err := ctx.ReadJSON(&propertyValue); err != nil {
49-
fmt.Println(err.Error());
43+
if err := ctx.ReadJSON(&product); err != nil {
44+
fmt.Println(err.Error())
5045
ctx.JSON(iris.StatusOK, iris.Map{
5146
"errNo" : model.ErrorCode.ERROR,
5247
"msg" : "参数无效",
@@ -55,36 +50,7 @@ func test(ctx *iris.Context) {
5550
return
5651
}
5752

58-
productID = propertyValue.ProductID
59-
propertyValue.Name = strings.TrimSpace(propertyValue.Name)
60-
61-
var isErr bool
62-
var errMsg = ""
63-
64-
if productID <= 0 {
65-
isErr = true
66-
errMsg = "无效的商品ID"
67-
} else if utf8.RuneCountInString(propertyValue.Name) > config.ServerConfig.MaxNameLen {
68-
isErr = true
69-
errMsg = "名称不能超过" + strconv.Itoa(config.ServerConfig.MaxNameLen) + "个字符"
70-
} else if utf8.RuneCountInString(propertyValue.Name) <= 0 {
71-
isErr = true
72-
errMsg = "名称不能为空"
73-
}
74-
75-
if isErr {
76-
ctx.JSON(iris.StatusOK, iris.Map{
77-
"errNo" : model.ErrorCode.ERROR,
78-
"msg" : errMsg,
79-
"data" : iris.Map{},
80-
})
81-
return
82-
}
83-
84-
var product model.Product
85-
86-
if err := model.DB.First(&product, productID).Error; err != nil {
87-
fmt.Println(err.Error())
53+
if err := model.DB.First(&product, product.ID).Error; err != nil {
8854
ctx.JSON(iris.StatusOK, iris.Map{
8955
"errNo" : model.ErrorCode.NotFound,
9056
"msg" : "错误的商品id",
@@ -97,33 +63,14 @@ func test(ctx *iris.Context) {
9763
fmt.Println(err.Error())
9864
ctx.JSON(iris.StatusOK, iris.Map{
9965
"errNo" : model.ErrorCode.ERROR,
100-
"msg" : "error",
66+
"msg" : "error.",
10167
"data" : iris.Map{},
10268
})
10369
return
10470
}
10571

106-
var properties = product.Properties
107-
var index = -1 //属性(新添加的属性值属于的属性)在属性数组中的索引
108-
for i := 0; i < len(properties); i++ {
109-
fmt.Println(123, properties[i].ID, propertyValue.PropertyID)
110-
if properties[i].ID == propertyValue.PropertyID {
111-
index = i
112-
break;
113-
}
114-
}
115-
116-
if index < 0 {
117-
ctx.JSON(iris.StatusOK, iris.Map{
118-
"errNo" : model.ErrorCode.ERROR,
119-
"msg" : "错误的propertyID",
120-
"data" : iris.Map{},
121-
})
122-
return
123-
}
124-
125-
for i := 0; i < len(properties); i++ {
126-
property := properties[i]
72+
for i := 0; i < len(product.Properties); i++ {
73+
property := product.Properties[i]
12774
if err := model.DB.Model(&property).Related(&property.PropertyValues).Error; err != nil {
12875
fmt.Println(err.Error())
12976
ctx.JSON(iris.StatusOK, iris.Map{
@@ -133,34 +80,12 @@ func test(ctx *iris.Context) {
13380
})
13481
return
13582
}
136-
properties[i] = property
137-
}
138-
139-
if err := model.DB.Create(&propertyValue).Error; err != nil {
140-
fmt.Println(err.Error())
141-
ctx.JSON(iris.StatusOK, iris.Map{
142-
"errNo" : model.ErrorCode.ERROR,
143-
"msg" : "error",
144-
"data" : iris.Map{},
145-
})
146-
return
147-
}
148-
149-
var inventories []model.Inventory
150-
if len(properties) == 1 {
151-
var inventory = model.Inventory{
152-
ProductID : productID,
153-
PropertyValues : append([]model.PropertyValue{}, propertyValue),
154-
}
155-
inventories = append(inventories, inventory)
156-
} else if len(properties) >= 2 {
157-
properties = append(properties[:index], properties[index + 1:]...)
158-
inventories = combinationPropValue(productID, properties)
159-
for i := 0; i < len(inventories); i++ {
160-
inventories[i].PropertyValues = append(inventories[i].PropertyValues, propertyValue)
161-
}
83+
product.Properties[i] = property
16284
}
16385

86+
properties := product.Properties
87+
inventories := combinationPropValue(product.ID, properties)
88+
fmt.Println(inventories)
16489
for i := 0; i < len(inventories); i++ {
16590
if err := model.DB.Create(&inventories[i]).Error; err != nil {
16691
fmt.Println(err.Error())
@@ -172,4 +97,12 @@ func test(ctx *iris.Context) {
17297
return
17398
}
17499
}
100+
101+
ctx.JSON(iris.StatusOK, iris.Map{
102+
"errNo" : model.ErrorCode.SUCCESS,
103+
"msg" : "success",
104+
"data" : iris.Map{
105+
"inventories": inventories,
106+
},
107+
})
175108
}

nodejs/static/javascripts/admin/actions/product/requestProduct.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,18 @@ import {
44
} from '../../constants';
55

66
function receive(data) {
7+
var product = data.product;
8+
if (product.inventories && product.inventories.length) {
9+
var inventories = product.inventories;
10+
for (var i = 0; i < inventories.length; i++) {
11+
inventories[i].propertyValues.sort(function(a, b) {
12+
return a.propertyID > b.propertyID;
13+
});
14+
}
15+
}
716
return {
817
type : REQUEST_PRODUCT_SUCCESS,
9-
product : data.product
18+
product : product
1019
};
1120
}
1221

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {
2+
REQUEST_RESET_INVENTORY,
3+
REQUEST_RESET_INVENTORY_SUCCESS,
4+
} from '../../constants';
5+
6+
function receive(data) {
7+
var inventories = data.inventories;
8+
for (var i = 0; i < inventories.length; i++) {
9+
inventories[i].propertyValues.sort(function(a, b) {
10+
return a.propertyID > b.propertyID;
11+
});
12+
}
13+
return {
14+
type : REQUEST_RESET_INVENTORY_SUCCESS,
15+
inventories : inventories
16+
};
17+
}
18+
19+
export default function(data) {
20+
return dispatch => {
21+
var url = pageConfig.apiPath + '/admin/product/inventory/reset';
22+
var reqData = {
23+
id : data.productID
24+
};
25+
return fetch(url, {
26+
method: 'POST',
27+
headers: {
28+
'Accept': 'application/json',
29+
'Content-Type': 'application/json'
30+
},
31+
body: JSON.stringify(reqData)
32+
})
33+
.then(response => response.json())
34+
.then(json => dispatch(receive(json.data)));
35+
};
36+
};
37+

nodejs/static/javascripts/admin/constants/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export const REQUEST_SAVE_PRODUCT_PROP_VALUE = 'requestSaveProductPropValue';
3232

3333
export const REQUEST_SAVE_PRODUCT_PROP_VALUE_SUCCESS = 'requestSaveProductPropValueSuccess';
3434

35+
export const REQUEST_RESET_INVENTORY = 'requestResetInventory';
36+
37+
export const REQUEST_RESET_INVENTORY_SUCCESS = 'requestResetInventorySuccess';
38+
3539
export const CHANGE_PRODUCT_STATUS = 'changeProductStatus';
3640

3741
export const REQUEST_CATEGORY_LIST = 'requestCategoryList';

nodejs/static/javascripts/admin/containers/product/EditProduct.js

+11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import requestSaveProduct from '../../actions/product/requestSaveProduct'
2323
import requestCategoryList from '../../actions/category/requestCategoryList';
2424
import requestSaveProperty from '../../actions/product/requestSaveProperty';
2525
import requestSavePropertyValue from '../../actions/product/requestSavePropertyValue';
26+
import requestResetInventory from '../../actions/product/requestResetInventory';
2627
import Software from '../Software';
2728
import utils from '../../utils';
2829
import analyze from '../../../sdk/analyze';
@@ -54,6 +55,7 @@ class EditProduct extends Component {
5455
this.onPropInput = this.onPropInput.bind(this);
5556
this.addProp = this.addProp.bind(this);
5657
this.cancelAddProp = this.cancelAddProp.bind(this);
58+
this.onResetInventory = this.onResetInventory.bind(this);
5759

5860
this.state = {
5961
productId : this.props.routeParams.id,
@@ -358,6 +360,12 @@ class EditProduct extends Component {
358360
propPopupVisible : false
359361
});
360362
}
363+
onResetInventory() {
364+
const { dispatch } = this.props;
365+
dispatch(requestResetInventory({
366+
productID : this.state.productId
367+
}));
368+
}
361369
render() {
362370
let self = this;
363371
let { data } = this.props;
@@ -571,6 +579,9 @@ class EditProduct extends Component {
571579
<Button type="primary">添加属性</Button>
572580
</Popover>
573581
</FormItem>
582+
<FormItem>
583+
<Button onClick={self.onResetInventory} type="primary">重置库存</Button>
584+
</FormItem>
574585
<FormItem {...formItemLayout} label="库存">
575586
{
576587
inventories.map(function(inv) {

nodejs/static/javascripts/admin/reducers/product.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
REQUEST_CATEGORY_LIST_SUCCESS,
99
REQUEST_SAVE_PRODUCT_SUCCESS,
1010
REQUEST_SAVE_PRODUCT_PROP_SUCCESS,
11-
REQUEST_SAVE_PRODUCT_PROP_VALUE_SUCCESS
11+
REQUEST_SAVE_PRODUCT_PROP_VALUE_SUCCESS,
12+
REQUEST_RESET_INVENTORY_SUCCESS
1213
} from '../constants';
1314

1415
let initState = {
@@ -98,6 +99,14 @@ export default (state = initState, action) => {
9899
product: product
99100
};
100101
}
102+
case REQUEST_RESET_INVENTORY_SUCCESS: {
103+
let product = state.product;
104+
product.inventories = action.inventories;
105+
return {
106+
...state,
107+
product: product
108+
};
109+
}
101110
default: {
102111
return state
103112
}

route/route.go

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func Route(app *iris.Framework) {
4646
adminRouter.Post("/product/status/update", product.UpdateStatus)
4747
adminRouter.Post("/product/property/saveval", product.AddPropertyValue)
4848
adminRouter.Post("/product/property/create", product.AddProperty)
49+
adminRouter.Post("/product/inventory/reset", product.ResetInventory)
4950

5051
adminRouter.Get("/order/analyze", order.Analyze)
5152
adminRouter.Get("/order/todaycount", order.TodayCount)

0 commit comments

Comments
 (0)