package com.szwl.controller; import cn.com.crbank.ommo.esUtil.BeanUtils; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; 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.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.szwl.constant.HuifuConstant; import com.szwl.constant.JoinpayConstant; import com.szwl.constant.ResponseCodesEnum; import com.szwl.exception.BizException; import com.szwl.feign.bean.OrderFeign; import com.szwl.feign.bean.SzwlFeign; import com.szwl.model.bo.JsonMessage; import com.szwl.model.bo.R; import com.szwl.model.bo.ResponseModel; import com.szwl.model.dto.OrderDetailsDTO; import com.szwl.model.entity.*; import com.szwl.model.utils.*; import com.szwl.service.*; import com.szwl.service.es.EsTOrderService; import com.szwl.utils.HuifuUtils; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.view.RedirectView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.math.RoundingMode; import java.net.URLDecoder; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; /** *

* 前端控制器 *

* * @author wuhs * @since 2022-06-17 */ @Slf4j @RestController @RequestMapping("/tOrder") public class TOrderController { @Autowired SzwlFeign szwlFeign; @Autowired OrderFeign orderFeign; @Autowired TOrderService orderService; @Autowired TOrderTaskService orderTaskService; @Autowired TJoinpayMchService joinpayMchService; @Autowired TShandeMchService shandeMchService; @Autowired THuifuMchService huifuMchService; @Autowired TEquipmentService equipmentService; @Autowired EsTOrderService esTOrderService; @Autowired TSzsmWxService szsmWxService; @Autowired private THuifuTempOrderService huifuTempOrderService; @Autowired private TOrderDetailsService orderDetailsService; @ApiOperation(value = "测试feign") @GetMapping("/testFeign") public ResponseModel testFeign(String id) { return szwlFeign.testGetAdmin(id); } /** * 合并付款接口,根据终端跳转微信或支付宝 * @param req * @param resp * @return * @throws Exception */ @ApiOperation(value = "请求在线支付 主扫") @RequestMapping("/aliPay") public RedirectView alipayforward(String wx, String zfb, HttpServletRequest req, HttpServletResponse resp) throws Exception { String userAgent = req.getHeader("User-Agent"); RedirectView redirectTarget = new RedirectView(); redirectTarget.setContextRelative(true); if (userAgent.contains("MicroMessenger")){ // 微信 redirectTarget.setUrl(wx); }else if(userAgent.contains("AlipayClient")){ // 支付宝 redirectTarget.setUrl(zfb); }else{ throw new BizException(ResponseCodesEnum.A0001,"请使用支付宝或微信扫码!"); } return redirectTarget; } /** * 请求在线支付 主扫 优惠码 盲盒 * * @param clientId 设备client id * @param productName 商品名称 * @param frpCode 支付方式 支付宝:ALIPAY_NATIVE , 微信:WEIXIN_NATIVE * @param pri 有值代表是盲盒 * @return */ @ApiOperation(value = "请求在线支付 主扫") @GetMapping("/uniPay") public Object uniPay(String code, String clientId, String productName, String frpCode,String pri) { TEquipment equipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(clientId)); if(equipment==null||equipment.getId()==null){ return JsonMessage.error("找不到设备"); } //验证优惠码 code=null 则不选用优惠码 TPromoCode promoCode = new TPromoCode(); if(StringUtils.isNotEmpty(code)){ //有优惠码,需要对优惠码进行验证 ResponseModel tPromoCodeResponseModel = szwlFeign.selectTPromoCode(code, equipment.getAdminId().toString()); TPromoCode tPromoCode = tPromoCodeResponseModel.getData(); promoCode = tPromoCode; if (tPromoCode == null||tPromoCode.getId()==null) { //不存在 return JsonMessage.success("1"); } if (promoCode.getId() == null) { //不存在 return JsonMessage.success("1"); } Date lastUseDate = null; if (promoCode != null&&promoCode.getId()!=null) { lastUseDate = promoCode.getLastUseDate(); } if (lastUseDate != null && lastUseDate.getTime() < ((new Date()).getTime())) { promoCode.setIsUse("2"); szwlFeign.updatePromoCode(String.valueOf(promoCode.getId()),"2"); //过期 return JsonMessage.success("4"); } if (promoCode.getIsUse().equals("1")) { //被使用 return JsonMessage.success("2"); } if(!promoCode.getAdminId().equals("1")){ if (String.valueOf(equipment.getAdminId()).equals(promoCode.getAdminId())) { } else { //不是本机 return JsonMessage.success("3"); } } if (promoCode.getDiscount() == null) { //旧优惠码 return JsonMessage.success("0"); } if (promoCode.getDiscount() != null && promoCode.getDiscount() == 0) { //0折 return JsonMessage.success("0"); } } // 支付方式 if (!frpCode.equals("ALIPAY_NATIVE") && !frpCode.equals("WEIXIN_NATIVE")) { return JsonMessage.error("参数错误"); } if (equipment == null) { return JsonMessage.error("找不到设备"); } Long equipmentId = equipment.getId(); //查找商品 ResponseModel product1 = szwlFeign.getProduct(String.valueOf(equipmentId), productName); TProduct product = product1.getData(); if (product == null) { return JsonMessage.error("找不到商品"); } BigDecimal price = product.getRmbPrice(); if(StringUtils.isNotEmpty(pri)){ price = new BigDecimal(pri); } // 计算折扣后的价格 if(StringUtils.isNotEmpty(code)){ BigDecimal discount = BigDecimal.valueOf(promoCode.getDiscount()); price = product.getRmbPrice().multiply(discount).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN); } // 判断价格是否小于等于0 if (BigDecimal.ZERO.compareTo(price) >= 0) { return JsonMessage.error("商品价格异常"); } // 生成订单编号 String sn = orderService.initSn(equipmentId); // 获取关联设备的分销 TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(equipmentId))); if (proportion == null&&proportion.getId()==null) { return JsonMessage.error("设备商家未完成分销设置"); } TAdmin admin = R.getDataIfSuccess(szwlFeign.getAdmin(String.valueOf(equipment.getAdminId()))); if (admin == null) { return JsonMessage.error("找不到设备商家"); } // 支付方式:0或null为汇聚,1为汇付支付 String payPlatform = admin.getPayPlatform(); // 汇聚分账参数 JSONArray altInfo = new JSONArray(); // 汇付分账明细参数 JSONObject acctSplitBunch = new JSONObject(); if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){ acctSplitBunch = getAcctSplitBunch(proportion, price); } else { altInfo = getAltInfo(proportion, price); } String orderNo = sn; BigDecimal amount = price.setScale(2, RoundingMode.HALF_DOWN); String productDesc = ""; String commonParameter = ""; String returnUrl = null; String notifyUrl = JoinpayConstant.Notify_Url; String isShowPic = "1"; String openId = null; String authCode = null; String appid = null; String transactionModel = null; String tradeMerchantNo = admin.getTradeMerchantNo(); String buyerId = null; String isAlt = "1"; String altType = "11"; String altUrl = null; BigDecimal marketingAmount = null; if(StringUtils.isNotEmpty(pri)){ productName ="棉花糖"; } Integer productNumber = 1; TOrder order = new TOrder(); order.setCreateDate(new Date()); order.setModifyDate(new Date()); order.setAdminId(admin.getId()); order.setSn(sn); order.setProductId(product.getId()); order.setProductNo(product.getNo()); order.setProductName(productName); order.setPrice(price); order.setClientId(equipment.getClientId()); order.setEquipmentId(equipmentId); order.setFrpCode(frpCode); order.setAltInfo(altInfo.toString()); order.setStatus(0); order.setType(admin.getType()); order.setProductNumber(productNumber); order.setCompanyType(admin.getCompanyType()); order.setMachineType(equipment.getMachineType()); if(admin.getType()==0){ order.setAdminProportion(proportion.getAdminProportion()); order.setAgencyProportion(proportion.getAgencyProportion()); order.setAgencyId(admin.getId()); } if(admin.getType()==1){ order.setAdminProportion(proportion.getAdminProportion()); order.setAgencyProportion(proportion.getAgencyProportion()); order.setAgencyId(proportion.getAgencyId()); order.setMerchantProportion(proportion.getMerchantProportion()); order.setMerchantId(admin.getId()); } if(admin.getType()==2){ order.setAdminProportion(proportion.getAdminProportion()); order.setAgencyProportion(proportion.getAgencyProportion()); order.setAgencyId(proportion.getAgencyId()); order.setMerchantProportion(proportion.getMerchantProportion()); order.setMerchantId(proportion.getMerchantId()); order.setPersonageProportion(proportion.getPersonageProportion()); order.setPersonageId(admin.getId()); } order.setPayPlatform(payPlatform); // 订单明细表 TOrderDetails orderDetails = new TOrderDetails(); orderDetails.setId(HuifuUtils.initDetailsId()); orderDetails.setAdminId(admin.getId()); orderDetails.setEquipmentId(equipmentId); orderDetails.setCreateDate(new Date()); orderDetails.setOrderSn(sn); orderDetails.setPrice(price); orderDetails.setProductName(productName); orderDetails.setProductNo(product.getNo()); orderDetails.setProductNumber(productNumber); orderDetails.setAmount(price); orderDetails.setRefundStatus("0"); orderDetails.setCompanyType(admin.getCompanyType()); orderDetails.setMachineType(equipment.getMachineType()); String client6 = clientId.substring(clientId.length() - 6); String productName1 = productName + "-" + equipment.getName() + "-" + client6; String result = null; if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){ // 汇付分账参数 order.setAcctSplitBunch(acctSplitBunch.toString()); if(frpCode.equals("WEIXIN_NATIVE")) { //添加到汇付临时表 THuifuTempOrder tHuifuTempOrder = new THuifuTempOrder(); String[] ignoreProperties = {"id"}; String id = HuifuUtils.inithuifuTempId(); tHuifuTempOrder.setId(id); BeanUtil.copyProperties(order,tHuifuTempOrder,ignoreProperties); huifuTempOrderService.save(tHuifuTempOrder); result = HuifuConstant.JUMP_URL+ "/#/weChatPay?id=" + id; System.out.println(result); String qrcode = toQrcode(result); JSONObject kindData = new JSONObject(); kindData.put("sn", sn); kindData.put("rd_Pic", qrcode); return JsonMessage.success(kindData.toString()); } // 汇付支付 try { result = huifuMchService.uniPay( orderNo, amount, productName1, productDesc, notifyUrl, frpCode, openId, appid, acctSplitBunch ); } catch (Exception e) { e.printStackTrace(); return JsonMessage.error("申请支付失败"); } if (StrUtil.isEmpty(result)) { return JsonMessage.error("找不到支付图片"); } String qrcode = toQrcode(result); JSONObject kindData = new JSONObject(); kindData.put("sn", sn); kindData.put("rd_Pic", qrcode); orderService.save(order); orderDetailsService.save(orderDetails); return JsonMessage.success(kindData.toString()); }else{ try { result = joinpayMchService.uniPay( orderNo, amount, productName1, productDesc, commonParameter, returnUrl, notifyUrl, frpCode, isShowPic, openId, authCode, appid, transactionModel, tradeMerchantNo, buyerId, isAlt, altType, altInfo, altUrl, marketingAmount ); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return JsonMessage.error("申请支付失败"); } JSONObject resultJson = JSONObject.parseObject(result); // 汇聚支付支付申请返回支付二维码图片 String rd_Pic = resultJson.getString("rd_Pic"); if (resultJson == null || StringUtils.isBlank(rd_Pic)) { return JsonMessage.error("找不到支付图片"); } JSONObject kindData = new JSONObject(); kindData.put("sn", sn); kindData.put("rd_Pic", rd_Pic); orderService.save(order); orderDetailsService.save(orderDetails); return JsonMessage.success(kindData.toString()); } } /** * 被扫支付 + 优惠码 盲盒 * * @param code 优惠码 * @param clientId 设备client id * @param productName 商品名称 * @param frpCode 支付方式 支付宝:ALIPAY_CARD , 微信:WEIXIN_CARD * @return */ @ApiOperation(value = "请求在线支付 被扫支付") @GetMapping("/CardPay") public Object CardPay(String code, String clientId, String productName, String frpCode, String authCode,String pri) { TEquipment equipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(clientId)); if(equipment==null||equipment.getId()==null){ return JsonMessage.error("找不到设备"); } //验证优惠码 code=null 则不选用优惠码 TPromoCode promoCode = new TPromoCode(); if(StringUtils.isEmpty(code)){ //没有优惠码 }else { //有优惠码,需要对优惠码进行验证 ResponseModel tPromoCodeResponseModel = szwlFeign.selectTPromoCode(code, equipment.getAdminId().toString()); TPromoCode tPromoCode = tPromoCodeResponseModel.getData(); promoCode = tPromoCode; if (tPromoCode == null||tPromoCode.getId()==null) { //不存在 return JsonMessage.success("1"); } if (promoCode.getId() == null) { //不存在 return JsonMessage.success("1"); } Date lastUseDate = null; if (promoCode != null&&promoCode.getId()!=null) { lastUseDate = promoCode.getLastUseDate(); } if (lastUseDate != null && lastUseDate.getTime() < ((new Date()).getTime())) { promoCode.setIsUse("2"); szwlFeign.updatePromoCode(String.valueOf(promoCode.getId()),"2"); //过期 return JsonMessage.success("4"); } if (promoCode.getIsUse().equals("1")) { //被使用 return JsonMessage.success("2"); } if(!promoCode.getAdminId().equals("1")){ if (String.valueOf(equipment.getAdminId()).equals(promoCode.getAdminId())) { } else { //不是本机 return JsonMessage.success("3"); } } if (promoCode.getDiscount() == null) { //旧优惠码 return JsonMessage.success("0"); } if (promoCode.getDiscount() != null && promoCode.getDiscount() == 0) { //0折 return JsonMessage.success("0"); } } if (!frpCode.equals("ALIPAY_CARD") && !frpCode.equals("WEIXIN_CARD")) { return JsonMessage.error("参数错误"); } if (equipment == null) { return JsonMessage.error("找不到设备"); } Long equipmentId = equipment.getId(); //查找商品 TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipmentId), productName)); if (product == null) { return JsonMessage.error("找不到商品"); } BigDecimal price = product.getRmbPrice(); if(StringUtils.isNotEmpty(pri)){ price = new BigDecimal(pri); } if(StringUtils.isNotEmpty(code)){ BigDecimal discount = BigDecimal.valueOf(promoCode.getDiscount()); price = product.getRmbPrice().multiply(discount).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN); } if (BigDecimal.ZERO.compareTo(price) >= 0) { return JsonMessage.error("商品价格异常"); } String sn = orderService.initSn(equipmentId); // 获取关联设备的分销 TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(equipmentId))); if (proportion == null&&proportion.getId()==null) { return JsonMessage.error("设备商家未完成分销设置"); } TAdmin admin = R.getDataIfSuccess(szwlFeign.getAdmin(String.valueOf(equipment.getAdminId()))); if (admin == null) { return JsonMessage.error("找不到设备商家"); } // 支付方式:0或null为汇聚,1为汇付支付 String payPlatform = admin.getPayPlatform(); // 汇聚分账参数 JSONArray altInfo = new JSONArray(); // 汇付分账明细参数 JSONObject acctSplitBunch = new JSONObject(); if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){ acctSplitBunch = getAcctSplitBunch(proportion, price); } else { altInfo = getAltInfo(proportion, price); } String orderNo = sn; Integer productNumber = 1; BigDecimal amount = price.setScale(2, RoundingMode.HALF_DOWN); String productDesc = ""; String commonParameter = ""; String returnUrl = null; String notifyUrl = JoinpayConstant.Notify_Url; String isShowPic = "1"; String openId = null; String appid = null; String transactionModel = null; String tradeMerchantNo = admin.getTradeMerchantNo(); String buyerId = null; String isAlt = "1"; String altType = "11"; String altUrl = null; if(StringUtils.isNotEmpty(pri)){ productName ="棉花糖"; } BigDecimal marketingAmount = null; TOrder order = new TOrder(); order.setCreateDate(new Date()); order.setModifyDate(new Date()); order.setAdminId(admin.getId()); order.setSn(sn); order.setProductId(product.getId()); order.setProductNo(product.getNo()); order.setProductName(productName); order.setPrice(price); order.setClientId(equipment.getClientId()); order.setEquipmentId(equipmentId); order.setFrpCode(frpCode); order.setAltInfo(altInfo.toString()); order.setStatus(0); order.setType(admin.getType()); order.setProductNumber(productNumber); order.setCompanyType(admin.getCompanyType()); order.setMachineType(equipment.getMachineType()); if(admin.getType()==0){ order.setAdminProportion(proportion.getAdminProportion()); order.setAgencyProportion(proportion.getAgencyProportion()); order.setAgencyId(admin.getId()); } if(admin.getType()==1){ order.setAdminProportion(proportion.getAdminProportion()); order.setAgencyProportion(proportion.getAgencyProportion()); order.setAgencyId(proportion.getAgencyId()); order.setMerchantProportion(proportion.getMerchantProportion()); order.setMerchantId(admin.getId()); } if(admin.getType()==2){ order.setAdminProportion(proportion.getAdminProportion()); order.setAgencyProportion(proportion.getAgencyProportion()); order.setAgencyId(proportion.getAgencyId()); order.setMerchantProportion(proportion.getMerchantProportion()); order.setMerchantId(proportion.getMerchantId()); order.setPersonageProportion(proportion.getPersonageProportion()); order.setPersonageId(admin.getId()); } order.setPayPlatform(payPlatform); if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){ order.setIsSettlement("0"); } // 订单明细表 TOrderDetails orderDetails = new TOrderDetails(); orderDetails.setId(HuifuUtils.initDetailsId()); orderDetails.setAdminId(admin.getId()); orderDetails.setEquipmentId(equipmentId); orderDetails.setCreateDate(new Date()); orderDetails.setOrderSn(sn); orderDetails.setPrice(price); orderDetails.setProductName(productName); orderDetails.setProductNo(product.getNo()); orderDetails.setProductNumber(productNumber); orderDetails.setAmount(price); orderDetails.setRefundStatus("0"); orderDetails.setCompanyType(admin.getCompanyType()); orderDetails.setMachineType(equipment.getMachineType()); String client6 = clientId.substring(clientId.length() - 6); String productName1 = productName + "-" + equipment.getName() + "-" + client6; String result = null; if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){ order.setAcctSplitBunch(acctSplitBunch.toString()); try { result = huifuMchService.cardPay( orderNo, amount, productName1, notifyUrl, authCode, acctSplitBunch ); } catch (Exception e) { e.printStackTrace(); return JsonMessage.error("申请支付失败"); } if(result.equals(HuifuConstant.RESP_CODE_0)) { orderService.save(order); orderDetailsService.save(orderDetails); JSONObject kindData = new JSONObject(); kindData.put("sn", sn); return JsonMessage.success(kindData.toString()); } else { return JsonMessage.error("申请支付失败"); } }else{ try { result = joinpayMchService.uniPay( orderNo, amount, productName1, productDesc, commonParameter, returnUrl, notifyUrl, frpCode, isShowPic, openId, authCode, appid, transactionModel, tradeMerchantNo, buyerId, isAlt, altType, altInfo, altUrl, marketingAmount ); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return JsonMessage.error("申请支付失败"); } JSONObject resultJson = JSONObject.parseObject(result); String raCode = resultJson.getString("ra_Code"); if(raCode.equals("100") || raCode.equals("102")){ orderService.save(order); orderDetailsService.save(orderDetails); JSONObject kindData = new JSONObject(); kindData.put("sn", sn); return JsonMessage.success(kindData.toString()); }else { return JsonMessage.error("申请支付失败"); } } } /** * 请求在线支付 主扫 优惠码 二码合一 * * @param clientId 设备client id * @param productName 商品名称 * @param * @return */ @ApiOperation(value = "请求在线支付 二码合一") @GetMapping("/twoPayCode") public Object twoPayCode(String code, String clientId, String productName) { TEquipment equipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(clientId)); if(equipment==null||equipment.getId()==null){ return JsonMessage.error("找不到设备"); } //验证优惠码 code=null 则不选用优惠码 TPromoCode promoCode = new TPromoCode(); if(StringUtils.isEmpty(code)){ //没有优惠码 }else { //有优惠码,需要对优惠码进行验证 ResponseModel tPromoCodeResponseModel = szwlFeign.selectTPromoCode(code, equipment.getAdminId().toString()); TPromoCode tPromoCode = tPromoCodeResponseModel.getData(); promoCode = tPromoCode; if (tPromoCode == null||tPromoCode.getId()==null) { //不存在 return JsonMessage.success("1"); } if (promoCode.getId() == null) { //不存在 return JsonMessage.success("1"); } Date lastUseDate = null; if (promoCode != null&&promoCode.getId()!=null) { lastUseDate = promoCode.getLastUseDate(); } if (lastUseDate != null && lastUseDate.getTime() < ((new Date()).getTime())) { promoCode.setIsUse("2"); szwlFeign.updatePromoCode(String.valueOf(promoCode.getId()),"2"); //过期 return JsonMessage.success("4"); } if (promoCode.getIsUse().equals("1")) { //被使用 return JsonMessage.success("2"); } if(!promoCode.getAdminId().equals("1")){ if (String.valueOf(equipment.getAdminId()).equals(promoCode.getAdminId())) { } else { //不是本机 return JsonMessage.success("3"); } } if (promoCode.getDiscount() == null) { //旧优惠码 return JsonMessage.success("0"); } if (promoCode.getDiscount() != null && promoCode.getDiscount() == 0) { //0折 return JsonMessage.success("0"); } } if (equipment == null) { return JsonMessage.error("找不到设备"); } Long equipmentId = equipment.getId(); //查找商品 TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipmentId), productName)); if (product == null) { return JsonMessage.error("找不到商品"); } BigDecimal price = product.getRmbPrice(); if(StringUtils.isNotEmpty(code)){ BigDecimal discount = BigDecimal.valueOf(promoCode.getDiscount()); price = product.getRmbPrice().multiply(discount).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN); } if (BigDecimal.ZERO.compareTo(price) >= 0) { return JsonMessage.error("商品价格异常"); } String sn1 = orderService.initSn(equipmentId); String sn2 = orderService.initSn(equipmentId); // 获取关联设备的分销 TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(equipmentId))); if (proportion == null&&proportion.getId()==null) { return JsonMessage.error("设备商家未完成分销设置"); } TAdmin admin = R.getDataIfSuccess(szwlFeign.getAdmin(String.valueOf(equipment.getAdminId()))); if (admin == null) { return JsonMessage.error("找不到设备商家"); } // 支付方式:0或null为汇聚,1为汇付支付 String payPlatform = admin.getPayPlatform(); // 汇聚分账参数 JSONArray altInfo = new JSONArray(); // 汇付分账明细参数 JSONObject acctSplitBunch = new JSONObject(); if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){ acctSplitBunch = getAcctSplitBunch(proportion, price); } else { altInfo = getAltInfo(proportion, price); } String orderNo1 = sn1; String orderNo2 = sn2; BigDecimal amount = price.setScale(2, RoundingMode.HALF_DOWN); String productDesc = ""; String commonParameter = ""; String returnUrl = null; String notifyUrl = JoinpayConstant.Notify_Url; String isShowPic = "1"; String openId = null; String authCode = null; String appid = null; String transactionModel = null; String tradeMerchantNo = admin.getTradeMerchantNo(); String buyerId = null; String isAlt = "1"; String altType = "11"; String altUrl = null; BigDecimal marketingAmount = null; String frpCode1 = "WEIXIN_NATIVE"; String frpCode2 = "ALIPAY_NATIVE"; Integer productNumber = 1; TOrder order = new TOrder(); order.setCreateDate(new Date()); order.setModifyDate(new Date()); order.setAdminId(admin.getId()); order.setSn(sn1); order.setProductId(product.getId()); order.setProductNo(product.getNo()); order.setProductName(productName); order.setProductNumber(productNumber); order.setPrice(price); order.setClientId(equipment.getClientId()); order.setEquipmentId(equipmentId); order.setFrpCode(frpCode1); order.setAltInfo(altInfo.toString()); order.setStatus(0); order.setType(admin.getType()); order.setCompanyType(admin.getCompanyType()); order.setMachineType(equipment.getMachineType()); if(admin.getType()==0){ order.setAdminProportion(proportion.getAdminProportion()); order.setAgencyProportion(proportion.getAgencyProportion()); order.setAgencyId(admin.getId()); } if(admin.getType()==1){ order.setAdminProportion(proportion.getAdminProportion()); order.setAgencyProportion(proportion.getAgencyProportion()); order.setAgencyId(proportion.getAgencyId()); order.setMerchantProportion(proportion.getMerchantProportion()); order.setMerchantId(admin.getId()); } if(admin.getType()==2){ order.setAdminProportion(proportion.getAdminProportion()); order.setAgencyProportion(proportion.getAgencyProportion()); order.setAgencyId(proportion.getAgencyId()); order.setMerchantProportion(proportion.getMerchantProportion()); order.setMerchantId(proportion.getMerchantId()); order.setPersonageProportion(proportion.getPersonageProportion()); order.setPersonageId(admin.getId()); } order.setPayPlatform(payPlatform); if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){ order.setIsSettlement("0"); } TOrder order2 = new TOrder(); order2.setCreateDate(new Date()); order2.setModifyDate(new Date()); order2.setAdminId(admin.getId()); order2.setSn(sn2); order2.setProductId(product.getId()); order2.setProductNo(product.getNo()); order2.setProductName(productName); order2.setProductNumber(productNumber); order2.setPrice(price); order2.setClientId(equipment.getClientId()); order2.setEquipmentId(equipmentId); order2.setFrpCode(frpCode2); order2.setAltInfo(altInfo.toString()); order2.setStatus(0); order2.setPayPlatform(payPlatform); order2.setCompanyType(admin.getCompanyType()); order2.setMachineType(equipment.getMachineType()); if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){ order2.setIsSettlement("0"); } // 订单明细表 TOrderDetails orderDetails = new TOrderDetails(); orderDetails.setAdminId(admin.getId()); orderDetails.setEquipmentId(equipmentId); orderDetails.setCreateDate(new Date()); orderDetails.setPrice(price); orderDetails.setProductName(productName); orderDetails.setProductNo(product.getNo()); orderDetails.setProductNumber(productNumber); orderDetails.setAmount(price); orderDetails.setCompanyType(admin.getCompanyType()); orderDetails.setRefundStatus("0"); orderDetails.setMachineType(equipment.getMachineType()); String client6 = clientId.substring(clientId.length() - 6); String productName1 = productName + "-" + equipment.getName() + "-" + client6; String result = null; if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){ // 汇付分账参数 order.setAcctSplitBunch(acctSplitBunch.toString()); order2.setAcctSplitBunch(acctSplitBunch.toString()); //添加到汇付临时表,微信 THuifuTempOrder tHuifuTempOrder = new THuifuTempOrder(); String[] ignoreProperties = {"id"}; String id = HuifuUtils.inithuifuTempId(); tHuifuTempOrder.setId(id); BeanUtil.copyProperties(order,tHuifuTempOrder,ignoreProperties); huifuTempOrderService.save(tHuifuTempOrder); String result1 = HuifuConstant.JUMP_URL+ "/#/weChatPay?id=" + id; // 支付宝 try { result = huifuMchService.uniPay( sn2, amount, productName1, productDesc, notifyUrl, frpCode2, openId, appid, acctSplitBunch ); } catch (Exception e) { e.printStackTrace(); return JsonMessage.error("申请支付失败"); } if (StrUtil.isEmpty(result)) { return JsonMessage.error("找不到支付图片"); } // 汇聚支付支付申请返回支付二维码图片 String code1 = "http://sz.sunzee.com.cn/PAY-SERVER/tOrder/aliPay?zfb="+result+"&wx="+result1; String qrcode = toQrcode(code1); JSONObject kindData = new JSONObject(); kindData.put("sn", sn1+"-"+sn2); kindData.put("rd_Pic", qrcode); orderDetails.setOrderSn(sn2); orderDetails.setId(HuifuUtils.initDetailsId()); orderDetailsService.save(orderDetails); orderService.save(order2); return JsonMessage.success(kindData.toString()); }else{ try { result = joinpayMchService.uniPay( orderNo1, amount, productName1, productDesc, commonParameter, returnUrl, notifyUrl, frpCode1, isShowPic, openId, authCode, appid, transactionModel, tradeMerchantNo, buyerId, isAlt, altType, altInfo, altUrl, marketingAmount ); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return JsonMessage.error("申请支付失败"); } JSONObject resultJson = JSONObject.parseObject(result); String rc_Result1 = resultJson.getString("rc_Result"); String result1 = null; try { result1 = joinpayMchService.uniPay( orderNo2, amount, productName1, productDesc, commonParameter, returnUrl, notifyUrl, frpCode2, isShowPic, openId, authCode, appid, transactionModel, tradeMerchantNo, buyerId, isAlt, altType, altInfo, altUrl, marketingAmount ); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return JsonMessage.error("申请支付失败"); } JSONObject resultJson1 = JSONObject.parseObject(result1); String rc_Result2 = resultJson1.getString("rc_Result"); // 汇聚支付支付申请返回支付二维码图片 String code1 = "http://sz.sunzee.com.cn/PAY-SERVER/tOrder/"+"aliPay?wx="+rc_Result1+"&zfb="+rc_Result2; String rd_Pic = toQrcode(code1); JSONObject kindData = new JSONObject(); kindData.put("sn", sn1+"-"+sn2); kindData.put("rd_Pic", rd_Pic); orderService.save(order); orderDetails.setOrderSn(sn1); orderDetails.setId(HuifuUtils.initDetailsId()); orderDetailsService.save(orderDetails); orderDetails.setOrderSn(sn2); orderDetails.setId(HuifuUtils.initDetailsId()); orderDetailsService.save(orderDetails); orderService.save(order2); return JsonMessage.success(kindData.toString()); } } @ApiOperation(value = "购物车 请求在线支付 二码合一") @PostMapping("/carsPay") public Object carsPay(String clientId, @RequestBody Map productNameMap) { if(productNameMap==null){ return JsonMessage.error("数据出错"); } StringBuffer note = new StringBuffer(); StringBuffer productName = new StringBuffer(); String productNo = ""; Map productMap = new HashMap<>(); Integer productNumber = 0; TEquipment equipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(clientId)); if(equipment==null||equipment.getId()==null){ return JsonMessage.error("找不到设备"); } BigDecimal price = new BigDecimal("0.00"); TEquipmentDesc equipmentDesc = R.getDataIfSuccess(szwlFeign.findEquipmentById(equipment.getId())); if(equipmentDesc==null||equipmentDesc.getEquipmentId()==null||equipmentDesc.getPayType()==null||equipmentDesc.getPayType().equals("0")){ // 1、type=0,原始设定,没有第二件半价 for (String key : productNameMap.keySet()) { String entryValue = String.valueOf(productNameMap.get(key)); List value = JSON.parseArray(entryValue, String.class); List promoCodeList = new ArrayList<>(); // 1、校验优惠码 if(value.size()>0){ for(String code:value){ if(!code.equals("0")){ TPromoCode promoCode; ResponseModel tPromoCodeResponseModel = szwlFeign.selectTPromoCode(code, equipment.getAdminId().toString()); TPromoCode tPromoCode = tPromoCodeResponseModel.getData(); promoCode = tPromoCode; if (tPromoCode == null||tPromoCode.getId()==null) { //不存在 return JsonMessage.success("1"); } Date lastUseDate = null; if (promoCode != null) { lastUseDate = promoCode.getLastUseDate(); } if (lastUseDate != null && lastUseDate.getTime() < ((new Date()).getTime())) { promoCode.setIsUse("2"); szwlFeign.updatePromoCode(String.valueOf(promoCode.getId()),"2"); //过期 return JsonMessage.success("4"); } if (promoCode.getId() == null) { //不存在 return JsonMessage.success("1"); } if (promoCode.getIsUse().equals("1")) { //被使用 return JsonMessage.success("2"); } if(!promoCode.getAdminId().equals("1")){ if (String.valueOf(equipment.getAdminId()).equals(promoCode.getAdminId())) { } else { //不是本机 return JsonMessage.success("3"); } } if (promoCode.getDiscount() == null) { //旧优惠码 return JsonMessage.success("0"); } if (promoCode.getDiscount() != null && promoCode.getDiscount() == 0) { //0折 return JsonMessage.success("0"); } promoCodeList.add(promoCode); } } } String[] productNum = key.split("-"); String productNamea = productNum[0]; String num = productNum[1]; productMap.put(productNamea,Integer.valueOf(num)); productNumber += Integer.valueOf(num); productName.append(productNamea).append("x").append(num).append(","); //确定价格,然后叠加 TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipment.getId()), productNamea)); if (product == null) { return JsonMessage.error("找不到商品"); } note.append(productNamea).append("-").append(product.getNo()).append(":"); BigDecimal productPrice = product.getRmbPrice(); productNo = product.getNo(); if(promoCodeList.size()>0){ //有优惠码 BigDecimal onePrice = new BigDecimal("0.00"); int i; for( i = 0;i= 0) { return JsonMessage.error("商品价格异常"); } String sn1 = orderService.initSn(equipmentId); String sn2 = orderService.initSn(equipmentId); // 获取关联设备的分销 TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(equipmentId))); if (proportion == null&&proportion.getId()==null) { return JsonMessage.error("设备商家未完成分销设置"); } TAdmin admin = R.getDataIfSuccess(szwlFeign.getAdmin(String.valueOf(equipment.getAdminId()))); if (admin == null) { return JsonMessage.error("找不到设备商家"); } // 支付方式:0或null为汇聚,1为汇付支付 String payPlatform = admin.getPayPlatform(); // 汇聚分账参数 JSONArray altInfo = new JSONArray(); // 汇付分账明细参数 JSONObject acctSplitBunch = new JSONObject(); if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){ acctSplitBunch = getAcctSplitBunch(proportion, price); } else { altInfo = getAltInfo(proportion, price); } String orderNo1 = sn1; String orderNo2 = sn2; BigDecimal amount = price.setScale(2, RoundingMode.HALF_DOWN); String productDesc = ""; String commonParameter = ""; String returnUrl = null; String notifyUrl = JoinpayConstant.Notify_Url; String isShowPic = "1"; String openId = null; String authCode = null; String appid = null; String transactionModel = null; String tradeMerchantNo = admin.getTradeMerchantNo(); String buyerId = null; String isAlt = "1"; String altType = "11"; String altUrl = null; BigDecimal marketingAmount = null; String frpCode1 = "WEIXIN_NATIVE"; String frpCode2 = "ALIPAY_NATIVE"; TOrder order = new TOrder(); order.setCreateDate(new Date()); order.setModifyDate(new Date()); order.setAdminId(admin.getId()); order.setSn(sn1); // 去掉后面的逗号 order.setProductNo(productNo); if(productNumber == 1) { productName.delete(productName.length() - 3, productName.length()); } else { productName.deleteCharAt(productName.length() - 1); } order.setProductName(productName.toString()); order.setPrice(price); order.setClientId(equipment.getClientId()); order.setEquipmentId(equipmentId); order.setFrpCode(frpCode1); order.setAltInfo(altInfo.toString()); order.setProductNumber(productNumber); order.setStatus(0); order.setType(admin.getType()); order.setCompanyType(admin.getCompanyType()); order.setMachineType(equipment.getMachineType()); if(admin.getType()==0){ order.setAdminProportion(proportion.getAdminProportion()); order.setAgencyProportion(proportion.getAgencyProportion()); order.setAgencyId(admin.getId()); } if(admin.getType()==1){ order.setAdminProportion(proportion.getAdminProportion()); order.setAgencyProportion(proportion.getAgencyProportion()); order.setAgencyId(proportion.getAgencyId()); order.setMerchantProportion(proportion.getMerchantProportion()); order.setMerchantId(admin.getId()); } if(admin.getType()==2){ order.setAdminProportion(proportion.getAdminProportion()); order.setAgencyProportion(proportion.getAgencyProportion()); order.setAgencyId(proportion.getAgencyId()); order.setMerchantProportion(proportion.getMerchantProportion()); order.setMerchantId(proportion.getMerchantId()); order.setPersonageProportion(proportion.getPersonageProportion()); order.setPersonageId(admin.getId()); } order.setPayPlatform(payPlatform); if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){ order.setIsSettlement("0"); } order.setNote(note.toString()); TOrder order2 = new TOrder(); order2.setCreateDate(new Date()); order2.setModifyDate(new Date()); order2.setAdminId(admin.getId()); order2.setSn(sn2); order2.setProductName(productName.toString()); order2.setProductNo(productNo); order2.setPrice(price); order2.setClientId(equipment.getClientId()); order2.setEquipmentId(equipmentId); order2.setFrpCode(frpCode2); order2.setAltInfo(altInfo.toString()); order2.setStatus(0); order2.setProductNumber(productNumber); order.setType(admin.getType()); order.setCompanyType(admin.getCompanyType()); order2.setMachineType(equipment.getMachineType()); if(admin.getType()==0){ order.setAdminProportion(proportion.getAdminProportion()); order.setAgencyProportion(proportion.getAgencyProportion()); order.setAgencyId(admin.getId()); } if(admin.getType()==1){ order.setAdminProportion(proportion.getAdminProportion()); order.setAgencyProportion(proportion.getAgencyProportion()); order.setAgencyId(proportion.getAgencyId()); order.setMerchantProportion(proportion.getMerchantProportion()); order.setMerchantId(admin.getId()); } if(admin.getType()==2){ order.setAdminProportion(proportion.getAdminProportion()); order.setAgencyProportion(proportion.getAgencyProportion()); order.setAgencyId(proportion.getAgencyId()); order.setMerchantProportion(proportion.getMerchantProportion()); order.setMerchantId(proportion.getMerchantId()); order.setPersonageProportion(proportion.getPersonageProportion()); order.setPersonageId(admin.getId()); } order2.setPayPlatform(payPlatform); if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){ order2.setIsSettlement("0"); } order2.setNote(note.toString()); // 订单明细表 TOrderDetails orderDetails = new TOrderDetails(); orderDetails.setAdminId(admin.getId()); orderDetails.setEquipmentId(equipmentId); orderDetails.setCreateDate(new Date()); orderDetails.setCompanyType(admin.getCompanyType()); orderDetails.setRefundStatus("0"); orderDetails.setMachineType(equipment.getMachineType()); String client6 = clientId.substring(clientId.length() - 6); String productName1 = ""; if(productNumber > 1) { if(equipment.getMachineType() == null || equipment.getMachineType().equals("0")) { productName1 = "棉花糖-" + equipment.getName() + "-" + client6; } else { productName1 = "爆米花-" + equipment.getName() + "-" + client6; } } else { productName1 = productName + "-" + equipment.getName() + "-" + client6; } String result = null; if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){ // 汇付分账参数 order.setAcctSplitBunch(acctSplitBunch.toString()); order2.setAcctSplitBunch(acctSplitBunch.toString()); //添加到汇付临时表,微信 THuifuTempOrder tHuifuTempOrder = new THuifuTempOrder(); String[] ignoreProperties = {"id"}; String id = HuifuUtils.inithuifuTempId(); tHuifuTempOrder.setId(id); BeanUtil.copyProperties(order,tHuifuTempOrder,ignoreProperties); huifuTempOrderService.save(tHuifuTempOrder); String result1 = HuifuConstant.JUMP_URL+"/#/weChatPay?id=" + id; // 支付宝 try { result = huifuMchService.uniPay( sn2, amount, productName1, productDesc, notifyUrl, frpCode2, openId, appid, acctSplitBunch ); } catch (Exception e) { e.printStackTrace(); return JsonMessage.error("申请支付失败"); } if (StrUtil.isEmpty(result)) { return JsonMessage.error("找不到支付图片"); } // 汇聚支付支付申请返回支付二维码图片 String code1 = "http://sz.sunzee.com.cn/PAY-SERVER/tOrder/aliPay?zfb="+result+"&wx="+result1; String qrcode = toQrcode(code1); JSONObject kindData = new JSONObject(); kindData.put("sn", sn1+"-"+sn2); kindData.put("rd_Pic", qrcode); for (String key : productMap.keySet()) { Integer productNum = productMap.get(key); TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipment.getId()), key)); orderDetails.setId(HuifuUtils.initDetailsId()); orderDetails.setProductNo(product.getNo()); orderDetails.setProductName(key); orderDetails.setPrice(product.getRmbPrice()); orderDetails.setProductNumber(productNum); orderDetails.setAmount(product.getRmbPrice().multiply(new BigDecimal(productNum))); orderDetails.setOrderSn(sn2); orderDetailsService.save(orderDetails); } orderService.save(order2); return JsonMessage.success(kindData.toString()); }else{ try { result = joinpayMchService.uniPay( orderNo1, amount, productName1, productDesc, commonParameter, returnUrl, notifyUrl, frpCode1, isShowPic, openId, authCode, appid, transactionModel, tradeMerchantNo, buyerId, isAlt, altType, altInfo, altUrl, marketingAmount ); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return JsonMessage.error("申请支付失败"); } JSONObject resultJson = JSONObject.parseObject(result); String rc_Result1 = resultJson.getString("rc_Result"); String result1 = null; try { result1 = joinpayMchService.uniPay( orderNo2, amount, productName1, productDesc, commonParameter, returnUrl, notifyUrl, frpCode2, isShowPic, openId, authCode, appid, transactionModel, tradeMerchantNo, buyerId, isAlt, altType, altInfo, altUrl, marketingAmount ); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return JsonMessage.error("申请支付失败"); } JSONObject resultJson1 = JSONObject.parseObject(result1); String rc_Result2 = resultJson1.getString("rc_Result"); // 汇聚支付支付申请返回支付二维码图片 String code1 = "http://app.sunzee.com.cn/"+"/api/order/aliPay.htm?wx="+rc_Result1+"&zfb="+rc_Result2; String rd_Pic = toQrcode(code1); JSONObject kindData = new JSONObject(); kindData.put("sn", sn1+"-"+sn2); kindData.put("rd_Pic", rd_Pic); // 添加到订单明细表 for (String key : productMap.keySet()) { Integer productNum = productMap.get(key); TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipment.getId()), key)); orderDetails.setProductNo(product.getNo()); orderDetails.setProductName(key); orderDetails.setPrice(product.getRmbPrice()); orderDetails.setProductNumber(productNum); orderDetails.setAmount(product.getRmbPrice().multiply(new BigDecimal(productNum))); orderDetails.setId(HuifuUtils.initDetailsId()); orderDetails.setOrderSn(sn1); orderDetailsService.save(orderDetails); orderDetails.setId(HuifuUtils.initDetailsId()); orderDetails.setOrderSn(sn2); orderDetailsService.save(orderDetails); } orderService.save(order); orderService.save(order2); return JsonMessage.success(kindData.toString()); } } @ApiOperation(value = "购物车 微信小程序支付") @PostMapping("/carsPayXCX") public Object carsPayXCX(String clientId,String id,String coupons, @RequestBody Map productNameMap) { if(productNameMap==null){ return JsonMessage.error("数据出错"); } StringBuffer note = new StringBuffer(); StringBuffer productName = new StringBuffer(); String productNo = ""; Map productMap = new HashMap<>(); Integer productNumber = 0; TEquipment equipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(clientId)); if(equipment==null||equipment.getId()==null){ return JsonMessage.error("找不到设备"); } BigDecimal price = new BigDecimal("0.00"); List promoCodeAll = new ArrayList<>(); TEquipmentDesc equipmentDesc = R.getDataIfSuccess(szwlFeign.findEquipmentById(equipment.getId())); if(StringUtils.isNotEmpty(equipmentDesc.getStatus())){ if(equipmentDesc.getStatus().equals("1")){ return JsonMessage.error("设备在做糖,请稍后再进行支付"); } } if(equipmentDesc==null||equipmentDesc.getEquipmentId()==null||equipmentDesc.getPayType()==null||equipmentDesc.getPayType().equals("0")){ //1,type=0,原始设定,没有第二件半价 for (String key : productNameMap.keySet()) { String entryValue = String.valueOf(productNameMap.get(key)); List value = JSON.parseArray(entryValue, String.class); List promoCodeList = new ArrayList<>(); //1,校验优惠码 if(value.size()>0){ for(String code:value){ if(!code.equals("0")){ TPromoCode promoCode = new TPromoCode(); ResponseModel promoCode1 = szwlFeign.getPromoCode(code + "-" + equipment.getAdminId().toString()); TPromoCode tPromoCode = promoCode1.getData(); promoCode = tPromoCode; if (tPromoCode == null||tPromoCode.getId()==null) { //不存在 return JsonMessage.success("1"); } // Date lastUseDate = null; if (promoCode != null) { lastUseDate = promoCode.getLastUseDate(); } if (lastUseDate != null && lastUseDate.getTime() < ((new Date()).getTime())) { promoCode.setIsUse("2"); szwlFeign.updatePromoCode(String.valueOf(promoCode.getId()),"2"); //过期 return JsonMessage.success("4"); } if (promoCode.getId() == null) { //不存在 return JsonMessage.success("1"); } if (promoCode.getIsUse().equals("1")) { //被使用 return JsonMessage.success("2"); } if(!promoCode.getAdminId().equals("1")){ if (String.valueOf(equipment.getAdminId()).equals(promoCode.getAdminId())) { } else { //不是本机 return JsonMessage.success("3"); } } if (promoCode.getDiscount() == null) { //旧优惠码 return JsonMessage.success("0"); } if (promoCode.getDiscount() != null && promoCode.getDiscount() == 0) { //0折 return JsonMessage.success("0"); } promoCodeList.add(promoCode); promoCodeAll.add(promoCode); } } } String[] productNum = key.split("-"); String productNamea = productNum[0]; String num = productNum[1]; productMap.put(productNamea,Integer.valueOf(num)); productNumber += Integer.valueOf(num); TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipment.getId()), productNamea)); productNo = product.getNo(); if (product == null) { return JsonMessage.error("找不到商品"); } productName.append(productNamea).append("x").append(num).append(","); String no = product.getNo(); if(StringUtils.isEmpty(no)){ no="A00"; } note.append(no).append("-").append(productNamea).append("-").append(num).append(":"); //确定价格,然后叠加R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipmentId), productName)); BigDecimal productPrice = product.getRmbPrice(); if(promoCodeList.size()>0){ //有优惠码 BigDecimal onePrice = new BigDecimal("0.00"); int i; for( i = 0;i value = JSON.parseArray(entryValue, String.class); String[] productNum = key.split("-"); String productNamea = productNum[0]; String num = productNum[1]; productMap.put(productNamea,Integer.valueOf(num)); TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipment.getId()), productNamea)); productNo = product.getNo(); BigDecimal productPrice = product.getRmbPrice(); productName.append(productNamea).append("x").append(num).append(","); note.append(productNamea).append(num).append(":"); switch (num) { case "1": //1个 0个半价 price =price.add(productPrice); note.append(price).append(","); break; case "2": //2个 1个半价 price =price.add(productPrice); price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN)); note.append(price).append(","); break; case "3": //3个 1个半价 price =price.add(productPrice); price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN)); price =price.add(productPrice); note.append(price).append(","); break; case "4": //4个 2个半价 price =price.add(productPrice); price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN)); price =price.add(productPrice); price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN)); note.append(price).append(","); break; case "5": //5个 2个半价 price =price.add(productPrice); price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN)); price =price.add(productPrice); price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN)); price =price.add(productPrice); note.append(price).append(","); break; } } } if (equipment == null) { return JsonMessage.error("找不到设备"); } Long equipmentId = equipment.getId(); //查找商品 String sn1 = orderService.initSn(equipmentId); // 获取关联设备的分销 TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(equipmentId))); if (proportion == null&&proportion.getId()==null) { return JsonMessage.error("设备商家未完成分销设置"); } TAdmin admin = R.getDataIfSuccess(szwlFeign.getAdmin(String.valueOf(equipment.getAdminId()))); if (admin == null) { return JsonMessage.error("找不到设备商家"); } // 支付方式:0或null为汇聚,1为汇付支付 String payPlatform = admin.getPayPlatform(); // 汇聚分账参数 JSONArray altInfo = new JSONArray(); // 汇付分账明细参数 JSONObject acctSplitBunch = new JSONObject(); if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){ acctSplitBunch = getAcctSplitBunch(proportion, price); } else { altInfo = getAltInfo(proportion, price); } String orderNo1 = sn1; String productDesc = ""; String commonParameter = ""; String returnUrl = null; String notifyUrl = JoinpayConstant.Notify_Url; String isShowPic = "1"; TSzsmWx szsmWx = szsmWxService.getById(id); String openId = szsmWx.getOpenId(); String authCode = null; String appid = "wx5071443e63295c29"; String transactionModel = null; String tradeMerchantNo = "777114600391409"; String buyerId = null; String isAlt = "1"; String altType = "11"; String altUrl = null; BigDecimal priceTemp = price.setScale(2, RoundingMode.HALF_DOWN); List yhjList = new ArrayList<>(); if(StringUtils.isNotEmpty(coupons)){ String[] cous = coupons.split(","); if(cous.length>0){ for(int i = 0 ;ilastUseDate.getTime()){ return R.ok(yhj.getCode(),"优惠券已过期"); } yhjList.add(yhj); } } } } } List couponList = new ArrayList<>(); //营销金额 BigDecimal marketingAmount = null; if(yhjList.size()>0){ for(TPromoCode yhj:yhjList){ if(yhj!=null&&yhj.getId()!=null){ if(marketingAmount==null){ marketingAmount = new BigDecimal("0.00"); } marketingAmount =marketingAmount.add(new BigDecimal(yhj.getDiscount()).setScale(2, RoundingMode.HALF_DOWN)); couponList.add(String.valueOf(yhj.getCode())); if(marketingAmount.compareTo(price)>=0){ marketingAmount = priceTemp.subtract(new BigDecimal("0.01")); price = new BigDecimal("0.01").setScale(2, RoundingMode.HALF_DOWN); break; }else { price = price.subtract(marketingAmount); } } } } String frpCode1 = "WEIXIN_XCX"; BigDecimal amount = price.setScale(2, RoundingMode.HALF_DOWN); TOrder order = new TOrder(); order.setCreateDate(new Date()); order.setModifyDate(new Date()); order.setAdminId(admin.getId()); order.setSn(sn1); order.setProductDesc(JSONArray.toJSONString(productNameMap)); // 去掉后面的逗号 order.setProductNo(productNo); if(productNumber == 1) { productName.delete(productName.length() - 3, productName.length()); } else { productName.deleteCharAt(productName.length() - 1); } order.setProductName(productName.toString()); order.setPrice(price); order.setClientId(equipment.getClientId()); order.setEquipmentId(equipmentId); order.setFrpCode(frpCode1); order.setAltInfo(altInfo.toString()); order.setStatus(0); order.setType(admin.getType()); order.setMarketingAmount(marketingAmount); order.setProductNumber(productNumber); order.setCompanyType(admin.getCompanyType()); order.setMachineType(equipment.getMachineType()); if(couponList.size()>0){ order.setCoupons(couponList.toString()); } if(admin.getType()==0){ order.setAdminProportion(proportion.getAdminProportion()); order.setAgencyProportion(proportion.getAgencyProportion()); order.setAgencyId(admin.getId()); } if(admin.getType()==1){ order.setAdminProportion(proportion.getAdminProportion()); order.setAgencyProportion(proportion.getAgencyProportion()); order.setAgencyId(proportion.getAgencyId()); order.setMerchantProportion(proportion.getMerchantProportion()); order.setMerchantId(admin.getId()); } if(admin.getType()==2){ order.setAdminProportion(proportion.getAdminProportion()); order.setAgencyProportion(proportion.getAgencyProportion()); order.setAgencyId(proportion.getAgencyId()); order.setMerchantProportion(proportion.getMerchantProportion()); order.setMerchantId(proportion.getMerchantId()); order.setPersonageProportion(proportion.getPersonageProportion()); order.setPersonageId(admin.getId()); } order.setPayPlatform(payPlatform); if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){ order.setIsSettlement("0"); } order.setNote(note.toString()); if(StringUtils.isNotEmpty(id)){ order.setWxId(Long.valueOf(id)); } if (BigDecimal.ZERO.compareTo(price) >= 0) { order.setStatus(1); orderService.updateById(order); //修改优惠码状态 if(promoCodeAll.size()>0){ for(TPromoCode code:promoCodeAll){ szwlFeign.updatePromoCode(String.valueOf(code.getId()),"2"); } } //发送远程命令到机器那边,让机器做糖 equipmentService.sentMessage(equipment.getClientId(),PushUtils.buildJson("cardPayXCX", String.valueOf(productNameMap)).toString()); return JsonMessage.success("支付成功"); } // 订单明细表 TOrderDetails orderDetails = new TOrderDetails(); orderDetails.setAdminId(admin.getId()); orderDetails.setEquipmentId(equipmentId); orderDetails.setCreateDate(new Date()); orderDetails.setCompanyType(admin.getCompanyType()); orderDetails.setRefundStatus("0"); orderDetails.setMachineType(equipment.getMachineType()); String client6 = clientId.substring(clientId.length() - 6); String productName1 = ""; if(productNumber > 1) { if(equipment.getMachineType() == null || equipment.getMachineType().equals("0")) { productName1 = "棉花糖-" + equipment.getName() + "-" + client6; } else { productName1 = "爆米花-" + equipment.getName() + "-" + client6; } } else { productName1 = productName + "-" + equipment.getName() + "-" + client6; } String result = null; if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){ order.setAcctSplitBunch(acctSplitBunch.toString()); try { result = huifuMchService.uniPay( orderNo1, amount, productName1, productDesc, notifyUrl, frpCode1, openId, appid, acctSplitBunch ); } catch (Exception e) { e.printStackTrace(); return JsonMessage.error("申请支付失败"); } // 返回支付二维码图片 if (result == null || StringUtils.isBlank(result)) { return JsonMessage.error("申请支付失败"); } Map map = new HashMap<>(); map.put("sn", sn1); map.put("rc_Result", result); for (String key : productMap.keySet()) { Integer productNum = productMap.get(key); TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipment.getId()), key)); orderDetails.setId(HuifuUtils.initDetailsId()); orderDetails.setProductNo(product.getNo()); orderDetails.setProductName(key); orderDetails.setPrice(product.getRmbPrice()); orderDetails.setProductNumber(productNum); orderDetails.setAmount(product.getRmbPrice().multiply(new BigDecimal(productNum))); orderDetails.setOrderSn(sn1); orderDetailsService.save(orderDetails); } orderService.save(order); return JsonMessage.success(map); }else{ try { result = joinpayMchService.uniPay( orderNo1, amount, productName1, productDesc, commonParameter, returnUrl, notifyUrl, frpCode1, isShowPic, openId, authCode, appid, transactionModel, tradeMerchantNo, buyerId, isAlt, altType, altInfo, altUrl, marketingAmount ); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return JsonMessage.error("申请支付失败"); } JSONObject resultJson = JSONObject.parseObject(result); // 汇聚支付支付申请返回支付二维码图片 String rc_Result = resultJson.getString("rc_Result"); if (resultJson == null || StringUtils.isBlank(rc_Result)) { return JsonMessage.error("申请支付失败"); } Map map = new HashMap<>(); map.put("sn", sn1); map.put("rc_Result", rc_Result); for (String key : productMap.keySet()) { Integer productNum = productMap.get(key); TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipment.getId()), key)); orderDetails.setId(HuifuUtils.initDetailsId()); orderDetails.setProductNo(product.getNo()); orderDetails.setProductName(key); orderDetails.setPrice(product.getRmbPrice()); orderDetails.setProductNumber(productNum); orderDetails.setAmount(product.getRmbPrice().multiply(new BigDecimal(productNum))); orderDetails.setOrderSn(sn1); orderDetailsService.save(orderDetails); } orderService.save(order); return JsonMessage.success(map); } } @ApiOperation(value = "汇付:获取code回调跳转到支付页面") @GetMapping("/callback") public void oauthCallback(HttpServletRequest request, HttpServletResponse response) throws IOException { // 获取code String code = request.getParameter("code"); String id = request.getParameter("state"); // 获取openid String openUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?" + "appid=" + HuifuConstant.WX_SUB_APP_ID + "&secret=" + HuifuConstant.WX_APP_SECRET + "&code=" + code + "&grant_type=authorization_code"; org.json.JSONObject jsonObject = HttpClientUtils.get(openUrl); String openid = jsonObject.getString("openid"); // 从临时表中获取订单信息 LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(THuifuTempOrder::getId,id); THuifuTempOrder huifuTempOrder = huifuTempOrderService.getOne(wrapper); String result = null; if(huifuTempOrder.getStatus() == 1) { // result = "http://szwltest.sunzee.com.cn/shenze/#/popPayment?status=1"; result = HuifuConstant.JUMP_URL+"/#/popPayment?status=1"; } else { try { result = huifuMchService.wetchatPay(huifuTempOrder,openid); } catch (Exception e) { e.printStackTrace(); } } log.info("支付链接:{}",result); response.sendRedirect(result); } /** * 支付成功回调 汇付 * */ @RequestMapping(value = "/huifuNotify", method = RequestMethod.POST) @ResponseBody public ResponseModel huifuNotify(HttpServletRequest request) { String respData = request.getParameter("resp_data"); JSONObject data = JSONObject.parseObject(respData); String transStat = data.getString("trans_stat"); // 订单号 String sn = data.getString("req_seq_id"); if(StrUtil.hasEmpty(sn)) { return R.fail(ResponseCodesEnum.A0001,"订单号为空!"); } // 查到指定订单 LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(TOrder::getSn,sn); TOrder order = orderService.getOne(wrapper); if(HuifuConstant.TRANS_STAT_S.equals(transStat)) { // 查到对应订单明细表 LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(TOrderDetails::getOrderSn,sn); List orderDetails = orderDetailsService.list(lambdaQueryWrapper); for (TOrderDetails orderDetail : orderDetails) { // 修改状态订单明细状态 orderDetail.setRefundStatus("1"); orderDetailsService.updateById(orderDetail); } // 已支付 order.setStatus(1); // 支付平台产生的全局流水号:hf_seq_id String hfSeqId = data.getString("hf_seq_id"); order.setTrxNo(hfSeqId); // 支付时间,格式:YYYY-MM-DD HH:mm:ss String payTime = data.getString("end_time"); SimpleDateFormat inputFormat = new SimpleDateFormat("yyyyMMddHHmmss"); SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 临时表修改支付状态 String id = data.getString("remark"); if(StrUtil.isNotEmpty(id)) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(THuifuTempOrder::getId,id); THuifuTempOrder huifuTempOrder = huifuTempOrderService.getOne(queryWrapper); huifuTempOrder.setStatus(1); huifuTempOrderService.updateById(huifuTempOrder); } try { Date date = inputFormat.parse(URLDecoder.decode(payTime, "UTF-8")); String formattedDate = outputFormat.format(date); Date payDate = outputFormat.parse(URLDecoder.decode(formattedDate, "UTF-8")); order.setPayDate(payDate); } catch (ParseException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } // 修改订单信息 orderService.updateById(order); // JSONObject kindData = new JSONObject(); kindData.put("sn", order.getSn()); kindData.put("productName", order.getProductName()); TEquipment equipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId())); if(order.getFrpCode().equals("WEIXIN_XCX")){ JSONObject kindData1 = new JSONObject(); kindData1.put("sn", order.getSn()); kindData1.put("productDesc", order.getProductDesc()); String s = order.getId().toString(); if(s.length()>6){ s = s.substring(s.length()-6,s.length()); } kindData1.put("mealCode", s); order.setOrderStatus("-1"); equipmentService.sentMessage(equipment.getClientId(), PushUtils.buildJson("cardPayXCX", kindData1.toString()).toString()); if(StringUtils.isNotEmpty(equipment.getApkVersion())){ //发送两次mq if(equipment.getApkVersion().equals("1.0.131-jz11")||equipment.getApkVersion().equals("1.0.131-jz12")||equipment.getApkVersion().equals("1.0.131-jz13")||equipment.getApkVersion().equals("1.0.131-jz14")||equipment.getApkVersion().equals("1.0.131-jz15")){ sendMqTwo(equipment.getClientId(),kindData1.toString()); } } TSzsmWx szsmWx = szsmWxService.getById(order.getWxId()); BigDecimal integral = szsmWx.getIntegral(); if(integral!=null){ integral = order.getPrice().add(integral); }else { integral = order.getPrice(); } szsmWx.setIntegral(integral); szsmWxService.updateById(szsmWx); if(StringUtils.isNotEmpty(order.getCoupons())){ String coupons = order.getCoupons(); coupons= coupons.substring(1,coupons.length()-1); List list1 = Arrays.asList(coupons); if(list1.size()>0){ for(String code:list1){ szwlFeign.useYHJ(code, String.valueOf(order.getAdminId()),order.getClientId()); } } } }else { equipmentService.sentMessage(equipment.getClientId(), PushUtils.buildJson("pay_success", kindData.toString()).toString()); } orderService.updateById(order); // equipmentService.sentMessage(equipment.getClientId(), PushUtils.buildJson("pay_success", kindData.toString()).toString()); try { if(order.getProductNumber()==null||order.getProductNumber()==0){ order.setProductNumber(1); } orderFeign.saveToEs(order); }catch (Exception e) { e.printStackTrace(); } try { TOrderTask orderTask = new TOrderTask(); BeanUtils.copyPropertiesIgnoreNull(order, orderTask, true); orderTask.setAgencyId(order.getId()); orderTaskService.save(orderTask); }catch (Exception e) { e.printStackTrace(); } //传送数据 sentJingZhun(order); return R.ok("支付成功"); } else { TEquipment equipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId())); String gtClientId = equipment.getGtClientId(); JSONObject kindData = new JSONObject(); kindData.put("sn", order.getSn()); equipmentService.sentMessage(equipment.getClientId(),PushUtils.buildJson("pay_faile", kindData.toString()).toString()); order.setNote(gtClientId + "支付失败"); orderService.updateById(order); return R.fail(ResponseCodesEnum.A0001,"支付失败"); } } /** * 支付成功回调 汇聚 * * @return */ @RequestMapping(value = "/notify", method = RequestMethod.GET) @ResponseBody public Object refund(HttpServletRequest request) { String r6_Status = request.getParameter("r6_Status"); // 订单号 String sn = request.getParameter("r2_OrderNo"); LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TOrder::getSn,sn); if(StringUtils.isEmpty(sn)){ return "error"; } TOrder order = orderService.getOne(query); if(order == null) { return "error"; } if (JoinpayConstant.r6_Status_100.equals(r6_Status)) { // 查到对应订单明细表 LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(TOrderDetails::getOrderSn,sn); List orderDetails = orderDetailsService.list(lambdaQueryWrapper); for (TOrderDetails orderDetail : orderDetails) { // 修改状态订单明细状态 orderDetail.setRefundStatus("1"); orderDetailsService.updateById(orderDetail); } // 已支付 order.setStatus(1); // 支付平台产生的流水号 String r7_TrxNo = request.getParameter("r7_TrxNo"); order.setTrxNo(r7_TrxNo); // 格式:YYYY-MM-DD HH:mm:ss String ra_PayTime = request.getParameter("ra_PayTime"); try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date payDate = sdf.parse(URLDecoder.decode(ra_PayTime, "UTF-8")); order.setPayDate(payDate); } catch (ParseException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } orderService.updateById(order); JSONObject kindData = new JSONObject(); kindData.put("sn", order.getSn()); kindData.put("productName", order.getProductName()); TEquipment byClientId = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId())); if(order.getFrpCode().equals("WEIXIN_XCX")){ JSONObject kindData1 = new JSONObject(); kindData1.put("sn", order.getSn()); kindData1.put("productDesc", order.getProductDesc()); String s = order.getId().toString(); if(s.length()>6){ s = s.substring(s.length()-6,s.length()); } kindData1.put("mealCode", s); order.setOrderStatus("-1"); equipmentService.sentMessage(byClientId.getClientId(), PushUtils.buildJson("cardPayXCX", kindData1.toString()).toString()); if(StringUtils.isNotEmpty(byClientId.getApkVersion())){ //发送两次mq if(byClientId.getApkVersion().equals("1.0.131-jz11")||byClientId.getApkVersion().equals("1.0.131-jz12")||byClientId.getApkVersion().equals("1.0.131-jz13")||byClientId.getApkVersion().equals("1.0.131-jz14")||byClientId.getApkVersion().equals("1.0.131-jz15")){ sendMqTwo(byClientId.getClientId(),kindData1.toString()); } } TSzsmWx szsmWx = szsmWxService.getById(order.getWxId()); BigDecimal integral = szsmWx.getIntegral(); if(integral!=null){ integral = order.getPrice().add(integral); }else { integral = order.getPrice(); } szsmWx.setIntegral(integral); szsmWxService.updateById(szsmWx); if(StringUtils.isNotEmpty(order.getCoupons())){ String coupons = order.getCoupons(); coupons= coupons.substring(1,coupons.length()-1); List list1 = Arrays.asList(coupons); if(list1.size()>0){ for(String code:list1){ szwlFeign.useYHJ(code, String.valueOf(order.getAdminId()),order.getClientId()); } } } }else { equipmentService.sentMessage(byClientId.getClientId(), PushUtils.buildJson("pay_success", kindData.toString()).toString()); } orderService.updateById(order); try { if(order.getProductNumber()==null||order.getProductNumber()==0){ order.setProductNumber(1); } orderFeign.saveToEs(order); }catch (Exception e) { e.printStackTrace(); } try { TOrderTask orderTask = new TOrderTask(); BeanUtils.copyPropertiesIgnoreNull(order, orderTask, true); orderTask.setAgencyId(order.getId()); orderTaskService.save(orderTask); }catch (Exception e) { e.printStackTrace(); } //传送数据 sentJingZhun(order); return "success"; } else { TEquipment byClientId = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId())); String gtClientId = byClientId.getGtClientId(); JSONObject kindData = new JSONObject(); kindData.put("sn", order.getSn()); equipmentService.sentMessage(byClientId.getClientId(),PushUtils.buildJson("pay_faile", kindData.toString()).toString()); order.setNote(gtClientId + "支付失败"); orderService.updateById(order); } return "success"; } //发送两次mq private void sendMqTwo(String clientId, String kind) { // 定时任务1 TimerTask timerTask = new TimerTask() { @Override public void run() { equipmentService.sentMessage(clientId, PushUtils.buildJson("cardPayXCX", kind).toString()); System.out.println("运行定时任务1:" + clientId); } }; // 定时任务2 TimerTask timerTask1 = new TimerTask() { @Override public void run() { equipmentService.sentMessage(clientId, PushUtils.buildJson("cardPayXCX", kind).toString()); System.out.println("运行定时任务1:" + clientId); } }; // 计时器 Timer timer = new Timer(); // 添加执行任务(延迟 5s 执行) timer.schedule(timerTask, 5000); Timer timer1 = new Timer(); timer1.schedule(timerTask1, 10000); } private void sentJingZhun( TOrder order) { order = orderService.getById(order.getId()); int i = 0; TAdmin admin = R.getDataIfSuccess(szwlFeign.getAdmin(String.valueOf(order.getAdminId()))); if(admin.getId().toString().equals("372")||admin.getRelationAdminId().equals("372")||admin.getId().toString().equals("639")||admin.getId().toString().equals("238")){ //发送数据到鲸准 String url = "http://47.106.13.109:3000/api/listen"; Map map = new HashMap<>(); map.put("clientId",order.getClientId()); map.put("price", String.valueOf(order.getPrice())); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); map.put("pay_date",sdf.format(order.getPayDate())); if(order.getWxId()!=null){ //小程序下单 TSzsmWx szsmWx = szsmWxService.getById(order.getWxId()); map.put("sourceChannel","1"); map.put("phone",szsmWx.getPhone()); if(order.getMarketingAmount()!=null){ map.put("amount",order.getMarketingAmount().toString()); }else{ map.put("amount","0"); } map.put("nickName","1"); map.put("uninoId",szsmWx.getOpenId()); }else { //机器下单 map.put("sourceChannel","0"); map.put("phone",""); map.put("amount","0"); map.put("nickName",""); map.put("uninoId",""); } TEquipment byClientId = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId())); map.put("latitude", String.valueOf(byClientId.getLatitude())); map.put("longitude", String.valueOf(byClientId.getLongitude())); String data = JSON.toJSONString(map); try { String s = HttpClientSslUtils.doPost(url, data); System.out.println("发送鲸准:"+s); } catch (Exception e) { e.printStackTrace(); } } } /** * 支付成功回调 杉德 * * @return */ @RequestMapping(value = "/shandeRefund", method = RequestMethod.GET) @ResponseBody public Object shandeRefund(HttpServletRequest request) { //订单号 String order_no = request.getParameter("out_order_no"); //交易状态 String trade_status = request.getParameter("trade_status"); String r6_Status = request.getParameter("r6_Status"); LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TOrder::getSn,order_no); if(StringUtils.isEmpty(order_no)){ return "error"; } List list =orderService.list(query); TOrder order = new TOrder(); if(list.size()>0){ order = list.get(0); } if (trade_status.equals("SUCCESS")) { // 已支付 order.setStatus(1); // 支付平台产生的流水号 String bank_order_no = request.getParameter("bank_order_no"); order.setTrxNo(bank_order_no); // 格式:YYYY-MM-DD HH:mm:ss String pay_success_time = request.getParameter("pay_success_time"); try { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); Date payDate = sdf.parse(URLDecoder.decode(pay_success_time, "UTF-8")); order.setPayDate(payDate); } catch (ParseException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } orderService.updateById(order); JSONObject kindData = new JSONObject(); kindData.put("sn", order.getSn()); kindData.put("productName", order.getProductName()); TEquipment byClientId = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId())); String gtClientId = byClientId.getGtClientId(); equipmentService.sentMessage(byClientId.getClientId(),PushUtils.buildJson("pay_success", kindData.toString()).toString()); // PushUtils.push(gtClientId, "支付成功", "您的订单支付成功", PushUtils.buildJson("pay_success", kindData.toString()).toString()); order.setNote(gtClientId + "已推送"); try { if(order.getProductNumber()==null||order.getProductNumber()==0){ order.setProductNumber(1); } esTOrderService.insertData(order); orderFeign.saveToEs(order); }catch (Exception e) { e.printStackTrace(); } //对金额表的当日数额进行修改 //1,获取商家的等级 BigDecimal price = order.getPrice(); TAdmin admin = R.getDataIfSuccess(szwlFeign.getAdmin(String.valueOf(byClientId.getAdminId()))); //获取分销 关联设备 TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(byClientId.getId()))); Integer type = proportion.getType(); switch (type) { case 0: BigDecimal agencyProportion = proportion.getProportion(); // 代理分销获得利润 BigDecimal agencyPrice = price.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN); //修改金额表的今日金额 TShandeMch shandeMch = R.getDataIfSuccess(szwlFeign.getShandeMch(String.valueOf(admin.getId()))); BigDecimal todayBalance = shandeMch.getTodayBalance(); todayBalance = todayBalance.add(agencyPrice).setScale(2, RoundingMode.HALF_DOWN); shandeMch.setTodayBalance(todayBalance); szwlFeign.updateShandeMch(shandeMch); break; case 1: BigDecimal agencyProportion1 = proportion.getAgencyProportion(); BigDecimal merchantProportion1 = proportion.getProportion(); // 代理分销获得利润 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); //修改代理金额表的今日金额 // ArrayList filters1 = new ArrayList<>(); // filters1.add(Filter.eq("adminId", admin.getAgencyId())); TShandeMch shandeMch2 = R.getDataIfSuccess(szwlFeign.getShandeMch(String.valueOf(proportion.getAgencyId()))); BigDecimal todayBalance2 = shandeMch2.getTodayBalance(); todayBalance2 = todayBalance2.add(agencyPrice1).setScale(2, RoundingMode.HALF_DOWN); shandeMch2.setTodayBalance(todayBalance2); szwlFeign.updateShandeMch(shandeMch2); //修改经销商金额表的今日金额 // ArrayList filters2 = new ArrayList<>(); // filters2.add(Filter.eq("adminId", admin.getId())); TShandeMch shandeMch3 = R.getDataIfSuccess(szwlFeign.getShandeMch(String.valueOf(admin.getId()))); BigDecimal todayBalance3 = shandeMch3.getTodayBalance(); todayBalance3 = todayBalance3.add(merchantAmount1).setScale(2, RoundingMode.HALF_DOWN); shandeMch3.setTodayBalance(todayBalance3); szwlFeign.updateShandeMch(shandeMch3); break; case 2: BigDecimal agencyProportion2 = proportion.getAgencyProportion(); BigDecimal merchantProportion2 = proportion.getMerchantProportion(); BigDecimal personageProportion2 = proportion.getProportion(); // 代理分销获得利润 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); //修改代理金额表的今日金额 // ArrayList filters3 = new ArrayList<>(); // filters3.add(Filter.eq("adminId", admin.getAgencyId())); // List list3 = shandeMchService.findList(null,filters3,null); TShandeMch shandeMch4 = R.getDataIfSuccess(szwlFeign.getShandeMch(String.valueOf(proportion.getAgencyId()))); BigDecimal todayBalance4 = shandeMch4.getTodayBalance(); todayBalance4 = todayBalance4.add(agencyAmount).setScale(2, RoundingMode.HALF_DOWN); shandeMch4.setTodayBalance(todayBalance4); szwlFeign.updateShandeMch(shandeMch4); //修改经销商金额表的今日金额 TShandeMch shandeMch5 = R.getDataIfSuccess(szwlFeign.getShandeMch(String.valueOf(proportion.getMerchantId()))); BigDecimal todayBalance5 = shandeMch5.getTodayBalance(); todayBalance5 = todayBalance5.add(merchantAmount).setScale(2, RoundingMode.HALF_DOWN); shandeMch5.setTodayBalance(todayBalance5); szwlFeign.updateShandeMch(shandeMch5); //修改终端金额表的今日金额 TShandeMch shandeMch6 = R.getDataIfSuccess(szwlFeign.getShandeMch(String.valueOf(admin.getId()))); BigDecimal todayBalance6 = shandeMch6.getTodayBalance(); todayBalance6 = todayBalance6.add(personageAmount).setScale(2, RoundingMode.HALF_DOWN); shandeMch6.setTodayBalance(todayBalance6); szwlFeign.updateShandeMch(shandeMch6); break; } return "SUCCESS"; } else { TEquipment byClientId = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId())); String gtClientId = byClientId.getGtClientId(); JSONObject kindData = new JSONObject(); kindData.put("sn", order.getSn()); equipmentService.sentMessage(byClientId.getClientId(),PushUtils.buildJson("pay_faile", kindData.toString()).toString()); // PushUtils.push(gtClientId, "支付成功", "您的订单支付成功", PushUtils.buildJson("pay_faile", kindData.toString()).toString()); order.setNote(gtClientId + "支付失败"); orderService.updateById(order); } return "SUCCESS"; } /** * MG280支付成功回调 * * @return */ @RequestMapping(value = "/mg280Notify", method = RequestMethod.GET) @ResponseBody public Object mg280Notify(HttpServletRequest request) { String r6_Status = request.getParameter("r6_Status"); // 订单号 String sn = request.getParameter("r2_OrderNo"); LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TOrder::getSn,sn); if(StringUtils.isEmpty(sn)){ return "error"; } List list = orderService.list(query); TOrder order = list.get(0); if (JoinpayConstant.r6_Status_100.equals(r6_Status)) { // 已支付 order.setStatus(1); // 支付平台产生的流水号 String r7_TrxNo = request.getParameter("r7_TrxNo"); order.setTrxNo(r7_TrxNo); // 格式:YYYY-MM-DD HH:mm:ss String ra_PayTime = request.getParameter("ra_PayTime"); try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date payDate = sdf.parse(URLDecoder.decode(ra_PayTime, "UTF-8")); order.setPayDate(payDate); } catch (ParseException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } orderService.updateById(order); JSONObject kindData = new JSONObject(); kindData.put("sn", order.getSn()); kindData.put("productName", order.getProductName()); TEquipment byClientId = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId())); String gtClientId = byClientId.getGtClientId(); Date endDate = byClientId.getEndDate(); Date date = new Date(); Date time = null; double price1Price = 0; BigDecimal price = order.getPrice(); // List priceList = priceService.findAll(); // for (Price price1 : priceList) { // if (price1.getName().equals("MG280")) { // price1Price = price1.getPrice(); // } // } TPrice price1 = R.getDataIfSuccess(szwlFeign.getPrice("MG280")); price1Price = price1.getPrice(); BigDecimal city = price.divide(BigDecimal.valueOf(price1Price)); int ci = city.intValue(); if (endDate.getTime() < date.getTime()) { Calendar c = Calendar.getInstance(); c.setTime(date); int i = c.get(Calendar.MONTH); c.set(Calendar.MONTH, i - 1); //上个月有多少天 int actualMaximum = c.getActualMaximum(Calendar.DAY_OF_MONTH); long t = date.getTime() - endDate.getTime(); long m = actualMaximum * 1000 * 60 * 60 * 24l; if (t <= m) { Calendar calendar = Calendar.getInstance(); calendar.setTime(endDate); calendar.set(Calendar.DAY_OF_MONTH, 10); //将小时至0 calendar.set(Calendar.HOUR_OF_DAY, 0); //将分钟至0 calendar.set(Calendar.MINUTE, 0); //将秒至0 calendar.set(Calendar.SECOND, 0); //将毫秒至0 calendar.set(Calendar.MILLISECOND, 0); calendar.add(Calendar.MONTH, ci); time = calendar.getTime(); } if (t > m) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DAY_OF_MONTH, 10); //将小时至0 calendar.set(Calendar.HOUR_OF_DAY, 0); //将分钟至0 calendar.set(Calendar.MINUTE, 0); //将秒至0 calendar.set(Calendar.SECOND, 0); //将毫秒至0 calendar.set(Calendar.MILLISECOND, 0); calendar.add(Calendar.MONTH, ci); time = calendar.getTime(); } } else { Calendar calendar = Calendar.getInstance(); calendar.setTime(endDate); calendar.set(Calendar.DAY_OF_MONTH, 10); //将小时至0 calendar.set(Calendar.HOUR_OF_DAY, 0); //将分钟至0 calendar.set(Calendar.MINUTE, 0); //将秒至0 calendar.set(Calendar.SECOND, 0); //将毫秒至0 calendar.set(Calendar.MILLISECOND, 0); calendar.add(Calendar.MONTH, ci); time = calendar.getTime(); } byClientId.setEndDate(time); //修改机器 szwlFeign.updateByEquipment(byClientId); order.setNote(gtClientId + "已推送"); orderService.updateById(order); equipmentService.sentMessage(byClientId.getClientId(),PushUtils.buildJson("endDate", String.valueOf(byClientId.getEndDate().getTime())).toString()); // PushUtils.push(byClientId.getGtClientId(), "", "", PushUtils.buildJson("endDate", String.valueOf(byClientId.getEndDate().getTime())).toString()); return "success"; } else { TEquipment byClientId = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId())); String gtClientId = byClientId.getGtClientId(); JSONObject kindData = new JSONObject(); kindData.put("sn", order.getSn()); order.setNote(gtClientId + "支付失败"); orderService.updateById(order); } return "success"; } //生成二维码,并返回base64格式 private String toQrcode(String code) { String imgtext = null; QRCodeWriter qrCodeWriter = new QRCodeWriter(); HashMap hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix bitMatrix = null; try { bitMatrix = qrCodeWriter.encode(code, BarcodeFormat.QR_CODE, 600, 600, hints); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); MatrixToImageWriter.writeToStream(bitMatrix, "PNG", outputStream); Base64.Encoder encoder = Base64.getEncoder(); String text = encoder.encodeToString(outputStream.toByteArray()); imgtext = "data:image/png;base64,"+text; } catch (Exception e) { e.printStackTrace(); } return imgtext; } /** * 汇付退款成功回调 * * @param request * @return */ @RequestMapping(value = "/refundNotify", method = RequestMethod.POST) @ResponseBody public ResponseModel refundNotify(HttpServletRequest request) { String respData = request.getParameter("resp_data"); JSONObject data = JSONObject.parseObject(respData); String transStat = data.getString("trans_stat"); // 订单号 String sn = data.getString("org_req_seq_id"); if(StrUtil.hasEmpty(sn)) { return R.fail(ResponseCodesEnum.A0001,"订单号为空!"); } LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(TOrder::getSn,sn); TOrder order = orderService.getOne(wrapper); if(order == null) { return R.fail(ResponseCodesEnum.A0001,"订单不存在"); } if(HuifuConstant.TRANS_STAT_S.equals(transStat)) { if(order.getRefundQuantity() < order.getProductNumber()) { order.setStatus(1); } else { order.setStatus(3); } String refundOrderNo = data.getString("req_seq_id"); String ordAmt = data.getString("ord_amt"); String refundTrxNo = data.getString("hf_seq_id"); order.setRefundOrderNo(refundOrderNo); order.setRefundDate(new Date()); order.setRefundTrxNo(refundTrxNo); // 更改订单明细表 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(TOrderDetails::getOrderSn, sn); // 处理退款中状态的明细 queryWrapper.eq(TOrderDetails::getRefundStatus, "4"); List tOrderDetails = orderDetailsService.list(queryWrapper); for (TOrderDetails tOrderDetail : tOrderDetails) { Integer refundQuantity = tOrderDetail.getRefundQuantity(); Integer productNumber = tOrderDetail.getProductNumber(); if(refundQuantity < productNumber) { // 部分退款 tOrderDetail.setRefundStatus("2"); orderDetailsService.updateById(tOrderDetail); } else { // 全部退款 tOrderDetail.setRefundStatus("3"); orderDetailsService.updateById(tOrderDetail); } } String acctSplitBunch = ""; // 部分退款或全部退款 TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(order.getEquipmentId()))); if(order.getRefundAmount() != null) { // 退款总额 BigDecimal newRefundAmount = order.getRefundAmount().add(new BigDecimal(ordAmt).setScale(2, RoundingMode.HALF_UP)); order.setRefundAmount(newRefundAmount); // 如果小于订单金额 if(newRefundAmount.compareTo(order.getPrice()) < 0) { acctSplitBunch = getAcctSplitBunch(proportion,order.getPrice().subtract(newRefundAmount)).toString(); order.setAcctSplitBunch(acctSplitBunch); } else { acctSplitBunch = getAcctSplitBunch(proportion,order.getPrice()).toString(); order.setAcctSplitBunch(acctSplitBunch); } } else { order.setRefundAmount(new BigDecimal(ordAmt).setScale(2,RoundingMode.HALF_UP)); if(new BigDecimal(ordAmt).setScale(2, RoundingMode.HALF_UP).compareTo(order.getPrice()) < 0) { // 重新设置分销 acctSplitBunch = getAcctSplitBunch(proportion,order.getPrice() .subtract(new BigDecimal(ordAmt).setScale(2,RoundingMode.HALF_UP))).toString(); order.setAcctSplitBunch(acctSplitBunch); } } try { esTOrderService.updateDataById(order); }catch (Exception e) { e.printStackTrace(); }finally { orderService.updateById(order); } return R.ok("退款成功"); } return R.fail(ResponseCodesEnum.A0001,"退款失败"); } /** * 汇聚退款成功回调 * * @param request * @return */ @RequestMapping(value = "/notifyRefund", method = RequestMethod.GET) @ResponseBody public Object notifyRefund(HttpServletRequest request) { String r6_Status = request.getParameter("ra_Status"); // 订单号 String sn = request.getParameter("r2_OrderNo"); // 退款订单号 // String refundSn = request.getParameter("r3_RefundOrderNo"); // String num = refundSn.substring(refundSn.length() - 1); //// Integer refundNumber = Integer.valueOf(num); if(StringUtils.isEmpty(sn)){ return "error"; } LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TOrder::getSn,sn); TOrder order = orderService.getOne(query); if (order == null) { return "error"; } if (order.getStatus() == 3) { return "success"; } // 判断退款版本号是否存在 // String refundOrderNo = order.getRefundOrderNo(); // if(StringUtils.isNotEmpty(refundOrderNo)) { // if(refundSn.equals(refundOrderNo)) { // try { // esTOrderService.updateDataById(order); // }catch (Exception e) { // e.printStackTrace(); // } // return "success"; // } // } // 订单商品数量 // Integer productNumber = order.getProductNumber(); if (JoinpayConstant.r6_Status_100.equals(r6_Status)) { if(order.getRefundQuantity() < order.getProductNumber()) { order.setStatus(1); } else { order.setStatus(3); } // // 设置退款版本号 // order.setRefundOrderNo(refundSn); // // 已退款 // if(productNumber >= refundNumber){ // productNumber -= refundNumber; // order.setProductNumber(productNumber); // } // 更改订单明细表 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(TOrderDetails::getOrderSn, sn); // 处理退款中状态的明细 queryWrapper.eq(TOrderDetails::getRefundStatus, "4"); List tOrderDetails = orderDetailsService.list(queryWrapper); for (TOrderDetails tOrderDetail : tOrderDetails) { Integer refundQuantity = tOrderDetail.getRefundQuantity(); Integer productNumber = tOrderDetail.getProductNumber(); if(refundQuantity < productNumber) { // 部分退款 tOrderDetail.setRefundStatus("2"); orderDetailsService.updateById(tOrderDetail); } else { // 全部退款 tOrderDetail.setRefundStatus("3"); orderDetailsService.updateById(tOrderDetail); } } // r4_refundAmount: 退款金额 BigDecimal r4_refundAmount = new BigDecimal(request.getParameter("r4_RefundAmount_str")); if(order.getRefundMarketingAmount()!=null){ r4_refundAmount =r4_refundAmount.add(order.getRefundMarketingAmount()); } // price: 订单金额, refundAmount:已退款的金额 BigDecimal price = order.getPrice(); BigDecimal refundAmount = order.getRefundAmount(); TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(order.getEquipmentId()))); if(order.getRefundAmount() != null) { BigDecimal newRefundAmount = refundAmount.add(r4_refundAmount); order.setRefundAmount(refundAmount.add(r4_refundAmount)); // 如果小于订单金额 if(newRefundAmount.compareTo(price) < 0) { JSONArray altInfo = getAltInfo(proportion, price.subtract(newRefundAmount)); order.setAltInfo(altInfo.toString()); } else { JSONArray altInfo = getAltInfo(proportion, price); order.setAltInfo(altInfo.toString()); } } else { order.setRefundAmount(r4_refundAmount); if(r4_refundAmount.compareTo(price) < 0) { JSONArray altInfo = getAltInfo(proportion, price.subtract(r4_refundAmount)); order.setAltInfo(altInfo.toString()); } } // 判断退款金额是否小于订单余额 // if(r4_refundAmount.compareTo(price)==-1){ // // refundAmount: 已退款金额 // BigDecimal refundAmount = order.getRefundAmount(); // if(refundAmount!=null){ // // 如果已退款金额不为空,则加上退款金额 // order.setRefundAmount(refundAmount.add(r4_refundAmount)); // }else { // // 已退款金额 // order.setRefundAmount(r4_refundAmount); // } // // 订单设置为支付状态 // order.setStatus(1); // // 改变订单的金额,原先金额减去退款金额 //// order.setPrice(price.subtract(r4_refundAmount)); // // 重置分销 // if(order.getAltInfo()!=null) { // TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(order.getEquipmentId()))); // JSONArray altInfo = getAltInfo(proportion, price.subtract(r4_refundAmount)); // order.setAltInfo(altInfo.toString()); // } // } // 判断退款金额是否等于订单余额 // if(r4_refundAmount.compareTo(price)==0){ // BigDecimal refundAmount = order.getRefundAmount(); //// order.setProductNumber(0); // if(refundAmount!=null){ // order.setRefundAmount(refundAmount.add(r4_refundAmount)); // // 全部退完款重新加回已退款的金额 //// order.setPrice(order.getPrice().add(refundAmount)); // }else { // // 退款金额 // order.setRefundAmount(r4_refundAmount); // } // order.setStatus(3); // if(order.getAltInfo()!=null) { // TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(order.getEquipmentId()))); // JSONArray altInfo = getAltInfo(proportion, order.getRefundAmount()); // order.setAltInfo(altInfo.toString()); // } // } // 支付平台产生的退款流水号 String r5_RefundTrxNo = request.getParameter("r5_RefundTrxNo"); order.setRefundTrxNo(r5_RefundTrxNo); order.setRefundDate(new Date()); order.setModifyDate(new Date()); // orderService.updateById(order); String orderStatus = order.getOrderStatus(); if(StringUtils.isNotEmpty(orderStatus)&&orderStatus.equals("0")){ if(order.getStatus().toString().equals("3")){ TEquipment byClientId = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId())); JSONObject kindData = new JSONObject(); String s = order.getId().toString(); if(s.length()>6){ s = s.substring(s.length()-6,s.length()); } kindData.put("mealCode", s); equipmentService.sentMessage(byClientId.getClientId(), PushUtils.buildJson("refuse_success", kindData.toString()).toString()); } } try { esTOrderService.updateDataById(order); }catch (Exception e) { e.printStackTrace(); }finally { orderService.updateById(order); } return "success"; } return "success"; } @ApiOperation(value = "添加订单") @PostMapping("/addOrder") public ResponseModel addOrder(@RequestBody TOrder order) { boolean save = orderService.save(order); return R.ok(save); } @ApiOperation(value = "修改订单") @PostMapping("/updateOrder") public ResponseModel updateOrder(@RequestBody TOrder order) { orderService.updateById(order); LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TOrderTask::getSn,order.getSn()); TOrderTask orderTask = orderTaskService.getOne(query); orderTask.setStatus(order.getStatus()); orderTaskService.updateById(orderTask); return R.ok(); } @ApiOperation(value = "获取订单") @GetMapping("/getOrder") public ResponseModel getOrder(@RequestParam String id) { TOrder order = orderService.getById(id); return R.ok(order); } @ApiOperation(value = "获取订单") @GetMapping("/getOrderBySn") public ResponseModel getOrderBySn(@RequestParam String sn) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(TOrder::getSn,sn); TOrder order = orderService.getOne(wrapper); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(TOrderDetails::getOrderSn,sn); List orderDetails = orderDetailsService.list(queryWrapper); OrderDetailsDTO orderDetailsDTO = new OrderDetailsDTO(); BeanUtil.copyProperties(order,orderDetailsDTO); orderDetailsDTO.setOrderDetails(orderDetails); return R.ok(orderDetailsDTO); } /** * 优惠码支付成功回调 * * @return */ @RequestMapping(value = "/promoCodeNotify", method = RequestMethod.GET) @ResponseBody public Object promoCoderefund(HttpServletRequest request) { String r6_Status = request.getParameter("r6_Status"); // 订单号 String sn = request.getParameter("r2_OrderNo"); //价格 String price = request.getParameter("r3_Amount"); Double money = null; TPrice tPrice = R.getDataIfSuccess(szwlFeign.getPrice("优惠码")); money = tPrice.getPrice(); //生成优惠码的个数 double num = Double.parseDouble(price) / money; int number = (int) num; LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TOrder::getSn,sn); List list = new ArrayList<>(); if(StringUtils.isNotEmpty(sn)){ list = orderService.list(query); } TOrder order = list.get(0); if (order.getStatus() != 0) { return "success"; } if (JoinpayConstant.r6_Status_100.equals(r6_Status)) { // 已支付 order.setStatus(1); // 支付平台产生的流水号 String r7_TrxNo = request.getParameter("r7_TrxNo"); order.setTrxNo(r7_TrxNo); // 格式:YYYY-MM-DD HH:mm:ss String ra_PayTime = request.getParameter("ra_PayTime"); try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date payDate = sdf.parse(URLDecoder.decode(ra_PayTime, "UTF-8")); order.setPayDate(payDate); } catch (ParseException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } orderService.updateById(order); JSONObject kindData = new JSONObject(); kindData.put("sn", order.getSn()); kindData.put("productName", order.getProductName()); String mont = order.getRefundTrxNo(); Long data = Calendar.getInstance().getTimeInMillis(); Float month = Float.valueOf(mont); Float mm = month*10; String m = String.valueOf(mm); String[] split = m.split("\\."); String th = split[0]; Long t = Long.valueOf(th); Long mon = 30L*24L*60L*60L*1000L; Long d = data + ((mon*t)/10); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String sd = sdf.format(new Date(Long.parseLong(String.valueOf(d)))); Date lastUseDate = new Date(Long.parseLong(String.valueOf(d))); szwlFeign.addPromoCode(String.valueOf(order.getAdminId()),lastUseDate,number); return "支付成功"; } return "success"; } /** * 查询订单是否付款 * * @param * @param * @return */ @RequestMapping(value = "/getOrderStatus", method = RequestMethod.GET) @ResponseBody public JsonMessage getOrderStatus(String sn) { LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TOrder::getSn,sn); List list = orderService.list(query); TOrder order = list.get(0); Integer status = order.getStatus(); String sta = status.toString(); if(status==1){ sta="pay"; } return JsonMessage.success(sta); } /** * 上传订单的做糖状态 * * @param * @param * @return */ @RequestMapping(value = "/sendOrderStatus", method = RequestMethod.GET) @ResponseBody public JsonMessage sendOrderStatus(String sn,String orderStatus) { if(StringUtils.isEmpty(sn)||StringUtils.isEmpty(orderStatus)){ return JsonMessage.error("数据有空"); } LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TOrder::getSn,sn); List list = orderService.list(query); TOrder order = list.get(0); order.setOrderStatus(orderStatus); orderService.updateById(order); if(orderStatus.equals("1")){ String s = order.getId().toString(); String clientId = order.getClientId(); szsmWxService.subscribeMessage(order.getWxId(),s.substring(s.length()-6,s.length()),clientId.substring(clientId.length()-6,clientId.length())); } LambdaQueryWrapper query1 = Wrappers.lambdaQuery(); query1.eq(TOrderTask::getSn,sn); TOrderTask orderTask = orderTaskService.getOne(query1); orderTask.setOrderStatus(orderStatus); orderTaskService.updateById(orderTask); return JsonMessage.success("success"); } /** * 删除订单 * * @param * @param * @return */ @RequestMapping(value = "/deleteOrder", method = RequestMethod.GET) @ResponseBody public JsonMessage deleteOrder(String sn) { if(StringUtils.isEmpty(sn)){ return JsonMessage.error(sn + "删除失败"); } LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TOrder::getSn,sn); List list = orderService.list(query); TOrder order = list.get(0); Integer status = order.getStatus(); String ss = status.toString(); if (status==0) { orderService.removeById(order.getId()); return JsonMessage.success(sn + "删除成功"); } // return JsonMessage.success(sn + "删除失败"); return JsonMessage.success(sn + "删除成功"); } /** * 添加订单 * * @param clientId 设备client id * @param productName 商品名称 * @param * @return */ @RequestMapping(value = "/addOrder", method = RequestMethod.GET) @ResponseBody public Object addOrder(String clientId, String productName, String pri,String sn) { TEquipment equipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(clientId)); if (equipment == null) { return JsonMessage.error("找不到设备"); } Long equipmentId = equipment.getId(); BigDecimal price = new BigDecimal(pri); TAdmin admin = R.getDataIfSuccess(szwlFeign.getAdmin(String.valueOf(equipment.getAdminId()))); if (admin == null) { return JsonMessage.error("找不到设备商家"); } JSONArray altInfo = new JSONArray(); String orderType = null; TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(equipment.getId()))); Integer type = proportion.getType(); switch (type) { case 0: orderType = "1"; break; case 1: orderType = "2"; break; case 2: orderType = "3"; break; } TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipmentId), productName)); TOrder order = new TOrder(); order.setCreateDate(new Date()); order.setModifyDate(new Date()); order.setAdminId(admin.getId()); order.setSn(sn); order.setProductId(product.getId()); order.setProductNo(product.getNo()); order.setProductName(productName); order.setPrice(price); order.setClientId(equipment.getClientId()); order.setEquipmentId(equipmentId); order.setAltInfo(altInfo.toString()); order.setStatus(1); order.setType(type); orderService.save(order); try { if(order.getProductNumber()==null||order.getProductNumber()==0){ order.setProductNumber(1); } esTOrderService.insertData(order); orderFeign.saveToEs(order); }catch (Exception e) { e.printStackTrace(); } return JsonMessage.success("success"); } /** * 汇聚分销明细 * @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); // 汇聚平台 selfMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getAdminId()))); 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); // 汇聚平台 agencyMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getAgencyId()))); selfMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getAdminId()))); 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); // 汇聚 agencyMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getAgencyId()))); merchantMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getMerchantId()))); selfMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getAdminId()))); 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 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 proportion * @param price * @return */ public JSONObject getAcctSplitBunch(TProportion proportion, BigDecimal price) { // 获取分账有几方: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 selfHuifu = R.getDataIfSuccess(szwlFeign.getHuifuId(proportion.getAdminId())); // 公司平台分销 adminJson = new JSONObject(); adminJson.put("div_amt", adminAmount.toString()); adminJson.put("huifu_id", HuifuConstant.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); // 汇付平台 selfHuifu = R.getDataIfSuccess(szwlFeign.getHuifuId(proportion.getAdminId())); agencyHuifu = R.getDataIfSuccess(szwlFeign.getHuifuId(proportion.getAgencyId())); // 公司平台分销 adminJson = new JSONObject(); adminJson.put("div_amt", adminAmount.toString()); adminJson.put("huifu_id", HuifuConstant.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); selfHuifu = R.getDataIfSuccess(szwlFeign.getHuifuId(proportion.getAdminId())); agencyHuifu = R.getDataIfSuccess(szwlFeign.getHuifuId(proportion.getAgencyId())); merchantHuifu = R.getDataIfSuccess(szwlFeign.getHuifuId(proportion.getMerchantId())); // 公司平台分销 adminJson = new JSONObject(); adminJson.put("div_amt", adminAmount.toString()); adminJson.put("huifu_id", HuifuConstant.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.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; } }