//保存(新增/编辑)
function save(){
var goodsId=$("#goodsId").val();
var typeId=$("#subtype").val();
var goodsName=$("#goodsName").val();
var model=$("#model").val();
var size=$("#size").val();
var material=$("#material").val();
var price=$("#price").val();
var brandId=$("#brand").val();
//var picFile=$("#picFile").get(0).files[0];
var picFile=document.getElementById("picFile").files[0];
var goods=new Object();
//form表单
var formData = new FormData();
var formData = new FormData();
formData.append("typeId",typeId);
formData.append("fileImage",picFile);
formData.append("goodsName",goodsName);
formData.append("model",model);
formData.append("size",size);
formData.append("material",material);
formData.append("price",price);
formData.append("brandId",brandId);
var goodsUrl="<%=path%>/goods/addGoods.do";
if(goodsId){
formData.append("id",goodsId);
formData.append("firstImageUrl",$("#picUrl").val());
goodsUrl="<%=path%>/goods/updateGoods.do";
}
$.ajax({
url: goodsUrl,
type: "POST",
data: formData,
contentType: false,
processData: false,
success: function (data) {
if (data.status == 200) {
$("#add").modal('hide');
layer.msg('保存成功 ',{time:2000});
$("#tbGoodsInventory").trigger("reloadGrid");
}else{
layer.msg(data.msg,{time:2000});
}
},
error: function () {
alert("保存失败!");
}
});
}
/**
* 添加商品
*
* @param request
* @param goods
* @return
* @date 2017年5月31日
*/
@RequestMapping("addGoods")
@ResponseBody
public AdminResult addGoods(HttpServletRequest request, @RequestParam("fileImage") CommonsMultipartFile fileImage,
Goods goods) {
if (null == fileImage || fileImage.isEmpty()) {
return AdminResult.error("图片为空");
}
if (StringUtils.isEmpty(goods.getGoodsName())) {
return AdminResult.error("商品名称为空");
}
try {
String fileName = fileImage.getOriginalFilename();
fileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));
// 上次图片到ftp服务器,并返回图片地址
String imagePath = FtpUtils.uploadFile(fileImage.getInputStream(), AdminConstant.PARENT_DIR_NAME, fileName);
goods.setPicUrl(imagePath);
// 商品编码
goods.setGoodsNo(CodeGenerator.generate(CodeGenerator.GOODS_CODE));
// 创建人
User user = (User) request.getSession().getAttribute("user");
goods.setCreateId(user.getId());
// 保存商品
goodsService.addGoods(goods);
} catch (Exception e) {
logger.error("添加商品失败", e);
e.printStackTrace();
return AdminResult.error();
}
return AdminResult.success();
}
/**
* 修改商品
*
* @param request
* @param goods
* @return
* @date 2017年5月31日
*/
@RequestMapping("updateGoods")
@ResponseBody
public AdminResult updateGoods(HttpServletRequest request, Goods goods) {
if (null == goods.getId()) {
return AdminResult.error("商品ID为空");
}
if (StringUtils.isEmpty(goods.getGoodsName())) {
return AdminResult.error("商品名称为空");
}
try {
MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
MultipartFile fileImage = mRequest.getFile("fileImage");
String imagePathRes = null;
if (null != fileImage && !fileImage.isEmpty()) {
String fileName = fileImage.getOriginalFilename();
fileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));
// 上传图片到ftp服务器,并返回图片地址
imagePathRes = FtpUtils.uploadFile(fileImage.getInputStream(), AdminConstant.PARENT_DIR_NAME, fileName);
//从ftp删除原图片文件
FtpUtils.delete(goods.getFirstImageUrl());
}
goods.setPicUrl(imagePathRes);
goodsService.updateGoods(goods);
} catch (Exception e) {
logger.error("修改商品失败", e);
e.printStackTrace();
return AdminResult.error();
}
return AdminResult.success();
}