zhangbin 1 year ago
parent
commit
9305b20de5

+ 21 - 2
nbjk-admin/src/main/java/com/miaxis/app/controller/wx/WxController.java

@@ -302,7 +302,6 @@ public class WxController extends BaseController {
             } else {
                 order.setProfitSharing(0); //待分账
             }
-            order.setProfitSharing(0); //待分账
             order.setOrderStatus(OrderStatusEnum.PROCESSING.getCode());
             wxOrderService.save(order);
             return Response.success(placeXcxWxOrder(order, "人生报告"));
@@ -356,6 +355,7 @@ public class WxController extends BaseController {
     @ApiOperation("微信公众号支付下单")
     public Response<JSONObject> gzhPrepareOrder(@RequestBody WxOrderDTO wxOrderDTO) throws Exception{
         UserInfo userInfo = SecurityUtils.getLoginUser().getStudent();
+        String profitSharing = configService.selectConfigByKey("profit_sharing");
         if(wxOrderDTO.getGoodsType()==1) {
             int count = vipUserExamService.getUserExamByUnionIdAndGoodsId(userInfo.getUnionId(), wxOrderDTO.getGoodsId());
             if (count > 0) {
@@ -378,6 +378,11 @@ public class WxController extends BaseController {
             order.setForeType(wxOrderDTO.getForeType()); //前端类型 1:app 2:小程序 3:公众号
             order.setTotal(examInfo.getPrice());
             order.setOrderStatus(OrderStatusEnum.PROCESSING.getCode());
+            if("1".equals(profitSharing)) {
+                order.setProfitSharing(1); //待分账
+            } else {
+                order.setProfitSharing(0); //不分账
+            }
             wxOrderService.save(order);
             return Response.success(placeGzhJsWxOrder(order, examInfo.getName()));
         } else if (wxOrderDTO.getGoodsType()==2) {
@@ -396,6 +401,7 @@ public class WxController extends BaseController {
      * @throws Exception
      */
     private JSONObject placeGzhJsWxOrder(WxOrder order, String goodsName) throws Exception {
+        String profitSharing = configService.selectConfigByKey("profit_sharing");
         HttpPost httpPost = initHttpPost(wxpayConfig.getV3JsUrl());
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         ObjectMapper objectMapper = new ObjectMapper();
@@ -407,6 +413,9 @@ public class WxController extends BaseController {
                 .put("out_trade_no", order.getOutTradeNo());
         rootNode.putObject("amount").put("total", order.getTotal());
         rootNode.putObject("payer").put("openid", order.getGzhOpenid());
+        if("1".equals(profitSharing)) {
+            rootNode.putObject("settle_info").put("profit_sharing", true);
+        }
         objectMapper.writeValue(bos, rootNode);
         httpPost.setEntity(new StringEntity(rootNode.toString(), "utf-8"));
         HttpResponse response = httpClient.execute(httpPost);
@@ -467,6 +476,8 @@ public class WxController extends BaseController {
     @ApiOperation("微信H5支付下单")
     public Response<JSONObject> h5PrepareOrder(@RequestBody WxOrderDTO wxOrderDTO) throws Exception{
         UserInfo userInfo = SecurityUtils.getLoginUser().getStudent();
+        String profitSharing = configService.selectConfigByKey("profit_sharing");
+
         if(wxOrderDTO.getGoodsType()==1) {
             int count = vipUserExamService.getUserExamByUnionIdAndGoodsId(userInfo.getUnionId(), wxOrderDTO.getGoodsId());
             if (count > 0) {
@@ -489,6 +500,11 @@ public class WxController extends BaseController {
             order.setForeType(wxOrderDTO.getForeType()); //前端类型 1:app 2:小程序 3:公众号
             order.setTotal(examInfo.getPrice());
             order.setOrderStatus(OrderStatusEnum.PROCESSING.getCode());
+            if("1".equals(profitSharing)) {
+                order.setProfitSharing(1); //待分账
+            } else {
+                order.setProfitSharing(0); //不分账
+            }
             wxOrderService.save(order);
             return Response.success(placeH5WxOrder(order, examInfo.getName()));
         } else if (wxOrderDTO.getGoodsType()==2) {
@@ -509,6 +525,7 @@ public class WxController extends BaseController {
      * @throws Exception
      */
     private JSONObject placeH5WxOrder(WxOrder order, String goodsName) throws Exception {
+        String profitSharing = configService.selectConfigByKey("profit_sharing");
         HttpPost httpPost = initHttpPost(wxpayConfig.getV3H5Url());
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         ObjectMapper objectMapper = new ObjectMapper();
@@ -536,7 +553,9 @@ public class WxController extends BaseController {
         sceneInfoNode.put("payer_client_ip", ipAddr)
                 .set("h5_info", h5InfoNode);
         rootNode.set("scene_info",sceneInfoNode);
-
+        if("1".equals(profitSharing)) {
+            rootNode.putObject("settle_info").put("profit_sharing", true);
+        }
         objectMapper.writeValue(bos, rootNode);
         httpPost.setEntity(new StringEntity(rootNode.toString(), "utf-8"));
         HttpResponse response = httpClient.execute(httpPost);

+ 1 - 1
nbjk-admin/src/main/java/com/miaxis/app/controller/wx/WxNotifyController.java

@@ -211,7 +211,7 @@ public class WxNotifyController {
         orderInfoService.writeVipDataToDb(wxOrder,payType);
 
 //        try {
-//            wxOrderService.wxAddFenZhang();  //添加分账用户
+ //           wxOrderService.wxAddFenZhang(wxOrder);  //添加分账用户
 //            wxOrderService.wxProfitsharing(wxOrder);  //进行分账
 //        } catch (Exception e) {
 //            e.printStackTrace();

+ 2 - 2
nbjk-admin/src/main/resources/application-local.yml

@@ -132,8 +132,8 @@ wxpay:
     merchantId: 1639487489
     serialNumber: 1304D67DC59D94217412C49E20D5B0E00A5A122D
     v3key: PIQKEpG1FEoBEELgv0GUxkUJKTkc0jGB
-    notifyUrl: https://1a0j71808.imdo.co/open-api/wx/notify/wxpay
-    notifyUrlRefund: https://1a0j71808.imdo.co/open-api/wx/notify/refund
+    notifyUrl: https://107j18o08.vicp.fun/open-api/wx/notify/wxpay
+    notifyUrlRefund: https://107j18o08.vicp.fun/open-api/wx/notify/refund
     #notifyUrl: https://1a0j71808.imdo.co/open-api/wx/notify/wxpay
     #notifyUrlRefund: https://1a0j71808.imdo.co/open-api/wx/notify/refund
 

+ 22 - 1
nbjk-admin/src/test/java/com/miaxis/test/WxFenZhangTest.java

@@ -1,6 +1,7 @@
 package com.miaxis.test;
 
 import com.miaxis.NbjkApplication;
+import com.miaxis.wx.domain.WxOrder;
 import com.miaxis.wx.service.IWxGzhService;
 import com.miaxis.wx.service.IWxOrderService;
 import org.junit.Test;
@@ -9,6 +10,8 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 
+import java.util.List;
+
 @SpringBootTest(classes = NbjkApplication.class)
 @RunWith(SpringRunner.class)
 public class WxFenZhangTest {
@@ -21,7 +24,25 @@ public class WxFenZhangTest {
     @Test
     public void test1() throws Exception {
 
-        wxOrderService.wxAddFenZhang();
+        List<WxOrder> wxOrderList = wxOrderService.selectWxOrderByWaitProfit();
+
+        for (int i = 0; i < wxOrderList.size(); i++) {
+            WxOrder wxOrder = wxOrderList.get(i);
+            wxOrderService.wxAddFenZhang(wxOrder); //添加
+            boolean flag = wxOrderService.wxProfitsharing(wxOrderList.get(i));
+
+            if(flag) {
+                wxOrder.setProfitSharing(3);
+                wxOrderService.saveOrUpdate(wxOrder);
+            } else {
+                wxOrder.setProfitSharing(2);
+                wxOrderService.saveOrUpdate(wxOrder);
+            }
+
+        }
+
+
+
 
     }
 

+ 0 - 4
nbjk-common/src/main/java/com/miaxis/common/core/domain/entity/UserInfo.java

@@ -82,10 +82,6 @@ public class UserInfo extends BaseBusinessEntity{
     @ApiModelProperty(value = "是否是vip 0-否 1-超级vip 2-普通vip")
     private Integer isVip;
 
-    /** 8字VIP到期时间 */
-    @ApiModelProperty(value = "创建时间")
-    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
-    private Date baziVip;
 
 
     /** 微信开放平台 */

+ 37 - 1
nbjk-service/src/main/java/com/miaxis/job/AppAdJob.java

@@ -2,11 +2,15 @@ package com.miaxis.job;
 
 import com.miaxis.common.core.domain.entity.SysDictData;
 import com.miaxis.system.service.ISysDictDataService;
+import com.miaxis.wx.domain.WxOrder;
+import com.miaxis.wx.service.IWxOrderService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
+import java.util.List;
+
 @Slf4j
 @Component
 public class AppAdJob {
@@ -14,7 +18,8 @@ public class AppAdJob {
 
     @Autowired
     private ISysDictDataService dictDataService;
-
+    @Autowired
+    private IWxOrderService wxOrderService;
 
    // @Scheduled(cron = "0 0 19 * * ?")
     public void openAd()  {
@@ -54,4 +59,35 @@ public class AppAdJob {
         log.info("--------现在是6点半!~---------------");
     }
 
+
+    @Scheduled(cron = "0 0/1 * * * ?")
+    public void wxProfitsharing()  {
+        log.info("--------现在开始执行每1分钟一次的微信分帐!~---------------");
+
+        List<WxOrder> wxOrderList = wxOrderService.selectWxOrderByWaitProfit();
+        boolean flag = false;
+        for (int i = 0; i < wxOrderList.size(); i++) {
+            WxOrder wxOrder = wxOrderList.get(i);
+
+            try {
+                wxOrderService.wxAddFenZhang(wxOrder); //添加
+                flag = wxOrderService.wxProfitsharing(wxOrderList.get(i));
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+
+            if(flag) {
+                wxOrder.setProfitSharing(3);
+                wxOrderService.saveOrUpdate(wxOrder);
+            } else {
+                wxOrder.setProfitSharing(2);
+                wxOrderService.saveOrUpdate(wxOrder);
+            }
+
+        }
+
+
+    }
+
+
 }

+ 1 - 1
nbjk-service/src/main/java/com/miaxis/wx/service/IWxOrderService.java

@@ -11,7 +11,7 @@ import java.util.List;
 public interface IWxOrderService extends IService<WxOrder>{
 
 
-    void wxAddFenZhang() throws Exception;
+    void wxAddFenZhang(WxOrder wxOrder) throws Exception;
 
     boolean wxProfitsharing(WxOrder wxOrder) throws  Exception;
 

+ 53 - 9
nbjk-service/src/main/java/com/miaxis/wx/service/impl/WxOrderServiceImpl.java

@@ -9,17 +9,16 @@ import com.github.wxpay.sdk.WXPayConstants;
 import com.github.wxpay.sdk.WXPayUtil;
 import com.miaxis.common.config.WxPayXcxConfigImpl;
 import com.miaxis.common.config.WxpayConfig;
+import com.miaxis.common.core.domain.entity.SysUser;
 import com.miaxis.common.core.domain.entity.UserInfo;
 import com.miaxis.common.exception.CustomException;
-import com.miaxis.common.utils.DateUtils;
-import com.miaxis.common.utils.RSAUtils;
-import com.miaxis.common.utils.SecurityUtils;
-import com.miaxis.common.utils.XmlUtil;
+import com.miaxis.common.utils.*;
 import com.miaxis.order.domain.OrderInfo;
 import com.miaxis.order.domain.OrderSplit;
 import com.miaxis.order.service.IOrderInfoService;
 import com.miaxis.order.service.IOrderSplitService;
 import com.miaxis.system.service.ISysConfigService;
+import com.miaxis.system.service.ISysUserService;
 import com.miaxis.user.service.IUserInfoService;
 import com.miaxis.wx.domain.WxExtract;
 import com.miaxis.wx.domain.WxExtractBank;
@@ -69,6 +68,8 @@ public class WxOrderServiceImpl extends ServiceImpl<WxOrderMapper, WxOrder> impl
     @Autowired
     private IOrderSplitService orderSplitService;
 
+    @Autowired
+    private ISysUserService sysUserService;
 
     @Autowired
     private RSAUtils rsaUtils;
@@ -93,7 +94,9 @@ public class WxOrderServiceImpl extends ServiceImpl<WxOrderMapper, WxOrder> impl
 
     @Override
     @Transactional
-    public void wxAddFenZhang() throws Exception {
+    public void wxAddFenZhang(WxOrder wxOrder) throws Exception {
+        //查询上级
+        String account = getAccount(wxOrder);
         String url = "https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver";
 
         String nonceStr = RandomStringUtils.randomAlphanumeric(32);
@@ -105,7 +108,11 @@ public class WxOrderServiceImpl extends ServiceImpl<WxOrderMapper, WxOrder> impl
         sortMap.put("sign_type", "HMAC-SHA256");
         Map<String, String> receiverMap = new TreeMap<String, String>();
         receiverMap.put("type", "PERSONAL_OPENID");
-        receiverMap.put("account", "o7N3j5XxUrgOKE4ulP-HseFpBCiM");
+        if(StringUtils.isNotEmpty(account)) {  //如果存在上级,分给上给
+            receiverMap.put("account", account);
+        } else {                               //如果不存在上级,分给固定的
+            receiverMap.put("account", "o7N3j5XxUrgOKE4ulP-HseFpBCiM");
+        }
         receiverMap.put("relation_type", "SERVICE_PROVIDER");  //合作合伙
         ObjectMapper objectMapper = new ObjectMapper();
         String receiverMapJson = objectMapper.writeValueAsString(receiverMap);
@@ -126,6 +133,8 @@ public class WxOrderServiceImpl extends ServiceImpl<WxOrderMapper, WxOrder> impl
     }
 
 
+
+
     /**
      * @return
      * @throws Exception
@@ -133,6 +142,8 @@ public class WxOrderServiceImpl extends ServiceImpl<WxOrderMapper, WxOrder> impl
     @Override
     @Transactional
     public boolean wxProfitsharing(WxOrder wxOrder) throws Exception {
+        //查询上级
+        String account = getAccount(wxOrder);
         String url = "https://api.mch.weixin.qq.com/secapi/pay/profitsharing";
         String nonceStr = RandomStringUtils.randomAlphanumeric(32);
         //用于签名
@@ -145,6 +156,7 @@ public class WxOrderServiceImpl extends ServiceImpl<WxOrderMapper, WxOrder> impl
         String orderCode = getOrderCode(wxOrder.getUserId()); //商户分账单号
         sortMap.put("out_order_no", orderCode);
 
+
         //扣除手续费后
         double commission = wxOrder.getTotal() * 0.01;
         double total = wxOrder.getTotal() - commission;
@@ -152,7 +164,13 @@ public class WxOrderServiceImpl extends ServiceImpl<WxOrderMapper, WxOrder> impl
 
         Map<String, Object> receiverMap = new TreeMap<String, Object>();
         receiverMap.put("type", "PERSONAL_OPENID");
-        receiverMap.put("account", "o7N3j5XxUrgOKE4ulP-HseFpBCiM");
+
+        if(StringUtils.isNotEmpty(account)) {  //如果存在上级,分给上给
+            receiverMap.put("account", account);
+        } else {                               //如果不存在上级,分给固定的
+            receiverMap.put("account", "o7N3j5XxUrgOKE4ulP-HseFpBCiM");
+        }
+
         receiverMap.put("amount", amount);
         receiverMap.put("description", "分到个人");  //合作合伙
         ObjectMapper objectMapper = new ObjectMapper();
@@ -166,6 +184,7 @@ public class WxOrderServiceImpl extends ServiceImpl<WxOrderMapper, WxOrder> impl
         WXPay pay = new WXPay(config);
         String xmlStr = pay.requestWithCert(url, sortMap, config.getHttpConnectTimeoutMs(), config.getHttpReadTimeoutMs());
         Map<String, String> resMap = XmlUtil.xmlToMap(xmlStr);
+        System.out.println(resMap);
         if (!"SUCCESS".equals(resMap.get("return_code")) || !"SUCCESS".equals(resMap.get("result_code"))) {
             //throw new CustomException("微信分账出错");
             log.info("微信分账出错");
@@ -178,7 +197,7 @@ public class WxOrderServiceImpl extends ServiceImpl<WxOrderMapper, WxOrder> impl
         OrderSplit orderSplit = new OrderSplit();
         orderSplit.setOutSplitNo(orderCode);
         orderSplit.setAmount(amount);
-      //  orderSplit.setOpenId("o7N3j5XxUrgOKE4ulP-HseFpBCiM");
+        //  orderSplit.setOpenId("o7N3j5XxUrgOKE4ulP-HseFpBCiM");
         orderSplit.setOpenId("obYLX6C7ln62silx-KP73Nd9jUB0");
         orderSplit.setDescription("分到个人");
         return orderSplitService.save(orderSplit);
@@ -306,7 +325,7 @@ public class WxOrderServiceImpl extends ServiceImpl<WxOrderMapper, WxOrder> impl
         // 获取当前时间
         Date currentDate = new Date();
         // 添加5分钟
-        long fiveMinutesInMillis = 5 * 60 * 1000; // 5分钟的毫秒数
+        long fiveMinutesInMillis = 1 * 60 * 1000; // 5分钟的毫秒数
         Date nowDate = new Date(currentDate.getTime() - fiveMinutesInMillis);
 
         WxOrderProfitDTO wxOrderProfitDTO = new WxOrderProfitDTO();
@@ -319,4 +338,29 @@ public class WxOrderServiceImpl extends ServiceImpl<WxOrderMapper, WxOrder> impl
     }
 
 
+    /**
+     * 查询出上级unionId
+     * @param wxOrder
+     * @return
+     */
+    private String getAccount(WxOrder wxOrder) {
+        String account = null;
+        UserInfo userInfo = sysUserService.getStudentByUnionId(wxOrder.getUnionId());
+        UserInfo pUserInfo = null;
+        if (userInfo.getPUnionId() != null) {
+            pUserInfo = sysUserService.getStudentByUnionId(userInfo.getPUnionId());
+        }
+        if (pUserInfo != null) {
+            if (StringUtils.isNotEmpty(pUserInfo.getXcxOpenid())) {
+                account =  pUserInfo.getXcxOpenid();
+            } else if (StringUtils.isNotEmpty(pUserInfo.getGzhOpenid())) {
+                account = pUserInfo.getGzhOpenid();
+            } else if (StringUtils.isNotEmpty(pUserInfo.getAppOpenid())) {
+                account = pUserInfo.getAppOpenid();
+            }
+        }
+        return account;
+    }
+
+
 }

+ 8 - 0
nbjk-service/src/main/resources/mapper/wx/WxOrderMapper.xml

@@ -136,4 +136,12 @@
     </select>
 
 
+    <select id="selectWxOrderProfitList" resultType="com.miaxis.wx.domain.WxOrder">
+        select * from wx_order where trade_state ='SUCCESS'
+        <if test="nowDate != null"><!-- 开始时间检索 -->
+            and profit_sharing = 1
+        </if>
+        order by success_time
+    </select>
+
 </mapper>

+ 1 - 2
nbjk-system/src/main/java/com/miaxis/system/service/ISysUserService.java

@@ -188,11 +188,10 @@ public interface ISysUserService
 
     SysUser selectUserByReferralCode(String referralCode);
 
-    UserInfo getStudentByUnionId(String identification);
+    UserInfo getStudentByUnionId(String unionId);
 
     void recharge(Integer rechargeNumber,Long userId);
 
     Map getAgentRemainingNumber();
 
-    UserInfo getStudentByUnionid(String unionId);
 }

+ 0 - 4
nbjk-system/src/main/java/com/miaxis/system/service/impl/SysUserServiceImpl.java

@@ -509,8 +509,4 @@ public class SysUserServiceImpl implements ISysUserService
         return dataMap;
     }
 
-    @Override
-    public UserInfo getStudentByUnionid(String unionId) {
-        return userMapper.getStudentByUnionid(unionId);
-    }
 }

+ 1 - 4
nbjk-system/src/main/resources/mapper/system/SysUserMapper.xml

@@ -244,9 +244,6 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  			#{userId}
         </foreach>
  	</delete>
-	<select id="getStudentByUnionid" parameterType="String" resultType="com.miaxis.common.core.domain.entity.UserInfo">
-		select * from user_info
-		where union_id = #{unionId}
-	</select>
+
 
 </mapper>