From cf7bbd3046e90847b98929d8a3b75f1154b6bdd6 Mon Sep 17 00:00:00 2001 From: shen100 Date: Thu, 13 Jul 2017 10:46:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=87=8D=E7=BD=AE=E5=95=86?= =?UTF-8?q?=E5=93=81=E5=BA=93=E5=AD=98=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/product/inventory.go | 109 ++++-------------- .../admin/actions/product/requestProduct.js | 11 +- .../actions/product/requestResetInventory.js | 37 ++++++ .../javascripts/admin/constants/index.js | 4 + .../admin/containers/product/EditProduct.js | 11 ++ .../javascripts/admin/reducers/product.js | 11 +- route/route.go | 1 + 7 files changed, 94 insertions(+), 90 deletions(-) create mode 100644 nodejs/static/javascripts/admin/actions/product/requestResetInventory.js diff --git a/controller/product/inventory.go b/controller/product/inventory.go index 35475064..c64f683a 100644 --- a/controller/product/inventory.go +++ b/controller/product/inventory.go @@ -2,11 +2,7 @@ package product import ( "fmt" - "strings" - "strconv" - "unicode/utf8" "wemall/model" - "wemall/config" "gopkg.in/kataras/iris.v6" ) @@ -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" : "参数无效", @@ -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", @@ -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{ @@ -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()) @@ -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, + }, + }) } \ No newline at end of file diff --git a/nodejs/static/javascripts/admin/actions/product/requestProduct.js b/nodejs/static/javascripts/admin/actions/product/requestProduct.js index 530da91d..956da21c 100644 --- a/nodejs/static/javascripts/admin/actions/product/requestProduct.js +++ b/nodejs/static/javascripts/admin/actions/product/requestProduct.js @@ -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 }; } diff --git a/nodejs/static/javascripts/admin/actions/product/requestResetInventory.js b/nodejs/static/javascripts/admin/actions/product/requestResetInventory.js new file mode 100644 index 00000000..b2340bba --- /dev/null +++ b/nodejs/static/javascripts/admin/actions/product/requestResetInventory.js @@ -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))); + }; +}; + diff --git a/nodejs/static/javascripts/admin/constants/index.js b/nodejs/static/javascripts/admin/constants/index.js index b3a8e412..b6d7ec51 100644 --- a/nodejs/static/javascripts/admin/constants/index.js +++ b/nodejs/static/javascripts/admin/constants/index.js @@ -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'; diff --git a/nodejs/static/javascripts/admin/containers/product/EditProduct.js b/nodejs/static/javascripts/admin/containers/product/EditProduct.js index 532f3917..a2672469 100644 --- a/nodejs/static/javascripts/admin/containers/product/EditProduct.js +++ b/nodejs/static/javascripts/admin/containers/product/EditProduct.js @@ -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'; @@ -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, @@ -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; @@ -571,6 +579,9 @@ class EditProduct extends Component { + + + { inventories.map(function(inv) { diff --git a/nodejs/static/javascripts/admin/reducers/product.js b/nodejs/static/javascripts/admin/reducers/product.js index 57dc3c20..4d4a6d17 100644 --- a/nodejs/static/javascripts/admin/reducers/product.js +++ b/nodejs/static/javascripts/admin/reducers/product.js @@ -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 = { @@ -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 } diff --git a/route/route.go b/route/route.go index 5e3f3231..7f625af9 100644 --- a/route/route.go +++ b/route/route.go @@ -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)