Skip to content

Commit

Permalink
增加重置商品库存接口
Browse files Browse the repository at this point in the history
  • Loading branch information
shen100 committed Jul 13, 2017
1 parent 67e77b3 commit cf7bbd3
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 90 deletions.
109 changes: 21 additions & 88 deletions controller/product/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ package product

import (
"fmt"
"strings"
"strconv"
"unicode/utf8"
"wemall/model"
"wemall/config"
"gopkg.in/kataras/iris.v6"
)

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

// test 添加商品属性值
func test(ctx *iris.Context) {
var productID uint
var propertyValue model.PropertyValue
// ResetInventory 复位库存
func ResetInventory(ctx *iris.Context) {
var product model.Product

if err := ctx.ReadJSON(&propertyValue); err != nil {
fmt.Println(err.Error());
if err := ctx.ReadJSON(&product); err != nil {
fmt.Println(err.Error())
ctx.JSON(iris.StatusOK, iris.Map{
"errNo" : model.ErrorCode.ERROR,
"msg" : "参数无效",
Expand All @@ -55,36 +50,7 @@ func test(ctx *iris.Context) {
return
}

productID = propertyValue.ProductID
propertyValue.Name = strings.TrimSpace(propertyValue.Name)

var isErr bool
var errMsg = ""

if productID <= 0 {
isErr = true
errMsg = "无效的商品ID"
} else if utf8.RuneCountInString(propertyValue.Name) > config.ServerConfig.MaxNameLen {
isErr = true
errMsg = "名称不能超过" + strconv.Itoa(config.ServerConfig.MaxNameLen) + "个字符"
} else if utf8.RuneCountInString(propertyValue.Name) <= 0 {
isErr = true
errMsg = "名称不能为空"
}

if isErr {
ctx.JSON(iris.StatusOK, iris.Map{
"errNo" : model.ErrorCode.ERROR,
"msg" : errMsg,
"data" : iris.Map{},
})
return
}

var product model.Product

if err := model.DB.First(&product, productID).Error; err != nil {
fmt.Println(err.Error())
if err := model.DB.First(&product, product.ID).Error; err != nil {
ctx.JSON(iris.StatusOK, iris.Map{
"errNo" : model.ErrorCode.NotFound,
"msg" : "错误的商品id",
Expand All @@ -97,33 +63,14 @@ func test(ctx *iris.Context) {
fmt.Println(err.Error())
ctx.JSON(iris.StatusOK, iris.Map{
"errNo" : model.ErrorCode.ERROR,
"msg" : "error",
"msg" : "error.",
"data" : iris.Map{},
})
return
}

var properties = product.Properties
var index = -1 //属性(新添加的属性值属于的属性)在属性数组中的索引
for i := 0; i < len(properties); i++ {
fmt.Println(123, properties[i].ID, propertyValue.PropertyID)
if properties[i].ID == propertyValue.PropertyID {
index = i
break;
}
}

if index < 0 {
ctx.JSON(iris.StatusOK, iris.Map{
"errNo" : model.ErrorCode.ERROR,
"msg" : "错误的propertyID",
"data" : iris.Map{},
})
return
}

for i := 0; i < len(properties); i++ {
property := properties[i]
for i := 0; i < len(product.Properties); i++ {
property := product.Properties[i]
if err := model.DB.Model(&property).Related(&property.PropertyValues).Error; err != nil {
fmt.Println(err.Error())
ctx.JSON(iris.StatusOK, iris.Map{
Expand All @@ -133,34 +80,12 @@ func test(ctx *iris.Context) {
})
return
}
properties[i] = property
}

if err := model.DB.Create(&propertyValue).Error; err != nil {
fmt.Println(err.Error())
ctx.JSON(iris.StatusOK, iris.Map{
"errNo" : model.ErrorCode.ERROR,
"msg" : "error",
"data" : iris.Map{},
})
return
}

var inventories []model.Inventory
if len(properties) == 1 {
var inventory = model.Inventory{
ProductID : productID,
PropertyValues : append([]model.PropertyValue{}, propertyValue),
}
inventories = append(inventories, inventory)
} else if len(properties) >= 2 {
properties = append(properties[:index], properties[index + 1:]...)
inventories = combinationPropValue(productID, properties)
for i := 0; i < len(inventories); i++ {
inventories[i].PropertyValues = append(inventories[i].PropertyValues, propertyValue)
}
product.Properties[i] = property
}

properties := product.Properties
inventories := combinationPropValue(product.ID, properties)
fmt.Println(inventories)
for i := 0; i < len(inventories); i++ {
if err := model.DB.Create(&inventories[i]).Error; err != nil {
fmt.Println(err.Error())
Expand All @@ -172,4 +97,12 @@ func test(ctx *iris.Context) {
return
}
}

ctx.JSON(iris.StatusOK, iris.Map{
"errNo" : model.ErrorCode.SUCCESS,
"msg" : "success",
"data" : iris.Map{
"inventories": inventories,
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@ import {
} from '../../constants';

function receive(data) {
var product = data.product;
if (product.inventories && product.inventories.length) {
var inventories = product.inventories;
for (var i = 0; i < inventories.length; i++) {
inventories[i].propertyValues.sort(function(a, b) {
return a.propertyID > b.propertyID;
});
}
}
return {
type : REQUEST_PRODUCT_SUCCESS,
product : data.product
product : product
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
REQUEST_RESET_INVENTORY,
REQUEST_RESET_INVENTORY_SUCCESS,
} from '../../constants';

function receive(data) {
var inventories = data.inventories;
for (var i = 0; i < inventories.length; i++) {
inventories[i].propertyValues.sort(function(a, b) {
return a.propertyID > b.propertyID;
});
}
return {
type : REQUEST_RESET_INVENTORY_SUCCESS,
inventories : inventories
};
}

export default function(data) {
return dispatch => {
var url = pageConfig.apiPath + '/admin/product/inventory/reset';
var reqData = {
id : data.productID
};
return fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(reqData)
})
.then(response => response.json())
.then(json => dispatch(receive(json.data)));
};
};

4 changes: 4 additions & 0 deletions nodejs/static/javascripts/admin/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export const REQUEST_SAVE_PRODUCT_PROP_VALUE = 'requestSaveProductPropValue';

export const REQUEST_SAVE_PRODUCT_PROP_VALUE_SUCCESS = 'requestSaveProductPropValueSuccess';

export const REQUEST_RESET_INVENTORY = 'requestResetInventory';

export const REQUEST_RESET_INVENTORY_SUCCESS = 'requestResetInventorySuccess';

export const CHANGE_PRODUCT_STATUS = 'changeProductStatus';

export const REQUEST_CATEGORY_LIST = 'requestCategoryList';
Expand Down
11 changes: 11 additions & 0 deletions nodejs/static/javascripts/admin/containers/product/EditProduct.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import requestSaveProduct from '../../actions/product/requestSaveProduct'
import requestCategoryList from '../../actions/category/requestCategoryList';
import requestSaveProperty from '../../actions/product/requestSaveProperty';
import requestSavePropertyValue from '../../actions/product/requestSavePropertyValue';
import requestResetInventory from '../../actions/product/requestResetInventory';
import Software from '../Software';
import utils from '../../utils';
import analyze from '../../../sdk/analyze';
Expand Down Expand Up @@ -54,6 +55,7 @@ class EditProduct extends Component {
this.onPropInput = this.onPropInput.bind(this);
this.addProp = this.addProp.bind(this);
this.cancelAddProp = this.cancelAddProp.bind(this);
this.onResetInventory = this.onResetInventory.bind(this);

this.state = {
productId : this.props.routeParams.id,
Expand Down Expand Up @@ -358,6 +360,12 @@ class EditProduct extends Component {
propPopupVisible : false
});
}
onResetInventory() {
const { dispatch } = this.props;
dispatch(requestResetInventory({
productID : this.state.productId
}));
}
render() {
let self = this;
let { data } = this.props;
Expand Down Expand Up @@ -571,6 +579,9 @@ class EditProduct extends Component {
<Button type="primary">添加属性</Button>
</Popover>
</FormItem>
<FormItem>
<Button onClick={self.onResetInventory} type="primary">重置库存</Button>
</FormItem>
<FormItem {...formItemLayout} label="库存">
{
inventories.map(function(inv) {
Expand Down
11 changes: 10 additions & 1 deletion nodejs/static/javascripts/admin/reducers/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
REQUEST_CATEGORY_LIST_SUCCESS,
REQUEST_SAVE_PRODUCT_SUCCESS,
REQUEST_SAVE_PRODUCT_PROP_SUCCESS,
REQUEST_SAVE_PRODUCT_PROP_VALUE_SUCCESS
REQUEST_SAVE_PRODUCT_PROP_VALUE_SUCCESS,
REQUEST_RESET_INVENTORY_SUCCESS
} from '../constants';

let initState = {
Expand Down Expand Up @@ -98,6 +99,14 @@ export default (state = initState, action) => {
product: product
};
}
case REQUEST_RESET_INVENTORY_SUCCESS: {
let product = state.product;
product.inventories = action.inventories;
return {
...state,
product: product
};
}
default: {
return state
}
Expand Down
1 change: 1 addition & 0 deletions route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func Route(app *iris.Framework) {
adminRouter.Post("/product/status/update", product.UpdateStatus)
adminRouter.Post("/product/property/saveval", product.AddPropertyValue)
adminRouter.Post("/product/property/create", product.AddProperty)
adminRouter.Post("/product/inventory/reset", product.ResetInventory)

adminRouter.Get("/order/analyze", order.Analyze)
adminRouter.Get("/order/todaycount", order.TodayCount)
Expand Down

0 comments on commit cf7bbd3

Please sign in to comment.