如果用全局对象 list添加后一定要clear 不然会和下次的累加。
/**
* 添加订单
* @param
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
@Log(operModul="订单管理",operType="添加",operDesc="新增订单信息")
public Map<String, String> add(
HttpServletResponse response,
@RequestParam(name="productList",required=false,defaultValue="") String productList,
@RequestParam(name="customer_id",required=false,defaultValue="") String customer_id,
@RequestParam(name="address",required=false,defaultValue="") String address,
@RequestParam(name="customer_order_no",required=false,defaultValue="") String customer_order_no
){
System.out.println("物料信息参数:"+productList);
//System.out.println("客户id:"+customer_id);
System.out.println(order.getCreate_by());
Map<String, String> ret = new HashMap<String, String>();
if(order == null){
ret.put("type", "error");
ret.put("msg", "请填写正确的订单信息!");
return ret;
}
JSONArray productArray = JSONArray.fromObject(productList);
if(productArray == null){
ret.put("type", "error");
ret.put("msg", "请添加物料料号信息!");
return ret;
}
System.out.println("productArray的次数:"+productArray.size());
double total=0;
for(int i = 0; i < productArray.size(); i++){
JSONObject jsonObject = productArray.getJSONObject(i);
OrderItem oi = new OrderItem();
oi.setMaterial_id(jsonObject.getInt("material_id")); //物料id
oi.setPrice(jsonObject.getDouble("price")); //单价
oi.setQuantity(Integer.parseInt(jsonObject.getString("quantity"))); //数量
oi.setSum(jsonObject.getDouble("sum")); //总价
total += jsonObject.getDouble("sum"); //合计金额
order.getOrderItemlist().add(oi); //将oi添加到orderItemlist中去
}
System.out.println("getOrderItemlist()的个数:"+order.getOrderItemlist().size());
String tabName="crm_order";
WaterCode waterCode = waterCodeService.findByTabName(tabName);
System.out.println("打印流水号:"+waterCode.getCurrentCode());
order.setCustomer_id(Integer.parseInt(customer_id)); //客户id
order.setOrder_code(waterCode.getCurrentCode());//订单号码
order.setCustomer_order_no(customer_order_no);//客户订单号码
order.setDelivery_address(address); //交货地址
order.setContract_amount(total); //合计总金额
String username= UserUtil.getUserName();
User user = userService.findByUsername(username);
if(user.getShowname()==null){
ret.put("type", "error");
ret.put("msg", "系统登录超时,请重新登录!");
}
order.setCreate_by(user.getShowname()); //创建人
order.setCreate_time(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));//创建时间
if(orderService.add(order) <= 0){
ret.put("type", "error");
ret.put("msg", "订单新增失败,请联系管理员!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "订单新增成功!");
//生成订单,删除order中的所有orderItemlist 因为用的是全局order对象 如不清除 则每次的orderItemlist会累加
order.getOrderItemlist().clear();
return ret;
}