/**
* 将购物车商品插入到订单表和订单物品表
*
* @description:
* @author:xuzn
* @date:2017-3-17 上午8:54:52
* @modify:
* @param addressid
* @param shopcartids
* @param uid
* @param request
* @param response
* @return
* @version:
*
*/
@RequestMapping("useraddResult")
@ResponseBody
@SuppressWarnings("null")
public LBResult useraddResult(String addressid, String shopcartids,
String uid, HttpServletRequest request, HttpServletResponse response) {
// 1.
JifenAddressInfo addressInfo = jifenAddressInfoService
.userAddressInfo(addressid);
// 2.根据购物车id查询信息
String[] ids = shopcartids.split(",");
ShoppingCart[] cart = new ShoppingCart[ids.length];
BigDecimal ordersum = new BigDecimal(0);
for (int j = 0; j < ids.length; j++) {
cart[j] = carShopService.carById(ids[j]);
BigDecimal sumBigDecimal1 = cart[j].getLogisticsjifen();
BigDecimal sumBigDecimal3 = cart[j].getGoodsjifen();
BigDecimal sumBigDecimal2 = new BigDecimal(cart[j].getGoodsnum());
// ordersum = ordersum.add(sumBigDecimal1.multiply(sumBigDecimal2));
ordersum = BigDecimalCalculateUtil
.add(ordersum, BigDecimalCalculateUtil.mul(sumBigDecimal1.add(sumBigDecimal3),
sumBigDecimal2));
}
// 3.根据购物车id,更新购物车状态
List<String> ids1 = null;
if (ids.length > 0) {
List<String> idsList1 = Arrays.asList(ids);
ids1 = new ArrayList<String>(idsList1);
}
int count = carShopService.txbatchUpdateState(ids1);
// 4.生成订单编号
String ordercodeString = LBUtil.getOrderNumber();
// 5.根据订单编号、地址信息、订单总积分、uid,插入订单表
// 获得地址信息表中得字段
String provinceString = addressInfo.getProvince();
String cityString = addressInfo.getCity();
String countryString = addressInfo.getCounty();
String addressnameString = addressInfo.getAddressname();
String addresstelString = addressInfo.getAddresstel();
String addressdetailed = addressInfo.getAddressdetailed();
OrderDetail orderDetail = new OrderDetail();
orderDetail.setOrdercode(ordercodeString);
orderDetail.setOrderstatus("01");
orderDetail.setOrdersum(ordersum);
orderDetail.setId(UUIDGenerator.getUUID());
Date nowDate = DateUtil1.getFormatDate(new Date(),
"yyyy-MM-dd HH:mm:ss");
orderDetail.setCreatetime(nowDate);
orderDetail.setCity(cityString);
orderDetail.setAddressname(addressnameString);
orderDetail.setAddresstel(addresstelString);
orderDetail.setCounty(countryString);
orderDetail.setAddressid(addressid);
orderDetail.setAddressdetailed(addressdetailed);
orderDetailService.txInsert(orderDetail);
// 插入订单结束
// 6.根据订单编号、购物车信息,插入订单商品表
List<OrderGoods> orderGoodsList = new ArrayList<OrderGoods>();
for (int j = 0; j < cart.length; j++) {
OrderGoods orderGoods = new OrderGoods();
orderGoods.setId(UUIDGenerator.getUUID());
orderGoods.setOrdercode(ordercodeString);
orderGoods.setGoodsname(cart[j].getGoodsname());
orderGoods.setGoodscode(cart[j].getGoodscode());
orderGoods.setGoodsnum(cart[j].getGoodsnum());
orderGoods.setImgname(cart[j].getImgname());
orderGoods.setImgaccesspath(cart[j].getImgaccesspath());
orderGoods.setImgpath(cart[j].getImgpath());
orderGoods.setCreatetime(cart[j].getCreatetime());
orderGoodsList.add(orderGoods);
}
int countNum = orderGoodsService.txinsertByBatch(orderGoodsList);
// 7.商品数量比较
,如果购物车中商品数量小于库存 -401 data:[{goodscode:123,goodsname:123,goodsnum:123},{},..]
GoodsDetailJifen[] goods = new GoodsDetailJifen[ids.length];
for (int j = 0; j < ids.length; j++) {
GoodsDetailJifenExample example = new GoodsDetailJifenExample();
example.createCriteria().andGoodscodeEqualTo(cart[j].getGoodscode());
goods[j] = goodsDetailJifenMapper.selectByExample(example).get(0);
}
for (int i = 0; i < goods.length; i++) {
for (int j = 0; j < cart.length; j++) {
int r = BigDecimalCalculateUtil.compareZero
(BigDecimalCalculateUtil.subtract(new BigDecimal(cart[j].getGoodsnum()), goods[i].getGoodsnum()));
if ( r==1) {
return LBResult.build(401, "商品库存不足", goods);
}
}
}
// 8.判断订单积分
int l = BigDecimalCalculateUtil.compareZero
(BigDecimalCalculateUtil.subtract(ordersum, jifenDetailService.temSumPoint(uid)));
if ( l==1) {
return LBResult.build(402, "用户积分不足", jifenDetailService.temSumPoint(uid));
}
//9 用户积分扣减
JifenDetail point = new JifenDetail();
point.setId(UUIDGenerator.getUUID());
point.setUid(uid);
Date nowDate1 = DateUtil1.getFormatDate(new Date(),
"yyyy-MM-dd HH:mm:ss");
point.setCreatetime(nowDate1);
point.setJifentype(constants.CONSUME);
point.setJifen(BigDecimalCalculateUtil.
convertMath(ordersum));
jifenDetailService.txInsert(point);
//10 商品库存扣减
for (int i = 0; i < goods.length; i++) {
goods[i].setGoodsnum(BigDecimalCalculateUtil.subtract(goods[i].getGoodsnum(),new BigDecimal(cart[i].getGoodsnum()) ));
GoodsDetailJifen goodsDetailJifen = goods[i];
goodsDetailJifenMapper.updateByPrimaryKeySelective(goodsDetailJifen);
}
return LBResult.build(400, "成功");
}