diff --git a/src/main/java/cn/chjyj/szwh/controller/api/OrderController.java b/src/main/java/cn/chjyj/szwh/controller/api/OrderController.java index 07aaf96..e8ed2da 100644 --- a/src/main/java/cn/chjyj/szwh/controller/api/OrderController.java +++ b/src/main/java/cn/chjyj/szwh/controller/api/OrderController.java @@ -195,156 +195,4 @@ public class OrderController extends BaseController { return jsonObject; } - /** - * 查找购买流程,并返回订单编号 - * @param userIsli - * @param goodsIsli - * @param userYears - * @return - */ - private String buyFindGoods(String userIsli,String goodsIsli,int userYears){ - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - Goods goods= goodsService.getGoodsByIsli(goodsIsli); - if(goods==null){ - throw new ChException("没有该标的",400); - } - - GoodsDetail goodsDetail = goodsDetailService.getGoodsDetailBygid(goods.getGoodsDetailId()); - int count = Integer.valueOf(userYears); - if(goodsDetail.getGoodsEntrust()==1){ - count=1; - } - // 结算设置 - AccountRatioSetting accountRatioSetting = accountRatioSettingService.getUsingAccRationSetting(); - //结算分配信息 - AccountRatioDetail accountRatioDetail = accountRatioDetailService.getAccRatioDetailUnderRole(accountRatioSetting.getId(), - 2); - // 判断当前订单是否存在问题 - Order order=new Order(); - //校验订单信息合法 - SzOrderUtils.checkOrder(goods,goodsDetail,userIsli); - //服务费 - BigDecimal serviceCharge=new BigDecimal(0.00); - //订单总费用 - BigDecimal totalMoney=new BigDecimal(0.00); - int status=0;//订单状态 - int payStatus=0;//支付状态 - // 付费商品 - if(goodsDetail.getChargesType()==2){ - if(accountRatioDetail.getCalculate()==1){ - //rate 小于等于0 - if(accountRatioDetail.getRatio().compareTo(new BigDecimal(0))!=1){ - // 服务费0 - serviceCharge = new BigDecimal(0.00); - }else{ - // 购买数量* 单价*(分配比例/100) - serviceCharge =new BigDecimal(count).multiply(goodsDetail.getPrice()) - .multiply(accountRatioDetail.getRatio().divide(new BigDecimal(100))); - } - }else{ - //固定金额 - // 购买数量*单机+固定费额 - serviceCharge = new BigDecimal(count).multiply(goodsDetail.getPrice()) - .add(accountRatioDetail.getAmount()); - } - //总的额度 - totalMoney = new BigDecimal(count).multiply(goodsDetail.getPrice()) - .add(serviceCharge) - .add(goodsDetail.getEarnestMoney()); - status=1; - payStatus=1; - }else{ - // 免费 - serviceCharge =new BigDecimal(0.00); - totalMoney = new BigDecimal(0.00); - status=2; - payStatus=2; //已支付 - } - // 订单编号 - String batchCode = SzOrderUtils.createOrderBatchcode(); - // 增加到订单商品详情 - OrderGoodsDetail orderGoodsDetail = SzOrderUtils.mkOrderGoodsDetail(goodsDetail,batchCode,count,userYears); - // 补充其他未添加的属性 - orderGoodsDetail.setEntrustIslicode(goods.getUserIslicode()); - // 订单服务费 - orderGoodsDetail.setServiceCharge(serviceCharge); - //总金额 - orderGoodsDetail.setMoney(totalMoney); - orderGoodsDetail.setIslicode(goods.getIsLicode()); - // 查找用户 - OrderUser orderUser = orderUserService.getOrderUser(batchCode,goods.getIsLicode()); - int userId=0; - if(orderUser!=null){ - userId = orderUser.getId(); - }else{ - // 获取远程用户信息 - JSONObject userjson = SzwhApiUtils.getApiUser(userIsli); - if(StringUtils.isBlank(userjson.getString("data"))){ - throw new ChException("未查询到用户信息,下单失败",400); - } - //添加用户 - OrderUser odu =SzOrderUtils.mkOrderUser(batchCode,userjson,0); - orderUserService.addOrderUser(odu); - userId = odu.getId(); - } - - // 订单实体 - order.setBatchcode(batchCode); - order.setUserId(userId); - order.setBuyIslicode(userIsli); - order.setTotalServiceCharge(serviceCharge); - order.setTotalMoney(totalMoney); - order.setType(1); - order.setStatus(status); - order.setPayStatus(payStatus); - // 分配设置编号 - order.setAccountRatioId(accountRatioSetting.getId()); - //如果是付费 - if(goodsDetail.getChargesType()==1){ - order.setPaymenttime(new Date()); - } - order.setCreatetime(new Date()); - // 订单信息入库 - int ret = orderService.addOrder(order); - if(ret==1){ - //if(status==2){ - // 更新订单信息 - //payService.payFinishOperate(batchCode); - // } - // 用户账单 - UserAccountBill userAccountBill = new UserAccountBill(); - userAccountBill.setUserIsli(userIsli); - userAccountBill.setBatchcode(batchCode); - userAccountBill.setOrderUserId(userId); - userAccountBill.setServiceCharge(serviceCharge); - userAccountBill.setThatdayBuyMoney(totalMoney); - // save user's bill - userAccountBillService.addUserAccBill(userAccountBill); - //调整库存 - if(goodsDetail.getGoodsEntrust()==1 || goodsDetail.getContractualPeriod()==2 && goodsDetail.getGoodsEntrust()!=2){ - int gdret = goodsDetailService.decGoodsDetailStock(goodsDetail.getId()); - if(gdret==1){ - String status_Str=""; - if(goodsDetail.getGoodsEntrust() ==1){ - goodsService.changeGoodsStatus(5,goods.getId()); - status_Str="暂停"; - }else { - goodsService.changeGoodsStatus(2,goods.getId()); - status_Str="下架"; - } - //更新订单用户状态信息 - String buyUserName = orderUser.getName(); - OperationLog oplog =new OperationLog(); - oplog.setType("goods"); - oplog.setLogid(goods.getId()); - oplog.setMessage(sdf.format(new Date())+"用户"+buyUserName+"下单,"+status_Str+"该委托标的"); - operationLogService.addLog(oplog); - return batchCode; - }else{ - throw new ChException("标的已卖完",400); - } - } - } - return batchCode; - } } diff --git a/src/main/java/cn/chjyj/szwh/service/OrderService.java b/src/main/java/cn/chjyj/szwh/service/OrderService.java index 20ee651..a5ae117 100644 --- a/src/main/java/cn/chjyj/szwh/service/OrderService.java +++ b/src/main/java/cn/chjyj/szwh/service/OrderService.java @@ -92,7 +92,7 @@ public interface OrderService { * @param userYears * @return */ - public String buyFindGoods(String userIsli,String goodsIsli,int userYears); + public Map buyFindGoods(String userIsli,String goodsIsli,int userYears); /** * 查询订单 diff --git a/src/main/java/cn/chjyj/szwh/service/impl/OrderServiceImpl.java b/src/main/java/cn/chjyj/szwh/service/impl/OrderServiceImpl.java index a8e50f0..5f96091 100644 --- a/src/main/java/cn/chjyj/szwh/service/impl/OrderServiceImpl.java +++ b/src/main/java/cn/chjyj/szwh/service/impl/OrderServiceImpl.java @@ -528,13 +528,21 @@ public class OrderServiceImpl implements OrderService { return orderMapper.addOrder(order); } + /** + * 查找购买流程,并返回订单编号 + * @param userIsli + * @param goodsIsli + * @param userYears + * @return + */ @Override - public String buyFindGoods(String userIsli, String goodsIsli, int userYears) { + public Map buyFindGoods(String userIsli, String goodsIsli, int userYears) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Map bretMap = new HashMap<>();// 返回的结果 Goods goods = goodsMapper.getGoodsByIsli(goodsIsli); if(goods==null){ - log.error("create order error!"+goodsIsli+""); - return null; + log.error("create order error!"+goodsIsli+",不存在"); + bretMap.put("error","没有该标的"); } // goods detail GoodsDetail goodsDetail = goodsDetailMapper.getGoodsDetailBygid(goods.getGoodsDetailId()); @@ -554,11 +562,11 @@ public class OrderServiceImpl implements OrderService { // 检查订单支付情况 if(orderDetailVo!=null) { if (orderDetailVo.getStatus() == 1) { - throw new ChException("您的待付款订单中有标的:" + goods.getGoodsIslicode() + ",请勿重复下单", 400); + bretMap.put("error","您的待付款订单中有标的:" + goods.getGoodsIslicode() + ",请勿重复下单"); } else if (orderDetailVo.getStatus() == 2) { - throw new ChException("您的未完成订单中有标的:" + goods.getGoodsIslicode() + ",请勿重复下单", 400); + bretMap.put("error","您的未完成订单中有标的:" + goods.getGoodsIslicode() + ",请勿重复下单"); } else { - throw new ChException("您的已完成订单中有标的:" + goods.getGoodsIslicode() + ",请勿重复下单", 400); + bretMap.put("error","您的已完成订单中有标的:" + goods.getGoodsIslicode() + ",请勿重复下单"); } } // 判断当前订单是否存在问题 @@ -590,8 +598,8 @@ public class OrderServiceImpl implements OrderService { payStatus=1; }else{ // 免费 - serviceCharge =new BigDecimal(0.00); - totalMoney = new BigDecimal(0.00); + serviceCharge =new BigDecimal("0.00"); + totalMoney = new BigDecimal("0.00"); status=2; payStatus=2; //已支付 } @@ -615,9 +623,8 @@ public class OrderServiceImpl implements OrderService { // 获取远程用户信息 JSONObject userjson = SzwhApiUtils.getApiUser(userIsli); if(StringUtils.isBlank(userjson.getString("data"))){ - log.error("未查询到用户信息,下单失败"); - return null; - //throw new ChException("未查询到用户信息,下单失败",400); + log.error("未查询到用户信息,下单失败"+userIsli); + bretMap.put("error","未查询到用户信息,下单失败"); } //添加用户 OrderUser odu =SzOrderUtils.mkOrderUser(batchCode,userjson,0); @@ -663,15 +670,16 @@ public class OrderServiceImpl implements OrderService { if(gdret==1){ String status_Str=""; // 更新goods 状态 - Map gmap = new HashMap(); - gmap.put("goods_status", status); + Map gstmap = new HashMap(); + int gsstatus=2; if(goodsDetail.getGoodsEntrust() ==1){ - goodsMapper..changeGoodsStatus(5,goods.getId()); + gsstatus=5; status_Str="暂停"; }else { - goodsService.changeGoodsStatus(2,goods.getId()); status_Str="下架"; } + gstmap.put("goods_status", gsstatus); + goodsMapper.updateGoods(gstmap,goods.getId()); //更新订单用户状态信息 String buyUserName = orderUser.getName(); OperationLog oplog =new OperationLog(); @@ -679,13 +687,14 @@ public class OrderServiceImpl implements OrderService { oplog.setLogid(goods.getId()); oplog.setMessage(sdf.format(new Date())+"用户"+buyUserName+"下单,"+status_Str+"该委托标的"); operationLogMapper.addLog(oplog); - return batchCode; + bretMap.put("batchcode",batchCode); }else{ - throw new ChException("标的已卖完",400); + log.error(goodsIsli+"标的已卖完"); + bretMap.put("error","标的已卖完"); } } } - return batchCode; + return bretMap; } /** @@ -786,14 +795,32 @@ public class OrderServiceImpl implements OrderService { retmap.put("code","error"); retmap.put("msg","用户不存在"); } - if(jsonArray.size()==1){ + // 该用户的购物车 + String rkey="car_"+userIsli; + if(jsonArray.size()==1){ // 下单商品只有一个 JSONObject tmpgjson = jsonArray.getJSONObject(0); - String batchcode = buyFindGoods(userIsli,tmpgjson.getString("goods_islid")) + String tuseYear = tmpgjson.getString("use_year"); + //todo check it has null + Integer ituseYear = Integer.valueOf(tuseYear); + String goodsIsli = tmpgjson.getString("goods_isli"); + Map bmap = buyFindGoods(userIsli,goodsIsli,ituseYear); + //检查是否有错误信息 + String berrmsg = (String)bmap.get("error"); + if(StringUtils.isNotEmpty(berrmsg)){ + retmap.put("code",400); + retmap.put("msg",berrmsg); + } + //返回订单编号 + String batchcode = (String)bmap.get("batcha"); + if(StringUtils.isNotEmpty(batchcode)){ + // 从购物车中删除该goodsisli + SzOrderUtils.delOdGoodsIsliFromCar(rkey,goodsIsli); + } + }else{ + // } - String ureidsKey="car_"+userIsli; - //查询购物车 - Object shopCar = RedisUtil.get(ureidsKey); - return null; + + return retmap; }