PayOrderController.java 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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. //小程序支付生成订单
  41. @RequestMapping(value = "/AddPayOrderInfoNew", consumes = "application/json", method = RequestMethod.POST)
  42. @ResponseBody
  43. @Transactional
  44. public String AddPayOrderInfoNew(@RequestBody PayOrderRequest payOrderRequest){
  45. logger.info("获取订单前端传来参数openId:"+ payOrderRequest.getOpenId());
  46. logger.info("获取订单前端传来参数userType:"+ payOrderRequest.getUserType());
  47. Gson gson =new Gson();
  48. ResultData resultData =null;
  49. AppUserInfo appUserInfo =new AppUserInfo();
  50. appUserInfo.setUserType(payOrderRequest.getUserType()); //订单类型 是小程序还是公众号
  51. if(payOrderRequest.getUserType().equals("1")){
  52. appUserInfo.setBlogOpenid(payOrderRequest.getOpenId()); //1 是公众号
  53. }else if(payOrderRequest.getUserType().equals("2")){
  54. appUserInfo.setMinaOpenid(payOrderRequest.getOpenId()); //2是小程序
  55. }
  56. List<AppUserInfo> appUserInfoList =appUserInfoService.Authentication(appUserInfo);
  57. Map<String ,Object> resultMap = new HashMap<>(); //返回结果集
  58. //判断登录人员是否验证成功
  59. if(appUserInfoList!=null && appUserInfoList.size()>0){
  60. PayOrder payOrder = new PayOrder();
  61. payOrder.setConsumer(appUserInfoList.get(0).getBlogNickName()); //昵称
  62. payOrder.setConsumerId(appUserInfoList.get(0).getUserId()); //用户id
  63. Random random = new Random();
  64. String result="";
  65. for(int i=0;i<6;i++){
  66. result+=random.nextInt(10);
  67. }
  68. String ordNo=System.nanoTime()+result;
  69. payOrder.setOrderNo(ordNo); //订单号
  70. logger.info("生成的订单号:" + ordNo);
  71. payOrder.setDiscountCouponAmt(payOrderRequest.getDiscountCouponAmt()); //优惠劵金额
  72. payOrder.setDiscountCoupon(payOrderRequest.getDiscountCoupon()); //优惠券
  73. payOrder.setCarNo(payOrderRequest.getCarNo()); //车牌号、
  74. payOrder.setCustomerGrade(payOrderRequest.getCustomerGrade()); //客户电话
  75. payOrder.setMemberNo(payOrderRequest.getMemberNo()); //会员卡号
  76. payOrder.setMemberAmt(payOrderRequest.getMemberAmt()); //会员支付金额
  77. //payOrder.setPrintCount(payOrderRequest.getPrintCount()); //小票数量
  78. payOrder.setScore(payOrderRequest.getScore()); //积分
  79. payOrder.setStationId(payOrderRequest.getStationId()); //油站id
  80. payOrder.setStatus("0"); //创建订单 未支付状态 0
  81. if(StringUtils.isNotBlank(payOrderRequest.getUserType())){
  82. String userType = payOrderRequest.getUserType(); //支付方式:02公众号,03小程序',
  83. switch (userType){
  84. case "1":
  85. payOrder.setPayWay("02");
  86. break;
  87. case "2":
  88. payOrder.setPayWay("03");
  89. break;
  90. }
  91. }
  92. payOrder.setCreatedDate(new Date()); //创建时间
  93. payOrder.setOrderType(payOrderRequest.getOrderType()); //订单类型
  94. payOrder.setOilGun(payOrderRequest.getOilGun()); //油枪号
  95. //根据油站和油枪获取加油员
  96. HashMap<String, Object> params = new HashMap<>();
  97. params.put("oliGunNo", payOrderRequest.getOilGun());
  98. params.put("stationId", payOrderRequest.getStationId());
  99. String oilPersonnel = "";
  100. List<Map> mapResultList = payOrderMapper.selectPersonelName(params); //使用list原因:一个枪号对应多个加油员场景
  101. logger.info("加油员信息:"+ mapResultList.toString());
  102. if(mapResultList != null && mapResultList.size() > 0){
  103. for (Map per : mapResultList){
  104. if(per.containsKey("personelName") && per.get("personelName").toString() !=null && per.get("personelName").toString() !=""){
  105. oilPersonnel += per.get("personelName").toString() +",";
  106. }
  107. }
  108. if(oilPersonnel !=""){
  109. payOrder.setOilPersonnel(oilPersonnel.substring(0, oilPersonnel.length()-1)); //加油员
  110. }
  111. }
  112. String orderType = payOrderRequest.getOrderType(); //获取订单类型
  113. logger.info("订单类型:" + orderType);
  114. if(orderType.equals("1")){ //订单类型是油品时计算
  115. //resultMap = this.calculateAmt(payOrderRequest); //计算油价 加油升数 优惠金额 实收金额 优惠价格
  116. resultMap = this.calculateAmtNew(payOrderRequest); //计算油价 加油升数 优惠金额 实收金额 优惠价格
  117. payOrder.setOilPirce(resultMap.get("oilPrice").toString());
  118. payOrder.setOrderLiters(resultMap.get("oilLiters").toString());
  119. payOrder.setDiscountAmt(Double.valueOf(resultMap.get("discountAmt").toString()));
  120. payOrder.setAmt(Double.valueOf(resultMap.get("amt").toString()));
  121. String stationCardEnabledFlag = electronicMembershipCardService.getStationCardEnabledFlag(payOrderRequest.getStationId());
  122. logger.info("油站是否开启电子会员卡功能: " +stationCardEnabledFlag);
  123. //查询油站是否开启电子会员卡功能
  124. if(stationCardEnabledFlag.equals("1")){
  125. Map<String, String> map = this.selectCardBalance(payOrderRequest.getStationId(), payOrderRequest.getOilName(), appUserInfoList.get(0).getUnionId());
  126. resultMap.put("balance", map.get("balance")); //电子卡余额
  127. resultMap.put("cardOilsType", map.get("cardOilsType")); //电子卡类型1.汽油; 2.柴油
  128. }
  129. }
  130. payOrder.setReceivableAmt(payOrderRequest.getReceivableAmt()); //订单应收金额
  131. String oilName =payOrderRequest.getOilName();
  132. payOrder.setOilName(oilName); //油品名称
  133. switch (oilName){
  134. case "92#":
  135. payOrder.setOilType("2");
  136. break;
  137. case "95#":
  138. payOrder.setOilType("2");
  139. break;
  140. case "97#":
  141. payOrder.setOilType("2");
  142. break;
  143. case "0#":
  144. payOrder.setOilType("1");
  145. break;
  146. case "-10#":
  147. payOrder.setOilType("1");
  148. break;
  149. case "-20#":
  150. payOrder.setOilType("1");
  151. break;
  152. }
  153. payOrder.setOrderType(orderType);
  154. StationInfo stationInfo = new StationInfo();
  155. stationInfo.setStationId(payOrderRequest.getStationId());
  156. StationInfo stationInfos = stationService.selectStationInfo(stationInfo);
  157. if(stationInfos !=null){
  158. payOrder.setStationName(stationInfos.getStationName());
  159. //获取油站图片和名称
  160. resultMap.put("stationPic", stationInfos.getStationPic());
  161. resultMap.put("stationName", stationInfos.getStationName());
  162. }
  163. //payOrder.setPayType("wx"); //支付类型
  164. switch (orderType){
  165. case "1": //油品
  166. payOrderMapper.insertSelective(payOrder);
  167. resultMap.put("ordNo", ordNo);
  168. break;
  169. case "2": //非油品
  170. payOrder.setAmt(Double.valueOf(payOrderRequest.getReceivableAmt())); //非油品金额
  171. payOrder.setDiscountAmt(Double.valueOf(0)); //优惠金额
  172. payOrder.setOilPirce(payOrderRequest.getReceivableAmt().toString()); //非油品价格
  173. payOrder.setOrderLiters("0");
  174. resultMap.put("discountAmt", "0"); //优惠价格
  175. resultMap.put("amt", payOrderRequest.getReceivableAmt()); //实收价格
  176. resultMap.put("receivableAmt", payOrderRequest.getReceivableAmt()); //应收价格
  177. resultMap.put("oilLiters", "0");
  178. resultMap.put("discountPrice", "0");
  179. payOrderMapper.insertSelective(payOrder);
  180. resultMap.put("ordNo", ordNo);
  181. break;
  182. }
  183. resultData = ResultData.success(resultMap);
  184. }else{
  185. //登录人验证失败
  186. resultData=ResultData.error(CodeMsg.USER_NOT_EXSIST);
  187. }
  188. return gson.toJson(resultData);
  189. }
  190. //查询用户电子会员卡余额
  191. public Map<String, String> selectCardBalance(Integer stationId, String oilName, String unionId) {
  192. String cardOilsType ="";
  193. switch (oilName){ //根据油品名称存储电子卡油品类型 1.柴油 2 汽油
  194. case "92#":
  195. cardOilsType ="1";
  196. break;
  197. case "95#":
  198. cardOilsType ="1";
  199. break;
  200. case "97#":
  201. cardOilsType ="1";
  202. break;
  203. case "0#":
  204. cardOilsType ="2";
  205. break;
  206. case "-10#":
  207. cardOilsType ="2";
  208. break;
  209. case "-20#":
  210. cardOilsType ="2";
  211. break;
  212. }
  213. CustomerElectronicCard customerElectronicCard = new CustomerElectronicCard();
  214. customerElectronicCard.setCardOilsType(cardOilsType);
  215. customerElectronicCard.setStationId(stationId);
  216. customerElectronicCard.setUnionId(unionId);
  217. CustomerElectronicCard electronicCardInfo = electronicMembershipCardService.getElectronicCardInfoByUnionId(customerElectronicCard);
  218. BigDecimal balance = null;
  219. String cardOilsType1 ="";
  220. if(electronicCardInfo !=null){
  221. balance = electronicCardInfo.getAmt();
  222. cardOilsType1 = electronicCardInfo.getCardOilsType();
  223. }
  224. Map<String, String> result = new HashMap<>();
  225. result.put("balance",balance.toString());
  226. result.put("cardOilsType",cardOilsType1);
  227. return result;
  228. }
  229. /***
  230. * 计算订单金额
  231. * @return
  232. */
  233. public Map<String ,Object> calculateAmt(PayOrderRequest payOrderRequest){
  234. Map<String ,Object> resultMap = new HashMap<>();
  235. if(payOrderRequest.getReceivableAmt() !=null){ //应收金额
  236. Double receivableAmt = payOrderRequest.getReceivableAmt(); //应收金额 用户输入的金额
  237. Integer stationId = payOrderRequest.getStationId(); //油站ID
  238. //String oilGun = payOrderRequest.getOilGun(); //油枪号
  239. String oilName = payOrderRequest.getOilName(); //油品名称
  240. String openId = payOrderRequest.getOpenId(); //用户的openId
  241. String mobilePhone = payOrderRequest.getCustomerPhone(); //客户手机号
  242. //获取油品价格
  243. StationOilPrice stationOilPrice = new StationOilPrice();
  244. stationOilPrice.setStationId(stationId);
  245. stationOilPrice.setOilName(oilName);
  246. StationOilPrice oilPriceInfo = stationService.getStationOilPrice(stationOilPrice);
  247. String oilPrice = oilPriceInfo.getOilPrice();
  248. //String stationNanme = oilPriceInfo.getStationNanme();
  249. BigDecimal oilLiters = null;
  250. BigDecimal discountAmt =null;
  251. BigDecimal discountPrice = null;
  252. BigDecimal amt = null;
  253. //resultMap.put("stationName", stationNanme);
  254. resultMap.put("oilPrice", oilPrice); //油品价格
  255. resultMap.put("receivableAmt", receivableAmt); //应收价格
  256. //该油站的优惠方式
  257. String stationDiscountWay = stationService.getStationDiscountWay(stationId.toString());
  258. Map<String ,Object> params = new HashMap<String, Object>();
  259. params.put("stationId", stationId);
  260. params.put("oilName", oilName);
  261. BigDecimal receivableAmt_b = new BigDecimal(receivableAmt); //传入的应收金额
  262. switch (stationDiscountWay){
  263. case "1": //1.等级直降
  264. //查询等级直降的优惠方式
  265. params.put("mobilePhone", mobilePhone);
  266. params.put("minaOpenid", openId);
  267. //查询客户等级信息
  268. List<Map<String, Object>> customerGradeInfo = customerGradeServices.getCustomerGradeInfo(params);
  269. if(customerGradeInfo != null && customerGradeInfo.size() >0){ //已存在客户等级信息
  270. //使用客户已存在的等级计算优惠
  271. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP);
  272. discountAmt = oilLiters.multiply(new BigDecimal(customerGradeInfo.get(0).get("gasoilDiscountLitre").toString()));
  273. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  274. amt = receivableAmt_b.subtract(discountAmt);
  275. discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  276. resultMap.put("discountAmt", discountAmt.doubleValue());
  277. resultMap.put("amt", amt.doubleValue());
  278. resultMap.put("oilLiters", oilLiters.doubleValue());
  279. resultMap.put("discountPrice", discountPrice.doubleValue());
  280. }else {
  281. //不存在客户等级信息时,查询油站的客户等级直降优惠(查询结果根据会员成长值条件做升序查询)
  282. List<Map> customerGradeList = customerGradeServices.getCustomerGradeList(params);
  283. //如果存在
  284. if(customerGradeList !=null && customerGradeList.size() >0){
  285. //用户第一次购买 取等级最低初始会员的优惠条件计算
  286. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); //加油升数
  287. discountAmt = oilLiters.multiply(new BigDecimal(customerGradeList.get(0).get("gasoilDiscountLitre").toString()));
  288. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  289. amt = receivableAmt_b.subtract(discountAmt);
  290. discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  291. resultMap.put("discountAmt", discountAmt.doubleValue());
  292. resultMap.put("amt", amt.doubleValue());
  293. resultMap.put("oilLiters", oilLiters.doubleValue());
  294. resultMap.put("discountPrice", discountPrice.doubleValue());
  295. }else {
  296. //不存在
  297. resultMap.put("discountAmt", 0);
  298. resultMap.put("amt", receivableAmt.doubleValue());
  299. oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP);
  300. resultMap.put("oilLiters", oilLiters.doubleValue());
  301. resultMap.put("discountPrice", oilPrice);
  302. }
  303. }
  304. break;
  305. case "2": //2.满减
  306. params.put("discountPlanType", "1");
  307. //营销方案
  308. List<Map> customerMarkertPlanList_1 = customerGradeServices.getCustomerMarkertPlanList(params);
  309. //有优惠规则
  310. if(customerMarkertPlanList_1 !=null && customerMarkertPlanList_1.size() >0){
  311. boolean flag = true;
  312. for (Map m : customerMarkertPlanList_1){
  313. //如果当前应收金额大于条件金额
  314. BigDecimal discountAmt_b = new BigDecimal(m.get("discountAmt").toString()); //条件金额
  315. if(receivableAmt_b.compareTo(discountAmt_b) ==1 || receivableAmt_b.compareTo(discountAmt_b) ==0){
  316. BigDecimal gasoilDiscountAmt_b = new BigDecimal(m.get("gasoilDiscountAmt").toString()); //每满多少优惠多少的金额
  317. discountAmt = receivableAmt_b.divide(discountAmt_b, 2, BigDecimal.ROUND_HALF_UP).multiply(gasoilDiscountAmt_b); //优惠金额
  318. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  319. amt = receivableAmt_b.subtract(discountAmt); //实收金额
  320. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
  321. discountPrice = amt.divide(oilLiters); //优惠价格
  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. flag = false;
  327. break;
  328. }
  329. }
  330. if(flag){
  331. resultMap.put("discountAmt", 0);
  332. resultMap.put("amt", receivableAmt.doubleValue());
  333. oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP);
  334. resultMap.put("oilLiters", oilLiters.doubleValue());
  335. resultMap.put("discountPrice", oilPrice);
  336. }
  337. }else {
  338. //没有优惠规则
  339. resultMap.put("discountAmt", 0);
  340. resultMap.put("amt", receivableAmt.doubleValue());
  341. oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP);
  342. resultMap.put("oilLiters", oilLiters.doubleValue());
  343. resultMap.put("discountPrice", oilPrice);
  344. }
  345. break;
  346. case "3": //3.立减
  347. params.put("discountPlanType", "2");
  348. //营销方案
  349. List<Map> customerMarkertPlanList_2 = customerGradeServices.getCustomerMarkertPlanList(params);
  350. if(customerMarkertPlanList_2 !=null && customerMarkertPlanList_2.size() >0){
  351. boolean flag = true;
  352. for (Map m : customerMarkertPlanList_2){
  353. //如果当前应收金额大于条件金额
  354. BigDecimal discountAmt_b = new BigDecimal(m.get("discountAmt").toString()); //条件金额
  355. if(receivableAmt_b.compareTo(discountAmt_b) ==1 || receivableAmt_b.compareTo(discountAmt_b) ==0){
  356. BigDecimal gasoilDiscountAmt_b = new BigDecimal(m.get("gasoilDiscountAmt").toString()); //每满多少优惠多少的金额
  357. discountAmt = gasoilDiscountAmt_b; //优惠金额
  358. amt = receivableAmt_b.subtract(gasoilDiscountAmt_b); //实收金额
  359. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); //加油升数
  360. discountPrice = amt.divide(oilLiters); //优惠价格
  361. resultMap.put("discountAmt", discountAmt.doubleValue());
  362. resultMap.put("amt", amt.doubleValue());
  363. resultMap.put("oilLiters", oilLiters.doubleValue());
  364. resultMap.put("discountPrice", discountPrice.doubleValue());
  365. flag = false;
  366. break;
  367. }
  368. }
  369. if(flag){ //用户加油金额不符合优惠条件
  370. resultMap.put("discountAmt", 0);
  371. resultMap.put("amt", receivableAmt.doubleValue());
  372. oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP);
  373. resultMap.put("oilLiters", oilLiters.doubleValue());
  374. resultMap.put("discountPrice", oilPrice);
  375. }
  376. }else {
  377. //没有优惠规则
  378. resultMap.put("discountAmt", 0);
  379. resultMap.put("amt", receivableAmt.doubleValue());
  380. oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP);
  381. resultMap.put("oilLiters", oilLiters.doubleValue());
  382. resultMap.put("discountPrice", oilPrice);
  383. }
  384. break;
  385. case "4": //4.独立直降
  386. params.put("discountPlanType", "3");
  387. //营销方案
  388. List<Map> customerMarkertPlanList_3 = customerGradeServices.getCustomerMarkertPlanList(params);
  389. if(customerMarkertPlanList_3 !=null && customerMarkertPlanList_3.size() >0){
  390. boolean flag= true;
  391. for(Map m : customerMarkertPlanList_3){
  392. BigDecimal discountAmt_b = new BigDecimal(m.get("discountAmt").toString()); //条件金额
  393. BigDecimal gasoilDiscountAmt_b = new BigDecimal(m.get("gasoilDiscountAmt").toString());
  394. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); //加油升数
  395. if(m.get("discountTerm").toString().equals("1")){ //按升数优惠
  396. if(oilLiters.compareTo(discountAmt_b) ==1 || oilLiters.compareTo(discountAmt_b) ==0){
  397. //计算
  398. discountAmt = oilLiters.multiply(gasoilDiscountAmt_b);
  399. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  400. amt = receivableAmt_b.subtract(discountAmt);
  401. discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); //优惠价格
  402. resultMap.put("discountAmt", discountAmt.doubleValue());
  403. resultMap.put("amt", amt.doubleValue());
  404. resultMap.put("oilLiters", oilLiters.doubleValue());
  405. resultMap.put("discountPrice", discountPrice.doubleValue());
  406. flag =false;
  407. break;
  408. }
  409. }else if(m.get("discountTerm").toString().equals("2")){ //按金额优惠
  410. if(receivableAmt_b.compareTo(discountAmt_b) ==1 || receivableAmt_b.compareTo(discountAmt_b) ==0){
  411. discountAmt = oilLiters.multiply(gasoilDiscountAmt_b);
  412. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  413. amt = receivableAmt_b.subtract(discountAmt);
  414. discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); //优惠价格
  415. resultMap.put("discountAmt", discountAmt.doubleValue());
  416. resultMap.put("amt", amt.doubleValue());
  417. resultMap.put("oilLiters", oilLiters.doubleValue());
  418. resultMap.put("discountPrice", discountPrice.doubleValue());
  419. flag =false;
  420. break;
  421. }
  422. }
  423. }
  424. if(flag){
  425. resultMap.put("discountAmt", 0);
  426. resultMap.put("amt", receivableAmt.doubleValue());
  427. oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP);
  428. resultMap.put("oilLiters", oilLiters.doubleValue());
  429. resultMap.put("discountPrice", oilPrice);
  430. }
  431. }else {
  432. //没有优惠规则
  433. resultMap.put("discountAmt", 0);
  434. resultMap.put("amt", receivableAmt.doubleValue());
  435. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP);
  436. resultMap.put("oilLiters", oilLiters.doubleValue());
  437. resultMap.put("discountPrice", oilPrice);
  438. }
  439. break;
  440. }
  441. //resultData = ResultData.success(resultMap);
  442. }
  443. return resultMap;
  444. }
  445. /*
  446. * 修订优惠计算: 2021年4月20日15:31:30
  447. * 修订内容 :
  448. * 修改为目前只有三种优惠:根据立减、满减、独立直降;
  449. * 注释:优惠为 1 是:表示不走满减、立减、独立直降优惠,只走等级直降的优惠;
  450. * 条件规则:
  451. * 一,优先满减(只有一档,不存在多档!)、立减(只有一档,不存在多档!)、独立直降(只有一档)
  452. * 二,若用户消费门槛 >= 优惠门槛,且启用优惠叠加时,则计算优惠规则和等级优惠规则;
  453. * 若用户消费门槛 >= 优惠门槛,且不启用优惠叠加时,则只计算优惠规则;
  454. * 若用户消费门槛 < 优惠门槛,且启用优惠叠加时,则只计算等级规则;
  455. * 若用户消费门槛 < 优惠门槛,且不启用优惠叠加时,则只计算等级规则;
  456. *
  457. */
  458. public Map<String ,Object> calculateAmtNew(PayOrderRequest payOrderRequest){
  459. Map<String ,Object> resultMap = new HashMap<>();
  460. if(payOrderRequest.getReceivableAmt() !=null){ //应收金额
  461. Double receivableAmt = payOrderRequest.getReceivableAmt(); //应收金额 用户输入的金额
  462. Integer stationId = payOrderRequest.getStationId(); //油站ID
  463. //String oilGun = payOrderRequest.getOilGun(); //油枪号
  464. String oilName = payOrderRequest.getOilName(); //油品名称
  465. String openId = payOrderRequest.getOpenId(); //用户的openId
  466. String mobilePhone = payOrderRequest.getCustomerPhone(); //客户手机号
  467. //获取油品价格
  468. StationOilPrice stationOilPrice = new StationOilPrice();
  469. stationOilPrice.setStationId(stationId);
  470. stationOilPrice.setOilName(oilName);
  471. StationOilPrice oilPriceInfo = stationService.getStationOilPrice(stationOilPrice);
  472. String oilPrice = oilPriceInfo.getOilPrice();
  473. //String stationNanme = oilPriceInfo.getStationNanme();
  474. BigDecimal oilLiters = null;
  475. BigDecimal discountAmt =null;
  476. BigDecimal discountPrice = null;
  477. BigDecimal amt = null;
  478. //resultMap.put("stationName", stationNanme);
  479. resultMap.put("oilPrice", oilPrice); //油品价格
  480. resultMap.put("receivableAmt", receivableAmt); //应收价格
  481. //该油站的优惠方式
  482. String stationDiscountWay = stationService.getStationDiscountWay(stationId.toString());
  483. Map<String ,Object> params = new HashMap<String, Object>();
  484. params.put("stationId", stationId);
  485. params.put("oilName", oilName);
  486. params.put("receivableAmt", receivableAmt);
  487. params.put("oilPrice", oilPrice);
  488. params.put("mobilePhone", mobilePhone);
  489. params.put("minaOpenid", openId);
  490. BigDecimal receivableAmt_b = new BigDecimal(receivableAmt); //传入的应收金额
  491. switch (stationDiscountWay){
  492. case "1": //1.等级直降
  493. //查询等级直降的优惠方式
  494. //查询客户等级信息
  495. List<Map<String, Object>> customerGradeInfo = customerGradeServices.getCustomerGradeInfo(params);
  496. if(customerGradeInfo != null && customerGradeInfo.size() >0){ //已存在客户等级信息
  497. //使用客户已存在的等级计算优惠
  498. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP);
  499. discountAmt = oilLiters.multiply(new BigDecimal(customerGradeInfo.get(0).get("gasoilDiscountLitre").toString()));
  500. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  501. amt = receivableAmt_b.subtract(discountAmt);
  502. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  503. //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  504. resultMap.put("discountAmt", discountAmt.doubleValue());
  505. resultMap.put("amt", amt.doubleValue());
  506. resultMap.put("oilLiters", oilLiters.doubleValue());
  507. resultMap.put("discountPrice", discountPrice.doubleValue());
  508. }else {
  509. //不存在客户等级信息时,查询油站的客户等级直降优惠(查询结果根据会员成长值条件做升序查询)
  510. List<Map> customerGradeList = customerGradeServices.getCustomerGradeList(params);
  511. //如果存在
  512. if(customerGradeList !=null && customerGradeList.size() >0){
  513. //用户第一次购买 取等级最低初始会员的优惠条件计算
  514. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); //加油升数
  515. discountAmt = oilLiters.multiply(new BigDecimal(customerGradeList.get(0).get("gasoilDiscountLitre").toString()));
  516. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  517. amt = receivableAmt_b.subtract(discountAmt);
  518. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  519. //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  520. resultMap.put("discountAmt", discountAmt.doubleValue());
  521. resultMap.put("amt", amt.doubleValue());
  522. resultMap.put("oilLiters", oilLiters.doubleValue());
  523. resultMap.put("discountPrice", discountPrice.doubleValue());
  524. }else {
  525. //不存在
  526. resultMap.put("discountAmt", 0);
  527. resultMap.put("amt", receivableAmt.doubleValue());
  528. oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP);
  529. resultMap.put("oilLiters", oilLiters.doubleValue());
  530. resultMap.put("discountPrice", oilPrice);
  531. }
  532. }
  533. break;
  534. case "2": //2.满减
  535. params.put("discountPlanType", "1");
  536. //营销方案
  537. List<Map> customerMarkertPlanList_1 = customerGradeServices.getCustomerMarkertPlanList(params);
  538. //有优惠规则
  539. if(customerMarkertPlanList_1 !=null && customerMarkertPlanList_1.size() >0){
  540. //是否共享会员优惠叠加: 1 代表是; 2 代表否;
  541. String vipDiscountyPlus = customerMarkertPlanList_1.get(0).get("vipDiscountyPlus").toString();
  542. //如果当前应收金额大于条件金额
  543. BigDecimal discountAmt_b = new BigDecimal(customerMarkertPlanList_1.get(0).get("discountAmt").toString()); //条件金额
  544. if(receivableAmt_b.compareTo(discountAmt_b) ==1 || receivableAmt_b.compareTo(discountAmt_b) ==0){
  545. BigDecimal gasoilDiscountAmt_b = new BigDecimal(customerMarkertPlanList_1.get(0).get("gasoilDiscountAmt").toString()); //每满多少优惠多少的金额
  546. discountAmt = receivableAmt_b.divide(discountAmt_b, 2, BigDecimal.ROUND_HALF_UP).multiply(gasoilDiscountAmt_b); //优惠金额
  547. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  548. amt = receivableAmt_b.subtract(discountAmt); //实收金额
  549. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP); //加油升数
  550. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  551. //discountPrice = amt.divide(oilLiters,2,BigDecimal.ROUND_HALF_UP); //优惠价格
  552. switch (vipDiscountyPlus){
  553. case "1":
  554. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  555. discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
  556. amt = amt.subtract(discountAmt);
  557. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  558. resultMap.put("discountAmt", discountAmt.doubleValue());
  559. resultMap.put("amt", amt.doubleValue());
  560. resultMap.put("oilLiters", oilLiters.doubleValue());
  561. resultMap.put("discountPrice", discountPrice.doubleValue());
  562. break;
  563. case "2":
  564. resultMap.put("discountAmt", discountAmt.doubleValue());
  565. resultMap.put("amt", amt.doubleValue());
  566. resultMap.put("oilLiters", oilLiters.doubleValue());
  567. resultMap.put("discountPrice", discountPrice.doubleValue());
  568. break;
  569. }
  570. }else{
  571. switch (vipDiscountyPlus){
  572. case "1":
  573. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  574. discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
  575. amt = amt.subtract(discountAmt);
  576. //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  577. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  578. resultMap.put("discountAmt", discountAmt.doubleValue());
  579. resultMap.put("amt", amt.doubleValue());
  580. resultMap.put("oilLiters", oilLiters.doubleValue());
  581. resultMap.put("discountPrice", discountPrice.doubleValue());
  582. break;
  583. case "2":
  584. resultMap.put("discountAmt", discountAmt.doubleValue());
  585. resultMap.put("amt", amt.doubleValue());
  586. resultMap.put("oilLiters", oilLiters.doubleValue());
  587. resultMap.put("discountPrice", discountPrice.doubleValue());
  588. break;
  589. }
  590. }
  591. }else {
  592. //用户没有设置优惠规则时
  593. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  594. resultMap.put("discountAmt", map.get("discountAmt").toString());
  595. resultMap.put("amt", map.get("amt").toString());
  596. resultMap.put("oilLiters", map.get("oilLiters").toString());
  597. resultMap.put("discountPrice", map.get("discountPrice").toString());
  598. }
  599. break;
  600. case "3": //3.立减
  601. params.put("discountPlanType", "2");
  602. //营销方案
  603. List<Map> customerMarkertPlanList_2 = customerGradeServices.getCustomerMarkertPlanList(params);
  604. //是否共享会员优惠叠加: 1 代表是; 2 代表否;
  605. String vipDiscountyPlus = customerMarkertPlanList_2.get(0).get("vipDiscountyPlus").toString();
  606. if(customerMarkertPlanList_2 !=null && customerMarkertPlanList_2.size() >0){
  607. //如果当前应收金额大于条件金额
  608. BigDecimal discountAmt_b = new BigDecimal(customerMarkertPlanList_2.get(0).get("discountAmt").toString()); //条件金额
  609. if(receivableAmt_b.compareTo(discountAmt_b) ==1 || receivableAmt_b.compareTo(discountAmt_b) ==0){
  610. BigDecimal gasoilDiscountAmt_b = new BigDecimal(customerMarkertPlanList_2.get(0).get("gasoilDiscountAmt").toString()); //满多少优惠多少的金额
  611. discountAmt = gasoilDiscountAmt_b; //优惠金额
  612. amt = receivableAmt_b.subtract(gasoilDiscountAmt_b); //实收金额
  613. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); //加油升数
  614. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  615. //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); //优惠价格
  616. switch (vipDiscountyPlus){
  617. case "1":
  618. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  619. discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
  620. amt = amt.subtract(discountAmt);
  621. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  622. //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  623. resultMap.put("discountAmt", discountAmt.doubleValue());
  624. resultMap.put("amt", amt.doubleValue());
  625. resultMap.put("oilLiters", oilLiters.doubleValue());
  626. resultMap.put("discountPrice", discountPrice.doubleValue());
  627. break;
  628. case "2":
  629. resultMap.put("discountAmt", discountAmt.doubleValue());
  630. resultMap.put("amt", amt.doubleValue());
  631. resultMap.put("oilLiters", oilLiters.doubleValue());
  632. resultMap.put("discountPrice", discountPrice.doubleValue());
  633. break;
  634. }
  635. }else {
  636. //不满足优惠条件
  637. switch (vipDiscountyPlus) {
  638. case "1":
  639. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  640. discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
  641. amt = amt.subtract(discountAmt);
  642. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  643. //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  644. resultMap.put("discountAmt", discountAmt.doubleValue());
  645. resultMap.put("amt", amt.doubleValue());
  646. resultMap.put("oilLiters", oilLiters.doubleValue());
  647. resultMap.put("discountPrice", discountPrice.doubleValue());
  648. break;
  649. case "2":
  650. resultMap.put("discountAmt", discountAmt.doubleValue());
  651. resultMap.put("amt", amt.doubleValue());
  652. resultMap.put("oilLiters", oilLiters.doubleValue());
  653. resultMap.put("discountPrice", discountPrice.doubleValue());
  654. break;
  655. }
  656. }
  657. }else {
  658. //用户没有设置优惠规则时
  659. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  660. resultMap.put("discountAmt", map.get("discountAmt").toString());
  661. resultMap.put("amt", map.get("amt").toString());
  662. resultMap.put("oilLiters", map.get("oilLiters").toString());
  663. resultMap.put("discountPrice", map.get("discountPrice").toString());
  664. }
  665. break;
  666. case "4": //4.独立直降
  667. params.put("discountPlanType", "3");
  668. //营销方案
  669. List<Map> customerMarkertPlanList_3 = customerGradeServices.getCustomerMarkertPlanList(params);
  670. if(customerMarkertPlanList_3 !=null && customerMarkertPlanList_3.size() >0){
  671. boolean flag= true;
  672. //独立直降时会存在多条
  673. for(Map m : customerMarkertPlanList_3){
  674. BigDecimal discountAmt_b = new BigDecimal(m.get("discountAmt").toString()); //条件金额
  675. BigDecimal gasoilDiscountAmt_b = new BigDecimal(m.get("gasoilDiscountAmt").toString());
  676. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); //加油升数
  677. if(m.get("discountTerm").toString().equals("1")){ //按升数优惠
  678. if(oilLiters.compareTo(discountAmt_b) ==1 || oilLiters.compareTo(discountAmt_b) ==0){
  679. //计算
  680. discountAmt = oilLiters.multiply(gasoilDiscountAmt_b);
  681. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  682. amt = receivableAmt_b.subtract(discountAmt);
  683. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  684. //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); //优惠价格
  685. //是否共享会员优惠叠加
  686. String vipDiscountyPlus_3 = m.get("vipDiscountyPlus").toString();
  687. if(vipDiscountyPlus_3.equals("1")){
  688. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  689. discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
  690. amt = amt.subtract(discountAmt);
  691. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  692. //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  693. }
  694. resultMap.put("discountAmt", discountAmt.doubleValue());
  695. resultMap.put("amt", amt.doubleValue());
  696. resultMap.put("oilLiters", oilLiters.doubleValue());
  697. resultMap.put("discountPrice", discountPrice.doubleValue());
  698. flag =false;
  699. break;
  700. }
  701. }else if(m.get("discountTerm").toString().equals("2")){ //按金额优惠
  702. if(receivableAmt_b.compareTo(discountAmt_b) ==1 || receivableAmt_b.compareTo(discountAmt_b) ==0){
  703. discountAmt = oilLiters.multiply(gasoilDiscountAmt_b);
  704. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  705. amt = receivableAmt_b.subtract(discountAmt);
  706. discountPrice = oilLiters !=new BigDecimal("0.00") ? amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP) : new BigDecimal(oilPrice);
  707. //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP); //优惠价格
  708. //是否共享会员优惠叠加
  709. String vipDiscountyPlus_3 = m.get("vipDiscountyPlus").toString();
  710. if(vipDiscountyPlus_3.equals("1")){
  711. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  712. discountAmt = discountAmt.add(new BigDecimal(map.get("discountAmt").toString()));
  713. amt = amt.subtract(discountAmt);
  714. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  715. //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  716. }
  717. resultMap.put("discountAmt", discountAmt.doubleValue());
  718. resultMap.put("amt", amt.doubleValue());
  719. resultMap.put("oilLiters", oilLiters.doubleValue());
  720. resultMap.put("discountPrice", discountPrice.doubleValue());
  721. flag =false;
  722. break;
  723. }
  724. }
  725. }
  726. //如果用户加油金额都不符合优惠门槛则计算等级的优惠
  727. if(flag){
  728. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  729. resultMap.put("discountAmt", map.get("discountAmt").toString());
  730. resultMap.put("amt", map.get("amt").toString());
  731. resultMap.put("oilLiters", map.get("oilLiters").toString());
  732. resultMap.put("discountPrice", map.get("discountPrice").toString());
  733. }
  734. }else {
  735. //用户没有设置优惠规则时
  736. Map<String, Object> map = this.calcuteDengJiZhiJiang(params);
  737. resultMap.put("discountAmt", map.get("discountAmt").toString());
  738. resultMap.put("amt", map.get("amt").toString());
  739. resultMap.put("oilLiters", map.get("oilLiters").toString());
  740. resultMap.put("discountPrice", map.get("discountPrice").toString());
  741. }
  742. break;
  743. }
  744. }
  745. return resultMap;
  746. }
  747. //计算等级直降
  748. public Map<String, Object> calcuteDengJiZhiJiang(Map params){
  749. Double receivableAmt = Double.valueOf(params.get("receivableAmt").toString());
  750. Map<String, Object> resultMap = new HashMap();
  751. BigDecimal oilLiters = null;
  752. BigDecimal discountAmt =null;
  753. BigDecimal discountPrice = null;
  754. BigDecimal amt = null;
  755. BigDecimal receivableAmt_b = new BigDecimal(receivableAmt); //传入的应收金额
  756. String oilPrice = params.get("oilPrice").toString();
  757. List<Map<String, Object>> customerGradeInfo = customerGradeServices.getCustomerGradeInfo(params);
  758. if(customerGradeInfo != null && customerGradeInfo.size() >0){ //已存在客户等级信息
  759. //使用客户已存在的等级计算优惠
  760. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice),2, BigDecimal.ROUND_HALF_UP);
  761. discountAmt = oilLiters.multiply(new BigDecimal(customerGradeInfo.get(0).get("gasoilDiscountLitre").toString()));
  762. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  763. amt = receivableAmt_b.subtract(discountAmt);
  764. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  765. //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  766. resultMap.put("discountAmt", discountAmt.doubleValue());
  767. resultMap.put("amt", amt.doubleValue());
  768. resultMap.put("oilLiters", oilLiters.doubleValue());
  769. resultMap.put("discountPrice", discountPrice.doubleValue());
  770. }else {
  771. //不存在客户等级信息时,查询油站的客户等级直降优惠(查询结果根据会员成长值条件做升序查询)
  772. List<Map> customerGradeList = customerGradeServices.getCustomerGradeList(params);
  773. //如果存在
  774. if(customerGradeList !=null && customerGradeList.size() >0){
  775. //用户第一次购买 取等级最低初始会员的优惠条件计算
  776. oilLiters = receivableAmt_b.divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP); //加油升数
  777. discountAmt = oilLiters.multiply(new BigDecimal(customerGradeList.get(0).get("gasoilDiscountLitre").toString()));
  778. discountAmt = discountAmt.setScale(2, BigDecimal.ROUND_HALF_UP);
  779. amt = receivableAmt_b.subtract(discountAmt);
  780. discountPrice = oilLiters.equals(new BigDecimal("0.00")) ? new BigDecimal(oilPrice) : amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  781. //discountPrice = amt.divide(oilLiters, 2, BigDecimal.ROUND_HALF_UP);
  782. resultMap.put("discountAmt", discountAmt.doubleValue());
  783. resultMap.put("amt", amt.doubleValue());
  784. resultMap.put("oilLiters", oilLiters.doubleValue());
  785. resultMap.put("discountPrice", discountPrice.doubleValue());
  786. }else {
  787. //不存在
  788. resultMap.put("discountAmt", 0);
  789. resultMap.put("amt", receivableAmt.doubleValue());
  790. oilLiters = new BigDecimal(receivableAmt).divide(new BigDecimal(oilPrice), 2, BigDecimal.ROUND_HALF_UP);
  791. resultMap.put("oilLiters", oilLiters.doubleValue());
  792. resultMap.put("discountPrice", oilPrice);
  793. }
  794. }
  795. return resultMap;
  796. }
  797. /**
  798. * 根据订单号获取订单信息
  799. * produces="application/json;charset=UTF-8",consumes = "application/json",
  800. * @RequestBody String orderno
  801. * @param
  802. * @return
  803. */
  804. @RequestMapping(value = "/getPayOrderList", method = RequestMethod.GET)
  805. @ResponseBody
  806. public String getPayOrderList(@RequestParam String orderNo,String openId,String userType){
  807. Gson gson =new Gson();
  808. ResultData resultData =null;
  809. //根据token 和 usertype来判断用户是否存在
  810. boolean flag = appUserInfoService.selectAppUserInfo(openId,userType);
  811. if(flag){
  812. if(StringUtils.isNotBlank(orderNo)){
  813. PayOrderRequest payOrderRequest =new PayOrderRequest();
  814. payOrderRequest.setOrderNo(orderNo);
  815. List<PayOrder> payOrderList = payOrderService.getPayOrderList(payOrderRequest.getOrderNo());
  816. resultData=ResultData.success(payOrderList);
  817. }else {
  818. resultData=ResultData.error(CodeMsg.SEARCH_FAIL);
  819. }
  820. }else{
  821. resultData=ResultData.error(CodeMsg.USER_NOT_EXSIST);
  822. }
  823. return gson.toJson(resultData);
  824. }
  825. /*
  826. * 根据用户openId和用户类型userType查询用户订单
  827. * @param openId
  828. * @param userType
  829. * @return
  830. */
  831. @RequestMapping(value = "/getOrderListByOpenIdAndUserType", method = RequestMethod.GET)
  832. @ResponseBody
  833. public String getOrderListByOpenIdAndUserType(@RequestParam("openId") String openId, @RequestParam("userType") String userType){
  834. Gson gson =new Gson();
  835. ResultData resultData =null;
  836. if(StringUtils.isNotBlank(openId) && StringUtils.isNotBlank(userType)){
  837. AppUserInfo appUserInfo = new AppUserInfo();
  838. switch (userType){
  839. case "1":
  840. appUserInfo.setBlogOpenid(openId);
  841. break;
  842. case "2":
  843. appUserInfo.setMinaOpenid(openId);
  844. break;
  845. }
  846. List<PayOrder> payOrderList = payOrderService.getOrderListByOpenIdAndUserType(appUserInfo);
  847. resultData=ResultData.success(payOrderList);
  848. }else {
  849. resultData=ResultData.error(CodeMsg.SEARCH_FAIL);
  850. }
  851. return gson.toJson(resultData);
  852. }
  853. }