12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232 |
- package com.szwl.controller;
- import cn.com.crbank.ommo.bean.ResultMessage;
- import cn.hutool.core.util.StrUtil;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.huifu.bspay.sdk.opps.core.utils.DateTools;
- import com.szwl.constant.HuifuConstant;
- import com.szwl.constant.ResponseCodesEnum;
- import com.szwl.feign.bean.PayFeign;
- import com.szwl.constant.JoinpayConstant;
- import com.szwl.model.bo.R;
- import com.szwl.model.bo.ResponseModel;
- import com.szwl.model.entity.*;
- import com.szwl.service.*;
- import com.szwl.service.es.EsTOrderService;
- import com.szwl.util.IDGenerator;
- import io.swagger.annotations.ApiOperation;
- import org.apache.commons.lang.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.ResponseEntity;
- import org.springframework.web.bind.annotation.*;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.util.*;
- /**
- * <p>
- * 前端控制器
- * </p>
- *
- * @author wuhs
- * @since 2022-04-23
- */
- @RestController
- @RequestMapping("/tOrder")
- public class TOrderController {
- @Autowired
- TAdminService tAdminService;
- @Autowired
- TEquipmentService tEquipmentService;
- @Autowired
- TOrderService orderService;
- @Autowired
- PayFeign payFeign;
- @Autowired
- TShandeMchService tShandeMchService;
- @Autowired
- EsTOrderService esTOrderService;
- @Autowired
- TPriceService priceService;
- @Autowired
- TJoinpayMchService joinpayMchService;
- @Autowired
- THuifuMchService huifuMchService;
- @Autowired
- private TProportionService proportionService;
- /**
- * 退款
- * @param
- * @return
- */
- @PostMapping("/refund")
- public ResponseModel<?> refund(@RequestBody TOrder torder) {
- // 退款金额: refusePrice
- BigDecimal refusePrice = torder.getPrice().setScale(2, RoundingMode.HALF_DOWN);
- // 退款数量: refundNumber
- Integer refundNumber = torder.getProductNumber();
- // 退款明细:note
- String note = torder.getNote();
- String[] refundDetails = note.split(",");
- Map<String, Integer> refundMap = new HashMap<>();
- for (String refundDetail : refundDetails) {
- String[] keyValue = refundDetail.split("-");
- String key = keyValue[0];
- String value = keyValue[1];
- refundMap.put(key, Integer.valueOf(value));
- }
- if(refusePrice.compareTo(new BigDecimal("0.00"))<=0){
- return R.fail(ResponseCodesEnum.A0001,"退款金额非法");
- }
- TOrder order = R.getDataIfSuccess(payFeign.getOrder(String.valueOf(torder.getId())));
- if(order.getRefundQuantity() != null && order.getRefundQuantity()>0) {
- order.setRefundQuantity(refundNumber + order.getRefundQuantity());
- } else {
- order.setRefundQuantity(refundNumber);
- }
- List<TOrderDetails> orderDetails = R.getDataIfSuccess(payFeign.getOrderDetailsBySn(order.getSn()));
- TEquipment equipment = tEquipmentService.getById(order.getEquipmentId());
- Long equipmentId = equipment.getId();
- if (order.getProductName().equals("280充值")) {
- return R.fail(ResponseCodesEnum.A0002,"订单不退款");
- }
- if (order.getStatus().equals("0")) {
- return R.fail(ResponseCodesEnum.A0002,"订单非支付状态");
- }
- //判断是不是直连微信退款
- if(StringUtils.isNotEmpty(order.getPayPlatform())&&order.getPayPlatform().equals("3")){
- String ifSuccess = R.getDataIfSuccess(payFeign.refund(order.getSn(),refusePrice));
- return R.ok(ifSuccess);
- }
- String notifyUrl = JoinpayConstant.Notify_Refund_Url;
- JSONArray altInfoArray = JSONArray.parseArray(order.getAltInfo());
- JSONArray altRefInfo = new JSONArray();
- // 汇付分销明细
- String acctSplitBunch = "";
- BigDecimal p = BigDecimal.ZERO;
- BigDecimal price = BigDecimal.ZERO;
- //订单总金额
- BigDecimal sumPrice = BigDecimal.ZERO;
- BigDecimal refundAmount = order.getRefundAmount();
- if(refundAmount != null){
- sumPrice = sumPrice.add(order.getPrice()).subtract(refundAmount).setScale(2, RoundingMode.HALF_DOWN);
- }else {
- sumPrice = sumPrice.add(order.getPrice()).setScale(2, RoundingMode.HALF_DOWN);
- }
- // BigDecimal pri = new BigDecimal(order.getPrice().toString()).setScale(2, RoundingMode.HALF_DOWN);
- BigDecimal p8_MarketRefAmount = BigDecimal.ZERO;
- //获取分销 关联设备
- LambdaQueryWrapper<TProportion> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(TProportion::getEquipmentId,equipmentId);
- TProportion proportion = proportionService.getOne(wrapper);
- if(refusePrice.compareTo(sumPrice)<0){
- //退部分的钱
- JSONArray altInfo = getAltInfo(proportion, refusePrice);
- altInfoArray = JSONArray.parseArray(altInfo.toString());
- for (int i = 0; i < altInfoArray.size(); i++) {
- JSONObject altInfoObject = altInfoArray.getJSONObject(i);
- JSONObject refInfo = new JSONObject();
- refInfo.put("altMchNo", altInfoObject.getString("altMchNo"));
- String altAmount = altInfoObject.getString("altAmount");
- BigDecimal altAmountPrice = new BigDecimal(altAmount);
- // altAmountPrice = altAmountPrice.multiply(refusePrice.divide(order.getPrice(),2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
- p = p.add(altAmountPrice);
- refInfo.put("altRefAmount", altAmountPrice.toString());
- if(order.getMarketingAmount()!=null){
- p8_MarketRefAmount = order.getMarketingAmount().multiply(refusePrice.divide(order.getPrice(),2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
- }
- price = price.add(altInfoObject.getBigDecimal("altAmount"));
- if (!altInfoObject.getBigDecimal("altAmount").toString().equals("0.00")) {
- altRefInfo.add(refInfo);
- }
- }
- }else {
- //退全部的钱
- for (int i = 0; i < altInfoArray.size(); i++) {
- JSONObject altInfoObject = altInfoArray.getJSONObject(i);
- JSONObject refInfo = new JSONObject();
- refInfo.put("altMchNo", altInfoObject.getString("altMchNo"));
- String altAmount = altInfoObject.getString("altAmount");
- BigDecimal altAmountPrice = new BigDecimal(altAmount).setScale(2, RoundingMode.HALF_DOWN);
- // BigDecimal divide = altAmountPrice.divide(order.getPrice(),3,BigDecimal.ROUND_HALF_UP).setScale(3, RoundingMode.HALF_DOWN);
- // altAmountPrice = refusePrice.multiply(divide).setScale(2, RoundingMode.HALF_DOWN);
- // refInfo.put("altRefAmount", altAmountPrice.toString());
- p = p.add(altAmountPrice);
- refInfo.put("altRefAmount", altAmountPrice.toString());
- if(order.getMarketingAmount()!=null){
- p8_MarketRefAmount = order.getMarketingAmount().multiply(refusePrice.divide(order.getPrice(),2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
- }
- price = price.add(altInfoObject.getBigDecimal("altAmount"));
- if (!altInfoObject.getBigDecimal("altAmount").toString().equals("0.00")) {
- altRefInfo.add(refInfo);
- }
- }
- }
- // 特殊处理下:添加多一个平台方分销的金额
- // 平台获得的收益
- BigDecimal adminPrice = refusePrice.subtract(p).setScale(2, RoundingMode.HALF_DOWN);
- JSONObject refInfo = new JSONObject();
- refInfo.put("altMchNo", JoinpayConstant.mch_no);
- refInfo.put("altRefAmount", adminPrice.toString());
- price = price.add(adminPrice);
- if (!adminPrice.toString().equals("0.00")) {
- altRefInfo.add(refInfo);
- }
- //需要知道订单是哪一个平台的,不能以账号的为标准 0或null为汇聚,1为汇付支付
- String payPlatform = order.getPayPlatform();
- String companyType = order.getCompanyType();
- if (StringUtils.isNotEmpty(payPlatform) && payPlatform.equals("1")) {
- try {
- Date date = order.getCreateDate();
- String payDate = DateTools.formatYYYYMMDD(date);
- String remark = "";
- // BigDecimal refundAmount = order.getRefundAmount();
- // 判断是否退过款
- if(refundAmount != null && refundAmount.compareTo(BigDecimal.ZERO) > 0) {
- acctSplitBunch = getAcctSplitBunch(proportion, refusePrice, companyType).toString();
- } else {
- // 如果退款金额等于订单金额,就直接用原来的分销明细,如果小于就重新计算分销明细
- if(refusePrice.compareTo(order.getPrice()) < 0) {
- acctSplitBunch = getAcctSplitBunch(proportion, refusePrice, companyType).toString();
- }
- }
- String refundData = huifuMchService.refund(order.getSn(), price, payDate, remark, acctSplitBunch, companyType);
- //订单号
- if(StrUtil.isNotEmpty(refundData)&&(refundData.equals(HuifuConstant.TRANS_STAT_S)||refundData.equals(HuifuConstant.TRANS_STAT_P))) {
- order.setStatus(2);
- payFeign.updateOrder(order);
- // 修改订单明细
- for (TOrderDetails orderDetail : orderDetails) {
- String productNo = orderDetail.getProductNo();
- Integer productNumber = refundMap.get(productNo);
- if(productNumber != null) {
- // 退款中
- orderDetail.setRefundStatus("4");
- // 退款数量
- orderDetail.setRefundQuantity(productNumber);
- // 退款金额
- orderDetail.setRefundAmount(orderDetail.getPrice().multiply(new BigDecimal(productNumber)));
- payFeign.updateOrderDetails(orderDetail);
- }
- }
- return R.ok("申请退款成功");
- }else {
- return R.fail(refundData);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- } else {
- try {
- String result = orderService.refund(order.getSn(), orderService.initRefundSn(equipment.getClientId(), refundNumber), refusePrice.subtract(p8_MarketRefAmount), p8_MarketRefAmount,"", notifyUrl, altRefInfo.toString());
- if ("退款申请成功".equals(result)) {
- order.setStatus(2);
- if(StringUtils.isNotEmpty(order.getCoupons())){
- //设置退款营销金额
- if(order.getRefundMarketingAmount()!=null){
- //累加
- order.setRefundMarketingAmount(order.getRefundMarketingAmount().add(p8_MarketRefAmount));
- }else {
- order.setRefundMarketingAmount(p8_MarketRefAmount);
- }
- }
- // 修改订单明细
- for (TOrderDetails orderDetail : orderDetails) {
- String productNo = orderDetail.getProductNo();
- Integer productNumber = refundMap.get(productNo);
- if(productNumber != null) {
- // 退款中
- orderDetail.setRefundStatus("4");
- // 退款数量
- orderDetail.setRefundQuantity(productNumber);
- // 退款金额
- orderDetail.setRefundAmount(orderDetail.getPrice().multiply(new BigDecimal(productNumber)));
- payFeign.updateOrderDetails(orderDetail);
- }
- }
- order.setRefundTrxNo(String.valueOf(refusePrice));
- payFeign.updateOrder(order);
- return R.ok("申请退款已成功");
- }else {
- return R.fail("申请退款失败");
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return R.ok("申请退款已成功");
- }
- /**
- * 小程序退款
- * @param
- * @return
- */
- @GetMapping("/refuseByXcu")
- public ResponseEntity<?> refuseByXcu(String id) {
- TOrder order = R.getDataIfSuccess(payFeign.getOrder(id));
- List<TOrderDetails> orderDetails = R.getDataIfSuccess(payFeign.getOrderDetailsBySn(order.getSn()));
- String orderStatus = order.getOrderStatus();
- Integer refundNumber = order.getProductNumber();
- order.setRefundQuantity(refundNumber);
- if(StringUtils.isNotEmpty(orderStatus)){
- if(orderStatus.equals("1")||orderStatus.equals("2")){
- return ResponseEntity
- .status(HttpStatus.OK)
- .body(new ResultMessage()
- .setCode(false)
- .setData("")
- .setMessage("订单非排队状态"));
- }
- }
- BigDecimal refusePrice = order.getPrice().setScale(2, RoundingMode.HALF_DOWN);
- if(refusePrice.compareTo(new BigDecimal("0.00"))<=0){
- return ResponseEntity
- .status(HttpStatus.OK)
- .body(new ResultMessage()
- .setCode(false)
- .setData("")
- .setMessage("退款金额非法"));
- }
- TEquipment equipment = tEquipmentService.getById(order.getEquipmentId());
- Long equipmentId = equipment.getId();
- if (order.getProductName().equals("280充值")) {
- return ResponseEntity
- .status(HttpStatus.OK)
- .body(new ResultMessage()
- .setCode(false)
- .setData("订单不退款")
- .setMessage("订单不退款"));
- }
- if (order.getStatus().equals("0")) {
- return ResponseEntity
- .status(HttpStatus.OK)
- .body(new ResultMessage()
- .setCode(false)
- .setData("订单非支付状态")
- .setMessage("订单非支付状态"));
- }
- String notifyUrl = JoinpayConstant.Notify_Refund_Url;
- JSONArray altInfoArray = JSONArray.parseArray(order.getAltInfo());
- JSONArray altRefInfo = new JSONArray();
- // 汇付分销明细
- String acctSplitBunch = "";
- BigDecimal p = BigDecimal.ZERO;
- BigDecimal price = BigDecimal.ZERO;
- //订单总金额
- BigDecimal sumPrice = BigDecimal.ZERO;
- if(order.getRefundAmount()!=null){
- sumPrice=sumPrice.add(order.getPrice()).subtract(order.getRefundAmount()).setScale(2, RoundingMode.HALF_DOWN);
- }else {
- sumPrice=sumPrice.add(order.getPrice()).setScale(2, RoundingMode.HALF_DOWN);
- }
- BigDecimal p8_MarketRefAmount = BigDecimal.ZERO;
- for (int i = 0; i < altInfoArray.size(); i++) {
- JSONObject altInfoObject = altInfoArray.getJSONObject(i);
- JSONObject refInfo = new JSONObject();
- refInfo.put("altMchNo", altInfoObject.getString("altMchNo"));
- String altAmount = altInfoObject.getString("altAmount");
- // BigDecimal pri = new BigDecimal(order.getPrice().toString()).setScale(2, RoundingMode.HALF_DOWN);
- if(refusePrice.compareTo(sumPrice)<0){
- //退部分的钱
- BigDecimal altAmountPrice = new BigDecimal(altAmount);
- altAmountPrice = altAmountPrice.multiply(refusePrice.divide(sumPrice,2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
- p = p.add(altAmountPrice);
- refInfo.put("altRefAmount", altAmountPrice.toString());
- if(order.getMarketingAmount()!=null){
- p8_MarketRefAmount = order.getMarketingAmount().multiply(refusePrice.divide(sumPrice,2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
- }
- }else {
- //退全部的钱
- BigDecimal altAmountPrice = new BigDecimal(altAmount).setScale(2, RoundingMode.HALF_DOWN);
- // BigDecimal divide = altAmountPrice.divide(sumPrice,2,BigDecimal.ROUND_HALF_UP).setScale(2, RoundingMode.HALF_DOWN);
- // altAmountPrice = refusePrice.multiply(divide).setScale(2, RoundingMode.HALF_DOWN);
- p = p.add(altAmountPrice);
- refInfo.put("altRefAmount", altAmountPrice.toString());
- if(order.getMarketingAmount()!=null){
- p8_MarketRefAmount = order.getMarketingAmount().multiply(refusePrice.divide(sumPrice,2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
- }
- }
- price = price.add(altInfoObject.getBigDecimal("altAmount"));
- if (!altInfoObject.getBigDecimal("altAmount").toString().equals("0.00")) {
- altRefInfo.add(refInfo);
- }
- }
- // 特殊处理下:添加多一个平台方分销的金额
- // 平台获得的收益
- // BigDecimal adminPrice = new BigDecimal(order.getPrice().toString()).subtract(price);
- BigDecimal adminPrice = refusePrice.subtract(p).setScale(2, RoundingMode.HALF_DOWN);
- JSONObject refInfo = new JSONObject();
- refInfo.put("altMchNo", JoinpayConstant.mch_no);
- refInfo.put("altRefAmount", adminPrice.toString());
- price = price.add(adminPrice);
- if (!adminPrice.toString().equals("0.00")) {
- altRefInfo.add(refInfo);
- }
- //需要知道订单是哪一个平台的,不能以账号的为标准 0或null为汇聚,1为杉德支付
- String payPlatform = order.getPayPlatform();
- String companyType = order.getCompanyType();
- if (StringUtils.isNotEmpty(payPlatform) && payPlatform.equals("1")) {
- try {
- Date date = order.getCreateDate();
- String payDate = DateTools.formatYYYYMMDD(date);
- String remark = "";
- BigDecimal refundAmount = order.getRefundAmount();
- //获取分销 关联设备
- LambdaQueryWrapper<TProportion> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(TProportion::getEquipmentId,equipmentId);
- TProportion proportion = proportionService.getOne(wrapper);
- // 判断是否退过款
- if(refundAmount != null && refundAmount.compareTo(BigDecimal.ZERO) > 0) {
- acctSplitBunch = getAcctSplitBunch(proportion, refusePrice, companyType).toString();
- } else {
- // 如果退款金额等于订单金额,就直接用原来的分销明细,如果小于就重新计算分销明细
- if(refusePrice.compareTo(order.getPrice()) < 0) {
- acctSplitBunch = getAcctSplitBunch(proportion, refusePrice, companyType).toString();
- }
- }
- String refundData = huifuMchService.refund(order.getSn(), price, payDate, remark, acctSplitBunch, companyType);
- //订单号
- if(StrUtil.isNotEmpty(refundData)&&(refundData.equals(HuifuConstant.TRANS_STAT_S)||refundData.equals(HuifuConstant.TRANS_STAT_P))) {
- order.setStatus(2);
- payFeign.updateOrder(order);
- // 修改订单明细
- for (TOrderDetails orderDetail : orderDetails) {
- // 退款中
- orderDetail.setRefundStatus("4");
- // 退款数量
- orderDetail.setRefundQuantity(orderDetail.getProductNumber());
- // 退款金额
- orderDetail.setRefundAmount(orderDetail.getAmount());
- payFeign.updateOrderDetails(orderDetail);
- }
- return ResponseEntity
- .status(HttpStatus.OK)
- .body(new ResultMessage()
- .setCode(true)
- .setData("申请退款已成功")
- .setMessage("申请退款已成功"));
- }else {
- return ResponseEntity
- .status(HttpStatus.OK)
- .body(new ResultMessage()
- .setCode(false)
- .setData("申请退款失败")
- .setMessage("申请退款失败"));
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- } else {
- try {
- String result = orderService.refund(order.getSn(), orderService.initRefundSn(equipment.getClientId(), refundNumber), refusePrice.subtract(p8_MarketRefAmount), p8_MarketRefAmount,"", notifyUrl, altRefInfo.toString());
- if ("退款申请成功".equals(result)) {
- order.setStatus(2);
- if(StringUtils.isNotEmpty(order.getCoupons())){
- //设置退款营销金额
- if(order.getRefundMarketingAmount()!=null){
- //累加
- order.setRefundMarketingAmount(order.getRefundMarketingAmount().add(p8_MarketRefAmount));
- }else {
- order.setRefundMarketingAmount(p8_MarketRefAmount);
- }
- }
- // 修改订单明细
- for (TOrderDetails orderDetail : orderDetails) {
- // 退款中
- orderDetail.setRefundStatus("4");
- // 退款数量
- orderDetail.setRefundQuantity(orderDetail.getProductNumber());
- // 退款金额
- orderDetail.setRefundAmount(orderDetail.getAmount());
- payFeign.updateOrderDetails(orderDetail);
- }
- order.setRefundTrxNo(String.valueOf(refusePrice));
- payFeign.updateOrder(order);
- return ResponseEntity
- .status(HttpStatus.OK)
- .body(new ResultMessage()
- .setCode(true)
- .setData("申请退款已成功")
- .setMessage("申请退款已成功"));
- }else {
- return ResponseEntity
- .status(HttpStatus.OK)
- .body(new ResultMessage()
- .setCode(false)
- .setData("申请退款失败")
- .setMessage("申请退款失败"));
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return ResponseEntity
- .status(HttpStatus.OK)
- .body(new ResultMessage()
- .setCode(true)
- .setData("申请退款已成功")
- .setMessage("申请退款已成功"));
- }
- @ApiOperation(value = "添加优惠码")
- @GetMapping("/equipmentPay")
- public ResponseModel<?> equipmentPay(Double number, Long equipmentId, String frpCode) {
- TEquipment equipment = tEquipmentService.getById(equipmentId);
- if (!frpCode.equals("ALIPAY_NATIVE") && !frpCode.equals("WEIXIN_NATIVE")) {
- return R.fail("参数错误");
- }
- if (equipment == null) {
- return R.fail("找不到设备");
- }
- TAdmin admin = tAdminService.getById(equipment.getAdminId());
- Double money = null;
- LambdaQueryWrapper<TPrice> query1 = Wrappers.lambdaQuery();
- query1.eq(TPrice::getName,"MG280");
- List<TPrice> prices = priceService.list(query1);
- for(TPrice price:prices){
- if(price.getName().equals("MG280")){
- money = price.getPrice();
- }
- }
- BigDecimal price = null;
- double pri = money * number;
- price = BigDecimal.valueOf(pri);
- String sn = orderService.initSn(equipmentId);
- String orderNo = sn;
- String productName = "280充值";
- BigDecimal cutPrice = price.multiply(new BigDecimal(100)).divide(new BigDecimal(100));
- BigDecimal amount = cutPrice;
- String productDesc = "";
- String commonParameter = "";
- String returnUrl = null;
- String notifyUrl = JoinpayConstant.Notify_Url_PromoCode;
- String isShowPic = "1";
- String openId = null;
- String authCode = null;
- String appid = null;
- String transactionModel = null;
- String tradeMerchantNo = null;
- String buyerId = null;
- String isAlt = "0";
- String altType = null;
- String altUrl = null;
- BigDecimal marketingAmount = null;
- // frpCode = "WEIXIN_NATIVE";
- TOrder order1 = new TOrder();
- order1.setId(IDGenerator.orderID());
- order1.setSn(sn);
- order1.setType(0);
- order1.setAdminId(admin.getId());
- order1.setProductName(productName);
- order1.setPrice(price);
- order1.setEquipmentId(equipmentId);
- order1.setClientId(equipment.getClientId());
- order1.setStatus(0);
- order1.setFrpCode(frpCode);
- order1.setCreateDate(new Date());
- order1.setModifyDate(new Date());
- payFeign.addOrder(order1);
- JSONArray altInfo = new JSONArray();
- String result = null;
- try {
- result = joinpayMchService.uniPay(
- orderNo, amount, productName, productDesc,
- commonParameter, returnUrl, notifyUrl, frpCode,
- isShowPic, openId, authCode, appid, transactionModel, tradeMerchantNo,
- buyerId, isAlt, altType, altInfo, altUrl, marketingAmount
- );
- } catch (Exception e) {
- e.printStackTrace();
- return R.fail("申请支付失败");
- }
- JSONObject resultJson = JSONObject.parseObject(result);
- // 汇聚支付支付申请返回支付二维码图片
- String rd_Pic = resultJson.getString("rd_Pic");
- if (resultJson == null || StringUtils.isBlank(rd_Pic)) {
- return R.fail("找不到支付图片");
- }
- JSONObject kindData = new JSONObject();
- kindData.put("sn", sn);
- kindData.put("price", price);
- kindData.put("image", rd_Pic);
- return R.ok(kindData);
- }
- /**
- * 汇付分销明细
- * @param proportion
- * @param price
- * @return
- */
- public JSONObject getAcctSplitBunch(TProportion proportion, BigDecimal price, String companyType) {
- // 获取分账有几方:0:分账方2个,1:分账方3个,2:分账方4个,3:分账方超4个
- Integer type = proportion.getType();
- // 汇付分账明细参数
- JSONObject acctSplitBunch = new JSONObject();
- JSONArray acctInfos = new JSONArray();
- BigDecimal adminProportion = null,
- agencyProportion = null,
- merchantProportion = null,
- selfProportion = null;
- BigDecimal agencyAmount = null;
- BigDecimal merchantAmount = null;
- BigDecimal selfAmount = null;
- BigDecimal adminAmount = null;
- THuifuMch agencyHuifu = null;
- THuifuMch merchantHuifu = null;
- THuifuMch selfHuifu = null;
- JSONObject agencyJson = null;
- JSONObject merchantJson = null;
- JSONObject selfJson = null;
- JSONObject adminJson = null;
- //取消平台扣手续费
- BigDecimal cutPrice = price.setScale(2, RoundingMode.HALF_UP);
- BigDecimal refuseAmount = new BigDecimal(0.00);
- switch (type) {
- case 0:
- // 公司平台分销
- adminProportion = proportion.getAdminProportion();
- // 商家自己的分销
- selfProportion = proportion.getProportion();
- // 平台分销获得利润
- adminAmount = cutPrice.multiply(adminProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_UP);
- // 商家分销获得利润
- selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- // 商家汇付信息:agencyHuifu
- LambdaQueryWrapper<THuifuMch> queryWrapper1 = new LambdaQueryWrapper<>();
- queryWrapper1.eq(THuifuMch::getAdminId,proportion.getAdminId());
- selfHuifu = huifuMchService.getOne(queryWrapper1);
- // 公司平台分销
- adminJson = new JSONObject();
- adminJson.put("div_amt", adminAmount.toString());
- if(StrUtil.isNotEmpty(companyType) && companyType.equals("1")) {
- adminJson.put("huifu_id", HuifuConstant.SC_DIV_HUIFU_ID);
- } else {
- adminJson.put("huifu_id", HuifuConstant.SZ_DIV_HUIFU_ID);
- }
- acctInfos.add(adminJson);
- // 商家分销获得利润
- selfJson = new JSONObject();
- selfJson.put("div_amt", selfAmount.toString());
- selfJson.put("huifu_id", selfHuifu.getHuifuId());
- acctInfos.add(selfJson);
- break;
- case 1:
- // 公司平台分销
- adminProportion = proportion.getAdminProportion();
- // 商家自己的分销
- selfProportion = proportion.getProportion();
- // 一级分销
- agencyProportion = proportion.getAgencyProportion();
- // 平台分销获得利润
- adminAmount = cutPrice.multiply(adminProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_UP);
- // 商家分销获得利润
- selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- // 汇付平台
- LambdaQueryWrapper<THuifuMch> queryWrapper2 = new LambdaQueryWrapper<>();
- queryWrapper2.eq(THuifuMch::getAdminId,proportion.getAdminId());
- selfHuifu = huifuMchService.getOne(queryWrapper2);
- LambdaQueryWrapper<THuifuMch> queryWrapper3 = new LambdaQueryWrapper<>();
- queryWrapper3.eq(THuifuMch::getAdminId,proportion.getAgencyId());
- agencyHuifu = huifuMchService.getOne(queryWrapper3);
- // 公司平台分销
- adminJson = new JSONObject();
- adminJson.put("div_amt", adminAmount.toString());
- if(StrUtil.isNotEmpty(companyType) && companyType.equals("1")) {
- adminJson.put("huifu_id", HuifuConstant.SC_DIV_HUIFU_ID);
- } else {
- adminJson.put("huifu_id", HuifuConstant.SZ_DIV_HUIFU_ID);
- }
- acctInfos.add(adminJson);
- // 商家分销获得利润
- selfJson = new JSONObject();
- selfJson.put("div_amt", selfAmount.toString());
- selfJson.put("huifu_id", selfHuifu.getHuifuId());
- acctInfos.add(selfJson);
- if(!(agencyProportion.compareTo(refuseAmount) == 0||agencyProportion==null)){
- // 一级分销获得利润
- agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- agencyJson = new JSONObject();
- agencyJson.put("div_amt", agencyAmount.toString());
- agencyJson.put("huifu_id", agencyHuifu.getHuifuId());
- acctInfos.add(agencyJson);
- }
- break;
- case 2:
- // 公司平台分销
- adminProportion = proportion.getAdminProportion();
- // 商家自己的分销
- selfProportion = proportion.getProportion();
- // 一级分销
- agencyProportion = proportion.getAgencyProportion();
- // 二级分销
- merchantProportion = proportion.getMerchantProportion();
- // 平台分销获得利润
- adminAmount = cutPrice.multiply(adminProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_UP);
- // 商家分销获得利润
- selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- LambdaQueryWrapper<THuifuMch> queryWrapper4 = new LambdaQueryWrapper<>();
- queryWrapper4.eq(THuifuMch::getAdminId,proportion.getAdminId());
- selfHuifu = huifuMchService.getOne(queryWrapper4);
- LambdaQueryWrapper<THuifuMch> queryWrapper5 = new LambdaQueryWrapper<>();
- queryWrapper5.eq(THuifuMch::getAdminId,proportion.getAgencyId());
- agencyHuifu = huifuMchService.getOne(queryWrapper5);
- LambdaQueryWrapper<THuifuMch> queryWrapper6 = new LambdaQueryWrapper<>();
- queryWrapper6.eq(THuifuMch::getAdminId,proportion.getMerchantId());
- merchantHuifu = huifuMchService.getOne(queryWrapper6);
- // 公司平台分销
- adminJson = new JSONObject();
- adminJson.put("div_amt", adminAmount.toString());
- if(StrUtil.isNotEmpty(companyType) && companyType.equals("1")) {
- adminJson.put("huifu_id", HuifuConstant.SC_DIV_HUIFU_ID);
- } else {
- adminJson.put("huifu_id", HuifuConstant.SZ_DIV_HUIFU_ID);
- }
- acctInfos.add(adminJson);
- // 商家分销获得利润
- selfJson = new JSONObject();
- selfJson.put("div_amt", selfAmount.toString());
- selfJson.put("huifu_id", selfHuifu.getHuifuId());
- acctInfos.add(selfJson);
- if(!(agencyProportion.compareTo(refuseAmount) == 0||agencyProportion==null)){
- // 一级分销获得利润
- agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- agencyJson = new JSONObject();
- agencyJson.put("div_amt", agencyAmount.toString());
- agencyJson.put("huifu_id", agencyHuifu.getHuifuId());
- acctInfos.add(agencyJson);
- }
- if(!(merchantProportion.compareTo(refuseAmount) == 0||merchantProportion==null)){
- // 二级分销获得利润
- merchantAmount = cutPrice.multiply(merchantProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- merchantJson = new JSONObject();
- merchantJson.put("div_amt", merchantAmount.toString());
- merchantJson.put("huifu_id", merchantHuifu.getHuifuId());
- acctInfos.add(merchantJson);
- }
- break;
- }
- // 计算div_amt的总和
- BigDecimal totalAmount = BigDecimal.ZERO;
- for (int i = 0; i < acctInfos.size(); i++) {
- JSONObject jsonObject = acctInfos.getJSONObject(i);
- BigDecimal divAmt = new BigDecimal(jsonObject.getString("div_amt"));
- totalAmount = totalAmount.add(divAmt);
- }
- // 如果总和超过原本金额,调整div_amt
- if (totalAmount.compareTo(price) > 0) {
- BigDecimal diffAmount = totalAmount.subtract(price);
- for (int i = 0; i < acctInfos.size(); i++) {
- JSONObject jsonObject = acctInfos.getJSONObject(i);
- String huifuId = jsonObject.getString("huifu_id");
- if (huifuId.equals(HuifuConstant.SZ_DIV_HUIFU_ID) || huifuId.equals(HuifuConstant.SC_DIV_HUIFU_ID)) {
- BigDecimal currentDivAmt = new BigDecimal(jsonObject.getString("div_amt"));
- BigDecimal adjustedDivAmt = currentDivAmt.subtract(diffAmount).max(BigDecimal.ZERO);
- jsonObject.put("div_amt", adjustedDivAmt.toString());
- break;
- }
- }
- }
- acctSplitBunch.put("acct_infos", acctInfos);
- return acctSplitBunch;
- }
- /**
- * 汇聚分销明细
- * @param proportion
- * @param price
- * @return
- */
- public JSONArray getAltInfo(TProportion proportion, BigDecimal price) {
- // 获取分账有几方:0:分账方2个,1:分账方3个,2:分账方4个,3:分账方超4个
- Integer type = proportion.getType();
- // 汇聚分账参数
- JSONArray altInfo = new JSONArray();
- BigDecimal agencyProportion = null,
- merchantProportion = null,
- selfProportion = null;
- BigDecimal agencyAmount = null;
- BigDecimal merchantAmount = null;
- BigDecimal selfAmount = null;
- TJoinpayMch agencyMch = null;
- TJoinpayMch merchantMch = null;
- TJoinpayMch selfMch = null;
- JSONObject agencyJson = null;
- JSONObject merchantJson = null;
- JSONObject selfJson = null;
- //取消平台扣手续费
- BigDecimal cutPrice = price.setScale(2, RoundingMode.HALF_UP);
- BigDecimal refuseAmount = new BigDecimal(0.00);
- switch (type) {
- case 0:
- // 商家自己的分销
- selfProportion = proportion.getProportion();
- // 商家分销获得利润
- selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- // 汇聚平台
- LambdaQueryWrapper<TJoinpayMch> queryWrapper1 = new LambdaQueryWrapper<>();
- queryWrapper1.eq(TJoinpayMch::getAdminId,proportion.getAdminId());
- selfMch = joinpayMchService.getOne(queryWrapper1);
- selfJson = new JSONObject();
- selfJson.put("altMchNo", selfMch.getAltMchNo());
- selfJson.put("altAmount", selfAmount.toString());
- selfJson.put("isGuar", "12");
- altInfo.add(selfJson);
- break;
- case 1:
- // 商家自己的分销
- selfProportion = proportion.getProportion();
- // 一级分销
- agencyProportion = proportion.getAgencyProportion();
- // 商家分销获得利润
- selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- // 汇聚平台
- LambdaQueryWrapper<TJoinpayMch> queryWrapper2 = new LambdaQueryWrapper<>();
- queryWrapper2.eq(TJoinpayMch::getAdminId,proportion.getAdminId());
- selfMch = joinpayMchService.getOne(queryWrapper2);
- LambdaQueryWrapper<TJoinpayMch> queryWrapper3 = new LambdaQueryWrapper<>();
- queryWrapper3.eq(TJoinpayMch::getAdminId,proportion.getAgencyId());
- agencyMch = joinpayMchService.getOne(queryWrapper3);
- if(!(agencyProportion.compareTo(refuseAmount) == 0||agencyProportion==null)){
- // 一级分销获得利润
- agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- agencyJson = new JSONObject();
- agencyJson.put("altMchNo", agencyMch.getAltMchNo());
- agencyJson.put("altAmount", agencyAmount.toString());
- agencyJson.put("isGuar", "12");
- altInfo.add(agencyJson);
- }
- // 商家自己分销获得利润
- selfJson = new JSONObject();
- selfJson.put("altMchNo", selfMch.getAltMchNo());
- selfJson.put("altAmount", selfAmount.toString());
- selfJson.put("isGuar", "12");
- altInfo.add(selfJson);
- break;
- case 2:
- // 商家自己的分销
- selfProportion = proportion.getProportion();
- // 一级分销
- agencyProportion = proportion.getAgencyProportion();
- // 二级分销
- merchantProportion = proportion.getMerchantProportion();
- // 三级分销
- // personageProportion = proportion.getPersonageProportion();
- // 商家分销获得利润
- selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- // 汇聚
- LambdaQueryWrapper<TJoinpayMch> queryWrapper4 = new LambdaQueryWrapper<>();
- queryWrapper4.eq(TJoinpayMch::getAdminId,proportion.getAdminId());
- selfMch = joinpayMchService.getOne(queryWrapper4);
- LambdaQueryWrapper<TJoinpayMch> queryWrapper5 = new LambdaQueryWrapper<>();
- queryWrapper5.eq(TJoinpayMch::getAdminId,proportion.getAgencyId());
- agencyMch = joinpayMchService.getOne(queryWrapper5);
- LambdaQueryWrapper<TJoinpayMch> queryWrapper6 = new LambdaQueryWrapper<>();
- queryWrapper6.eq(TJoinpayMch::getAdminId,proportion.getMerchantId());
- merchantMch = joinpayMchService.getOne(queryWrapper6);
- if(!(agencyProportion.compareTo(refuseAmount) == 0||agencyProportion==null)){
- // 代理分销获得利润
- agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- agencyJson = new JSONObject();
- agencyJson.put("altMchNo", agencyMch.getAltMchNo());
- agencyJson.put("altAmount", agencyAmount.toString());
- agencyJson.put("isGuar", "12");
- altInfo.add(agencyJson);
- }
- if(!(merchantProportion.compareTo(refuseAmount) == 0||merchantProportion==null)){
- // 经销商分销获得利润
- merchantAmount = cutPrice.multiply(merchantProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- merchantJson = new JSONObject();
- merchantJson.put("altMchNo", merchantMch.getAltMchNo());
- merchantJson.put("altAmount", merchantAmount.toString());
- merchantJson.put("isGuar", "12");
- altInfo.add(merchantJson);
- }
- // 商家自己分销获得利润
- merchantJson = new JSONObject();
- merchantJson.put("altMchNo", selfMch.getAltMchNo());
- merchantJson.put("altAmount", selfAmount.toString());
- merchantJson.put("isGuar", "12");altInfo.add(merchantJson);
- break;
- }
- Iterator<Object> iterator = altInfo.iterator();
- while (iterator.hasNext()) {
- JSONObject jsonObject = (JSONObject) iterator.next();
- String altAmount = jsonObject.getString("altAmount");
- if (altAmount.equals("0.00")) {
- iterator.remove();
- }
- }
- return altInfo;
- }
- /**
- * 退款
- * @param
- * @return
- */
- @PostMapping("/oldRefund")
- public ResponseEntity<?> oldRefund(@RequestBody TOrder torder) {
- // 退款金额: refusePrice
- BigDecimal refusePrice = torder.getPrice().setScale(2, RoundingMode.HALF_DOWN);
- // 退款数量: refundNumber
- Integer refundNumber = torder.getProductNumber();
- if(refusePrice.compareTo(new BigDecimal("0.00"))<=0){
- return ResponseEntity
- .status(HttpStatus.OK)
- .body(new ResultMessage()
- .setCode(false)
- .setData("")
- .setMessage("退款金额非法"));
- }
- TOrder order = R.getDataIfSuccess(payFeign.getOrder(String.valueOf(torder.getId())));
- // 已退款的金额: refundAmount
- // BigDecimal refundAmount = order.getRefundAmount();
- TEquipment equipment = tEquipmentService.getById(order.getEquipmentId());
- Long equipmentId = equipment.getId();
- if (order.getProductName().equals("280充值")) {
- return ResponseEntity
- .status(HttpStatus.OK)
- .body(new ResultMessage()
- .setCode(false)
- .setData("订单不退款")
- .setMessage("订单不退款"));
- }
- if (order.getStatus().equals("0")) {
- return ResponseEntity
- .status(HttpStatus.OK)
- .body(new ResultMessage()
- .setCode(false)
- .setData("订单非支付状态")
- .setMessage("订单非支付状态"));
- }
- //判断是不是直连微信退款
- if(StringUtils.isNotEmpty(order.getPayPlatform())&&order.getPayPlatform().equals("3")){
- String ifSuccess = R.getDataIfSuccess(payFeign.refund(order.getSn(),refusePrice));
- return ResponseEntity
- .status(HttpStatus.OK)
- .body(new ResultMessage()
- .setCode(true)
- .setData(ifSuccess)
- .setMessage(ifSuccess));
- }
- String notifyUrl = JoinpayConstant.OLD_Notify_Refund_Url;
- JSONArray altInfoArray = JSONArray.parseArray(order.getAltInfo());
- JSONArray altRefInfo = new JSONArray();
- BigDecimal p = BigDecimal.ZERO;
- BigDecimal price = BigDecimal.ZERO;
- //订单总金额
- BigDecimal sumPrice = BigDecimal.ZERO;
- if(order.getRefundAmount()!=null){
- sumPrice=sumPrice.add(order.getPrice()).add(order.getRefundAmount()).setScale(2, RoundingMode.HALF_DOWN);
- }else {
- sumPrice=sumPrice.add(order.getPrice()).setScale(2, RoundingMode.HALF_DOWN);
- }
- BigDecimal p8_MarketRefAmount = BigDecimal.ZERO;
- for (int i = 0; i < altInfoArray.size(); i++) {
- JSONObject altInfoObject = altInfoArray.getJSONObject(i);
- JSONObject refInfo = new JSONObject();
- refInfo.put("altMchNo", altInfoObject.getString("altMchNo"));
- String altAmount = altInfoObject.getString("altAmount");
- BigDecimal pri = new BigDecimal(order.getPrice().toString()).setScale(2, RoundingMode.HALF_DOWN);
- if(refusePrice.compareTo(pri)<0){
- //退部分的钱
- BigDecimal altAmountPrice = new BigDecimal(altAmount);
- altAmountPrice = altAmountPrice.multiply(refusePrice.divide(order.getPrice(),2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
- p = p.add(altAmountPrice);
- refInfo.put("altRefAmount", altAmountPrice.toString());
- if(order.getMarketingAmount()!=null){
- p8_MarketRefAmount = order.getMarketingAmount().multiply(refusePrice.divide(order.getPrice(),2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
- }
- }else {
- //退全部的钱
- BigDecimal altAmountPrice = new BigDecimal(altAmount).setScale(2, RoundingMode.HALF_DOWN);
- BigDecimal divide = altAmountPrice.divide(order.getPrice(),3,BigDecimal.ROUND_HALF_UP).setScale(3, RoundingMode.HALF_DOWN);
- altAmountPrice = refusePrice.multiply(divide).setScale(2, RoundingMode.HALF_DOWN);
- p = p.add(altAmountPrice);
- refInfo.put("altRefAmount", altAmountPrice.toString());
- if(order.getMarketingAmount()!=null){
- p8_MarketRefAmount = order.getMarketingAmount().multiply(refusePrice.divide(order.getPrice(),2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
- }
- }
- price = price.add(altInfoObject.getBigDecimal("altAmount"));
- if (!altInfoObject.getBigDecimal("altAmount").toString().equals("0.00")) {
- altRefInfo.add(refInfo);
- }
- }
- // 特殊处理下:添加多一个平台方分销的金额
- // 平台获得的收益
- // BigDecimal adminPrice = new BigDecimal(order.getPrice().toString()).subtract(price);
- BigDecimal adminPrice = refusePrice.subtract(p).setScale(2, RoundingMode.HALF_DOWN);
- JSONObject refInfo = new JSONObject();
- refInfo.put("altMchNo", JoinpayConstant.mch_no);
- refInfo.put("altRefAmount", adminPrice.toString());
- price = price.add(adminPrice);
- if (!adminPrice.toString().equals("0.00")) {
- altRefInfo.add(refInfo);
- }
- String text = "";
- //需要知道订单是哪一个平台的,不能以账号的为标准 0或null为汇聚,1为杉德支付
- String payPlatform = order.getPayPlatform();
- if (StringUtils.isNotEmpty(payPlatform) && payPlatform.equals("1")) {
- try {
- String refund = tShandeMchService.refund(order.getId(), orderService.initRefundSn(equipment.getClientId(), refundNumber).toString(), price, "", altRefInfo.toString());
- com.gexin.fastjson.JSONObject jsonObject = com.gexin.fastjson.JSON.parseObject(refund);
- String sub_code = jsonObject.getString("sub_code");
- //订单号
- if (StringUtils.isNotEmpty(sub_code)&&sub_code.equals("REFUND_SUCCESS")) {
- order.setStatus(3);
- String refund_settle_amount = jsonObject.getString("refund_settle_amount");
- String refund_plat_trx_no = jsonObject.getString("refund_plat_trx_no");
- order.setRefundTrxNo(refund_plat_trx_no);
- order.setRefundAmount(new BigDecimal(refund_settle_amount));
- order.setRefundDate(new Date());
- payFeign.updateOrder(order);
- //金额表今日金额要改 要分类型
- TAdmin admin = tAdminService.getById(order.getAdminId());
- Integer orderType = order.getType();
- switch (orderType) {
- case 0:
- BigDecimal agencyProportion = order.getAgencyProportion();
- // 代理分销获得利润
- BigDecimal agencyPrice = price.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- //修改金额表的今日金额
- LambdaQueryWrapper<TShandeMch> query = Wrappers.lambdaQuery();
- query.eq(TShandeMch::getAdminId,order.getAdminId());
- List<TShandeMch> list = tShandeMchService.list(query);
- if(list.size()>0){
- TShandeMch shandeMch = list.get(0);
- BigDecimal todayBalance = shandeMch.getTodayBalance();
- todayBalance = todayBalance.subtract(agencyPrice).setScale(2, RoundingMode.HALF_DOWN);
- shandeMch.setTodayBalance(todayBalance);
- shandeMch.setModifyDate(new Date());
- tShandeMchService.updateById(shandeMch);
- }
- break;
- case 1:
- BigDecimal agencyProportion1 = order.getAgencyProportion();
- BigDecimal merchantProportion1 = order.getMerchantProportion();
- // 代理分销获得利润
- BigDecimal agencyPrice1 = price.multiply(agencyProportion1.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- // 经销商分销获得利润
- BigDecimal merchantAmount1 = price.multiply(merchantProportion1.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- //修改代理金额表的今日金额
- LambdaQueryWrapper<TShandeMch> query1 = Wrappers.lambdaQuery();
- query1.eq(TShandeMch::getAdminId,order.getAgencyId());
- List<TShandeMch> list1 = tShandeMchService.list(query1);
- if(list1.size()>0){
- TShandeMch shandeMch = list1.get(0);
- BigDecimal todayBalance = shandeMch.getTodayBalance();
- todayBalance = todayBalance.subtract(agencyPrice1).setScale(2, RoundingMode.HALF_DOWN);
- shandeMch.setTodayBalance(todayBalance);
- shandeMch.setModifyDate(new Date());
- tShandeMchService.updateById(shandeMch);
- }
- //修改经销商金额表的今日金额;
- LambdaQueryWrapper<TShandeMch> query2 = Wrappers.lambdaQuery();
- query2.eq(TShandeMch::getAdminId,admin.getId());
- List<TShandeMch> list2 = tShandeMchService.list(query2);
- if(list2.size()>0){
- TShandeMch shandeMch = list2.get(0);
- BigDecimal todayBalance = shandeMch.getTodayBalance();
- todayBalance = todayBalance.subtract(merchantAmount1).setScale(2, RoundingMode.HALF_DOWN);
- shandeMch.setTodayBalance(todayBalance);
- shandeMch.setModifyDate(new Date());
- tShandeMchService.updateById(shandeMch);
- }
- break;
- case 2:
- BigDecimal agencyProportion2 = order.getAgencyProportion();
- BigDecimal merchantProportion2 = order.getMerchantProportion();
- BigDecimal personageProportion2 = order.getPersonageProportion();
- // 代理分销获得利润
- BigDecimal agencyAmount = price.multiply(agencyProportion2.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- // 经销商分销获得利润
- BigDecimal merchantAmount = price.multiply(merchantProportion2.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- // 经销商分销获得利润
- BigDecimal personageAmount = price.multiply(personageProportion2.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- //修改代理金额表的今日金额
- LambdaQueryWrapper<TShandeMch> query3 = Wrappers.lambdaQuery();
- query3.eq(TShandeMch::getAdminId,order.getAgencyId());
- List<TShandeMch> list3 = tShandeMchService.list(query3);
- if(list3.size()>0){
- TShandeMch shandeMch = list3.get(0);
- BigDecimal todayBalance = shandeMch.getTodayBalance();
- todayBalance = todayBalance.subtract(agencyAmount).setScale(2, RoundingMode.HALF_DOWN);
- shandeMch.setTodayBalance(todayBalance);
- shandeMch.setModifyDate(new Date());
- tShandeMchService.updateById(shandeMch);
- }
- //修改经销商金额表的今日金额
- LambdaQueryWrapper<TShandeMch> query4 = Wrappers.lambdaQuery();
- query4.eq(TShandeMch::getAdminId,order.getMerchantId());
- List<TShandeMch> list4 = tShandeMchService.list(query4);
- if(list4.size()>0){
- TShandeMch shandeMch = list4.get(0);
- BigDecimal todayBalance = shandeMch.getTodayBalance();
- todayBalance = todayBalance.subtract(merchantAmount).setScale(2, RoundingMode.HALF_DOWN);
- shandeMch.setTodayBalance(todayBalance);
- shandeMch.setModifyDate(new Date());
- tShandeMchService.updateById(shandeMch);
- }
- //修改终端金额表的今日金额
- LambdaQueryWrapper<TShandeMch> query5 = Wrappers.lambdaQuery();
- query5.eq(TShandeMch::getAdminId,admin.getId());
- List<TShandeMch> list5 = tShandeMchService.list(query5);
- if(list5.size()>0){
- TShandeMch shandeMch = list5.get(0);
- BigDecimal todayBalance = shandeMch.getTodayBalance();
- todayBalance = todayBalance.subtract(personageAmount).setScale(2, RoundingMode.HALF_DOWN);
- shandeMch.setTodayBalance(todayBalance);
- shandeMch.setModifyDate(new Date());
- tShandeMchService.updateById(shandeMch);
- }
- break;
- }
- TOrder order1 = R.getDataIfSuccess(payFeign.getOrder(String.valueOf(torder.getId())));
- order1.setModifyDate(new Date());
- esTOrderService.updateDataById(order1);
- return ResponseEntity
- .status(HttpStatus.OK)
- .body(new ResultMessage()
- .setCode(true)
- .setData("申请退款已成功")
- .setMessage("申请退款已成功"));
- } else {
- return ResponseEntity
- .status(HttpStatus.OK)
- .body(new ResultMessage()
- .setCode(false)
- .setData("申请退款失败")
- .setMessage(refund));
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- } else {
- try {
- String result = orderService.refund(order.getSn(), orderService.initRefundSn(equipment.getClientId(), refundNumber), refusePrice.subtract(p8_MarketRefAmount), p8_MarketRefAmount,"", notifyUrl, altRefInfo.toString());
- if ("退款申请成功".equals(result)) {
- order.setStatus(2);
- if(StringUtils.isNotEmpty(order.getCoupons())){
- //设置退款营销金额
- if(order.getRefundMarketingAmount()!=null){
- //累加
- order.setRefundMarketingAmount(order.getRefundMarketingAmount().add(p8_MarketRefAmount));
- }else {
- order.setRefundMarketingAmount(p8_MarketRefAmount);
- }
- }
- order.setRefundTrxNo(String.valueOf(refusePrice));
- payFeign.updateOrder(order);
- return ResponseEntity
- .status(HttpStatus.OK)
- .body(new ResultMessage()
- .setCode(true)
- .setData("申请退款已成功")
- .setMessage("申请退款已成功"));
- }else {
- return ResponseEntity
- .status(HttpStatus.OK)
- .body(new ResultMessage()
- .setCode(false)
- .setData("申请退款失败")
- .setMessage("申请退款失败"));
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return ResponseEntity
- .status(HttpStatus.OK)
- .body(new ResultMessage()
- .setCode(true)
- .setData("申请退款已成功")
- .setMessage("申请退款已成功"));
- }
- }
|