123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131 |
- 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;
- @Resource
- private CustomerManagerService customerManagerService;
- //小程序支付生成订单
- @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<AppUserInfo> appUserInfoList =appUserInfoService.Authentication(appUserInfo);
- Map<String ,Object> 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<String, Object> params = new HashMap<>();
- params.put("oliGunNo", payOrderRequest.getOilGun());
- params.put("stationId", payOrderRequest.getStationId());
- String oilPersonnel = "";
- List<Map> 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")){
- CustomerCardSetting customerCardSetting = new CustomerCardSetting();
- customerCardSetting.setStationId(payOrderRequest.getStationId());
- List<CustomerCardSetting> cardRechargeSettingList = electronicMembershipCardService.getCardRechargeSettingList(customerCardSetting);
- if(cardRechargeSettingList !=null && cardRechargeSettingList.size()>0){
- String isMarket = cardRechargeSettingList.get(0).getIsMarket(); //是否叠加营销方案
- String isGradeSetting = cardRechargeSettingList.get(0).getIsGradeSetting(); //是否叠加等级直降
- Map<String, Object> paramsMap = new HashMap<>();
- paramsMap.put("stationId", payOrderRequest.getStationId());
- paramsMap.put("oilName", payOrderRequest.getOilName());
- paramsMap.put("receivableAmt", payOrderRequest.getReceivableAmt());
- //获取油品价格
- StationOilPrice stationOilPrice = new StationOilPrice();
- stationOilPrice.setStationId(payOrderRequest.getStationId());
- stationOilPrice.setOilName(payOrderRequest.getOilName());
- StationOilPrice oilPriceInfo = stationService.getStationOilPrice(stationOilPrice);
- String oilPrice = oilPriceInfo.getOilPrice();
- paramsMap.put("oilPrice", oilPrice);
- paramsMap.put("mobilePhone", appUserInfoList.get(0).getMobilePhone());
- paramsMap.put("minaOpenid", appUserInfoList.get(0).getMinaOpenid());
- if (StringUtils.isNotBlank(isMarket) && isMarket.equals("0")){
- if(StringUtils.isNotBlank(isGradeSetting) && isGradeSetting.equals("0")){
- //如果营销和等级都设置叠加
- resultMap.put("amt_dzk", resultMap.get("amt").toString());
- }else {
- //如果营销叠加电子卡不叠加:则用总的实际所有的支付金额+等级优惠的金额
- Map<String, Object> map = this.calcuteDengJiZhiJiang(paramsMap);
- String string = map.get("discountAmt").toString();
- BigDecimal amt_dzk = new BigDecimal(resultMap.get("amt").toString()).add(new BigDecimal(string));
- resultMap.put("amt_dzk",amt_dzk.doubleValue());
- }
- }else {
- //如果不叠加营销,只叠加等级优惠
- if(StringUtils.isNotBlank(isGradeSetting) && isGradeSetting.equals("0")){
- Map<String, Object> map = this.calcuteDengJiZhiJiang(paramsMap);
- String string = map.get("discountAmt").toString();
- BigDecimal amt_dzk = new BigDecimal(payOrderRequest.getReceivableAmt()).subtract(new BigDecimal(string));
- resultMap.put("amt_dzk", amt_dzk);
- }else {
- //如果既不叠加营销优惠也不叠加等级优惠
- resultMap.put("amt_dzk", payOrderRequest.getReceivableAmt());
- }
- }
- }
- //获取用户电子卡余额
- Map<String, String> 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 "98#":
- 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<String, String> selectCardBalance(Integer stationId, String oilName, String unionId) {
- String cardOilsType ="";
- switch (oilName){
- case "92#":
- cardOilsType ="1";
- break;
- case "95#":
- cardOilsType ="1";
- break;
- case "98#":
- 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 ="";
- Map<String, String> result = new HashMap<>();
- if(electronicCardInfo !=null){
- balance = electronicCardInfo.getAmt();
- cardOilsType1 = electronicCardInfo.getCardOilsType();
- result.put("balance",balance.toString());
- result.put("cardOilsType",cardOilsType1);
- }
- return result;
- }
- /***
- * 计算订单金额
- * @return
- */
- public Map<String ,Object> calculateAmt(PayOrderRequest payOrderRequest){
- Map<String ,Object> 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<String ,Object> params = new HashMap<String, Object>();
- 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<Map<String, Object>> customerGradeInfo = customerGradeServices.getCustomerGradeInfo(params);
- if(customerGradeInfo != null && customerGradeInfo.size() >0){ //已存在客户等级信息
- //使用客户已存在的等级计算优惠
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
- discountAmt = oilLiters.multiply(new BigDecimal(customerGradeInfo.get(0).get("gasoilDiscountLitre").toString()));
- discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_UP);
- amt = receivableAmt_b.subtract(discountAmt);
- discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_UP);
- resultMap.put("discountAmt", discountAmt.doubleValue());
- resultMap.put("amt", amt.doubleValue());
- resultMap.put("oilLiters", oilLiters.doubleValue());
- resultMap.put("discountPrice", discountPrice.doubleValue());
- }else {
- //不存在客户等级信息时,查询油站的客户等级直降优惠(查询结果根据会员成长值条件做升序查询)
- List<Map> customerGradeList = customerGradeServices.getCustomerGradeList(params);
- //如果存在
- if(customerGradeList !=null && customerGradeList.size() >0){
- //用户第一次购买 取等级最低初始会员的优惠条件计算
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_UP); //加油升数
- discountAmt = oilLiters.multiply(new BigDecimal(customerGradeList.get(0).get("gasoilDiscountLitre").toString()));
- discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_UP);
- amt = receivableAmt_b.subtract(discountAmt);
- discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_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_UP);
- resultMap.put("oilLiters", oilLiters.doubleValue());
- resultMap.put("discountPrice", oilPrice);
- }
- }
- break;
- case "2": //2.满减
- params.put("discountPlanType", "1");
- //营销方案
- List<Map> 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_UP).multiply(gasoilDiscountAmt_b); //优惠金额
- discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_UP);
- amt = receivableAmt_b.subtract(discountAmt); //实收金额
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_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_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_UP);
- resultMap.put("oilLiters", oilLiters.doubleValue());
- resultMap.put("discountPrice", oilPrice);
- }
- break;
- case "3": //3.立减
- params.put("discountPlanType", "2");
- //营销方案
- List<Map> 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_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_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_UP);
- resultMap.put("oilLiters", oilLiters.doubleValue());
- resultMap.put("discountPrice", oilPrice);
- }
- break;
- case "4": //4.独立直降
- params.put("discountPlanType", "3");
- //营销方案
- List<Map> 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_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_UP);
- amt = receivableAmt_b.subtract(discountAmt);
- discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_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_UP);
- amt = receivableAmt_b.subtract(discountAmt);
- discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_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_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_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<String ,Object> calculateAmtNew(PayOrderRequest payOrderRequest){
- Map<String ,Object> 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 = new BigDecimal(0);
- BigDecimal discountAmt =new BigDecimal(0);
- 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<String ,Object> params = new HashMap<String, Object>();
- 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<Map<String, Object>> customerGradeInfo = customerGradeServices.getCustomerGradeInfo(params);
- if(customerGradeInfo != null && customerGradeInfo.size() >0){ //已存在客户等级信息
- //使用客户已存在的等级计算优惠
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
- if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
- }
- if(customerGradeInfo.get(0).containsKey("gasoilDiscountLitre")){ //表示可以查到客户在当前油站的等级信息,油站等级体系没有删除
- if(customerGradeInfo.get(0).get("gasoilDiscountLitre") !=null){
- discountAmt = oilLiters.multiply(new BigDecimal(customerGradeInfo.get(0).get("gasoilDiscountLitre").toString()));
- discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
- }else {
- discountAmt =new BigDecimal("0.00");
- }
- }else {
- //如果存在客户等级,但油站修改等级体系,客户等级已超出等级体系时
- List<Map> cList = customerGradeServices.getCustomerGradeListOrderByDesc(params);
- if(cList !=null && cList.size() >0){
- CustomerManage customerManage = new CustomerManage();
- customerManage.setStationId(Integer.valueOf(params.get("stationId").toString())); //油站ID
- customerManage.setOilName(params.get("oilName").toString()); //油品名称
- customerManage.setPhoneNumber(params.get("mobilePhone").toString());
- customerManage.setMinaOpenid(params.get("minaOpenid").toString());
- CustomerManage existCustomer = customerManagerService.getCustomerManageInfo(customerManage); //已存在用户信息 existCustomer
- for(Map m : cList){
- if(Double.valueOf(existCustomer.getAmt().toString()) >= Double.valueOf(m.get("memberConditStart").toString())){
- discountAmt = oilLiters.multiply(new BigDecimal(cList.get(0).get("gasoilDiscountLitre").toString()));
- discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
- break;
- }
- }
- }
- }
- amt = receivableAmt_b.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());
- }else {
- //不存在客户等级信息时,查询油站的客户等级直降优惠(查询结果根据会员成长值条件做升序查询)
- List<Map> customerGradeList = customerGradeServices.getCustomerGradeList(params);
- //如果存在
- if(customerGradeList !=null && customerGradeList.size() >0){
- //用户第一次购买 取等级最低初始会员的优惠条件计算
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
- if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
- }
- if(customerGradeList.get(0).get("gasoilDiscountLitre") !=null){
- discountAmt = oilLiters.multiply(new BigDecimal(customerGradeList.get(0).get("gasoilDiscountLitre").toString()));
- discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
- }else {
- discountAmt =new BigDecimal("0.00");
- }
- amt = receivableAmt_b.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());
- }else {
- //不存在
- resultMap.put("discountAmt", 0);
- resultMap.put("amt", receivableAmt.doubleValue());
- oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_UP);
- resultMap.put("oilLiters", oilLiters.doubleValue());
- resultMap.put("discountPrice", oilPrice);
- }
- }
- break;
- case "2": //2.满减
- params.put("discountPlanType", "1");
- //营销方案
- List<Map> 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); //加油升数
- if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
- }
- discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
- switch (vipDiscountyPlus){
- case "1":
- Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
- discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
- amt = receivableAmt_b.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<String, Object> map = this.calcuteDengJiZhiJiang(params);
- discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
- oilLiters = new BigDecimal(map.get("oilLiters").toString());
- amt = receivableAmt_b.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", 0);
- resultMap.put("amt", receivableAmt.doubleValue());
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
- if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
- }
- resultMap.put("oilLiters", oilLiters.doubleValue());
- resultMap.put("discountPrice", oilPrice);
- break;
- }
- }
- }else {
- //用户没有设置优惠规则时
- Map<String, Object> 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<Map> customerMarkertPlanList_2 = customerGradeServices.getCustomerMarkertPlanList(params);
- if(customerMarkertPlanList_2 !=null && customerMarkertPlanList_2.size() >0){
- //是否共享会员优惠叠加: 1 代表是; 2 代表否;
- String vipDiscountyPlus = customerMarkertPlanList_2.get(0).get("vipDiscountyPlus").toString();
- //如果当前应收金额大于条件金额
- 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); //加油升数
- if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
- }
- discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_UP);
- switch (vipDiscountyPlus){
- case "1":
- Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
- discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
- 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_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<String, Object> map = this.calcuteDengJiZhiJiang(params);
- discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
- oilLiters = new BigDecimal(map.get("oilLiters").toString());
- 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_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", 0);
- resultMap.put("amt", receivableAmt.doubleValue());
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
- if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
- }
- resultMap.put("oilLiters", oilLiters.doubleValue());
- resultMap.put("discountPrice", oilPrice);
- break;
- }
- }
- }else {
- //用户没有设置优惠规则时
- Map<String, Object> 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<Map> customerMarkertPlanList_3 = customerGradeServices.getCustomerMarkertPlanList(params);
- if(customerMarkertPlanList_3 !=null && customerMarkertPlanList_3.size() >0){
- //是否共享会员优惠叠加: 1 代表是; 2 代表否;
- String vipDiscountyPlus_4 = customerMarkertPlanList_3.get(0).get("vipDiscountyPlus").toString();
- BigDecimal discountAmt_b = new BigDecimal(customerMarkertPlanList_3.get(0).get("discountAmt").toString()); //条件金额
- BigDecimal gasoilDiscountAmt_b = new BigDecimal(customerMarkertPlanList_3.get(0).get("gasoilDiscountAmt").toString());
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
- if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
- }
- if(customerMarkertPlanList_3.get(0).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_UP); //优惠价格
- if(vipDiscountyPlus_4.equals("1")){
- Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
- discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
- 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_UP);
- }
- resultMap.put("discountAmt", discountAmt.doubleValue());
- resultMap.put("amt", amt.doubleValue());
- resultMap.put("oilLiters", oilLiters.doubleValue());
- resultMap.put("discountPrice", discountPrice.doubleValue());
- break;
- }else {
- //不满足优惠条件
- switch (vipDiscountyPlus_4) {
- case "1":
- Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
- discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
- oilLiters = new BigDecimal(map.get("oilLiters").toString());
- amt = receivableAmt_b.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", 0);
- resultMap.put("amt", receivableAmt.doubleValue());
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
- if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
- }
- resultMap.put("oilLiters", oilLiters.doubleValue());
- resultMap.put("discountPrice", oilPrice);
- break;
- }
- }
- }else if(customerMarkertPlanList_3.get(0).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.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
- //是否共享会员优惠叠加
- if(vipDiscountyPlus_4.equals("1")){
- Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
- discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
- 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_UP);
- }
- resultMap.put("discountAmt", discountAmt.doubleValue());
- resultMap.put("amt", amt.doubleValue());
- resultMap.put("oilLiters", oilLiters.doubleValue());
- resultMap.put("discountPrice", discountPrice.doubleValue());
- break;
- }else {
- //不满足优惠条件
- switch (vipDiscountyPlus_4) {
- case "1":
- Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
- discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
- oilLiters = new BigDecimal(map.get("oilLiters").toString());
- amt = receivableAmt_b.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", 0);
- resultMap.put("amt", receivableAmt.doubleValue());
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
- if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
- }
- resultMap.put("oilLiters", oilLiters.doubleValue());
- resultMap.put("discountPrice", oilPrice);
- break;
- }
- }
- }
- }else {
- //没有设置优惠条件时
- Map<String, Object> 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<String, Object> calcuteDengJiZhiJiang(Map params){
- Double receivableAmt = Double.valueOf(params.get("receivableAmt").toString());
- Map<String, Object> resultMap = new HashMap();
- BigDecimal oilLiters = null;
- BigDecimal discountAmt =new BigDecimal(0);
- BigDecimal discountPrice = null;
- BigDecimal amt = null;
- BigDecimal receivableAmt_b = new BigDecimal(receivableAmt); //传入的应收金额
- String oilPrice = params.get("oilPrice").toString();
- List<Map<String, Object>> customerGradeInfo = customerGradeServices.getCustomerGradeInfo(params);
- if(customerGradeInfo != null && customerGradeInfo.size() >0){ //已存在客户等级信息
- //使用客户已存在的等级计算优惠
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
- if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
- }
- if(customerGradeInfo.get(0).containsKey("gasoilDiscountLitre")){
- if(customerGradeInfo.get(0).get("gasoilDiscountLitre") !=null){
- discountAmt = oilLiters.multiply(new BigDecimal(customerGradeInfo.get(0).get("gasoilDiscountLitre").toString()));
- discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
- }else {
- discountAmt =new BigDecimal("0.00");
- }
- }else {
- //如果存在客户等级,但油站修改等级体系,客户等级已超出等级体系时
- List<Map> cList = customerGradeServices.getCustomerGradeListOrderByDesc(params);
- if(cList !=null && cList.size() >0){
- CustomerManage customerManage = new CustomerManage();
- customerManage.setStationId(Integer.valueOf(params.get("stationId").toString())); //油站ID
- customerManage.setOilName(params.get("oilName").toString()); //油品名称
- customerManage.setPhoneNumber(params.get("mobilePhone").toString());
- customerManage.setMinaOpenid(params.get("minaOpenid").toString());
- CustomerManage existCustomer = customerManagerService.getCustomerManageInfo(customerManage); //已存在用户信息 existCustomer
- for(Map m : cList){
- if(Double.valueOf(existCustomer.getAmt().toString()) >= Double.valueOf(m.get("memberConditStart").toString())){
- discountAmt = oilLiters.multiply(new BigDecimal(cList.get(0).get("gasoilDiscountLitre").toString()));
- discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
- break;
- }
- }
- }
- }
- 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_UP);
- resultMap.put("discountAmt", discountAmt.doubleValue());
- resultMap.put("amt", amt.doubleValue());
- resultMap.put("oilLiters", oilLiters.doubleValue());
- resultMap.put("discountPrice", discountPrice.doubleValue());
- }else {
- //不存在客户等级信息时,查询油站的客户等级直降优惠(查询结果根据会员成长值条件做升序查询)
- List<Map> customerGradeList = customerGradeServices.getCustomerGradeList(params);
- //如果存在
- if(customerGradeList !=null && customerGradeList.size() >0){
- //用户第一次购买 取等级最低初始会员的优惠条件计算
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
- if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
- }
- if(customerGradeList.get(0).get("gasoilDiscountLitre") !=null){
- discountAmt = oilLiters.multiply(new BigDecimal(customerGradeList.get(0).get("gasoilDiscountLitre").toString()));
- discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
- }else {
- discountAmt =new BigDecimal("0.00");
- }
- 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_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 = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
- if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
- }
- resultMap.put("oilLiters", oilLiters.doubleValue());
- resultMap.put("discountPrice", oilPrice);
- }
- }
- return resultMap;
- }
- //计算满减
- public Map<String, Object> calcuteManjian(Map params){
- Map<String, Object> resultMap = new HashMap<>();
- Double receivableAmt = Double.valueOf(params.get("receivableAmt").toString());
- BigDecimal oilLiters = null;
- BigDecimal discountAmt =null;
- BigDecimal discountPrice = null;
- BigDecimal amt = null;
- BigDecimal receivableAmt_b = new BigDecimal(receivableAmt); //传入的应收金额
- String oilPrice = params.get("oilPrice").toString();
- params.put("discountPlanType", "1");
- //营销方案
- List<Map> 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_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); //加油升数
- if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
- }
- discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
- switch (vipDiscountyPlus){
- case "1":
- Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
- discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
- amt = receivableAmt_b.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<String, Object> map = this.calcuteDengJiZhiJiang(params);
- discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
- amt = receivableAmt_b.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", 0);
- resultMap.put("amt", receivableAmt.doubleValue());
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
- if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
- oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
- }
- resultMap.put("oilLiters", oilLiters.doubleValue());
- resultMap.put("discountPrice", oilPrice);
- break;
- }
- }
- }else {
- //用户没有设置优惠规则时
- Map<String, Object> 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());
- }
- 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<PayOrder> 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<PayOrder> payOrderList = payOrderService.getOrderListByOpenIdAndUserType(appUserInfo);
- resultData=ResultData.success(payOrderList);
- }else {
- resultData=ResultData.error(CodeMsg.SEARCH_FAIL);
- }
- return gson.toJson(resultData);
- }
- }
|