PayOrderController.java 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. package com.platform.yijia.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.google.gson.Gson;
  4. import com.platform.yijia.dao.PayOrderMapper;
  5. import com.platform.yijia.param.request.PayOrderRequest;
  6. import com.platform.yijia.pojo.*;
  7. import com.platform.yijia.service.*;
  8. import com.platform.yijia.utils.CodeMsg;
  9. import com.platform.yijia.utils.ResultData;
  10. import com.platform.yijia.utils.weixinapp.WxPushUtil;
  11. import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
  12. import org.apache.commons.lang3.StringUtils;
  13. import org.apache.ibatis.javassist.util.HotSwapAgent;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.stereotype.Controller;
  17. import org.springframework.transaction.annotation.Transactional;
  18. import org.springframework.web.bind.annotation.*;
  19. import javax.annotation.Resource;
  20. import javax.naming.ldap.HasControls;
  21. import java.math.BigDecimal;
  22. import java.text.SimpleDateFormat;
  23. import java.util.*;
  24. @Controller
  25. @RequestMapping("/demo")
  26. public class PayOrderController {
  27. private static Logger logger =(Logger) LoggerFactory.getLogger(PayOrderController.class);
  28. @Resource
  29. private PayOrderService payOrderService;
  30. @Resource
  31. private AppUserInfoService appUserInfoService;
  32. @Resource
  33. private StationService stationService;
  34. @Resource
  35. private CustomerGradeServices customerGradeServices;
  36. @Resource
  37. private PayOrderMapper payOrderMapper;
  38. @Resource
  39. private ElectronicMembershipCardService electronicMembershipCardService;
  40. @Resource
  41. private CustomerManagerService customerManagerService;
  42. //小程序支付生成订单
  43. @RequestMapping(value = "/AddPayOrderInfoNew", consumes = "application/json", method = RequestMethod.POST)
  44. @ResponseBody
  45. @Transactional
  46. public String AddPayOrderInfoNew(@RequestBody PayOrderRequest payOrderRequest){
  47. logger.info("获取订单前端传来参数openId:"+ payOrderRequest.getOpenId());
  48. logger.info("获取订单前端传来参数userType:"+ payOrderRequest.getUserType());
  49. Gson gson =new Gson();
  50. ResultData resultData =null;
  51. AppUserInfo appUserInfo =new AppUserInfo();
  52. appUserInfo.setUserType(payOrderRequest.getUserType()); //订单类型 是小程序还是公众号
  53. if(payOrderRequest.getUserType().equals("1")){
  54. appUserInfo.setBlogOpenid(payOrderRequest.getOpenId()); //1 是公众号
  55. }else if(payOrderRequest.getUserType().equals("2")){
  56. appUserInfo.setMinaOpenid(payOrderRequest.getOpenId()); //2是小程序
  57. }
  58. List<AppUserInfo> appUserInfoList =appUserInfoService.Authentication(appUserInfo);
  59. Map<String ,Object> resultMap = new HashMap<>(); //返回结果集
  60. //判断登录人员是否验证成功
  61. if(appUserInfoList!=null && appUserInfoList.size()>0){
  62. PayOrder payOrder = new PayOrder();
  63. payOrder.setConsumer(appUserInfoList.get(0).getBlogNickName()); //昵称
  64. payOrder.setConsumerId(appUserInfoList.get(0).getUserId()); //用户id
  65. Random random = new Random();
  66. String result="";
  67. for(int i=0;i<6;i++){
  68. result+=random.nextInt(10);
  69. }
  70. String ordNo=System.nanoTime()+result;
  71. payOrder.setOrderNo(ordNo); //订单号
  72. logger.info("生成的订单号:" + ordNo);
  73. payOrder.setDiscountCouponAmt(payOrderRequest.getDiscountCouponAmt()); //优惠劵金额
  74. payOrder.setDiscountCoupon(payOrderRequest.getDiscountCoupon()); //优惠券
  75. payOrder.setCarNo(payOrderRequest.getCarNo()); //车牌号、
  76. payOrder.setCustomerGrade(payOrderRequest.getCustomerGrade()); //客户电话
  77. payOrder.setMemberNo(payOrderRequest.getMemberNo()); //会员卡号
  78. payOrder.setMemberAmt(payOrderRequest.getMemberAmt()); //会员支付金额
  79. //payOrder.setPrintCount(payOrderRequest.getPrintCount()); //小票数量
  80. payOrder.setScore(payOrderRequest.getScore()); //积分
  81. payOrder.setStationId(payOrderRequest.getStationId()); //油站id
  82. payOrder.setStatus("0"); //创建订单 未支付状态 0
  83. if(StringUtils.isNotBlank(payOrderRequest.getUserType())){
  84. String userType = payOrderRequest.getUserType(); //支付方式:02公众号,03小程序',
  85. switch (userType){
  86. case "1":
  87. payOrder.setPayWay("02");
  88. break;
  89. case "2":
  90. payOrder.setPayWay("03");
  91. break;
  92. }
  93. }
  94. payOrder.setCreatedDate(new Date()); //创建时间
  95. payOrder.setOrderType(payOrderRequest.getOrderType()); //订单类型
  96. payOrder.setOilGun(payOrderRequest.getOilGun()); //油枪号
  97. //根据油站和油枪获取加油员
  98. HashMap<String, Object> params = new HashMap<>();
  99. params.put("oliGunNo", payOrderRequest.getOilGun());
  100. params.put("stationId", payOrderRequest.getStationId());
  101. String oilPersonnel = "";
  102. List<Map> mapResultList = payOrderMapper.selectPersonelName(params); //使用list原因:一个枪号对应多个加油员场景
  103. logger.info("加油员信息:"+ mapResultList.toString());
  104. if(mapResultList != null && mapResultList.size() > 0){
  105. for (Map per : mapResultList){
  106. if(per.containsKey("personelName") && per.get("personelName").toString() !=null && per.get("personelName").toString() !=""){
  107. oilPersonnel += per.get("personelName").toString() +",";
  108. }
  109. }
  110. if(oilPersonnel !=""){
  111. payOrder.setOilPersonnel(oilPersonnel.substring(0, oilPersonnel.length()-1)); //加油员
  112. }
  113. }
  114. String orderType = payOrderRequest.getOrderType(); //获取订单类型
  115. logger.info("订单类型:" + orderType);
  116. if(orderType.equals("1")){ //订单类型是油品时计算
  117. //resultMap = this.calculateAmt(payOrderRequest); //计算油价 加油升数 优惠金额 实收金额 优惠价格
  118. resultMap = this.calculateAmtNew(payOrderRequest); //计算油价 加油升数 优惠金额 实收金额 优惠价格
  119. payOrder.setOilPirce(resultMap.get("oilPrice").toString());
  120. payOrder.setOrderLiters(resultMap.get("oilLiters").toString());
  121. payOrder.setDiscountAmt(Double.valueOf(resultMap.get("discountAmt").toString()));
  122. payOrder.setAmt(Double.valueOf(resultMap.get("amt").toString()));
  123. String stationCardEnabledFlag = electronicMembershipCardService.getStationCardEnabledFlag(payOrderRequest.getStationId());
  124. logger.info("油站是否开启电子会员卡功能: " +stationCardEnabledFlag);
  125. //查询油站是否开启电子会员卡功能
  126. if(stationCardEnabledFlag.equals("1")){
  127. CustomerCardSetting customerCardSetting = new CustomerCardSetting();
  128. customerCardSetting.setStationId(payOrderRequest.getStationId());
  129. List<CustomerCardSetting> cardRechargeSettingList = electronicMembershipCardService.getCardRechargeSettingList(customerCardSetting);
  130. if(cardRechargeSettingList !=null && cardRechargeSettingList.size()>0){
  131. String isMarket = cardRechargeSettingList.get(0).getIsMarket(); //是否叠加营销方案
  132. String isGradeSetting = cardRechargeSettingList.get(0).getIsGradeSetting(); //是否叠加等级直降
  133. Map<String, Object> paramsMap = new HashMap<>();
  134. paramsMap.put("stationId", payOrderRequest.getStationId());
  135. paramsMap.put("oilName", payOrderRequest.getOilName());
  136. paramsMap.put("receivableAmt", payOrderRequest.getReceivableAmt());
  137. //获取油品价格
  138. StationOilPrice stationOilPrice = new StationOilPrice();
  139. stationOilPrice.setStationId(payOrderRequest.getStationId());
  140. stationOilPrice.setOilName(payOrderRequest.getOilName());
  141. StationOilPrice oilPriceInfo = stationService.getStationOilPrice(stationOilPrice);
  142. String oilPrice = oilPriceInfo.getOilPrice();
  143. paramsMap.put("oilPrice", oilPrice);
  144. paramsMap.put("mobilePhone", appUserInfoList.get(0).getMobilePhone());
  145. paramsMap.put("minaOpenid", appUserInfoList.get(0).getMinaOpenid());
  146. if (StringUtils.isNotBlank(isMarket) && isMarket.equals("0")){
  147. if(StringUtils.isNotBlank(isGradeSetting) && isGradeSetting.equals("0")){
  148. //如果营销和等级都设置叠加
  149. resultMap.put("amt_dzk", resultMap.get("amt").toString());
  150. }else {
  151. //如果营销叠加电子卡不叠加:则用总的实际所有的支付金额+等级优惠的金额
  152. Map<String, Object> map = this.calcuteDengJiZhiJiang(paramsMap);
  153. String string = map.get("discountAmt").toString();
  154. BigDecimal amt_dzk = new BigDecimal(resultMap.get("amt").toString()).add(new BigDecimal(string));
  155. resultMap.put("amt_dzk",amt_dzk.doubleValue());
  156. }
  157. }else {
  158. //如果不叠加营销,只叠加等级优惠
  159. if(StringUtils.isNotBlank(isGradeSetting) && isGradeSetting.equals("0")){
  160. Map<String, Object> map = this.calcuteDengJiZhiJiang(paramsMap);
  161. String string = map.get("discountAmt").toString();
  162. BigDecimal amt_dzk = new BigDecimal(payOrderRequest.getReceivableAmt()).subtract(new BigDecimal(string));
  163. resultMap.put("amt_dzk", amt_dzk);
  164. }else {
  165. //如果既不叠加营销优惠也不叠加等级优惠
  166. resultMap.put("amt_dzk", payOrderRequest.getReceivableAmt());
  167. }
  168. }
  169. }
  170. //获取用户电子卡余额
  171. Map<String, String> map = this.selectCardBalance(payOrderRequest.getStationId(), payOrderRequest.getOilName(), appUserInfoList.get(0).getUnionId());
  172. resultMap.put("balance", map.get("balance")); //电子卡余额
  173. resultMap.put("cardOilsType", map.get("cardOilsType")); //电子卡类型1.汽油; 2.柴油
  174. }
  175. }
  176. payOrder.setReceivableAmt(payOrderRequest.getReceivableAmt()); //订单应收金额
  177. String oilName =payOrderRequest.getOilName();
  178. payOrder.setOilName(oilName); //油品名称
  179. switch (oilName){
  180. case "92#":
  181. payOrder.setOilType("2");
  182. break;
  183. case "95#":
  184. payOrder.setOilType("2");
  185. break;
  186. case "98#":
  187. payOrder.setOilType("2");
  188. break;
  189. case "0#":
  190. payOrder.setOilType("1");
  191. break;
  192. case "-10#":
  193. payOrder.setOilType("1");
  194. break;
  195. case "-20#":
  196. payOrder.setOilType("1");
  197. break;
  198. }
  199. payOrder.setOrderType(orderType);
  200. StationInfo stationInfo = new StationInfo();
  201. stationInfo.setStationId(payOrderRequest.getStationId());
  202. StationInfo stationInfos = stationService.selectStationInfo(stationInfo);
  203. if(stationInfos !=null){
  204. payOrder.setStationName(stationInfos.getStationName());
  205. //获取油站图片和名称
  206. resultMap.put("stationPic", stationInfos.getStationPic());
  207. resultMap.put("stationName", stationInfos.getStationName());
  208. }
  209. //payOrder.setPayType("wx"); //支付类型
  210. switch (orderType){
  211. case "1": //油品
  212. payOrderMapper.insertSelective(payOrder);
  213. resultMap.put("ordNo", ordNo);
  214. break;
  215. case "2": //非油品
  216. payOrder.setAmt(Double.valueOf(payOrderRequest.getReceivableAmt())); //非油品金额
  217. payOrder.setDiscountAmt(Double.valueOf(0)); //优惠金额
  218. payOrder.setOilPirce(payOrderRequest.getReceivableAmt().toString()); //非油品价格
  219. payOrder.setOrderLiters("0");
  220. resultMap.put("discountAmt", "0"); //优惠价格
  221. resultMap.put("amt", payOrderRequest.getReceivableAmt()); //实收价格
  222. resultMap.put("receivableAmt", payOrderRequest.getReceivableAmt()); //应收价格
  223. resultMap.put("oilLiters", "0");
  224. resultMap.put("discountPrice", "0");
  225. payOrderMapper.insertSelective(payOrder);
  226. resultMap.put("ordNo", ordNo);
  227. break;
  228. }
  229. resultData = ResultData.success(resultMap);
  230. }else{
  231. //登录人验证失败
  232. resultData=ResultData.error(CodeMsg.USER_NOT_EXSIST);
  233. }
  234. return gson.toJson(resultData);
  235. }
  236. //查询用户电子会员卡余额
  237. public Map<String, String> selectCardBalance(Integer stationId, String oilName, String unionId) {
  238. String cardOilsType ="";
  239. switch (oilName){
  240. case "92#":
  241. cardOilsType ="1";
  242. break;
  243. case "95#":
  244. cardOilsType ="1";
  245. break;
  246. case "98#":
  247. cardOilsType ="1";
  248. break;
  249. case "0#":
  250. cardOilsType ="2";
  251. break;
  252. case "-10#":
  253. cardOilsType ="2";
  254. break;
  255. case "-20#":
  256. cardOilsType ="2";
  257. break;
  258. }
  259. CustomerElectronicCard customerElectronicCard = new CustomerElectronicCard();
  260. customerElectronicCard.setCardOilsType(cardOilsType);
  261. customerElectronicCard.setStationId(stationId);
  262. customerElectronicCard.setUnionId(unionId);
  263. CustomerElectronicCard electronicCardInfo = electronicMembershipCardService.getElectronicCardInfoByUnionId(customerElectronicCard);
  264. BigDecimal balance = null;
  265. String cardOilsType1 ="";
  266. Map<String, String> result = new HashMap<>();
  267. if(electronicCardInfo !=null){
  268. balance = electronicCardInfo.getAmt();
  269. cardOilsType1 = electronicCardInfo.getCardOilsType();
  270. result.put("balance",balance.toString());
  271. result.put("cardOilsType",cardOilsType1);
  272. }
  273. return result;
  274. }
  275. /***
  276. * 计算订单金额
  277. * @return
  278. */
  279. public Map<String ,Object> calculateAmt(PayOrderRequest payOrderRequest){
  280. Map<String ,Object> resultMap = new HashMap<>();
  281. if(payOrderRequest.getReceivableAmt() !=null){ //应收金额
  282. Double receivableAmt = payOrderRequest.getReceivableAmt(); //应收金额 用户输入的金额
  283. Integer stationId = payOrderRequest.getStationId(); //油站ID
  284. //String oilGun = payOrderRequest.getOilGun(); //油枪号
  285. String oilName = payOrderRequest.getOilName(); //油品名称
  286. String openId = payOrderRequest.getOpenId(); //用户的openId
  287. String mobilePhone = payOrderRequest.getCustomerPhone(); //客户手机号
  288. //获取油品价格
  289. StationOilPrice stationOilPrice = new StationOilPrice();
  290. stationOilPrice.setStationId(stationId);
  291. stationOilPrice.setOilName(oilName);
  292. StationOilPrice oilPriceInfo = stationService.getStationOilPrice(stationOilPrice);
  293. String oilPrice = oilPriceInfo.getOilPrice();
  294. //String stationNanme = oilPriceInfo.getStationNanme();
  295. BigDecimal oilLiters = null;
  296. BigDecimal discountAmt =null;
  297. BigDecimal discountPrice = null;
  298. BigDecimal amt = null;
  299. //resultMap.put("stationName", stationNanme);
  300. resultMap.put("oilPrice", oilPrice); //油品价格
  301. resultMap.put("receivableAmt", receivableAmt); //应收价格
  302. //该油站的优惠方式
  303. String stationDiscountWay = stationService.getStationDiscountWay(stationId.toString());
  304. Map<String ,Object> params = new HashMap<String, Object>();
  305. params.put("stationId", stationId);
  306. params.put("oilName", oilName);
  307. BigDecimal receivableAmt_b = new BigDecimal(receivableAmt); //传入的应收金额
  308. switch (stationDiscountWay){
  309. case "1": //1.等级直降
  310. //查询等级直降的优惠方式
  311. params.put("mobilePhone", mobilePhone);
  312. params.put("minaOpenid", openId);
  313. //查询客户等级信息
  314. List<Map<String, Object>> customerGradeInfo = customerGradeServices.getCustomerGradeInfo(params);
  315. if(customerGradeInfo != null && customerGradeInfo.size() >0){ //已存在客户等级信息
  316. //使用客户已存在的等级计算优惠
  317. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
  318. discountAmt = oilLiters.multiply(new BigDecimal(customerGradeInfo.get(0).get("gasoilDiscountLitre").toString()));
  319. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_UP);
  320. amt = receivableAmt_b.subtract(discountAmt);
  321. discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_UP);
  322. resultMap.put("discountAmt", discountAmt.doubleValue());
  323. resultMap.put("amt", amt.doubleValue());
  324. resultMap.put("oilLiters", oilLiters.doubleValue());
  325. resultMap.put("discountPrice", discountPrice.doubleValue());
  326. }else {
  327. //不存在客户等级信息时,查询油站的客户等级直降优惠(查询结果根据会员成长值条件做升序查询)
  328. List<Map> customerGradeList = customerGradeServices.getCustomerGradeList(params);
  329. //如果存在
  330. if(customerGradeList !=null && customerGradeList.size() >0){
  331. //用户第一次购买 取等级最低初始会员的优惠条件计算
  332. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_UP); //加油升数
  333. discountAmt = oilLiters.multiply(new BigDecimal(customerGradeList.get(0).get("gasoilDiscountLitre").toString()));
  334. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_UP);
  335. amt = receivableAmt_b.subtract(discountAmt);
  336. discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_UP);
  337. resultMap.put("discountAmt", discountAmt.doubleValue());
  338. resultMap.put("amt", amt.doubleValue());
  339. resultMap.put("oilLiters", oilLiters.doubleValue());
  340. resultMap.put("discountPrice", discountPrice.doubleValue());
  341. }else {
  342. //不存在
  343. resultMap.put("discountAmt", 0);
  344. resultMap.put("amt", receivableAmt.doubleValue());
  345. oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_UP);
  346. resultMap.put("oilLiters", oilLiters.doubleValue());
  347. resultMap.put("discountPrice", oilPrice);
  348. }
  349. }
  350. break;
  351. case "2": //2.满减
  352. params.put("discountPlanType", "1");
  353. //营销方案
  354. List<Map> customerMarkertPlanList_1 = customerGradeServices.getCustomerMarkertPlanList(params);
  355. //有优惠规则
  356. if(customerMarkertPlanList_1 !=null && customerMarkertPlanList_1.size() >0){
  357. boolean flag = true;
  358. for (Map m : customerMarkertPlanList_1){
  359. //如果当前应收金额大于条件金额
  360. BigDecimal discountAmt_b = new BigDecimal(m.get("discountAmt").toString()); //条件金额
  361. if(receivableAmt_b.compareTo(discountAmt_b) ==1 || receivableAmt_b.compareTo(discountAmt_b) ==0){
  362. BigDecimal gasoilDiscountAmt_b = new BigDecimal(m.get("gasoilDiscountAmt").toString()); //每满多少优惠多少的金额
  363. discountAmt = receivableAmt_b.divide(discountAmt_b, 2, BigDecimal.ROUND_UP).multiply(gasoilDiscountAmt_b); //优惠金额
  364. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_UP);
  365. amt = receivableAmt_b.subtract(discountAmt); //实收金额
  366. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP); //加油升数
  367. discountPrice = amt.divide(oilLiters); //优惠价格
  368. resultMap.put("discountAmt", discountAmt.doubleValue());
  369. resultMap.put("amt", amt.doubleValue());
  370. resultMap.put("oilLiters", oilLiters.doubleValue());
  371. resultMap.put("discountPrice", discountPrice.doubleValue());
  372. flag = false;
  373. break;
  374. }
  375. }
  376. if(flag){
  377. resultMap.put("discountAmt", 0);
  378. resultMap.put("amt", receivableAmt.doubleValue());
  379. oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
  380. resultMap.put("oilLiters", oilLiters.doubleValue());
  381. resultMap.put("discountPrice", oilPrice);
  382. }
  383. }else {
  384. //没有优惠规则
  385. resultMap.put("discountAmt", 0);
  386. resultMap.put("amt", receivableAmt.doubleValue());
  387. oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_UP);
  388. resultMap.put("oilLiters", oilLiters.doubleValue());
  389. resultMap.put("discountPrice", oilPrice);
  390. }
  391. break;
  392. case "3": //3.立减
  393. params.put("discountPlanType", "2");
  394. //营销方案
  395. List<Map> customerMarkertPlanList_2 = customerGradeServices.getCustomerMarkertPlanList(params);
  396. if(customerMarkertPlanList_2 !=null && customerMarkertPlanList_2.size() >0){
  397. boolean flag = true;
  398. for (Map m : customerMarkertPlanList_2){
  399. //如果当前应收金额大于条件金额
  400. BigDecimal discountAmt_b = new BigDecimal(m.get("discountAmt").toString()); //条件金额
  401. if(receivableAmt_b.compareTo(discountAmt_b) ==1 || receivableAmt_b.compareTo(discountAmt_b) ==0){
  402. BigDecimal gasoilDiscountAmt_b = new BigDecimal(m.get("gasoilDiscountAmt").toString()); //每满多少优惠多少的金额
  403. discountAmt = gasoilDiscountAmt_b; //优惠金额
  404. amt = receivableAmt_b.subtract(gasoilDiscountAmt_b); //实收金额
  405. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_UP); //加油升数
  406. discountPrice = amt.divide(oilLiters); //优惠价格
  407. resultMap.put("discountAmt", discountAmt.doubleValue());
  408. resultMap.put("amt", amt.doubleValue());
  409. resultMap.put("oilLiters", oilLiters.doubleValue());
  410. resultMap.put("discountPrice", discountPrice.doubleValue());
  411. flag = false;
  412. break;
  413. }
  414. }
  415. if(flag){ //用户加油金额不符合优惠条件
  416. resultMap.put("discountAmt", 0);
  417. resultMap.put("amt", receivableAmt.doubleValue());
  418. oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_UP);
  419. resultMap.put("oilLiters", oilLiters.doubleValue());
  420. resultMap.put("discountPrice", oilPrice);
  421. }
  422. }else {
  423. //没有优惠规则
  424. resultMap.put("discountAmt", 0);
  425. resultMap.put("amt", receivableAmt.doubleValue());
  426. oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_UP);
  427. resultMap.put("oilLiters", oilLiters.doubleValue());
  428. resultMap.put("discountPrice", oilPrice);
  429. }
  430. break;
  431. case "4": //4.独立直降
  432. params.put("discountPlanType", "3");
  433. //营销方案
  434. List<Map> customerMarkertPlanList_3 = customerGradeServices.getCustomerMarkertPlanList(params);
  435. if(customerMarkertPlanList_3 !=null && customerMarkertPlanList_3.size() >0){
  436. boolean flag= true;
  437. for(Map m : customerMarkertPlanList_3){
  438. BigDecimal discountAmt_b = new BigDecimal(m.get("discountAmt").toString()); //条件金额
  439. BigDecimal gasoilDiscountAmt_b = new BigDecimal(m.get("gasoilDiscountAmt").toString());
  440. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_UP); //加油升数
  441. if(m.get("discountTerm").toString().equals("1")){ //按升数优惠
  442. if(oilLiters.compareTo(discountAmt_b) ==1 || oilLiters.compareTo(discountAmt_b) ==0){
  443. //计算
  444. discountAmt = oilLiters.multiply(gasoilDiscountAmt_b);
  445. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_UP);
  446. amt = receivableAmt_b.subtract(discountAmt);
  447. discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_UP); //优惠价格
  448. resultMap.put("discountAmt", discountAmt.doubleValue());
  449. resultMap.put("amt", amt.doubleValue());
  450. resultMap.put("oilLiters", oilLiters.doubleValue());
  451. resultMap.put("discountPrice", discountPrice.doubleValue());
  452. flag =false;
  453. break;
  454. }
  455. }else if(m.get("discountTerm").toString().equals("2")){ //按金额优惠
  456. if(receivableAmt_b.compareTo(discountAmt_b) ==1 || receivableAmt_b.compareTo(discountAmt_b) ==0){
  457. discountAmt = oilLiters.multiply(gasoilDiscountAmt_b);
  458. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_UP);
  459. amt = receivableAmt_b.subtract(discountAmt);
  460. discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_UP); //优惠价格
  461. resultMap.put("discountAmt", discountAmt.doubleValue());
  462. resultMap.put("amt", amt.doubleValue());
  463. resultMap.put("oilLiters", oilLiters.doubleValue());
  464. resultMap.put("discountPrice", discountPrice.doubleValue());
  465. flag =false;
  466. break;
  467. }
  468. }
  469. }
  470. if(flag){
  471. resultMap.put("discountAmt", 0);
  472. resultMap.put("amt", receivableAmt.doubleValue());
  473. oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_UP);
  474. resultMap.put("oilLiters", oilLiters.doubleValue());
  475. resultMap.put("discountPrice", oilPrice);
  476. }
  477. }else {
  478. //没有优惠规则
  479. resultMap.put("discountAmt", 0);
  480. resultMap.put("amt", receivableAmt.doubleValue());
  481. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_UP);
  482. resultMap.put("oilLiters", oilLiters.doubleValue());
  483. resultMap.put("discountPrice", oilPrice);
  484. }
  485. break;
  486. }
  487. //resultData = ResultData.success(resultMap);
  488. }
  489. return resultMap;
  490. }
  491. /*
  492. * ===========================计算订单金额======================================
  493. * 第一次修订时间:2021年4月20日15:31:30
  494. * 修订标题:修订优惠计算
  495. * 修订内容 :
  496. * 修改为目前只有三种优惠:根据立减、满减、独立直降;
  497. * 注释:优惠为 1 是:表示不走满减、立减、独立直降优惠,只走等级直降的优惠;
  498. * 条件规则:
  499. * 一,优先满减(只有一档,不存在多档!)、立减(只有一档,不存在多档!)、独立直降(只有一档)
  500. * 二,若用户消费门槛 >= 优惠门槛,且启用优惠叠加时,则计算优惠规则和等级优惠规则;
  501. * 若用户消费门槛 >= 优惠门槛,且不启用优惠叠加时,则只计算优惠规则;
  502. * 若用户消费门槛 < 优惠门槛,且启用优惠叠加时,则只计算等级规则;
  503. * 若用户消费门槛 < 优惠门槛,且不启用优惠叠加时,则只计算等级规则;
  504. *
  505. * ===========================================================================
  506. */
  507. public Map<String ,Object> calculateAmtNew(PayOrderRequest payOrderRequest){
  508. Map<String ,Object> resultMap = new HashMap<>();
  509. if(payOrderRequest.getReceivableAmt() !=null){ //应收金额
  510. Double receivableAmt = payOrderRequest.getReceivableAmt(); //应收金额 用户输入的金额
  511. Integer stationId = payOrderRequest.getStationId(); //油站ID
  512. //String oilGun = payOrderRequest.getOilGun(); //油枪号
  513. String oilName = payOrderRequest.getOilName(); //油品名称
  514. String openId = payOrderRequest.getOpenId(); //用户的openId
  515. String mobilePhone = payOrderRequest.getCustomerPhone(); //客户手机号
  516. //获取油品价格
  517. StationOilPrice stationOilPrice = new StationOilPrice();
  518. stationOilPrice.setStationId(stationId);
  519. stationOilPrice.setOilName(oilName);
  520. StationOilPrice oilPriceInfo = stationService.getStationOilPrice(stationOilPrice);
  521. String oilPrice = oilPriceInfo.getOilPrice();
  522. //String stationNanme = oilPriceInfo.getStationNanme();
  523. BigDecimal oilLiters = new BigDecimal(0);
  524. BigDecimal discountAmt =new BigDecimal(0);
  525. BigDecimal discountPrice = null;
  526. BigDecimal amt = null;
  527. //resultMap.put("stationName", stationNanme);
  528. resultMap.put("oilPrice", oilPrice); //油品价格
  529. resultMap.put("receivableAmt", receivableAmt); //应收价格
  530. //该油站的优惠方式
  531. String stationDiscountWay = stationService.getStationDiscountWay(stationId.toString());
  532. Map<String ,Object> params = new HashMap<String, Object>();
  533. params.put("stationId", stationId);
  534. params.put("oilName", oilName);
  535. params.put("receivableAmt", receivableAmt);
  536. params.put("oilPrice", oilPrice);
  537. params.put("mobilePhone", mobilePhone);
  538. params.put("minaOpenid", openId);
  539. BigDecimal receivableAmt_b = new BigDecimal(receivableAmt); //传入的应收金额
  540. switch (stationDiscountWay){
  541. case "1": //1.等级直降
  542. //查询等级直降的优惠方式
  543. //查询客户等级信息
  544. List<Map<String, Object>> customerGradeInfo = customerGradeServices.getCustomerGradeInfo(params);
  545. if(customerGradeInfo != null && customerGradeInfo.size() >0){ //已存在客户等级信息
  546. //使用客户已存在的等级计算优惠
  547. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
  548. if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
  549. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
  550. }
  551. if(customerGradeInfo.get(0).containsKey("gasoilDiscountLitre")){ //表示可以查到客户在当前油站的等级信息,油站等级体系没有删除
  552. if(customerGradeInfo.get(0).get("gasoilDiscountLitre") !=null){
  553. discountAmt = oilLiters.multiply(new BigDecimal(customerGradeInfo.get(0).get("gasoilDiscountLitre").toString()));
  554. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  555. }else {
  556. discountAmt =new BigDecimal("0.00");
  557. }
  558. }else {
  559. //如果存在客户等级,但油站修改等级体系,客户等级已超出等级体系时
  560. List<Map> cList = customerGradeServices.getCustomerGradeListOrderByDesc(params);
  561. if(cList !=null && cList.size() >0){
  562. CustomerManage customerManage = new CustomerManage();
  563. customerManage.setStationId(Integer.valueOf(params.get("stationId").toString())); //油站ID
  564. customerManage.setOilName(params.get("oilName").toString()); //油品名称
  565. customerManage.setPhoneNumber(params.get("mobilePhone").toString());
  566. customerManage.setMinaOpenid(params.get("minaOpenid").toString());
  567. CustomerManage existCustomer = customerManagerService.getCustomerManageInfo(customerManage); //已存在用户信息 existCustomer
  568. for(Map m : cList){
  569. if(Double.valueOf(existCustomer.getAmt().toString()) >= Double.valueOf(m.get("memberConditStart").toString())){
  570. discountAmt = oilLiters.multiply(new BigDecimal(cList.get(0).get("gasoilDiscountLitre").toString()));
  571. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  572. break;
  573. }
  574. }
  575. }
  576. }
  577. amt = receivableAmt_b.subtract(discountAmt);
  578. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  579. resultMap.put("discountAmt", discountAmt.doubleValue());
  580. resultMap.put("amt", amt.doubleValue());
  581. resultMap.put("oilLiters", oilLiters.doubleValue());
  582. resultMap.put("discountPrice", discountPrice.doubleValue());
  583. }else {
  584. //不存在客户等级信息时,查询油站的客户等级直降优惠(查询结果根据会员成长值条件做升序查询)
  585. List<Map> customerGradeList = customerGradeServices.getCustomerGradeList(params);
  586. //如果存在
  587. if(customerGradeList !=null && customerGradeList.size() >0){
  588. //用户第一次购买 取等级最低初始会员的优惠条件计算
  589. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
  590. if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
  591. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
  592. }
  593. if(customerGradeList.get(0).get("gasoilDiscountLitre") !=null){
  594. discountAmt = oilLiters.multiply(new BigDecimal(customerGradeList.get(0).get("gasoilDiscountLitre").toString()));
  595. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  596. }else {
  597. discountAmt =new BigDecimal("0.00");
  598. }
  599. amt = receivableAmt_b.subtract(discountAmt);
  600. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  601. resultMap.put("discountAmt", discountAmt.doubleValue());
  602. resultMap.put("amt", amt.doubleValue());
  603. resultMap.put("oilLiters", oilLiters.doubleValue());
  604. resultMap.put("discountPrice", discountPrice.doubleValue());
  605. }else {
  606. //不存在
  607. resultMap.put("discountAmt", 0);
  608. resultMap.put("amt", receivableAmt.doubleValue());
  609. oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_UP);
  610. resultMap.put("oilLiters", oilLiters.doubleValue());
  611. resultMap.put("discountPrice", oilPrice);
  612. }
  613. }
  614. break;
  615. case "2": //2.满减
  616. params.put("discountPlanType", "1");
  617. //营销方案
  618. List<Map> customerMarkertPlanList_1 = customerGradeServices.getCustomerMarkertPlanList(params);
  619. //有优惠规则
  620. if(customerMarkertPlanList_1 !=null && customerMarkertPlanList_1.size() >0){
  621. //是否共享会员优惠叠加: 1 代表是; 2 代表否;
  622. String vipDiscountyPlus = customerMarkertPlanList_1.get(0).get("vipDiscountyPlus").toString();
  623. //如果当前应收金额大于条件金额
  624. BigDecimal discountAmt_b = new BigDecimal(customerMarkertPlanList_1.get(0).get("discountAmt").toString()); //条件金额
  625. if(receivableAmt_b.compareTo(discountAmt_b) ==1 || receivableAmt_b.compareTo(discountAmt_b) ==0){
  626. BigDecimal gasoilDiscountAmt_b = new BigDecimal(customerMarkertPlanList_1.get(0).get("gasoilDiscountAmt").toString()); //每满多少优惠多少的金额
  627. discountAmt = receivableAmt_b.divide(discountAmt_b, 2, BigDecimal.ROUND_HALF_UP).multiply(gasoilDiscountAmt_b); //优惠金额
  628. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  629. amt = receivableAmt_b.subtract(discountAmt); //实收金额
  630. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
  631. if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
  632. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
  633. }
  634. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  635. switch (vipDiscountyPlus){
  636. case "1":
  637. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  638. discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
  639. amt = receivableAmt_b.subtract(discountAmt);
  640. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  641. resultMap.put("discountAmt", discountAmt.doubleValue());
  642. resultMap.put("amt", amt.doubleValue());
  643. resultMap.put("oilLiters", oilLiters.doubleValue());
  644. resultMap.put("discountPrice", discountPrice.doubleValue());
  645. break;
  646. case "2":
  647. resultMap.put("discountAmt", discountAmt.doubleValue());
  648. resultMap.put("amt", amt.doubleValue());
  649. resultMap.put("oilLiters", oilLiters.doubleValue());
  650. resultMap.put("discountPrice", discountPrice.doubleValue());
  651. break;
  652. }
  653. }else{
  654. switch (vipDiscountyPlus){
  655. case "1":
  656. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  657. discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
  658. oilLiters = new BigDecimal(map.get("oilLiters").toString());
  659. amt = receivableAmt_b.subtract(discountAmt);
  660. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  661. resultMap.put("discountAmt", discountAmt.doubleValue());
  662. resultMap.put("amt", amt.doubleValue());
  663. resultMap.put("oilLiters", oilLiters.doubleValue());
  664. resultMap.put("discountPrice", discountPrice.doubleValue());
  665. break;
  666. case "2":
  667. resultMap.put("discountAmt", 0);
  668. resultMap.put("amt", receivableAmt.doubleValue());
  669. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
  670. if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
  671. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
  672. }
  673. resultMap.put("oilLiters", oilLiters.doubleValue());
  674. resultMap.put("discountPrice", oilPrice);
  675. break;
  676. }
  677. }
  678. }else {
  679. //用户没有设置优惠规则时
  680. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  681. resultMap.put("discountAmt", map.get("discountAmt").toString());
  682. resultMap.put("amt", map.get("amt").toString());
  683. resultMap.put("oilLiters", map.get("oilLiters").toString());
  684. resultMap.put("discountPrice", map.get("discountPrice").toString());
  685. }
  686. break;
  687. case "3": //3.立减
  688. params.put("discountPlanType", "2");
  689. //营销方案
  690. List<Map> customerMarkertPlanList_2 = customerGradeServices.getCustomerMarkertPlanList(params);
  691. if(customerMarkertPlanList_2 !=null && customerMarkertPlanList_2.size() >0){
  692. //是否共享会员优惠叠加: 1 代表是; 2 代表否;
  693. String vipDiscountyPlus = customerMarkertPlanList_2.get(0).get("vipDiscountyPlus").toString();
  694. //如果当前应收金额大于条件金额
  695. BigDecimal discountAmt_b = new BigDecimal(customerMarkertPlanList_2.get(0).get("discountAmt").toString()); //条件金额
  696. if(receivableAmt_b.compareTo(discountAmt_b) ==1 || receivableAmt_b.compareTo(discountAmt_b) ==0){
  697. BigDecimal gasoilDiscountAmt_b = new BigDecimal(customerMarkertPlanList_2.get(0).get("gasoilDiscountAmt").toString()); //满多少优惠多少的金额
  698. discountAmt = gasoilDiscountAmt_b; //优惠金额
  699. amt = receivableAmt_b.subtract(gasoilDiscountAmt_b); //实收金额
  700. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
  701. if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
  702. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
  703. }
  704. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_UP);
  705. switch (vipDiscountyPlus){
  706. case "1":
  707. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  708. discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
  709. amt = receivableAmt_b.subtract(discountAmt);
  710. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  711. //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_UP);
  712. resultMap.put("discountAmt", discountAmt.doubleValue());
  713. resultMap.put("amt", amt.doubleValue());
  714. resultMap.put("oilLiters", oilLiters.doubleValue());
  715. resultMap.put("discountPrice", discountPrice.doubleValue());
  716. break;
  717. case "2":
  718. resultMap.put("discountAmt", discountAmt.doubleValue());
  719. resultMap.put("amt", amt.doubleValue());
  720. resultMap.put("oilLiters", oilLiters.doubleValue());
  721. resultMap.put("discountPrice", discountPrice.doubleValue());
  722. break;
  723. }
  724. }else {
  725. //不满足优惠条件
  726. switch (vipDiscountyPlus) {
  727. case "1":
  728. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  729. discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
  730. oilLiters = new BigDecimal(map.get("oilLiters").toString());
  731. amt = receivableAmt_b.subtract(discountAmt);
  732. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  733. //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_UP);
  734. resultMap.put("discountAmt", discountAmt.doubleValue());
  735. resultMap.put("amt", amt.doubleValue());
  736. resultMap.put("oilLiters", oilLiters.doubleValue());
  737. resultMap.put("discountPrice", discountPrice.doubleValue());
  738. break;
  739. case "2":
  740. resultMap.put("discountAmt", 0);
  741. resultMap.put("amt", receivableAmt.doubleValue());
  742. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
  743. if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
  744. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
  745. }
  746. resultMap.put("oilLiters", oilLiters.doubleValue());
  747. resultMap.put("discountPrice", oilPrice);
  748. break;
  749. }
  750. }
  751. }else {
  752. //用户没有设置优惠规则时
  753. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  754. resultMap.put("discountAmt", map.get("discountAmt").toString());
  755. resultMap.put("amt", map.get("amt").toString());
  756. resultMap.put("oilLiters", map.get("oilLiters").toString());
  757. resultMap.put("discountPrice", map.get("discountPrice").toString());
  758. }
  759. break;
  760. case "4": //4.独立直降
  761. params.put("discountPlanType", "3");
  762. //营销方案
  763. List<Map> customerMarkertPlanList_3 = customerGradeServices.getCustomerMarkertPlanList(params);
  764. if(customerMarkertPlanList_3 !=null && customerMarkertPlanList_3.size() >0){
  765. //是否共享会员优惠叠加: 1 代表是; 2 代表否;
  766. String vipDiscountyPlus_4 = customerMarkertPlanList_3.get(0).get("vipDiscountyPlus").toString();
  767. BigDecimal discountAmt_b = new BigDecimal(customerMarkertPlanList_3.get(0).get("discountAmt").toString()); //条件金额
  768. BigDecimal gasoilDiscountAmt_b = new BigDecimal(customerMarkertPlanList_3.get(0).get("gasoilDiscountAmt").toString());
  769. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
  770. if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
  771. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
  772. }
  773. if(customerMarkertPlanList_3.get(0).get("discountTerm").toString().equals("1")){ //按升数优惠
  774. if(oilLiters.compareTo(discountAmt_b) ==1 || oilLiters.compareTo(discountAmt_b) ==0){
  775. //计算
  776. discountAmt = oilLiters.multiply(gasoilDiscountAmt_b);
  777. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  778. amt = receivableAmt_b.subtract(discountAmt);
  779. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  780. //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_UP); //优惠价格
  781. if(vipDiscountyPlus_4.equals("1")){
  782. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  783. discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
  784. amt = receivableAmt_b.subtract(discountAmt);
  785. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  786. //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_UP);
  787. }
  788. resultMap.put("discountAmt", discountAmt.doubleValue());
  789. resultMap.put("amt", amt.doubleValue());
  790. resultMap.put("oilLiters", oilLiters.doubleValue());
  791. resultMap.put("discountPrice", discountPrice.doubleValue());
  792. break;
  793. }else {
  794. //不满足优惠条件
  795. switch (vipDiscountyPlus_4) {
  796. case "1":
  797. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  798. discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
  799. oilLiters = new BigDecimal(map.get("oilLiters").toString());
  800. amt = receivableAmt_b.subtract(discountAmt);
  801. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  802. resultMap.put("discountAmt", discountAmt.doubleValue());
  803. resultMap.put("amt", amt.doubleValue());
  804. resultMap.put("oilLiters", oilLiters.doubleValue());
  805. resultMap.put("discountPrice", discountPrice.doubleValue());
  806. break;
  807. case "2":
  808. resultMap.put("discountAmt", 0);
  809. resultMap.put("amt", receivableAmt.doubleValue());
  810. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
  811. if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
  812. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
  813. }
  814. resultMap.put("oilLiters", oilLiters.doubleValue());
  815. resultMap.put("discountPrice", oilPrice);
  816. break;
  817. }
  818. }
  819. }else if(customerMarkertPlanList_3.get(0).get("discountTerm").toString().equals("2")){ //按金额优惠
  820. if(receivableAmt_b.compareTo(discountAmt_b) ==1 || receivableAmt_b.compareTo(discountAmt_b) ==0){
  821. discountAmt = oilLiters.multiply(gasoilDiscountAmt_b);
  822. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  823. amt = receivableAmt_b.subtract(discountAmt);
  824. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  825. //是否共享会员优惠叠加
  826. if(vipDiscountyPlus_4.equals("1")){
  827. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  828. discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
  829. amt = receivableAmt_b.subtract(discountAmt);
  830. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  831. //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_UP);
  832. }
  833. resultMap.put("discountAmt", discountAmt.doubleValue());
  834. resultMap.put("amt", amt.doubleValue());
  835. resultMap.put("oilLiters", oilLiters.doubleValue());
  836. resultMap.put("discountPrice", discountPrice.doubleValue());
  837. break;
  838. }else {
  839. //不满足优惠条件
  840. switch (vipDiscountyPlus_4) {
  841. case "1":
  842. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  843. discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
  844. oilLiters = new BigDecimal(map.get("oilLiters").toString());
  845. amt = receivableAmt_b.subtract(discountAmt);
  846. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  847. resultMap.put("discountAmt", discountAmt.doubleValue());
  848. resultMap.put("amt", amt.doubleValue());
  849. resultMap.put("oilLiters", oilLiters.doubleValue());
  850. resultMap.put("discountPrice", discountPrice.doubleValue());
  851. break;
  852. case "2":
  853. resultMap.put("discountAmt", 0);
  854. resultMap.put("amt", receivableAmt.doubleValue());
  855. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
  856. if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
  857. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
  858. }
  859. resultMap.put("oilLiters", oilLiters.doubleValue());
  860. resultMap.put("discountPrice", oilPrice);
  861. break;
  862. }
  863. }
  864. }
  865. }else {
  866. //没有设置优惠条件时
  867. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  868. resultMap.put("discountAmt", map.get("discountAmt").toString());
  869. resultMap.put("amt", map.get("amt").toString());
  870. resultMap.put("oilLiters", map.get("oilLiters").toString());
  871. resultMap.put("discountPrice", map.get("discountPrice").toString());
  872. }
  873. break;
  874. }
  875. }
  876. return resultMap;
  877. }
  878. //计算
  879. //计算等级直降
  880. public Map<String, Object> calcuteDengJiZhiJiang(Map params){
  881. Double receivableAmt = Double.valueOf(params.get("receivableAmt").toString());
  882. Map<String, Object> resultMap = new HashMap();
  883. BigDecimal oilLiters = null;
  884. BigDecimal discountAmt =new BigDecimal(0);
  885. BigDecimal discountPrice = null;
  886. BigDecimal amt = null;
  887. BigDecimal receivableAmt_b = new BigDecimal(receivableAmt); //传入的应收金额
  888. String oilPrice = params.get("oilPrice").toString();
  889. List<Map<String, Object>> customerGradeInfo = customerGradeServices.getCustomerGradeInfo(params);
  890. if(customerGradeInfo != null && customerGradeInfo.size() >0){ //已存在客户等级信息
  891. //使用客户已存在的等级计算优惠
  892. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
  893. if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
  894. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
  895. }
  896. if(customerGradeInfo.get(0).containsKey("gasoilDiscountLitre")){
  897. if(customerGradeInfo.get(0).get("gasoilDiscountLitre") !=null){
  898. discountAmt = oilLiters.multiply(new BigDecimal(customerGradeInfo.get(0).get("gasoilDiscountLitre").toString()));
  899. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  900. }else {
  901. discountAmt =new BigDecimal("0.00");
  902. }
  903. }else {
  904. //如果存在客户等级,但油站修改等级体系,客户等级已超出等级体系时
  905. List<Map> cList = customerGradeServices.getCustomerGradeListOrderByDesc(params);
  906. if(cList !=null && cList.size() >0){
  907. CustomerManage customerManage = new CustomerManage();
  908. customerManage.setStationId(Integer.valueOf(params.get("stationId").toString())); //油站ID
  909. customerManage.setOilName(params.get("oilName").toString()); //油品名称
  910. customerManage.setPhoneNumber(params.get("mobilePhone").toString());
  911. customerManage.setMinaOpenid(params.get("minaOpenid").toString());
  912. CustomerManage existCustomer = customerManagerService.getCustomerManageInfo(customerManage); //已存在用户信息 existCustomer
  913. for(Map m : cList){
  914. if(Double.valueOf(existCustomer.getAmt().toString()) >= Double.valueOf(m.get("memberConditStart").toString())){
  915. discountAmt = oilLiters.multiply(new BigDecimal(cList.get(0).get("gasoilDiscountLitre").toString()));
  916. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  917. break;
  918. }
  919. }
  920. }
  921. }
  922. amt = receivableAmt_b.subtract(discountAmt);
  923. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  924. //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_UP);
  925. resultMap.put("discountAmt", discountAmt.doubleValue());
  926. resultMap.put("amt", amt.doubleValue());
  927. resultMap.put("oilLiters", oilLiters.doubleValue());
  928. resultMap.put("discountPrice", discountPrice.doubleValue());
  929. }else {
  930. //不存在客户等级信息时,查询油站的客户等级直降优惠(查询结果根据会员成长值条件做升序查询)
  931. List<Map> customerGradeList = customerGradeServices.getCustomerGradeList(params);
  932. //如果存在
  933. if(customerGradeList !=null && customerGradeList.size() >0){
  934. //用户第一次购买 取等级最低初始会员的优惠条件计算
  935. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
  936. if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
  937. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
  938. }
  939. if(customerGradeList.get(0).get("gasoilDiscountLitre") !=null){
  940. discountAmt = oilLiters.multiply(new BigDecimal(customerGradeList.get(0).get("gasoilDiscountLitre").toString()));
  941. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  942. }else {
  943. discountAmt =new BigDecimal("0.00");
  944. }
  945. amt = receivableAmt_b.subtract(discountAmt);
  946. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  947. //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_UP);
  948. resultMap.put("discountAmt", discountAmt.doubleValue());
  949. resultMap.put("amt", amt.doubleValue());
  950. resultMap.put("oilLiters", oilLiters.doubleValue());
  951. resultMap.put("discountPrice", discountPrice.doubleValue());
  952. }else {
  953. //不存在
  954. resultMap.put("discountAmt", 0);
  955. resultMap.put("amt", receivableAmt.doubleValue());
  956. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
  957. if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
  958. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
  959. }
  960. resultMap.put("oilLiters", oilLiters.doubleValue());
  961. resultMap.put("discountPrice", oilPrice);
  962. }
  963. }
  964. return resultMap;
  965. }
  966. //计算满减
  967. public Map<String, Object> calcuteManjian(Map params){
  968. Map<String, Object> resultMap = new HashMap<>();
  969. Double receivableAmt = Double.valueOf(params.get("receivableAmt").toString());
  970. BigDecimal oilLiters = null;
  971. BigDecimal discountAmt =null;
  972. BigDecimal discountPrice = null;
  973. BigDecimal amt = null;
  974. BigDecimal receivableAmt_b = new BigDecimal(receivableAmt); //传入的应收金额
  975. String oilPrice = params.get("oilPrice").toString();
  976. params.put("discountPlanType", "1");
  977. //营销方案
  978. List<Map> customerMarkertPlanList_1 = customerGradeServices.getCustomerMarkertPlanList(params);
  979. //有优惠规则
  980. if(customerMarkertPlanList_1 !=null && customerMarkertPlanList_1.size() >0){
  981. //是否共享会员优惠叠加: 1 代表是; 2 代表否;
  982. String vipDiscountyPlus = customerMarkertPlanList_1.get(0).get("vipDiscountyPlus").toString();
  983. //如果当前应收金额大于条件金额
  984. BigDecimal discountAmt_b = new BigDecimal(customerMarkertPlanList_1.get(0).get("discountAmt").toString()); //条件金额
  985. if(receivableAmt_b.compareTo(discountAmt_b) ==1 || receivableAmt_b.compareTo(discountAmt_b) ==0){
  986. BigDecimal gasoilDiscountAmt_b = new BigDecimal(customerMarkertPlanList_1.get(0).get("gasoilDiscountAmt").toString()); //每满多少优惠多少的金额
  987. discountAmt = receivableAmt_b.divide(discountAmt_b, 2, BigDecimal.ROUND_UP).multiply(gasoilDiscountAmt_b); //优惠金额
  988. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  989. amt = receivableAmt_b.subtract(discountAmt); //实收金额
  990. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
  991. if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
  992. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
  993. }
  994. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  995. switch (vipDiscountyPlus){
  996. case "1":
  997. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  998. discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
  999. amt = receivableAmt_b.subtract(discountAmt);
  1000. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  1001. resultMap.put("discountAmt", discountAmt.doubleValue());
  1002. resultMap.put("amt", amt.doubleValue());
  1003. resultMap.put("oilLiters", oilLiters.doubleValue());
  1004. resultMap.put("discountPrice", discountPrice.doubleValue());
  1005. break;
  1006. case "2":
  1007. resultMap.put("discountAmt", discountAmt.doubleValue());
  1008. resultMap.put("amt", amt.doubleValue());
  1009. resultMap.put("oilLiters", oilLiters.doubleValue());
  1010. resultMap.put("discountPrice", discountPrice.doubleValue());
  1011. break;
  1012. }
  1013. }else{
  1014. switch (vipDiscountyPlus){
  1015. case "1":
  1016. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  1017. discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
  1018. amt = receivableAmt_b.subtract(discountAmt);
  1019. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  1020. resultMap.put("discountAmt", discountAmt.doubleValue());
  1021. resultMap.put("amt", amt.doubleValue());
  1022. resultMap.put("oilLiters", oilLiters.doubleValue());
  1023. resultMap.put("discountPrice", discountPrice.doubleValue());
  1024. break;
  1025. case "2":
  1026. resultMap.put("discountAmt", 0);
  1027. resultMap.put("amt", receivableAmt.doubleValue());
  1028. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
  1029. if(!oilLiters.toString().substring(oilLiters.toString().indexOf(".") + 1).equals("00")){
  1030. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_UP);
  1031. }
  1032. resultMap.put("oilLiters", oilLiters.doubleValue());
  1033. resultMap.put("discountPrice", oilPrice);
  1034. break;
  1035. }
  1036. }
  1037. }else {
  1038. //用户没有设置优惠规则时
  1039. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  1040. resultMap.put("discountAmt", map.get("discountAmt").toString());
  1041. resultMap.put("amt", map.get("amt").toString());
  1042. resultMap.put("oilLiters", map.get("oilLiters").toString());
  1043. resultMap.put("discountPrice", map.get("discountPrice").toString());
  1044. }
  1045. return resultMap;
  1046. };
  1047. /**
  1048. * 根据订单号获取订单信息
  1049. * produces="application/json;charset=UTF-8",consumes = "application/json",
  1050. * @RequestBody String orderno
  1051. * @param
  1052. * @return
  1053. */
  1054. @RequestMapping(value = "/getPayOrderList", method = RequestMethod.GET)
  1055. @ResponseBody
  1056. public String getPayOrderList(@RequestParam String orderNo,String openId,String userType){
  1057. Gson gson =new Gson();
  1058. ResultData resultData =null;
  1059. //根据token 和 usertype来判断用户是否存在
  1060. boolean flag = appUserInfoService.selectAppUserInfo(openId,userType);
  1061. if(flag){
  1062. if(StringUtils.isNotBlank(orderNo)){
  1063. PayOrderRequest payOrderRequest =new PayOrderRequest();
  1064. payOrderRequest.setOrderNo(orderNo);
  1065. List<PayOrder> payOrderList = payOrderService.getPayOrderList(payOrderRequest.getOrderNo());
  1066. resultData=ResultData.success(payOrderList);
  1067. }else {
  1068. resultData=ResultData.error(CodeMsg.SEARCH_FAIL);
  1069. }
  1070. }else{
  1071. resultData=ResultData.error(CodeMsg.USER_NOT_EXSIST);
  1072. }
  1073. return gson.toJson(resultData);
  1074. }
  1075. /*
  1076. * 根据用户openId和用户类型userType查询用户订单
  1077. * @param openId
  1078. * @param userType
  1079. * @return
  1080. */
  1081. @RequestMapping(value = "/getOrderListByOpenIdAndUserType", method = RequestMethod.GET)
  1082. @ResponseBody
  1083. public String getOrderListByOpenIdAndUserType(@RequestParam("openId") String openId, @RequestParam("userType") String userType){
  1084. Gson gson =new Gson();
  1085. ResultData resultData =null;
  1086. if(StringUtils.isNotBlank(openId) && StringUtils.isNotBlank(userType)){
  1087. AppUserInfo appUserInfo = new AppUserInfo();
  1088. switch (userType){
  1089. case "1":
  1090. appUserInfo.setBlogOpenid(openId);
  1091. break;
  1092. case "2":
  1093. appUserInfo.setMinaOpenid(openId);
  1094. break;
  1095. }
  1096. List<PayOrder> payOrderList = payOrderService.getOrderListByOpenIdAndUserType(appUserInfo);
  1097. resultData=ResultData.success(payOrderList);
  1098. }else {
  1099. resultData=ResultData.error(CodeMsg.SEARCH_FAIL);
  1100. }
  1101. return gson.toJson(resultData);
  1102. }
  1103. }