package com.platform.yijia.controller; import com.alibaba.fastjson.JSONObject; import com.google.gson.Gson; import com.platform.yijia.dao.PayOrderMapper; import com.platform.yijia.param.request.PayOrderRequest; import com.platform.yijia.pojo.*; import com.platform.yijia.service.*; import com.platform.yijia.utils.CodeMsg; import com.platform.yijia.utils.ResultData; import com.platform.yijia.utils.weixinapp.WxPushUtil; import me.chanjar.weixin.mp.bean.template.WxMpTemplateData; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.javassist.util.HotSwapAgent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.naming.ldap.HasControls; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.*; @Controller @RequestMapping("/demo") public class PayOrderController { private static Logger logger =(Logger) LoggerFactory.getLogger(PayOrderController.class); @Resource private PayOrderService payOrderService; @Resource private AppUserInfoService appUserInfoService; @Resource private StationService stationService; @Resource private CustomerGradeServices customerGradeServices; @Resource private PayOrderMapper payOrderMapper; @Resource private ElectronicMembershipCardService electronicMembershipCardService; //小程序支付生成订单 @RequestMapping(value = "/AddPayOrderInfoNew", consumes = "application/json", method = RequestMethod.POST) @ResponseBody @Transactional public String AddPayOrderInfoNew(@RequestBody PayOrderRequest payOrderRequest){ logger.info("获取订单前端传来参数openId:"+ payOrderRequest.getOpenId()); logger.info("获取订单前端传来参数userType:"+ payOrderRequest.getUserType()); Gson gson =new Gson(); ResultData resultData =null; AppUserInfo appUserInfo =new AppUserInfo(); appUserInfo.setUserType(payOrderRequest.getUserType()); //订单类型 是小程序还是公众号 if(payOrderRequest.getUserType().equals("1")){ appUserInfo.setBlogOpenid(payOrderRequest.getOpenId()); //1 是公众号 }else if(payOrderRequest.getUserType().equals("2")){ appUserInfo.setMinaOpenid(payOrderRequest.getOpenId()); //2是小程序 } List appUserInfoList =appUserInfoService.Authentication(appUserInfo); Map resultMap = new HashMap<>(); //返回结果集 //判断登录人员是否验证成功 if(appUserInfoList!=null && appUserInfoList.size()>0){ PayOrder payOrder = new PayOrder(); payOrder.setConsumer(appUserInfoList.get(0).getBlogNickName()); //昵称 payOrder.setConsumerId(appUserInfoList.get(0).getUserId()); //用户id Random random = new Random(); String result=""; for(int i=0;i<6;i++){ result+=random.nextInt(10); } String ordNo=System.nanoTime()+result; payOrder.setOrderNo(ordNo); //订单号 logger.info("生成的订单号:" + ordNo); payOrder.setDiscountCouponAmt(payOrderRequest.getDiscountCouponAmt()); //优惠劵金额 payOrder.setDiscountCoupon(payOrderRequest.getDiscountCoupon()); //优惠券 payOrder.setCarNo(payOrderRequest.getCarNo()); //车牌号、 payOrder.setCustomerGrade(payOrderRequest.getCustomerGrade()); //客户电话 payOrder.setMemberNo(payOrderRequest.getMemberNo()); //会员卡号 payOrder.setMemberAmt(payOrderRequest.getMemberAmt()); //会员支付金额 //payOrder.setPrintCount(payOrderRequest.getPrintCount()); //小票数量 payOrder.setScore(payOrderRequest.getScore()); //积分 payOrder.setStationId(payOrderRequest.getStationId()); //油站id payOrder.setStatus("0"); //创建订单 未支付状态 0 if(StringUtils.isNotBlank(payOrderRequest.getUserType())){ String userType = payOrderRequest.getUserType(); //支付方式:02公众号,03小程序', switch (userType){ case "1": payOrder.setPayWay("02"); break; case "2": payOrder.setPayWay("03"); break; } } payOrder.setCreatedDate(new Date()); //创建时间 payOrder.setOrderType(payOrderRequest.getOrderType()); //订单类型 payOrder.setOilGun(payOrderRequest.getOilGun()); //油枪号 //根据油站和油枪获取加油员 HashMap params = new HashMap<>(); params.put("oliGunNo", payOrderRequest.getOilGun()); params.put("stationId", payOrderRequest.getStationId()); String oilPersonnel = ""; List mapResultList = payOrderMapper.selectPersonelName(params); //使用list原因:一个枪号对应多个加油员场景 logger.info("加油员信息:"+ mapResultList.toString()); if(mapResultList != null && mapResultList.size() > 0){ for (Map per : mapResultList){ if(per.containsKey("personelName") && per.get("personelName").toString() !=null && per.get("personelName").toString() !=""){ oilPersonnel += per.get("personelName").toString() +","; } } if(oilPersonnel !=""){ payOrder.setOilPersonnel(oilPersonnel.substring(0, oilPersonnel.length()-1)); //加油员 } } String orderType = payOrderRequest.getOrderType(); //获取订单类型 logger.info("订单类型:" + orderType); if(orderType.equals("1")){ //订单类型是油品时计算 //resultMap = this.calculateAmt(payOrderRequest); //计算油价 加油升数 优惠金额 实收金额 优惠价格 resultMap = this.calculateAmtNew(payOrderRequest); //计算油价 加油升数 优惠金额 实收金额 优惠价格 payOrder.setOilPirce(resultMap.get("oilPrice").toString()); payOrder.setOrderLiters(resultMap.get("oilLiters").toString()); payOrder.setDiscountAmt(Double.valueOf(resultMap.get("discountAmt").toString())); payOrder.setAmt(Double.valueOf(resultMap.get("amt").toString())); String stationCardEnabledFlag = electronicMembershipCardService.getStationCardEnabledFlag(payOrderRequest.getStationId()); logger.info("油站是否开启电子会员卡功能: " +stationCardEnabledFlag); //查询油站是否开启电子会员卡功能 if(stationCardEnabledFlag.equals("1")){ Map map = this.selectCardBalance(payOrderRequest.getStationId(), payOrderRequest.getOilName(), appUserInfoList.get(0).getUnionId()); resultMap.put("balance", map.get("balance")); //电子卡余额 resultMap.put("cardOilsType", map.get("cardOilsType")); //电子卡类型1.汽油; 2.柴油 } } payOrder.setReceivableAmt(payOrderRequest.getReceivableAmt()); //订单应收金额 String oilName =payOrderRequest.getOilName(); payOrder.setOilName(oilName); //油品名称 switch (oilName){ case "92#": payOrder.setOilType("2"); break; case "95#": payOrder.setOilType("2"); break; case "97#": payOrder.setOilType("2"); break; case "0#": payOrder.setOilType("1"); break; case "-10#": payOrder.setOilType("1"); break; case "-20#": payOrder.setOilType("1"); break; } payOrder.setOrderType(orderType); StationInfo stationInfo = new StationInfo(); stationInfo.setStationId(payOrderRequest.getStationId()); StationInfo stationInfos = stationService.selectStationInfo(stationInfo); if(stationInfos !=null){ payOrder.setStationName(stationInfos.getStationName()); //获取油站图片和名称 resultMap.put("stationPic", stationInfos.getStationPic()); resultMap.put("stationName", stationInfos.getStationName()); } //payOrder.setPayType("wx"); //支付类型 switch (orderType){ case "1": //油品 payOrderMapper.insertSelective(payOrder); resultMap.put("ordNo", ordNo); break; case "2": //非油品 payOrder.setAmt(Double.valueOf(payOrderRequest.getReceivableAmt())); //非油品金额 payOrder.setDiscountAmt(Double.valueOf(0)); //优惠金额 payOrder.setOilPirce(payOrderRequest.getReceivableAmt().toString()); //非油品价格 payOrder.setOrderLiters("0"); resultMap.put("discountAmt", "0"); //优惠价格 resultMap.put("amt", payOrderRequest.getReceivableAmt()); //实收价格 resultMap.put("receivableAmt", payOrderRequest.getReceivableAmt()); //应收价格 resultMap.put("oilLiters", "0"); resultMap.put("discountPrice", "0"); payOrderMapper.insertSelective(payOrder); resultMap.put("ordNo", ordNo); break; } resultData = ResultData.success(resultMap); }else{ //登录人验证失败 resultData=ResultData.error(CodeMsg.USER_NOT_EXSIST); } return gson.toJson(resultData); } //查询用户电子会员卡余额 public Map selectCardBalance(Integer stationId, String oilName, String unionId) { String cardOilsType =""; switch (oilName){ //根据油品名称存储电子卡油品类型 1.柴油 2 汽油 case "92#": cardOilsType ="1"; break; case "95#": cardOilsType ="1"; break; case "97#": cardOilsType ="1"; break; case "0#": cardOilsType ="2"; break; case "-10#": cardOilsType ="2"; break; case "-20#": cardOilsType ="2"; break; } CustomerElectronicCard customerElectronicCard = new CustomerElectronicCard(); customerElectronicCard.setCardOilsType(cardOilsType); customerElectronicCard.setStationId(stationId); customerElectronicCard.setUnionId(unionId); CustomerElectronicCard electronicCardInfo = electronicMembershipCardService.getElectronicCardInfoByUnionId(customerElectronicCard); BigDecimal balance = null; String cardOilsType1 =""; if(electronicCardInfo !=null){ balance = electronicCardInfo.getAmt(); cardOilsType1 = electronicCardInfo.getCardOilsType(); } Map result = new HashMap<>(); result.put("balance",balance.toString()); result.put("cardOilsType",cardOilsType1); return result; } /*** * 计算订单金额 * @return */ public Map calculateAmt(PayOrderRequest payOrderRequest){ Map resultMap = new HashMap<>(); if(payOrderRequest.getReceivableAmt() !=null){ //应收金额 Double receivableAmt = payOrderRequest.getReceivableAmt(); //应收金额 用户输入的金额 Integer stationId = payOrderRequest.getStationId(); //油站ID //String oilGun = payOrderRequest.getOilGun(); //油枪号 String oilName = payOrderRequest.getOilName(); //油品名称 String openId = payOrderRequest.getOpenId(); //用户的openId String mobilePhone = payOrderRequest.getCustomerPhone(); //客户手机号 //获取油品价格 StationOilPrice stationOilPrice = new StationOilPrice(); stationOilPrice.setStationId(stationId); stationOilPrice.setOilName(oilName); StationOilPrice oilPriceInfo = stationService.getStationOilPrice(stationOilPrice); String oilPrice = oilPriceInfo.getOilPrice(); //String stationNanme = oilPriceInfo.getStationNanme(); BigDecimal oilLiters = null; BigDecimal discountAmt =null; BigDecimal discountPrice = null; BigDecimal amt = null; //resultMap.put("stationName", stationNanme); resultMap.put("oilPrice", oilPrice); //油品价格 resultMap.put("receivableAmt", receivableAmt); //应收价格 //该油站的优惠方式 String stationDiscountWay = stationService.getStationDiscountWay(stationId.toString()); Map params = new HashMap(); params.put("stationId", stationId); params.put("oilName", oilName); BigDecimal receivableAmt_b = new BigDecimal(receivableAmt); //传入的应收金额 switch (stationDiscountWay){ case "1": //1.等级直降 //查询等级直降的优惠方式 params.put("mobilePhone", mobilePhone); params.put("minaOpenid", openId); //查询客户等级信息 List> customerGradeInfo = customerGradeServices.getCustomerGradeInfo(params); if(customerGradeInfo != null && customerGradeInfo.size() >0){ //已存在客户等级信息 //使用客户已存在的等级计算优惠 oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); discountAmt = oilLiters.multiply(new BigDecimal(customerGradeInfo.get(0).get("gasoilDiscountLitre").toString())); discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP); amt = receivableAmt_b.subtract(discountAmt); discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); resultMap.put("discountAmt", discountAmt.doubleValue()); resultMap.put("amt", amt.doubleValue()); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", discountPrice.doubleValue()); }else { //不存在客户等级信息时,查询油站的客户等级直降优惠(查询结果根据会员成长值条件做升序查询) List customerGradeList = customerGradeServices.getCustomerGradeList(params); //如果存在 if(customerGradeList !=null && customerGradeList.size() >0){ //用户第一次购买 取等级最低初始会员的优惠条件计算 oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); //加油升数 discountAmt = oilLiters.multiply(new BigDecimal(customerGradeList.get(0).get("gasoilDiscountLitre").toString())); discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP); amt = receivableAmt_b.subtract(discountAmt); discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); resultMap.put("discountAmt", discountAmt.doubleValue()); resultMap.put("amt", amt.doubleValue()); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", discountPrice.doubleValue()); }else { //不存在 resultMap.put("discountAmt", 0); resultMap.put("amt", receivableAmt.doubleValue()); oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", oilPrice); } } break; case "2": //2.满减 params.put("discountPlanType", "1"); //营销方案 List customerMarkertPlanList_1 = customerGradeServices.getCustomerMarkertPlanList(params); //有优惠规则 if(customerMarkertPlanList_1 !=null && customerMarkertPlanList_1.size() >0){ boolean flag = true; for (Map m : customerMarkertPlanList_1){ //如果当前应收金额大于条件金额 BigDecimal discountAmt_b = new BigDecimal(m.get("discountAmt").toString()); //条件金额 if(receivableAmt_b.compareTo(discountAmt_b) ==1 || receivableAmt_b.compareTo(discountAmt_b) ==0){ BigDecimal gasoilDiscountAmt_b = new BigDecimal(m.get("gasoilDiscountAmt").toString()); //每满多少优惠多少的金额 discountAmt = receivableAmt_b.divide(discountAmt_b, 2, BigDecimal.ROUND_HALF_UP).multiply(gasoilDiscountAmt_b); //优惠金额 discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP); amt = receivableAmt_b.subtract(discountAmt); //实收金额 oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数 discountPrice = amt.divide(oilLiters); //优惠价格 resultMap.put("discountAmt", discountAmt.doubleValue()); resultMap.put("amt", amt.doubleValue()); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", discountPrice.doubleValue()); flag = false; break; } } if(flag){ resultMap.put("discountAmt", 0); resultMap.put("amt", receivableAmt.doubleValue()); oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", oilPrice); } }else { //没有优惠规则 resultMap.put("discountAmt", 0); resultMap.put("amt", receivableAmt.doubleValue()); oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", oilPrice); } break; case "3": //3.立减 params.put("discountPlanType", "2"); //营销方案 List customerMarkertPlanList_2 = customerGradeServices.getCustomerMarkertPlanList(params); if(customerMarkertPlanList_2 !=null && customerMarkertPlanList_2.size() >0){ boolean flag = true; for (Map m : customerMarkertPlanList_2){ //如果当前应收金额大于条件金额 BigDecimal discountAmt_b = new BigDecimal(m.get("discountAmt").toString()); //条件金额 if(receivableAmt_b.compareTo(discountAmt_b) ==1 || receivableAmt_b.compareTo(discountAmt_b) ==0){ BigDecimal gasoilDiscountAmt_b = new BigDecimal(m.get("gasoilDiscountAmt").toString()); //每满多少优惠多少的金额 discountAmt = gasoilDiscountAmt_b; //优惠金额 amt = receivableAmt_b.subtract(gasoilDiscountAmt_b); //实收金额 oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); //加油升数 discountPrice = amt.divide(oilLiters); //优惠价格 resultMap.put("discountAmt", discountAmt.doubleValue()); resultMap.put("amt", amt.doubleValue()); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", discountPrice.doubleValue()); flag = false; break; } } if(flag){ //用户加油金额不符合优惠条件 resultMap.put("discountAmt", 0); resultMap.put("amt", receivableAmt.doubleValue()); oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", oilPrice); } }else { //没有优惠规则 resultMap.put("discountAmt", 0); resultMap.put("amt", receivableAmt.doubleValue()); oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", oilPrice); } break; case "4": //4.独立直降 params.put("discountPlanType", "3"); //营销方案 List customerMarkertPlanList_3 = customerGradeServices.getCustomerMarkertPlanList(params); if(customerMarkertPlanList_3 !=null && customerMarkertPlanList_3.size() >0){ boolean flag= true; for(Map m : customerMarkertPlanList_3){ BigDecimal discountAmt_b = new BigDecimal(m.get("discountAmt").toString()); //条件金额 BigDecimal gasoilDiscountAmt_b = new BigDecimal(m.get("gasoilDiscountAmt").toString()); oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); //加油升数 if(m.get("discountTerm").toString().equals("1")){ //按升数优惠 if(oilLiters.compareTo(discountAmt_b) ==1 || oilLiters.compareTo(discountAmt_b) ==0){ //计算 discountAmt = oilLiters.multiply(gasoilDiscountAmt_b); discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP); amt = receivableAmt_b.subtract(discountAmt); discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); //优惠价格 resultMap.put("discountAmt", discountAmt.doubleValue()); resultMap.put("amt", amt.doubleValue()); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", discountPrice.doubleValue()); flag =false; break; } }else if(m.get("discountTerm").toString().equals("2")){ //按金额优惠 if(receivableAmt_b.compareTo(discountAmt_b) ==1 || receivableAmt_b.compareTo(discountAmt_b) ==0){ discountAmt = oilLiters.multiply(gasoilDiscountAmt_b); discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP); amt = receivableAmt_b.subtract(discountAmt); discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); //优惠价格 resultMap.put("discountAmt", discountAmt.doubleValue()); resultMap.put("amt", amt.doubleValue()); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", discountPrice.doubleValue()); flag =false; break; } } } if(flag){ resultMap.put("discountAmt", 0); resultMap.put("amt", receivableAmt.doubleValue()); oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", oilPrice); } }else { //没有优惠规则 resultMap.put("discountAmt", 0); resultMap.put("amt", receivableAmt.doubleValue()); oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", oilPrice); } break; } //resultData = ResultData.success(resultMap); } return resultMap; } /* * 修订优惠计算: 2021年4月20日15:31:30 * 修订内容 : * 修改为目前只有三种优惠:根据立减、满减、独立直降; * 注释:优惠为 1 是:表示不走满减、立减、独立直降优惠,只走等级直降的优惠; * 条件规则: * 一,优先满减(只有一档,不存在多档!)、立减(只有一档,不存在多档!)、独立直降(只有一档) * 二,若用户消费门槛 >= 优惠门槛,且启用优惠叠加时,则计算优惠规则和等级优惠规则; * 若用户消费门槛 >= 优惠门槛,且不启用优惠叠加时,则只计算优惠规则; * 若用户消费门槛 < 优惠门槛,且启用优惠叠加时,则只计算等级规则; * 若用户消费门槛 < 优惠门槛,且不启用优惠叠加时,则只计算等级规则; * */ public Map calculateAmtNew(PayOrderRequest payOrderRequest){ Map resultMap = new HashMap<>(); if(payOrderRequest.getReceivableAmt() !=null){ //应收金额 Double receivableAmt = payOrderRequest.getReceivableAmt(); //应收金额 用户输入的金额 Integer stationId = payOrderRequest.getStationId(); //油站ID //String oilGun = payOrderRequest.getOilGun(); //油枪号 String oilName = payOrderRequest.getOilName(); //油品名称 String openId = payOrderRequest.getOpenId(); //用户的openId String mobilePhone = payOrderRequest.getCustomerPhone(); //客户手机号 //获取油品价格 StationOilPrice stationOilPrice = new StationOilPrice(); stationOilPrice.setStationId(stationId); stationOilPrice.setOilName(oilName); StationOilPrice oilPriceInfo = stationService.getStationOilPrice(stationOilPrice); String oilPrice = oilPriceInfo.getOilPrice(); //String stationNanme = oilPriceInfo.getStationNanme(); BigDecimal oilLiters = null; BigDecimal discountAmt =null; BigDecimal discountPrice = null; BigDecimal amt = null; //resultMap.put("stationName", stationNanme); resultMap.put("oilPrice", oilPrice); //油品价格 resultMap.put("receivableAmt", receivableAmt); //应收价格 //该油站的优惠方式 String stationDiscountWay = stationService.getStationDiscountWay(stationId.toString()); Map params = new HashMap(); params.put("stationId", stationId); params.put("oilName", oilName); params.put("receivableAmt", receivableAmt); params.put("oilPrice", oilPrice); params.put("mobilePhone", mobilePhone); params.put("minaOpenid", openId); BigDecimal receivableAmt_b = new BigDecimal(receivableAmt); //传入的应收金额 switch (stationDiscountWay){ case "1": //1.等级直降 //查询等级直降的优惠方式 //查询客户等级信息 List> customerGradeInfo = customerGradeServices.getCustomerGradeInfo(params); if(customerGradeInfo != null && customerGradeInfo.size() >0){ //已存在客户等级信息 //使用客户已存在的等级计算优惠 oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); discountAmt = oilLiters.multiply(new BigDecimal(customerGradeInfo.get(0).get("gasoilDiscountLitre").toString())); discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP); amt = receivableAmt_b.subtract(discountAmt); discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); resultMap.put("discountAmt", discountAmt.doubleValue()); resultMap.put("amt", amt.doubleValue()); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", discountPrice.doubleValue()); }else { //不存在客户等级信息时,查询油站的客户等级直降优惠(查询结果根据会员成长值条件做升序查询) List customerGradeList = customerGradeServices.getCustomerGradeList(params); //如果存在 if(customerGradeList !=null && customerGradeList.size() >0){ //用户第一次购买 取等级最低初始会员的优惠条件计算 oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); //加油升数 discountAmt = oilLiters.multiply(new BigDecimal(customerGradeList.get(0).get("gasoilDiscountLitre").toString())); discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP); amt = receivableAmt_b.subtract(discountAmt); discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); resultMap.put("discountAmt", discountAmt.doubleValue()); resultMap.put("amt", amt.doubleValue()); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", discountPrice.doubleValue()); }else { //不存在 resultMap.put("discountAmt", 0); resultMap.put("amt", receivableAmt.doubleValue()); oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", oilPrice); } } break; case "2": //2.满减 params.put("discountPlanType", "1"); //营销方案 List customerMarkertPlanList_1 = customerGradeServices.getCustomerMarkertPlanList(params); //有优惠规则 if(customerMarkertPlanList_1 !=null && customerMarkertPlanList_1.size() >0){ //是否共享会员优惠叠加: 1 代表是; 2 代表否; String vipDiscountyPlus = customerMarkertPlanList_1.get(0).get("vipDiscountyPlus").toString(); //如果当前应收金额大于条件金额 BigDecimal discountAmt_b = new BigDecimal(customerMarkertPlanList_1.get(0).get("discountAmt").toString()); //条件金额 if(receivableAmt_b.compareTo(discountAmt_b) ==1 || receivableAmt_b.compareTo(discountAmt_b) ==0){ BigDecimal gasoilDiscountAmt_b = new BigDecimal(customerMarkertPlanList_1.get(0).get("gasoilDiscountAmt").toString()); //每满多少优惠多少的金额 discountAmt = receivableAmt_b.divide(discountAmt_b, 2, BigDecimal.ROUND_HALF_UP).multiply(gasoilDiscountAmt_b); //优惠金额 discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP); amt = receivableAmt_b.subtract(discountAmt); //实收金额 oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数 discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); //discountPrice = amt.divide(oilLiters,2,BigDecimal.ROUND_HALF_UP); //优惠价格 switch (vipDiscountyPlus){ case "1": Map map = this.calcuteDengJiZhiJiang(params); discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString())); amt = amt.subtract(discountAmt); discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); resultMap.put("discountAmt", discountAmt.doubleValue()); resultMap.put("amt", amt.doubleValue()); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", discountPrice.doubleValue()); break; case "2": resultMap.put("discountAmt", discountAmt.doubleValue()); resultMap.put("amt", amt.doubleValue()); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", discountPrice.doubleValue()); break; } }else{ switch (vipDiscountyPlus){ case "1": Map map = this.calcuteDengJiZhiJiang(params); discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString())); amt = amt.subtract(discountAmt); //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); resultMap.put("discountAmt", discountAmt.doubleValue()); resultMap.put("amt", amt.doubleValue()); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", discountPrice.doubleValue()); break; case "2": resultMap.put("discountAmt", discountAmt.doubleValue()); resultMap.put("amt", amt.doubleValue()); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", discountPrice.doubleValue()); break; } } }else { //用户没有设置优惠规则时 Map map = this.calcuteDengJiZhiJiang(params); resultMap.put("discountAmt", map.get("discountAmt").toString()); resultMap.put("amt", map.get("amt").toString()); resultMap.put("oilLiters", map.get("oilLiters").toString()); resultMap.put("discountPrice", map.get("discountPrice").toString()); } break; case "3": //3.立减 params.put("discountPlanType", "2"); //营销方案 List customerMarkertPlanList_2 = customerGradeServices.getCustomerMarkertPlanList(params); //是否共享会员优惠叠加: 1 代表是; 2 代表否; String vipDiscountyPlus = customerMarkertPlanList_2.get(0).get("vipDiscountyPlus").toString(); if(customerMarkertPlanList_2 !=null && customerMarkertPlanList_2.size() >0){ //如果当前应收金额大于条件金额 BigDecimal discountAmt_b = new BigDecimal(customerMarkertPlanList_2.get(0).get("discountAmt").toString()); //条件金额 if(receivableAmt_b.compareTo(discountAmt_b) ==1 || receivableAmt_b.compareTo(discountAmt_b) ==0){ BigDecimal gasoilDiscountAmt_b = new BigDecimal(customerMarkertPlanList_2.get(0).get("gasoilDiscountAmt").toString()); //满多少优惠多少的金额 discountAmt = gasoilDiscountAmt_b; //优惠金额 amt = receivableAmt_b.subtract(gasoilDiscountAmt_b); //实收金额 oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); //加油升数 discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); //优惠价格 switch (vipDiscountyPlus){ case "1": Map map = this.calcuteDengJiZhiJiang(params); discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString())); amt = amt.subtract(discountAmt); discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); resultMap.put("discountAmt", discountAmt.doubleValue()); resultMap.put("amt", amt.doubleValue()); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", discountPrice.doubleValue()); break; case "2": resultMap.put("discountAmt", discountAmt.doubleValue()); resultMap.put("amt", amt.doubleValue()); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", discountPrice.doubleValue()); break; } }else { //不满足优惠条件 switch (vipDiscountyPlus) { case "1": Map map = this.calcuteDengJiZhiJiang(params); discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString())); amt = amt.subtract(discountAmt); discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); resultMap.put("discountAmt", discountAmt.doubleValue()); resultMap.put("amt", amt.doubleValue()); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", discountPrice.doubleValue()); break; case "2": resultMap.put("discountAmt", discountAmt.doubleValue()); resultMap.put("amt", amt.doubleValue()); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", discountPrice.doubleValue()); break; } } }else { //用户没有设置优惠规则时 Map map = this.calcuteDengJiZhiJiang(params); resultMap.put("discountAmt", map.get("discountAmt").toString()); resultMap.put("amt", map.get("amt").toString()); resultMap.put("oilLiters", map.get("oilLiters").toString()); resultMap.put("discountPrice", map.get("discountPrice").toString()); } break; case "4": //4.独立直降 params.put("discountPlanType", "3"); //营销方案 List customerMarkertPlanList_3 = customerGradeServices.getCustomerMarkertPlanList(params); if(customerMarkertPlanList_3 !=null && customerMarkertPlanList_3.size() >0){ boolean flag= true; //独立直降时会存在多条 for(Map m : customerMarkertPlanList_3){ BigDecimal discountAmt_b = new BigDecimal(m.get("discountAmt").toString()); //条件金额 BigDecimal gasoilDiscountAmt_b = new BigDecimal(m.get("gasoilDiscountAmt").toString()); oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); //加油升数 if(m.get("discountTerm").toString().equals("1")){ //按升数优惠 if(oilLiters.compareTo(discountAmt_b) ==1 || oilLiters.compareTo(discountAmt_b) ==0){ //计算 discountAmt = oilLiters.multiply(gasoilDiscountAmt_b); discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP); amt = receivableAmt_b.subtract(discountAmt); discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); //优惠价格 //是否共享会员优惠叠加 String vipDiscountyPlus_3 = m.get("vipDiscountyPlus").toString(); if(vipDiscountyPlus_3.equals("1")){ Map map = this.calcuteDengJiZhiJiang(params); discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString())); amt = amt.subtract(discountAmt); discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); } resultMap.put("discountAmt", discountAmt.doubleValue()); resultMap.put("amt", amt.doubleValue()); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", discountPrice.doubleValue()); flag =false; break; } }else if(m.get("discountTerm").toString().equals("2")){ //按金额优惠 if(receivableAmt_b.compareTo(discountAmt_b) ==1 || receivableAmt_b.compareTo(discountAmt_b) ==0){ discountAmt = oilLiters.multiply(gasoilDiscountAmt_b); discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP); amt = receivableAmt_b.subtract(discountAmt); discountPrice = oilLiters !=new BigDecimal("0.00") ? amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP) : new BigDecimal(oilPrice); //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); //优惠价格 //是否共享会员优惠叠加 String vipDiscountyPlus_3 = m.get("vipDiscountyPlus").toString(); if(vipDiscountyPlus_3.equals("1")){ Map map = this.calcuteDengJiZhiJiang(params); discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString())); amt = amt.subtract(discountAmt); discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); } resultMap.put("discountAmt", discountAmt.doubleValue()); resultMap.put("amt", amt.doubleValue()); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", discountPrice.doubleValue()); flag =false; break; } } } //如果用户加油金额都不符合优惠门槛则计算等级的优惠 if(flag){ Map map = this.calcuteDengJiZhiJiang(params); resultMap.put("discountAmt", map.get("discountAmt").toString()); resultMap.put("amt", map.get("amt").toString()); resultMap.put("oilLiters", map.get("oilLiters").toString()); resultMap.put("discountPrice", map.get("discountPrice").toString()); } }else { //用户没有设置优惠规则时 Map map = this.calcuteDengJiZhiJiang(params); resultMap.put("discountAmt", map.get("discountAmt").toString()); resultMap.put("amt", map.get("amt").toString()); resultMap.put("oilLiters", map.get("oilLiters").toString()); resultMap.put("discountPrice", map.get("discountPrice").toString()); } break; } } return resultMap; } //计算等级直降 public Map calcuteDengJiZhiJiang(Map params){ Double receivableAmt = Double.valueOf(params.get("receivableAmt").toString()); Map resultMap = new HashMap(); BigDecimal oilLiters = null; BigDecimal discountAmt =null; BigDecimal discountPrice = null; BigDecimal amt = null; BigDecimal receivableAmt_b = new BigDecimal(receivableAmt); //传入的应收金额 String oilPrice = params.get("oilPrice").toString(); List> customerGradeInfo = customerGradeServices.getCustomerGradeInfo(params); if(customerGradeInfo != null && customerGradeInfo.size() >0){ //已存在客户等级信息 //使用客户已存在的等级计算优惠 oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); discountAmt = oilLiters.multiply(new BigDecimal(customerGradeInfo.get(0).get("gasoilDiscountLitre").toString())); discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP); amt = receivableAmt_b.subtract(discountAmt); discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); resultMap.put("discountAmt", discountAmt.doubleValue()); resultMap.put("amt", amt.doubleValue()); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", discountPrice.doubleValue()); }else { //不存在客户等级信息时,查询油站的客户等级直降优惠(查询结果根据会员成长值条件做升序查询) List customerGradeList = customerGradeServices.getCustomerGradeList(params); //如果存在 if(customerGradeList !=null && customerGradeList.size() >0){ //用户第一次购买 取等级最低初始会员的优惠条件计算 oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); //加油升数 discountAmt = oilLiters.multiply(new BigDecimal(customerGradeList.get(0).get("gasoilDiscountLitre").toString())); discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP); amt = receivableAmt_b.subtract(discountAmt); discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); resultMap.put("discountAmt", discountAmt.doubleValue()); resultMap.put("amt", amt.doubleValue()); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", discountPrice.doubleValue()); }else { //不存在 resultMap.put("discountAmt", 0); resultMap.put("amt", receivableAmt.doubleValue()); oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); resultMap.put("oilLiters", oilLiters.doubleValue()); resultMap.put("discountPrice", oilPrice); } } return resultMap; } /** * 根据订单号获取订单信息 * produces="application/json;charset=UTF-8",consumes = "application/json", * @RequestBody String orderno * @param * @return */ @RequestMapping(value = "/getPayOrderList", method = RequestMethod.GET) @ResponseBody public String getPayOrderList(@RequestParam String orderNo,String openId,String userType){ Gson gson =new Gson(); ResultData resultData =null; //根据token 和 usertype来判断用户是否存在 boolean flag = appUserInfoService.selectAppUserInfo(openId,userType); if(flag){ if(StringUtils.isNotBlank(orderNo)){ PayOrderRequest payOrderRequest =new PayOrderRequest(); payOrderRequest.setOrderNo(orderNo); List payOrderList = payOrderService.getPayOrderList(payOrderRequest.getOrderNo()); resultData=ResultData.success(payOrderList); }else { resultData=ResultData.error(CodeMsg.SEARCH_FAIL); } }else{ resultData=ResultData.error(CodeMsg.USER_NOT_EXSIST); } return gson.toJson(resultData); } /* * 根据用户openId和用户类型userType查询用户订单 * @param openId * @param userType * @return */ @RequestMapping(value = "/getOrderListByOpenIdAndUserType", method = RequestMethod.GET) @ResponseBody public String getOrderListByOpenIdAndUserType(@RequestParam("openId") String openId, @RequestParam("userType") String userType){ Gson gson =new Gson(); ResultData resultData =null; if(StringUtils.isNotBlank(openId) && StringUtils.isNotBlank(userType)){ AppUserInfo appUserInfo = new AppUserInfo(); switch (userType){ case "1": appUserInfo.setBlogOpenid(openId); break; case "2": appUserInfo.setMinaOpenid(openId); break; } List payOrderList = payOrderService.getOrderListByOpenIdAndUserType(appUserInfo); resultData=ResultData.success(payOrderList); }else { resultData=ResultData.error(CodeMsg.SEARCH_FAIL); } return gson.toJson(resultData); } }