TOrderController.java 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  1. package com.szwl.controller;
  2. import cn.com.crbank.ommo.bean.ResultMessage;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  7. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  8. import com.huifu.bspay.sdk.opps.core.utils.DateTools;
  9. import com.szwl.constant.HuifuConstant;
  10. import com.szwl.constant.ResponseCodesEnum;
  11. import com.szwl.feign.bean.PayFeign;
  12. import com.szwl.constant.JoinpayConstant;
  13. import com.szwl.model.bo.R;
  14. import com.szwl.model.bo.ResponseModel;
  15. import com.szwl.model.entity.*;
  16. import com.szwl.service.*;
  17. import com.szwl.service.es.EsTOrderService;
  18. import com.szwl.util.IDGenerator;
  19. import io.swagger.annotations.ApiOperation;
  20. import org.apache.commons.lang.StringUtils;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.http.HttpStatus;
  23. import org.springframework.http.ResponseEntity;
  24. import org.springframework.web.bind.annotation.*;
  25. import java.math.BigDecimal;
  26. import java.math.RoundingMode;
  27. import java.util.*;
  28. /**
  29. * <p>
  30. * 前端控制器
  31. * </p>
  32. *
  33. * @author wuhs
  34. * @since 2022-04-23
  35. */
  36. @RestController
  37. @RequestMapping("/tOrder")
  38. public class TOrderController {
  39. @Autowired
  40. TAdminService tAdminService;
  41. @Autowired
  42. TEquipmentService tEquipmentService;
  43. @Autowired
  44. TOrderService orderService;
  45. @Autowired
  46. PayFeign payFeign;
  47. @Autowired
  48. TShandeMchService tShandeMchService;
  49. @Autowired
  50. EsTOrderService esTOrderService;
  51. @Autowired
  52. TPriceService priceService;
  53. @Autowired
  54. TJoinpayMchService joinpayMchService;
  55. @Autowired
  56. THuifuMchService huifuMchService;
  57. @Autowired
  58. private TProportionService proportionService;
  59. /**
  60. * 退款
  61. * @param
  62. * @return
  63. */
  64. @PostMapping("/refund")
  65. public ResponseModel<?> refund(@RequestBody TOrder torder) {
  66. // 退款金额: refusePrice
  67. BigDecimal refusePrice = torder.getPrice().setScale(2, RoundingMode.HALF_DOWN);
  68. // 退款数量: refundNumber
  69. Integer refundNumber = torder.getProductNumber();
  70. // 退款明细:note
  71. String note = torder.getNote();
  72. String[] refundDetails = note.split(",");
  73. Map<String, Integer> refundMap = new HashMap<>();
  74. for (String refundDetail : refundDetails) {
  75. String[] keyValue = refundDetail.split("-");
  76. String key = keyValue[0];
  77. String value = keyValue[1];
  78. refundMap.put(key, Integer.valueOf(value));
  79. }
  80. if(refusePrice.compareTo(new BigDecimal("0.00"))<=0){
  81. return R.fail(ResponseCodesEnum.A0001,"退款金额非法");
  82. }
  83. TOrder order = R.getDataIfSuccess(payFeign.getOrder(String.valueOf(torder.getId())));
  84. if(order.getRefundQuantity() != null && order.getRefundQuantity()>0) {
  85. order.setRefundQuantity(refundNumber + order.getRefundQuantity());
  86. } else {
  87. order.setRefundQuantity(refundNumber);
  88. }
  89. List<TOrderDetails> orderDetails = R.getDataIfSuccess(payFeign.getOrderDetailsBySn(order.getSn()));
  90. TEquipment equipment = tEquipmentService.getById(order.getEquipmentId());
  91. Long equipmentId = equipment.getId();
  92. if (order.getProductName().equals("280充值")) {
  93. return R.fail(ResponseCodesEnum.A0002,"订单不退款");
  94. }
  95. if (order.getStatus().equals("0")) {
  96. return R.fail(ResponseCodesEnum.A0002,"订单非支付状态");
  97. }
  98. //判断是不是直连微信退款
  99. if(StringUtils.isNotEmpty(order.getPayPlatform())&&order.getPayPlatform().equals("3")){
  100. String ifSuccess = R.getDataIfSuccess(payFeign.refund(order.getSn(),refusePrice));
  101. return R.ok(ifSuccess);
  102. }
  103. String notifyUrl = JoinpayConstant.Notify_Refund_Url;
  104. JSONArray altInfoArray = JSONArray.parseArray(order.getAltInfo());
  105. JSONArray altRefInfo = new JSONArray();
  106. // 汇付分销明细
  107. String acctSplitBunch = "";
  108. BigDecimal p = BigDecimal.ZERO;
  109. BigDecimal price = BigDecimal.ZERO;
  110. //订单总金额
  111. BigDecimal sumPrice = BigDecimal.ZERO;
  112. BigDecimal refundAmount = order.getRefundAmount();
  113. if(refundAmount != null){
  114. sumPrice = sumPrice.add(order.getPrice()).subtract(refundAmount).setScale(2, RoundingMode.HALF_DOWN);
  115. }else {
  116. sumPrice = sumPrice.add(order.getPrice()).setScale(2, RoundingMode.HALF_DOWN);
  117. }
  118. // BigDecimal pri = new BigDecimal(order.getPrice().toString()).setScale(2, RoundingMode.HALF_DOWN);
  119. BigDecimal p8_MarketRefAmount = BigDecimal.ZERO;
  120. //获取分销 关联设备
  121. LambdaQueryWrapper<TProportion> wrapper = new LambdaQueryWrapper<>();
  122. wrapper.eq(TProportion::getEquipmentId,equipmentId);
  123. TProportion proportion = proportionService.getOne(wrapper);
  124. if(refusePrice.compareTo(sumPrice)<0){
  125. //退部分的钱
  126. JSONArray altInfo = getAltInfo(proportion, refusePrice);
  127. altInfoArray = JSONArray.parseArray(altInfo.toString());
  128. for (int i = 0; i < altInfoArray.size(); i++) {
  129. JSONObject altInfoObject = altInfoArray.getJSONObject(i);
  130. JSONObject refInfo = new JSONObject();
  131. refInfo.put("altMchNo", altInfoObject.getString("altMchNo"));
  132. String altAmount = altInfoObject.getString("altAmount");
  133. BigDecimal altAmountPrice = new BigDecimal(altAmount);
  134. // altAmountPrice = altAmountPrice.multiply(refusePrice.divide(order.getPrice(),2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
  135. p = p.add(altAmountPrice);
  136. refInfo.put("altRefAmount", altAmountPrice.toString());
  137. if(order.getMarketingAmount()!=null){
  138. p8_MarketRefAmount = order.getMarketingAmount().multiply(refusePrice.divide(order.getPrice(),2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
  139. }
  140. price = price.add(altInfoObject.getBigDecimal("altAmount"));
  141. if (!altInfoObject.getBigDecimal("altAmount").toString().equals("0.00")) {
  142. altRefInfo.add(refInfo);
  143. }
  144. }
  145. }else {
  146. //退全部的钱
  147. for (int i = 0; i < altInfoArray.size(); i++) {
  148. JSONObject altInfoObject = altInfoArray.getJSONObject(i);
  149. JSONObject refInfo = new JSONObject();
  150. refInfo.put("altMchNo", altInfoObject.getString("altMchNo"));
  151. String altAmount = altInfoObject.getString("altAmount");
  152. BigDecimal altAmountPrice = new BigDecimal(altAmount).setScale(2, RoundingMode.HALF_DOWN);
  153. // BigDecimal divide = altAmountPrice.divide(order.getPrice(),3,BigDecimal.ROUND_HALF_UP).setScale(3, RoundingMode.HALF_DOWN);
  154. // altAmountPrice = refusePrice.multiply(divide).setScale(2, RoundingMode.HALF_DOWN);
  155. // refInfo.put("altRefAmount", altAmountPrice.toString());
  156. p = p.add(altAmountPrice);
  157. refInfo.put("altRefAmount", altAmountPrice.toString());
  158. if(order.getMarketingAmount()!=null){
  159. p8_MarketRefAmount = order.getMarketingAmount().multiply(refusePrice.divide(order.getPrice(),2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
  160. }
  161. price = price.add(altInfoObject.getBigDecimal("altAmount"));
  162. if (!altInfoObject.getBigDecimal("altAmount").toString().equals("0.00")) {
  163. altRefInfo.add(refInfo);
  164. }
  165. }
  166. }
  167. // 特殊处理下:添加多一个平台方分销的金额
  168. // 平台获得的收益
  169. BigDecimal adminPrice = refusePrice.subtract(p).setScale(2, RoundingMode.HALF_DOWN);
  170. JSONObject refInfo = new JSONObject();
  171. refInfo.put("altMchNo", JoinpayConstant.mch_no);
  172. refInfo.put("altRefAmount", adminPrice.toString());
  173. price = price.add(adminPrice);
  174. if (!adminPrice.toString().equals("0.00")) {
  175. altRefInfo.add(refInfo);
  176. }
  177. //需要知道订单是哪一个平台的,不能以账号的为标准 0或null为汇聚,1为汇付支付
  178. String payPlatform = order.getPayPlatform();
  179. String companyType = order.getCompanyType();
  180. if (StringUtils.isNotEmpty(payPlatform) && payPlatform.equals("1")) {
  181. try {
  182. Date date = order.getCreateDate();
  183. String payDate = DateTools.formatYYYYMMDD(date);
  184. String remark = "";
  185. // BigDecimal refundAmount = order.getRefundAmount();
  186. // 判断是否退过款
  187. if(refundAmount != null && refundAmount.compareTo(BigDecimal.ZERO) > 0) {
  188. acctSplitBunch = getAcctSplitBunch(proportion, refusePrice, companyType).toString();
  189. } else {
  190. // 如果退款金额等于订单金额,就直接用原来的分销明细,如果小于就重新计算分销明细
  191. if(refusePrice.compareTo(order.getPrice()) < 0) {
  192. acctSplitBunch = getAcctSplitBunch(proportion, refusePrice, companyType).toString();
  193. }
  194. }
  195. String refundData = huifuMchService.refund(order.getSn(), price, payDate, remark, acctSplitBunch, companyType);
  196. //订单号
  197. if(StrUtil.isNotEmpty(refundData)&&(refundData.equals(HuifuConstant.TRANS_STAT_S)||refundData.equals(HuifuConstant.TRANS_STAT_P))) {
  198. order.setStatus(2);
  199. payFeign.updateOrder(order);
  200. // 修改订单明细
  201. for (TOrderDetails orderDetail : orderDetails) {
  202. String productNo = orderDetail.getProductNo();
  203. Integer productNumber = refundMap.get(productNo);
  204. if(productNumber != null) {
  205. // 退款中
  206. orderDetail.setRefundStatus("4");
  207. // 退款数量
  208. orderDetail.setRefundQuantity(productNumber);
  209. // 退款金额
  210. orderDetail.setRefundAmount(orderDetail.getPrice().multiply(new BigDecimal(productNumber)));
  211. payFeign.updateOrderDetails(orderDetail);
  212. }
  213. }
  214. return R.ok("申请退款成功");
  215. }else {
  216. return R.fail(refundData);
  217. }
  218. } catch (Exception e) {
  219. e.printStackTrace();
  220. }
  221. } else {
  222. try {
  223. String result = orderService.refund(order.getSn(), orderService.initRefundSn(equipment.getClientId(), refundNumber), refusePrice.subtract(p8_MarketRefAmount), p8_MarketRefAmount,"", notifyUrl, altRefInfo.toString());
  224. if ("退款申请成功".equals(result)) {
  225. order.setStatus(2);
  226. if(StringUtils.isNotEmpty(order.getCoupons())){
  227. //设置退款营销金额
  228. if(order.getRefundMarketingAmount()!=null){
  229. //累加
  230. order.setRefundMarketingAmount(order.getRefundMarketingAmount().add(p8_MarketRefAmount));
  231. }else {
  232. order.setRefundMarketingAmount(p8_MarketRefAmount);
  233. }
  234. }
  235. // 修改订单明细
  236. for (TOrderDetails orderDetail : orderDetails) {
  237. String productNo = orderDetail.getProductNo();
  238. Integer productNumber = refundMap.get(productNo);
  239. if(productNumber != null) {
  240. // 退款中
  241. orderDetail.setRefundStatus("4");
  242. // 退款数量
  243. orderDetail.setRefundQuantity(productNumber);
  244. // 退款金额
  245. orderDetail.setRefundAmount(orderDetail.getPrice().multiply(new BigDecimal(productNumber)));
  246. payFeign.updateOrderDetails(orderDetail);
  247. }
  248. }
  249. order.setRefundTrxNo(String.valueOf(refusePrice));
  250. payFeign.updateOrder(order);
  251. return R.ok("申请退款已成功");
  252. }else {
  253. return R.fail("申请退款失败");
  254. }
  255. } catch (Exception e) {
  256. e.printStackTrace();
  257. }
  258. }
  259. return R.ok("申请退款已成功");
  260. }
  261. /**
  262. * 小程序退款
  263. * @param
  264. * @return
  265. */
  266. @GetMapping("/refuseByXcu")
  267. public ResponseEntity<?> refuseByXcu(String id) {
  268. TOrder order = R.getDataIfSuccess(payFeign.getOrder(id));
  269. List<TOrderDetails> orderDetails = R.getDataIfSuccess(payFeign.getOrderDetailsBySn(order.getSn()));
  270. String orderStatus = order.getOrderStatus();
  271. Integer refundNumber = order.getProductNumber();
  272. order.setRefundQuantity(refundNumber);
  273. if(StringUtils.isNotEmpty(orderStatus)){
  274. if(orderStatus.equals("1")||orderStatus.equals("2")){
  275. return ResponseEntity
  276. .status(HttpStatus.OK)
  277. .body(new ResultMessage()
  278. .setCode(false)
  279. .setData("")
  280. .setMessage("订单非排队状态"));
  281. }
  282. }
  283. BigDecimal refusePrice = order.getPrice().setScale(2, RoundingMode.HALF_DOWN);
  284. if(refusePrice.compareTo(new BigDecimal("0.00"))<=0){
  285. return ResponseEntity
  286. .status(HttpStatus.OK)
  287. .body(new ResultMessage()
  288. .setCode(false)
  289. .setData("")
  290. .setMessage("退款金额非法"));
  291. }
  292. TEquipment equipment = tEquipmentService.getById(order.getEquipmentId());
  293. Long equipmentId = equipment.getId();
  294. if (order.getProductName().equals("280充值")) {
  295. return ResponseEntity
  296. .status(HttpStatus.OK)
  297. .body(new ResultMessage()
  298. .setCode(false)
  299. .setData("订单不退款")
  300. .setMessage("订单不退款"));
  301. }
  302. if (order.getStatus().equals("0")) {
  303. return ResponseEntity
  304. .status(HttpStatus.OK)
  305. .body(new ResultMessage()
  306. .setCode(false)
  307. .setData("订单非支付状态")
  308. .setMessage("订单非支付状态"));
  309. }
  310. String notifyUrl = JoinpayConstant.Notify_Refund_Url;
  311. JSONArray altInfoArray = JSONArray.parseArray(order.getAltInfo());
  312. JSONArray altRefInfo = new JSONArray();
  313. // 汇付分销明细
  314. String acctSplitBunch = "";
  315. BigDecimal p = BigDecimal.ZERO;
  316. BigDecimal price = BigDecimal.ZERO;
  317. //订单总金额
  318. BigDecimal sumPrice = BigDecimal.ZERO;
  319. if(order.getRefundAmount()!=null){
  320. sumPrice=sumPrice.add(order.getPrice()).subtract(order.getRefundAmount()).setScale(2, RoundingMode.HALF_DOWN);
  321. }else {
  322. sumPrice=sumPrice.add(order.getPrice()).setScale(2, RoundingMode.HALF_DOWN);
  323. }
  324. BigDecimal p8_MarketRefAmount = BigDecimal.ZERO;
  325. for (int i = 0; i < altInfoArray.size(); i++) {
  326. JSONObject altInfoObject = altInfoArray.getJSONObject(i);
  327. JSONObject refInfo = new JSONObject();
  328. refInfo.put("altMchNo", altInfoObject.getString("altMchNo"));
  329. String altAmount = altInfoObject.getString("altAmount");
  330. // BigDecimal pri = new BigDecimal(order.getPrice().toString()).setScale(2, RoundingMode.HALF_DOWN);
  331. if(refusePrice.compareTo(sumPrice)<0){
  332. //退部分的钱
  333. BigDecimal altAmountPrice = new BigDecimal(altAmount);
  334. altAmountPrice = altAmountPrice.multiply(refusePrice.divide(sumPrice,2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
  335. p = p.add(altAmountPrice);
  336. refInfo.put("altRefAmount", altAmountPrice.toString());
  337. if(order.getMarketingAmount()!=null){
  338. p8_MarketRefAmount = order.getMarketingAmount().multiply(refusePrice.divide(sumPrice,2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
  339. }
  340. }else {
  341. //退全部的钱
  342. BigDecimal altAmountPrice = new BigDecimal(altAmount).setScale(2, RoundingMode.HALF_DOWN);
  343. // BigDecimal divide = altAmountPrice.divide(sumPrice,2,BigDecimal.ROUND_HALF_UP).setScale(2, RoundingMode.HALF_DOWN);
  344. // altAmountPrice = refusePrice.multiply(divide).setScale(2, RoundingMode.HALF_DOWN);
  345. p = p.add(altAmountPrice);
  346. refInfo.put("altRefAmount", altAmountPrice.toString());
  347. if(order.getMarketingAmount()!=null){
  348. p8_MarketRefAmount = order.getMarketingAmount().multiply(refusePrice.divide(sumPrice,2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
  349. }
  350. }
  351. price = price.add(altInfoObject.getBigDecimal("altAmount"));
  352. if (!altInfoObject.getBigDecimal("altAmount").toString().equals("0.00")) {
  353. altRefInfo.add(refInfo);
  354. }
  355. }
  356. // 特殊处理下:添加多一个平台方分销的金额
  357. // 平台获得的收益
  358. // BigDecimal adminPrice = new BigDecimal(order.getPrice().toString()).subtract(price);
  359. BigDecimal adminPrice = refusePrice.subtract(p).setScale(2, RoundingMode.HALF_DOWN);
  360. JSONObject refInfo = new JSONObject();
  361. refInfo.put("altMchNo", JoinpayConstant.mch_no);
  362. refInfo.put("altRefAmount", adminPrice.toString());
  363. price = price.add(adminPrice);
  364. if (!adminPrice.toString().equals("0.00")) {
  365. altRefInfo.add(refInfo);
  366. }
  367. //需要知道订单是哪一个平台的,不能以账号的为标准 0或null为汇聚,1为杉德支付
  368. String payPlatform = order.getPayPlatform();
  369. String companyType = order.getCompanyType();
  370. if (StringUtils.isNotEmpty(payPlatform) && payPlatform.equals("1")) {
  371. try {
  372. Date date = order.getCreateDate();
  373. String payDate = DateTools.formatYYYYMMDD(date);
  374. String remark = "";
  375. BigDecimal refundAmount = order.getRefundAmount();
  376. //获取分销 关联设备
  377. LambdaQueryWrapper<TProportion> wrapper = new LambdaQueryWrapper<>();
  378. wrapper.eq(TProportion::getEquipmentId,equipmentId);
  379. TProportion proportion = proportionService.getOne(wrapper);
  380. // 判断是否退过款
  381. if(refundAmount != null && refundAmount.compareTo(BigDecimal.ZERO) > 0) {
  382. acctSplitBunch = getAcctSplitBunch(proportion, refusePrice, companyType).toString();
  383. } else {
  384. // 如果退款金额等于订单金额,就直接用原来的分销明细,如果小于就重新计算分销明细
  385. if(refusePrice.compareTo(order.getPrice()) < 0) {
  386. acctSplitBunch = getAcctSplitBunch(proportion, refusePrice, companyType).toString();
  387. }
  388. }
  389. String refundData = huifuMchService.refund(order.getSn(), price, payDate, remark, acctSplitBunch, companyType);
  390. //订单号
  391. if(StrUtil.isNotEmpty(refundData)&&(refundData.equals(HuifuConstant.TRANS_STAT_S)||refundData.equals(HuifuConstant.TRANS_STAT_P))) {
  392. order.setStatus(2);
  393. payFeign.updateOrder(order);
  394. // 修改订单明细
  395. for (TOrderDetails orderDetail : orderDetails) {
  396. // 退款中
  397. orderDetail.setRefundStatus("4");
  398. // 退款数量
  399. orderDetail.setRefundQuantity(orderDetail.getProductNumber());
  400. // 退款金额
  401. orderDetail.setRefundAmount(orderDetail.getAmount());
  402. payFeign.updateOrderDetails(orderDetail);
  403. }
  404. return ResponseEntity
  405. .status(HttpStatus.OK)
  406. .body(new ResultMessage()
  407. .setCode(true)
  408. .setData("申请退款已成功")
  409. .setMessage("申请退款已成功"));
  410. }else {
  411. return ResponseEntity
  412. .status(HttpStatus.OK)
  413. .body(new ResultMessage()
  414. .setCode(false)
  415. .setData("申请退款失败")
  416. .setMessage("申请退款失败"));
  417. }
  418. } catch (Exception e) {
  419. e.printStackTrace();
  420. }
  421. } else {
  422. try {
  423. String result = orderService.refund(order.getSn(), orderService.initRefundSn(equipment.getClientId(), refundNumber), refusePrice.subtract(p8_MarketRefAmount), p8_MarketRefAmount,"", notifyUrl, altRefInfo.toString());
  424. if ("退款申请成功".equals(result)) {
  425. order.setStatus(2);
  426. if(StringUtils.isNotEmpty(order.getCoupons())){
  427. //设置退款营销金额
  428. if(order.getRefundMarketingAmount()!=null){
  429. //累加
  430. order.setRefundMarketingAmount(order.getRefundMarketingAmount().add(p8_MarketRefAmount));
  431. }else {
  432. order.setRefundMarketingAmount(p8_MarketRefAmount);
  433. }
  434. }
  435. // 修改订单明细
  436. for (TOrderDetails orderDetail : orderDetails) {
  437. // 退款中
  438. orderDetail.setRefundStatus("4");
  439. // 退款数量
  440. orderDetail.setRefundQuantity(orderDetail.getProductNumber());
  441. // 退款金额
  442. orderDetail.setRefundAmount(orderDetail.getAmount());
  443. payFeign.updateOrderDetails(orderDetail);
  444. }
  445. order.setRefundTrxNo(String.valueOf(refusePrice));
  446. payFeign.updateOrder(order);
  447. return ResponseEntity
  448. .status(HttpStatus.OK)
  449. .body(new ResultMessage()
  450. .setCode(true)
  451. .setData("申请退款已成功")
  452. .setMessage("申请退款已成功"));
  453. }else {
  454. return ResponseEntity
  455. .status(HttpStatus.OK)
  456. .body(new ResultMessage()
  457. .setCode(false)
  458. .setData("申请退款失败")
  459. .setMessage("申请退款失败"));
  460. }
  461. } catch (Exception e) {
  462. e.printStackTrace();
  463. }
  464. }
  465. return ResponseEntity
  466. .status(HttpStatus.OK)
  467. .body(new ResultMessage()
  468. .setCode(true)
  469. .setData("申请退款已成功")
  470. .setMessage("申请退款已成功"));
  471. }
  472. @ApiOperation(value = "添加优惠码")
  473. @GetMapping("/equipmentPay")
  474. public ResponseModel<?> equipmentPay(Double number, Long equipmentId, String frpCode) {
  475. TEquipment equipment = tEquipmentService.getById(equipmentId);
  476. if (!frpCode.equals("ALIPAY_NATIVE") && !frpCode.equals("WEIXIN_NATIVE")) {
  477. return R.fail("参数错误");
  478. }
  479. if (equipment == null) {
  480. return R.fail("找不到设备");
  481. }
  482. TAdmin admin = tAdminService.getById(equipment.getAdminId());
  483. Double money = null;
  484. LambdaQueryWrapper<TPrice> query1 = Wrappers.lambdaQuery();
  485. query1.eq(TPrice::getName,"MG280");
  486. List<TPrice> prices = priceService.list(query1);
  487. for(TPrice price:prices){
  488. if(price.getName().equals("MG280")){
  489. money = price.getPrice();
  490. }
  491. }
  492. BigDecimal price = null;
  493. double pri = money * number;
  494. price = BigDecimal.valueOf(pri);
  495. String sn = orderService.initSn(equipmentId);
  496. String orderNo = sn;
  497. String productName = "280充值";
  498. BigDecimal cutPrice = price.multiply(new BigDecimal(100)).divide(new BigDecimal(100));
  499. BigDecimal amount = cutPrice;
  500. String productDesc = "";
  501. String commonParameter = "";
  502. String returnUrl = null;
  503. String notifyUrl = JoinpayConstant.Notify_Url_PromoCode;
  504. String isShowPic = "1";
  505. String openId = null;
  506. String authCode = null;
  507. String appid = null;
  508. String transactionModel = null;
  509. String tradeMerchantNo = null;
  510. String buyerId = null;
  511. String isAlt = "0";
  512. String altType = null;
  513. String altUrl = null;
  514. BigDecimal marketingAmount = null;
  515. // frpCode = "WEIXIN_NATIVE";
  516. TOrder order1 = new TOrder();
  517. order1.setId(IDGenerator.orderID());
  518. order1.setSn(sn);
  519. order1.setType(0);
  520. order1.setAdminId(admin.getId());
  521. order1.setProductName(productName);
  522. order1.setPrice(price);
  523. order1.setEquipmentId(equipmentId);
  524. order1.setClientId(equipment.getClientId());
  525. order1.setStatus(0);
  526. order1.setFrpCode(frpCode);
  527. order1.setCreateDate(new Date());
  528. order1.setModifyDate(new Date());
  529. payFeign.addOrder(order1);
  530. JSONArray altInfo = new JSONArray();
  531. String result = null;
  532. try {
  533. result = joinpayMchService.uniPay(
  534. orderNo, amount, productName, productDesc,
  535. commonParameter, returnUrl, notifyUrl, frpCode,
  536. isShowPic, openId, authCode, appid, transactionModel, tradeMerchantNo,
  537. buyerId, isAlt, altType, altInfo, altUrl, marketingAmount
  538. );
  539. } catch (Exception e) {
  540. e.printStackTrace();
  541. return R.fail("申请支付失败");
  542. }
  543. JSONObject resultJson = JSONObject.parseObject(result);
  544. // 汇聚支付支付申请返回支付二维码图片
  545. String rd_Pic = resultJson.getString("rd_Pic");
  546. if (resultJson == null || StringUtils.isBlank(rd_Pic)) {
  547. return R.fail("找不到支付图片");
  548. }
  549. JSONObject kindData = new JSONObject();
  550. kindData.put("sn", sn);
  551. kindData.put("price", price);
  552. kindData.put("image", rd_Pic);
  553. return R.ok(kindData);
  554. }
  555. /**
  556. * 汇付分销明细
  557. * @param proportion
  558. * @param price
  559. * @return
  560. */
  561. public JSONObject getAcctSplitBunch(TProportion proportion, BigDecimal price, String companyType) {
  562. // 获取分账有几方:0:分账方2个,1:分账方3个,2:分账方4个,3:分账方超4个
  563. Integer type = proportion.getType();
  564. // 汇付分账明细参数
  565. JSONObject acctSplitBunch = new JSONObject();
  566. JSONArray acctInfos = new JSONArray();
  567. BigDecimal adminProportion = null,
  568. agencyProportion = null,
  569. merchantProportion = null,
  570. selfProportion = null;
  571. BigDecimal agencyAmount = null;
  572. BigDecimal merchantAmount = null;
  573. BigDecimal selfAmount = null;
  574. BigDecimal adminAmount = null;
  575. THuifuMch agencyHuifu = null;
  576. THuifuMch merchantHuifu = null;
  577. THuifuMch selfHuifu = null;
  578. JSONObject agencyJson = null;
  579. JSONObject merchantJson = null;
  580. JSONObject selfJson = null;
  581. JSONObject adminJson = null;
  582. //取消平台扣手续费
  583. BigDecimal cutPrice = price.setScale(2, RoundingMode.HALF_UP);
  584. BigDecimal refuseAmount = new BigDecimal(0.00);
  585. switch (type) {
  586. case 0:
  587. // 公司平台分销
  588. adminProportion = proportion.getAdminProportion();
  589. // 商家自己的分销
  590. selfProportion = proportion.getProportion();
  591. // 平台分销获得利润
  592. adminAmount = cutPrice.multiply(adminProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_UP);
  593. // 商家分销获得利润
  594. selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  595. // 商家汇付信息:agencyHuifu
  596. LambdaQueryWrapper<THuifuMch> queryWrapper1 = new LambdaQueryWrapper<>();
  597. queryWrapper1.eq(THuifuMch::getAdminId,proportion.getAdminId());
  598. selfHuifu = huifuMchService.getOne(queryWrapper1);
  599. // 公司平台分销
  600. adminJson = new JSONObject();
  601. adminJson.put("div_amt", adminAmount.toString());
  602. if(StrUtil.isNotEmpty(companyType) && companyType.equals("1")) {
  603. adminJson.put("huifu_id", HuifuConstant.SC_DIV_HUIFU_ID);
  604. } else {
  605. adminJson.put("huifu_id", HuifuConstant.SZ_DIV_HUIFU_ID);
  606. }
  607. acctInfos.add(adminJson);
  608. // 商家分销获得利润
  609. selfJson = new JSONObject();
  610. selfJson.put("div_amt", selfAmount.toString());
  611. selfJson.put("huifu_id", selfHuifu.getHuifuId());
  612. acctInfos.add(selfJson);
  613. break;
  614. case 1:
  615. // 公司平台分销
  616. adminProportion = proportion.getAdminProportion();
  617. // 商家自己的分销
  618. selfProportion = proportion.getProportion();
  619. // 一级分销
  620. agencyProportion = proportion.getAgencyProportion();
  621. // 平台分销获得利润
  622. adminAmount = cutPrice.multiply(adminProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_UP);
  623. // 商家分销获得利润
  624. selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  625. // 汇付平台
  626. LambdaQueryWrapper<THuifuMch> queryWrapper2 = new LambdaQueryWrapper<>();
  627. queryWrapper2.eq(THuifuMch::getAdminId,proportion.getAdminId());
  628. selfHuifu = huifuMchService.getOne(queryWrapper2);
  629. LambdaQueryWrapper<THuifuMch> queryWrapper3 = new LambdaQueryWrapper<>();
  630. queryWrapper3.eq(THuifuMch::getAdminId,proportion.getAgencyId());
  631. agencyHuifu = huifuMchService.getOne(queryWrapper3);
  632. // 公司平台分销
  633. adminJson = new JSONObject();
  634. adminJson.put("div_amt", adminAmount.toString());
  635. if(StrUtil.isNotEmpty(companyType) && companyType.equals("1")) {
  636. adminJson.put("huifu_id", HuifuConstant.SC_DIV_HUIFU_ID);
  637. } else {
  638. adminJson.put("huifu_id", HuifuConstant.SZ_DIV_HUIFU_ID);
  639. }
  640. acctInfos.add(adminJson);
  641. // 商家分销获得利润
  642. selfJson = new JSONObject();
  643. selfJson.put("div_amt", selfAmount.toString());
  644. selfJson.put("huifu_id", selfHuifu.getHuifuId());
  645. acctInfos.add(selfJson);
  646. if(!(agencyProportion.compareTo(refuseAmount) == 0||agencyProportion==null)){
  647. // 一级分销获得利润
  648. agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  649. agencyJson = new JSONObject();
  650. agencyJson.put("div_amt", agencyAmount.toString());
  651. agencyJson.put("huifu_id", agencyHuifu.getHuifuId());
  652. acctInfos.add(agencyJson);
  653. }
  654. break;
  655. case 2:
  656. // 公司平台分销
  657. adminProportion = proportion.getAdminProportion();
  658. // 商家自己的分销
  659. selfProportion = proportion.getProportion();
  660. // 一级分销
  661. agencyProportion = proportion.getAgencyProportion();
  662. // 二级分销
  663. merchantProportion = proportion.getMerchantProportion();
  664. // 平台分销获得利润
  665. adminAmount = cutPrice.multiply(adminProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_UP);
  666. // 商家分销获得利润
  667. selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  668. LambdaQueryWrapper<THuifuMch> queryWrapper4 = new LambdaQueryWrapper<>();
  669. queryWrapper4.eq(THuifuMch::getAdminId,proportion.getAdminId());
  670. selfHuifu = huifuMchService.getOne(queryWrapper4);
  671. LambdaQueryWrapper<THuifuMch> queryWrapper5 = new LambdaQueryWrapper<>();
  672. queryWrapper5.eq(THuifuMch::getAdminId,proportion.getAgencyId());
  673. agencyHuifu = huifuMchService.getOne(queryWrapper5);
  674. LambdaQueryWrapper<THuifuMch> queryWrapper6 = new LambdaQueryWrapper<>();
  675. queryWrapper6.eq(THuifuMch::getAdminId,proportion.getMerchantId());
  676. merchantHuifu = huifuMchService.getOne(queryWrapper6);
  677. // 公司平台分销
  678. adminJson = new JSONObject();
  679. adminJson.put("div_amt", adminAmount.toString());
  680. if(StrUtil.isNotEmpty(companyType) && companyType.equals("1")) {
  681. adminJson.put("huifu_id", HuifuConstant.SC_DIV_HUIFU_ID);
  682. } else {
  683. adminJson.put("huifu_id", HuifuConstant.SZ_DIV_HUIFU_ID);
  684. }
  685. acctInfos.add(adminJson);
  686. // 商家分销获得利润
  687. selfJson = new JSONObject();
  688. selfJson.put("div_amt", selfAmount.toString());
  689. selfJson.put("huifu_id", selfHuifu.getHuifuId());
  690. acctInfos.add(selfJson);
  691. if(!(agencyProportion.compareTo(refuseAmount) == 0||agencyProportion==null)){
  692. // 一级分销获得利润
  693. agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  694. agencyJson = new JSONObject();
  695. agencyJson.put("div_amt", agencyAmount.toString());
  696. agencyJson.put("huifu_id", agencyHuifu.getHuifuId());
  697. acctInfos.add(agencyJson);
  698. }
  699. if(!(merchantProportion.compareTo(refuseAmount) == 0||merchantProportion==null)){
  700. // 二级分销获得利润
  701. merchantAmount = cutPrice.multiply(merchantProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  702. merchantJson = new JSONObject();
  703. merchantJson.put("div_amt", merchantAmount.toString());
  704. merchantJson.put("huifu_id", merchantHuifu.getHuifuId());
  705. acctInfos.add(merchantJson);
  706. }
  707. break;
  708. }
  709. // 计算div_amt的总和
  710. BigDecimal totalAmount = BigDecimal.ZERO;
  711. for (int i = 0; i < acctInfos.size(); i++) {
  712. JSONObject jsonObject = acctInfos.getJSONObject(i);
  713. BigDecimal divAmt = new BigDecimal(jsonObject.getString("div_amt"));
  714. totalAmount = totalAmount.add(divAmt);
  715. }
  716. // 如果总和超过原本金额,调整div_amt
  717. if (totalAmount.compareTo(price) > 0) {
  718. BigDecimal diffAmount = totalAmount.subtract(price);
  719. for (int i = 0; i < acctInfos.size(); i++) {
  720. JSONObject jsonObject = acctInfos.getJSONObject(i);
  721. String huifuId = jsonObject.getString("huifu_id");
  722. if (huifuId.equals(HuifuConstant.SZ_DIV_HUIFU_ID) || huifuId.equals(HuifuConstant.SC_DIV_HUIFU_ID)) {
  723. BigDecimal currentDivAmt = new BigDecimal(jsonObject.getString("div_amt"));
  724. BigDecimal adjustedDivAmt = currentDivAmt.subtract(diffAmount).max(BigDecimal.ZERO);
  725. jsonObject.put("div_amt", adjustedDivAmt.toString());
  726. break;
  727. }
  728. }
  729. }
  730. acctSplitBunch.put("acct_infos", acctInfos);
  731. return acctSplitBunch;
  732. }
  733. /**
  734. * 汇聚分销明细
  735. * @param proportion
  736. * @param price
  737. * @return
  738. */
  739. public JSONArray getAltInfo(TProportion proportion, BigDecimal price) {
  740. // 获取分账有几方:0:分账方2个,1:分账方3个,2:分账方4个,3:分账方超4个
  741. Integer type = proportion.getType();
  742. // 汇聚分账参数
  743. JSONArray altInfo = new JSONArray();
  744. BigDecimal agencyProportion = null,
  745. merchantProportion = null,
  746. selfProportion = null;
  747. BigDecimal agencyAmount = null;
  748. BigDecimal merchantAmount = null;
  749. BigDecimal selfAmount = null;
  750. TJoinpayMch agencyMch = null;
  751. TJoinpayMch merchantMch = null;
  752. TJoinpayMch selfMch = null;
  753. JSONObject agencyJson = null;
  754. JSONObject merchantJson = null;
  755. JSONObject selfJson = null;
  756. //取消平台扣手续费
  757. BigDecimal cutPrice = price.setScale(2, RoundingMode.HALF_UP);
  758. BigDecimal refuseAmount = new BigDecimal(0.00);
  759. switch (type) {
  760. case 0:
  761. // 商家自己的分销
  762. selfProportion = proportion.getProportion();
  763. // 商家分销获得利润
  764. selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  765. // 汇聚平台
  766. LambdaQueryWrapper<TJoinpayMch> queryWrapper1 = new LambdaQueryWrapper<>();
  767. queryWrapper1.eq(TJoinpayMch::getAdminId,proportion.getAdminId());
  768. selfMch = joinpayMchService.getOne(queryWrapper1);
  769. selfJson = new JSONObject();
  770. selfJson.put("altMchNo", selfMch.getAltMchNo());
  771. selfJson.put("altAmount", selfAmount.toString());
  772. selfJson.put("isGuar", "12");
  773. altInfo.add(selfJson);
  774. break;
  775. case 1:
  776. // 商家自己的分销
  777. selfProportion = proportion.getProportion();
  778. // 一级分销
  779. agencyProportion = proportion.getAgencyProportion();
  780. // 商家分销获得利润
  781. selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  782. // 汇聚平台
  783. LambdaQueryWrapper<TJoinpayMch> queryWrapper2 = new LambdaQueryWrapper<>();
  784. queryWrapper2.eq(TJoinpayMch::getAdminId,proportion.getAdminId());
  785. selfMch = joinpayMchService.getOne(queryWrapper2);
  786. LambdaQueryWrapper<TJoinpayMch> queryWrapper3 = new LambdaQueryWrapper<>();
  787. queryWrapper3.eq(TJoinpayMch::getAdminId,proportion.getAgencyId());
  788. agencyMch = joinpayMchService.getOne(queryWrapper3);
  789. if(!(agencyProportion.compareTo(refuseAmount) == 0||agencyProportion==null)){
  790. // 一级分销获得利润
  791. agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  792. agencyJson = new JSONObject();
  793. agencyJson.put("altMchNo", agencyMch.getAltMchNo());
  794. agencyJson.put("altAmount", agencyAmount.toString());
  795. agencyJson.put("isGuar", "12");
  796. altInfo.add(agencyJson);
  797. }
  798. // 商家自己分销获得利润
  799. selfJson = new JSONObject();
  800. selfJson.put("altMchNo", selfMch.getAltMchNo());
  801. selfJson.put("altAmount", selfAmount.toString());
  802. selfJson.put("isGuar", "12");
  803. altInfo.add(selfJson);
  804. break;
  805. case 2:
  806. // 商家自己的分销
  807. selfProportion = proportion.getProportion();
  808. // 一级分销
  809. agencyProportion = proportion.getAgencyProportion();
  810. // 二级分销
  811. merchantProportion = proportion.getMerchantProportion();
  812. // 三级分销
  813. // personageProportion = proportion.getPersonageProportion();
  814. // 商家分销获得利润
  815. selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  816. // 汇聚
  817. LambdaQueryWrapper<TJoinpayMch> queryWrapper4 = new LambdaQueryWrapper<>();
  818. queryWrapper4.eq(TJoinpayMch::getAdminId,proportion.getAdminId());
  819. selfMch = joinpayMchService.getOne(queryWrapper4);
  820. LambdaQueryWrapper<TJoinpayMch> queryWrapper5 = new LambdaQueryWrapper<>();
  821. queryWrapper5.eq(TJoinpayMch::getAdminId,proportion.getAgencyId());
  822. agencyMch = joinpayMchService.getOne(queryWrapper5);
  823. LambdaQueryWrapper<TJoinpayMch> queryWrapper6 = new LambdaQueryWrapper<>();
  824. queryWrapper6.eq(TJoinpayMch::getAdminId,proportion.getMerchantId());
  825. merchantMch = joinpayMchService.getOne(queryWrapper6);
  826. if(!(agencyProportion.compareTo(refuseAmount) == 0||agencyProportion==null)){
  827. // 代理分销获得利润
  828. agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  829. agencyJson = new JSONObject();
  830. agencyJson.put("altMchNo", agencyMch.getAltMchNo());
  831. agencyJson.put("altAmount", agencyAmount.toString());
  832. agencyJson.put("isGuar", "12");
  833. altInfo.add(agencyJson);
  834. }
  835. if(!(merchantProportion.compareTo(refuseAmount) == 0||merchantProportion==null)){
  836. // 经销商分销获得利润
  837. merchantAmount = cutPrice.multiply(merchantProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  838. merchantJson = new JSONObject();
  839. merchantJson.put("altMchNo", merchantMch.getAltMchNo());
  840. merchantJson.put("altAmount", merchantAmount.toString());
  841. merchantJson.put("isGuar", "12");
  842. altInfo.add(merchantJson);
  843. }
  844. // 商家自己分销获得利润
  845. merchantJson = new JSONObject();
  846. merchantJson.put("altMchNo", selfMch.getAltMchNo());
  847. merchantJson.put("altAmount", selfAmount.toString());
  848. merchantJson.put("isGuar", "12");altInfo.add(merchantJson);
  849. break;
  850. }
  851. Iterator<Object> iterator = altInfo.iterator();
  852. while (iterator.hasNext()) {
  853. JSONObject jsonObject = (JSONObject) iterator.next();
  854. String altAmount = jsonObject.getString("altAmount");
  855. if (altAmount.equals("0.00")) {
  856. iterator.remove();
  857. }
  858. }
  859. return altInfo;
  860. }
  861. /**
  862. * 退款
  863. * @param
  864. * @return
  865. */
  866. @PostMapping("/oldRefund")
  867. public ResponseEntity<?> oldRefund(@RequestBody TOrder torder) {
  868. // 退款金额: refusePrice
  869. BigDecimal refusePrice = torder.getPrice().setScale(2, RoundingMode.HALF_DOWN);
  870. // 退款数量: refundNumber
  871. Integer refundNumber = torder.getProductNumber();
  872. if(refusePrice.compareTo(new BigDecimal("0.00"))<=0){
  873. return ResponseEntity
  874. .status(HttpStatus.OK)
  875. .body(new ResultMessage()
  876. .setCode(false)
  877. .setData("")
  878. .setMessage("退款金额非法"));
  879. }
  880. TOrder order = R.getDataIfSuccess(payFeign.getOrder(String.valueOf(torder.getId())));
  881. // 已退款的金额: refundAmount
  882. // BigDecimal refundAmount = order.getRefundAmount();
  883. TEquipment equipment = tEquipmentService.getById(order.getEquipmentId());
  884. Long equipmentId = equipment.getId();
  885. if (order.getProductName().equals("280充值")) {
  886. return ResponseEntity
  887. .status(HttpStatus.OK)
  888. .body(new ResultMessage()
  889. .setCode(false)
  890. .setData("订单不退款")
  891. .setMessage("订单不退款"));
  892. }
  893. if (order.getStatus().equals("0")) {
  894. return ResponseEntity
  895. .status(HttpStatus.OK)
  896. .body(new ResultMessage()
  897. .setCode(false)
  898. .setData("订单非支付状态")
  899. .setMessage("订单非支付状态"));
  900. }
  901. //判断是不是直连微信退款
  902. if(StringUtils.isNotEmpty(order.getPayPlatform())&&order.getPayPlatform().equals("3")){
  903. String ifSuccess = R.getDataIfSuccess(payFeign.refund(order.getSn(),refusePrice));
  904. return ResponseEntity
  905. .status(HttpStatus.OK)
  906. .body(new ResultMessage()
  907. .setCode(true)
  908. .setData(ifSuccess)
  909. .setMessage(ifSuccess));
  910. }
  911. String notifyUrl = JoinpayConstant.OLD_Notify_Refund_Url;
  912. JSONArray altInfoArray = JSONArray.parseArray(order.getAltInfo());
  913. JSONArray altRefInfo = new JSONArray();
  914. BigDecimal p = BigDecimal.ZERO;
  915. BigDecimal price = BigDecimal.ZERO;
  916. //订单总金额
  917. BigDecimal sumPrice = BigDecimal.ZERO;
  918. if(order.getRefundAmount()!=null){
  919. sumPrice=sumPrice.add(order.getPrice()).add(order.getRefundAmount()).setScale(2, RoundingMode.HALF_DOWN);
  920. }else {
  921. sumPrice=sumPrice.add(order.getPrice()).setScale(2, RoundingMode.HALF_DOWN);
  922. }
  923. BigDecimal p8_MarketRefAmount = BigDecimal.ZERO;
  924. for (int i = 0; i < altInfoArray.size(); i++) {
  925. JSONObject altInfoObject = altInfoArray.getJSONObject(i);
  926. JSONObject refInfo = new JSONObject();
  927. refInfo.put("altMchNo", altInfoObject.getString("altMchNo"));
  928. String altAmount = altInfoObject.getString("altAmount");
  929. BigDecimal pri = new BigDecimal(order.getPrice().toString()).setScale(2, RoundingMode.HALF_DOWN);
  930. if(refusePrice.compareTo(pri)<0){
  931. //退部分的钱
  932. BigDecimal altAmountPrice = new BigDecimal(altAmount);
  933. altAmountPrice = altAmountPrice.multiply(refusePrice.divide(order.getPrice(),2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
  934. p = p.add(altAmountPrice);
  935. refInfo.put("altRefAmount", altAmountPrice.toString());
  936. if(order.getMarketingAmount()!=null){
  937. p8_MarketRefAmount = order.getMarketingAmount().multiply(refusePrice.divide(order.getPrice(),2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
  938. }
  939. }else {
  940. //退全部的钱
  941. BigDecimal altAmountPrice = new BigDecimal(altAmount).setScale(2, RoundingMode.HALF_DOWN);
  942. BigDecimal divide = altAmountPrice.divide(order.getPrice(),3,BigDecimal.ROUND_HALF_UP).setScale(3, RoundingMode.HALF_DOWN);
  943. altAmountPrice = refusePrice.multiply(divide).setScale(2, RoundingMode.HALF_DOWN);
  944. p = p.add(altAmountPrice);
  945. refInfo.put("altRefAmount", altAmountPrice.toString());
  946. if(order.getMarketingAmount()!=null){
  947. p8_MarketRefAmount = order.getMarketingAmount().multiply(refusePrice.divide(order.getPrice(),2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
  948. }
  949. }
  950. price = price.add(altInfoObject.getBigDecimal("altAmount"));
  951. if (!altInfoObject.getBigDecimal("altAmount").toString().equals("0.00")) {
  952. altRefInfo.add(refInfo);
  953. }
  954. }
  955. // 特殊处理下:添加多一个平台方分销的金额
  956. // 平台获得的收益
  957. // BigDecimal adminPrice = new BigDecimal(order.getPrice().toString()).subtract(price);
  958. BigDecimal adminPrice = refusePrice.subtract(p).setScale(2, RoundingMode.HALF_DOWN);
  959. JSONObject refInfo = new JSONObject();
  960. refInfo.put("altMchNo", JoinpayConstant.mch_no);
  961. refInfo.put("altRefAmount", adminPrice.toString());
  962. price = price.add(adminPrice);
  963. if (!adminPrice.toString().equals("0.00")) {
  964. altRefInfo.add(refInfo);
  965. }
  966. String text = "";
  967. //需要知道订单是哪一个平台的,不能以账号的为标准 0或null为汇聚,1为杉德支付
  968. String payPlatform = order.getPayPlatform();
  969. if (StringUtils.isNotEmpty(payPlatform) && payPlatform.equals("1")) {
  970. try {
  971. String refund = tShandeMchService.refund(order.getId(), orderService.initRefundSn(equipment.getClientId(), refundNumber).toString(), price, "", altRefInfo.toString());
  972. com.gexin.fastjson.JSONObject jsonObject = com.gexin.fastjson.JSON.parseObject(refund);
  973. String sub_code = jsonObject.getString("sub_code");
  974. //订单号
  975. if (StringUtils.isNotEmpty(sub_code)&&sub_code.equals("REFUND_SUCCESS")) {
  976. order.setStatus(3);
  977. String refund_settle_amount = jsonObject.getString("refund_settle_amount");
  978. String refund_plat_trx_no = jsonObject.getString("refund_plat_trx_no");
  979. order.setRefundTrxNo(refund_plat_trx_no);
  980. order.setRefundAmount(new BigDecimal(refund_settle_amount));
  981. order.setRefundDate(new Date());
  982. payFeign.updateOrder(order);
  983. //金额表今日金额要改 要分类型
  984. TAdmin admin = tAdminService.getById(order.getAdminId());
  985. Integer orderType = order.getType();
  986. switch (orderType) {
  987. case 0:
  988. BigDecimal agencyProportion = order.getAgencyProportion();
  989. // 代理分销获得利润
  990. BigDecimal agencyPrice = price.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  991. //修改金额表的今日金额
  992. LambdaQueryWrapper<TShandeMch> query = Wrappers.lambdaQuery();
  993. query.eq(TShandeMch::getAdminId,order.getAdminId());
  994. List<TShandeMch> list = tShandeMchService.list(query);
  995. if(list.size()>0){
  996. TShandeMch shandeMch = list.get(0);
  997. BigDecimal todayBalance = shandeMch.getTodayBalance();
  998. todayBalance = todayBalance.subtract(agencyPrice).setScale(2, RoundingMode.HALF_DOWN);
  999. shandeMch.setTodayBalance(todayBalance);
  1000. shandeMch.setModifyDate(new Date());
  1001. tShandeMchService.updateById(shandeMch);
  1002. }
  1003. break;
  1004. case 1:
  1005. BigDecimal agencyProportion1 = order.getAgencyProportion();
  1006. BigDecimal merchantProportion1 = order.getMerchantProportion();
  1007. // 代理分销获得利润
  1008. BigDecimal agencyPrice1 = price.multiply(agencyProportion1.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  1009. // 经销商分销获得利润
  1010. BigDecimal merchantAmount1 = price.multiply(merchantProportion1.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  1011. //修改代理金额表的今日金额
  1012. LambdaQueryWrapper<TShandeMch> query1 = Wrappers.lambdaQuery();
  1013. query1.eq(TShandeMch::getAdminId,order.getAgencyId());
  1014. List<TShandeMch> list1 = tShandeMchService.list(query1);
  1015. if(list1.size()>0){
  1016. TShandeMch shandeMch = list1.get(0);
  1017. BigDecimal todayBalance = shandeMch.getTodayBalance();
  1018. todayBalance = todayBalance.subtract(agencyPrice1).setScale(2, RoundingMode.HALF_DOWN);
  1019. shandeMch.setTodayBalance(todayBalance);
  1020. shandeMch.setModifyDate(new Date());
  1021. tShandeMchService.updateById(shandeMch);
  1022. }
  1023. //修改经销商金额表的今日金额;
  1024. LambdaQueryWrapper<TShandeMch> query2 = Wrappers.lambdaQuery();
  1025. query2.eq(TShandeMch::getAdminId,admin.getId());
  1026. List<TShandeMch> list2 = tShandeMchService.list(query2);
  1027. if(list2.size()>0){
  1028. TShandeMch shandeMch = list2.get(0);
  1029. BigDecimal todayBalance = shandeMch.getTodayBalance();
  1030. todayBalance = todayBalance.subtract(merchantAmount1).setScale(2, RoundingMode.HALF_DOWN);
  1031. shandeMch.setTodayBalance(todayBalance);
  1032. shandeMch.setModifyDate(new Date());
  1033. tShandeMchService.updateById(shandeMch);
  1034. }
  1035. break;
  1036. case 2:
  1037. BigDecimal agencyProportion2 = order.getAgencyProportion();
  1038. BigDecimal merchantProportion2 = order.getMerchantProportion();
  1039. BigDecimal personageProportion2 = order.getPersonageProportion();
  1040. // 代理分销获得利润
  1041. BigDecimal agencyAmount = price.multiply(agencyProportion2.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  1042. // 经销商分销获得利润
  1043. BigDecimal merchantAmount = price.multiply(merchantProportion2.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  1044. // 经销商分销获得利润
  1045. BigDecimal personageAmount = price.multiply(personageProportion2.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  1046. //修改代理金额表的今日金额
  1047. LambdaQueryWrapper<TShandeMch> query3 = Wrappers.lambdaQuery();
  1048. query3.eq(TShandeMch::getAdminId,order.getAgencyId());
  1049. List<TShandeMch> list3 = tShandeMchService.list(query3);
  1050. if(list3.size()>0){
  1051. TShandeMch shandeMch = list3.get(0);
  1052. BigDecimal todayBalance = shandeMch.getTodayBalance();
  1053. todayBalance = todayBalance.subtract(agencyAmount).setScale(2, RoundingMode.HALF_DOWN);
  1054. shandeMch.setTodayBalance(todayBalance);
  1055. shandeMch.setModifyDate(new Date());
  1056. tShandeMchService.updateById(shandeMch);
  1057. }
  1058. //修改经销商金额表的今日金额
  1059. LambdaQueryWrapper<TShandeMch> query4 = Wrappers.lambdaQuery();
  1060. query4.eq(TShandeMch::getAdminId,order.getMerchantId());
  1061. List<TShandeMch> list4 = tShandeMchService.list(query4);
  1062. if(list4.size()>0){
  1063. TShandeMch shandeMch = list4.get(0);
  1064. BigDecimal todayBalance = shandeMch.getTodayBalance();
  1065. todayBalance = todayBalance.subtract(merchantAmount).setScale(2, RoundingMode.HALF_DOWN);
  1066. shandeMch.setTodayBalance(todayBalance);
  1067. shandeMch.setModifyDate(new Date());
  1068. tShandeMchService.updateById(shandeMch);
  1069. }
  1070. //修改终端金额表的今日金额
  1071. LambdaQueryWrapper<TShandeMch> query5 = Wrappers.lambdaQuery();
  1072. query5.eq(TShandeMch::getAdminId,admin.getId());
  1073. List<TShandeMch> list5 = tShandeMchService.list(query5);
  1074. if(list5.size()>0){
  1075. TShandeMch shandeMch = list5.get(0);
  1076. BigDecimal todayBalance = shandeMch.getTodayBalance();
  1077. todayBalance = todayBalance.subtract(personageAmount).setScale(2, RoundingMode.HALF_DOWN);
  1078. shandeMch.setTodayBalance(todayBalance);
  1079. shandeMch.setModifyDate(new Date());
  1080. tShandeMchService.updateById(shandeMch);
  1081. }
  1082. break;
  1083. }
  1084. TOrder order1 = R.getDataIfSuccess(payFeign.getOrder(String.valueOf(torder.getId())));
  1085. order1.setModifyDate(new Date());
  1086. esTOrderService.updateDataById(order1);
  1087. return ResponseEntity
  1088. .status(HttpStatus.OK)
  1089. .body(new ResultMessage()
  1090. .setCode(true)
  1091. .setData("申请退款已成功")
  1092. .setMessage("申请退款已成功"));
  1093. } else {
  1094. return ResponseEntity
  1095. .status(HttpStatus.OK)
  1096. .body(new ResultMessage()
  1097. .setCode(false)
  1098. .setData("申请退款失败")
  1099. .setMessage(refund));
  1100. }
  1101. } catch (Exception e) {
  1102. e.printStackTrace();
  1103. }
  1104. } else {
  1105. try {
  1106. String result = orderService.refund(order.getSn(), orderService.initRefundSn(equipment.getClientId(), refundNumber), refusePrice.subtract(p8_MarketRefAmount), p8_MarketRefAmount,"", notifyUrl, altRefInfo.toString());
  1107. if ("退款申请成功".equals(result)) {
  1108. order.setStatus(2);
  1109. if(StringUtils.isNotEmpty(order.getCoupons())){
  1110. //设置退款营销金额
  1111. if(order.getRefundMarketingAmount()!=null){
  1112. //累加
  1113. order.setRefundMarketingAmount(order.getRefundMarketingAmount().add(p8_MarketRefAmount));
  1114. }else {
  1115. order.setRefundMarketingAmount(p8_MarketRefAmount);
  1116. }
  1117. }
  1118. order.setRefundTrxNo(String.valueOf(refusePrice));
  1119. payFeign.updateOrder(order);
  1120. return ResponseEntity
  1121. .status(HttpStatus.OK)
  1122. .body(new ResultMessage()
  1123. .setCode(true)
  1124. .setData("申请退款已成功")
  1125. .setMessage("申请退款已成功"));
  1126. }else {
  1127. return ResponseEntity
  1128. .status(HttpStatus.OK)
  1129. .body(new ResultMessage()
  1130. .setCode(false)
  1131. .setData("申请退款失败")
  1132. .setMessage("申请退款失败"));
  1133. }
  1134. } catch (Exception e) {
  1135. e.printStackTrace();
  1136. }
  1137. }
  1138. return ResponseEntity
  1139. .status(HttpStatus.OK)
  1140. .body(new ResultMessage()
  1141. .setCode(true)
  1142. .setData("申请退款已成功")
  1143. .setMessage("申请退款已成功"));
  1144. }
  1145. }