From 093b73aadb022895a02dbbcd64b87613e19b547b Mon Sep 17 00:00:00 2001
From: 1098226878@qq.com <1098226878@qq.com>
Date: 星期一, 10 一月 2022 18:05:21 +0800
Subject: [PATCH] Merge branch 'master' of http://git.homecommunity.cn/supervip/MicroCommunity

---
 service-fee/src/main/java/com/java110/fee/cmd/fee/PayFeePreCmd.java |  127 +++++++++++++++++++++++++++++++++---------
 1 files changed, 99 insertions(+), 28 deletions(-)

diff --git a/service-fee/src/main/java/com/java110/fee/cmd/fee/PayFeePreCmd.java b/service-fee/src/main/java/com/java110/fee/cmd/fee/PayFeePreCmd.java
index 1443d4c..f0e1ce1 100644
--- a/service-fee/src/main/java/com/java110/fee/cmd/fee/PayFeePreCmd.java
+++ b/service-fee/src/main/java/com/java110/fee/cmd/fee/PayFeePreCmd.java
@@ -8,10 +8,12 @@
 import com.java110.core.event.cmd.CmdEvent;
 import com.java110.core.factory.GenerateCodeFactory;
 import com.java110.core.smo.IComputeFeeSMO;
+import com.java110.dto.account.AccountDto;
 import com.java110.dto.couponUser.CouponUserDto;
 import com.java110.dto.fee.FeeDetailDto;
 import com.java110.dto.fee.FeeDto;
 import com.java110.dto.feeDiscount.ComputeDiscountDto;
+import com.java110.intf.acct.IAccountInnerServiceSMO;
 import com.java110.intf.acct.ICouponUserV1InnerServiceSMO;
 import com.java110.intf.community.IRepairUserInnerServiceSMO;
 import com.java110.intf.community.IRoomInnerServiceSMO;
@@ -22,6 +24,7 @@
 import com.java110.utils.exception.CmdException;
 import com.java110.utils.exception.ListenerExecuteException;
 import com.java110.utils.util.Assert;
+import com.java110.utils.util.BeanConvertUtil;
 import com.java110.utils.util.DateUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -46,7 +49,7 @@
  * 娓╅Θ鎻愮ず锛氬鏋滄偍瀵规鏂囦欢杩涜淇敼 璇蜂笉瑕佸垹闄ゅ師鏈変綔鑰呭強娉ㄩ噴淇℃伅锛岃琛ュ厖鎮ㄧ殑 淇敼鐨勫師鍥犱互鍙婅仈绯婚偖绠卞涓�
  * // modify by 寮犱笁 at 2021-09-12 绗�10琛屽湪鏌愮鍦烘櫙涓嬪瓨鍦ㄦ煇绉峛ug 闇�瑕佷慨澶嶏紝娉ㄩ噴10鑷�20琛� 鍔犲叆 20琛岃嚦30琛�
  */
-@Java110Cmd(serviceCode = "fee.payFeePreNew")
+@Java110Cmd(serviceCode = "fee.payFeePre")
 public class PayFeePreCmd extends AbstractServiceCmdListener {
     private static Logger logger = LoggerFactory.getLogger(PayFeePreCmd.class);
 
@@ -83,6 +86,9 @@
     @Autowired
     private ICouponUserV1InnerServiceSMO couponUserV1InnerServiceSMOImpl;
 
+    @Autowired
+    private IAccountInnerServiceSMO accountInnerServiceSMOImpl;
+
     @Override
     public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) {
         Assert.jsonObjectHaveKey(reqJson, "communityId", "璇锋眰鎶ユ枃涓湭鍖呭惈communityId鑺傜偣");
@@ -102,16 +108,9 @@
     public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {
         logger.debug("ServiceDataFlowEvent : {}", event);
 
-        JSONObject paramObj = reqJson;
-
-        JSONArray businesses = new JSONArray();
-        //鍒ゆ柇鏄惁鏈夋姌鎵f儏鍐�
-        judgeDiscount(paramObj);
-        //3.0 鑰冭檻浼樻儬鍗�
-        checkCouponUser(paramObj);
-
         String appId = cmdDataFlowContext.getReqHeaders().get("app-id");
         reqJson.put("appId", appId);
+
         FeeDto feeDto = new FeeDto();
         feeDto.setFeeId(reqJson.getString("feeId"));
         feeDto.setCommunityId(reqJson.getString("communityId"));
@@ -120,40 +119,110 @@
             throw new ListenerExecuteException(ResponseConstant.RESULT_CODE_ERROR, "鏌ヨ璐圭敤淇℃伅澶辫触锛屾湭鏌ュ埌鏁版嵁鎴栨煡鍒板鏉℃暟鎹�");
         }
         feeDto = feeDtos.get(0);
-
+        reqJson.put("feeTypeCd", feeDto.getFeeTypeCd());
+        reqJson.put("feeId", feeDto.getFeeId());
         Map feePriceAll = computeFeeSMOImpl.getFeePrice(feeDto);
-
         BigDecimal receivableAmount = new BigDecimal(feePriceAll.get("feePrice").toString());
         BigDecimal cycles = new BigDecimal(Double.parseDouble(reqJson.getString("cycles")));
         double tmpReceivableAmount = cycles.multiply(receivableAmount).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
-        double discountPrice = reqJson.getDouble("discountPrice");
-        double couponPrice = reqJson.getDouble("couponPrice");
         JSONObject paramOut = new JSONObject();
         paramOut.put("receivableAmount", tmpReceivableAmount);
         paramOut.put("oId", GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_oId));
-        //1.0 鑰冭檻浼樻儬閲戦
+
+        //瀹炴敹閲戦
+        BigDecimal tmpReceivedAmout = new BigDecimal(tmpReceivableAmount);
+
+        //鍒ゆ柇鏄惁鏈夋姌鎵f儏鍐�
+        double discountPrice = judgeDiscount(reqJson);
+        tmpReceivedAmout = tmpReceivedAmout.subtract(new BigDecimal(discountPrice)).setScale(2, BigDecimal.ROUND_HALF_EVEN);
         //2.0 鑰冭檻璐︽埛鎶垫秷
+        double accountPrice = judgeAccount(reqJson);
+        tmpReceivedAmout = tmpReceivedAmout.subtract(new BigDecimal(accountPrice)).setScale(2, BigDecimal.ROUND_HALF_EVEN);
+
         //3.0 鑰冭檻浼樻儬鍗�
-        BigDecimal tmpReceivedAmout = new BigDecimal(tmpReceivableAmount).subtract(new BigDecimal(discountPrice)).setScale(2, BigDecimal.ROUND_HALF_EVEN);
-        double receivedAmount = tmpReceivedAmout.subtract(new BigDecimal(couponPrice)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
+        double couponPrice = checkCouponUser(reqJson);
+        tmpReceivedAmout = tmpReceivedAmout.subtract(new BigDecimal(couponPrice)).setScale(2, BigDecimal.ROUND_HALF_EVEN);
+
+
+        double receivedAmount = tmpReceivedAmout.doubleValue();
         //鎵�鏈� 浼樻儬鎶樻墸璁$畻瀹屽悗锛屽鏋滄�婚噾棰濆皬浜庣瓑浜�0锛屽垯杩斿洖鎬绘墸娆句负0
         if (receivedAmount <= 0) {
             receivedAmount = 0.0;
         }
         paramOut.put("receivedAmount", receivedAmount);
+
         ResponseEntity<String> responseEntity = new ResponseEntity<>(paramOut.toJSONString(), HttpStatus.OK);
+        reqJson.putAll(paramOut);
         CommonCache.setValue("payFeePre" + paramOut.getString("oId"), reqJson.toJSONString(), 24 * 60 * 60);
         cmdDataFlowContext.setResponseEntity(responseEntity);
     }
 
-    private void checkCouponUser(JSONObject paramObj) {
+    /**
+     * 鑰冭檻璐︽埛鎶垫秷
+     *
+     * @param reqJson
+     */
+    private double judgeAccount(JSONObject reqJson) {
+        if (!reqJson.containsKey("deductionAmount")) {
+            reqJson.put("deductionAmount", 0.0);
+            return 0.0;
+        }
+
+        double deductionAmount = reqJson.getDouble("deductionAmount");
+        if (deductionAmount <= 0) {
+            reqJson.put("deductionAmount", 0.0);
+            return 0.0;
+        }
+
+        if (!reqJson.containsKey("selectUserAccount")) {
+            reqJson.put("deductionAmount", 0.0);
+            return 0.0;
+        }
+
+        JSONArray selectUserAccount = reqJson.getJSONArray("selectUserAccount");
+        if (selectUserAccount == null || selectUserAccount.size() < 1) {
+            reqJson.put("deductionAmount", 0.0);
+            return 0.0;
+        }
+        List<String> acctIds = new ArrayList<>();
+        for (int userAccountIndex = 0; userAccountIndex < selectUserAccount.size(); userAccountIndex++) {
+            acctIds.add(selectUserAccount.getJSONObject(userAccountIndex).getString("acctId"));
+        }
+
+        AccountDto accountDto = new AccountDto();
+        accountDto.setAcctIds(acctIds.toArray(new String[acctIds.size()]));
+        List<AccountDto> accountDtos = accountInnerServiceSMOImpl.queryAccounts(accountDto);
+
+        if (accountDtos == null || accountDtos.size() < 1) {
+            reqJson.put("deductionAmount", 0.0);
+            return 0.0;
+        }
+
+        BigDecimal totalAccountAmount = new BigDecimal(0);
+        for (AccountDto tmpAccountDto : accountDtos) {
+            totalAccountAmount = totalAccountAmount.add(new BigDecimal(tmpAccountDto.getAmount()));
+        }
+
+        deductionAmount = totalAccountAmount.subtract(new BigDecimal(deductionAmount)).doubleValue();
+        if (deductionAmount < 0) {
+            reqJson.put("deductionAmount", totalAccountAmount.doubleValue());
+            reqJson.put("selectUserAccount", BeanConvertUtil.beanCovertJSONArray(accountDtos));
+            return totalAccountAmount.doubleValue();
+        }
+        reqJson.put("deductionAmount", deductionAmount);
+        reqJson.put("selectUserAccount", BeanConvertUtil.beanCovertJSONArray(accountDtos));
+        return deductionAmount;
+    }
+
+    private double checkCouponUser(JSONObject paramObj) {
         JSONArray couponList = paramObj.getJSONArray("couponList");
         BigDecimal couponPrice = new BigDecimal(0.0);
         List<String> couponIds = new ArrayList<String>();
 
         if (couponList == null || couponList.size() < 1) {
-            paramObj.put("couponPrice", couponPrice);
-            return;
+            paramObj.put("couponPrice", couponPrice.doubleValue());
+            paramObj.put("couponUserDtos", new JSONArray()); //杩欓噷鑰冭檻绌�
+            return couponPrice.doubleValue();
         }
         for (int couponIndex = 0; couponIndex < couponList.size(); couponIndex++) {
             couponIds.add(couponList.getJSONObject(couponIndex).getString("couponId"));
@@ -162,21 +231,22 @@
         couponUserDto.setCouponIds(couponIds.toArray(new String[couponIds.size()]));
         List<CouponUserDto> couponUserDtos = couponUserV1InnerServiceSMOImpl.queryCouponUsers(couponUserDto);
         if (couponUserDtos == null || couponUserDtos.size() < 1) {
-            paramObj.put("couponPrice", couponPrice);
-            return;
+            paramObj.put("couponPrice", couponPrice.doubleValue());
+            return couponPrice.doubleValue();
         }
         for (CouponUserDto couponUser : couponUserDtos) {
             //涓嶈绠楀凡杩囨湡璐墿鍒搁噾棰�
             if (couponUser.getEndTime().compareTo(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B)) >= 0) {
-                couponPrice.add(new BigDecimal(Double.parseDouble(couponUser.getActualPrice())));
+                couponPrice = couponPrice.add(new BigDecimal(Double.parseDouble(couponUser.getActualPrice())));
             }
         }
-        paramObj.put("couponPrice", couponPrice);
-        paramObj.put("couponUserDtos", couponUserDtos);
+        paramObj.put("couponPrice", couponPrice.doubleValue());
+        paramObj.put("couponUserDtos", BeanConvertUtil.beanCovertJSONArray(couponUserDtos));
+        return couponPrice.doubleValue();
     }
 
 
-    private void judgeDiscount(JSONObject paramObj) {
+    private double judgeDiscount(JSONObject paramObj) {
         FeeDetailDto feeDetailDto = new FeeDetailDto();
         feeDetailDto.setCommunityId(paramObj.getString("communityId"));
         feeDetailDto.setFeeId(paramObj.getString("feeId"));
@@ -197,13 +267,14 @@
 
         if (computeDiscountDtos == null || computeDiscountDtos.size() < 1) {
             paramObj.put("discountPrice", 0.0);
-            return;
+            return 0.0;
         }
         BigDecimal discountPrice = new BigDecimal(0);
         for (ComputeDiscountDto computeDiscountDto : computeDiscountDtos) {
             discountPrice = discountPrice.add(new BigDecimal(computeDiscountDto.getDiscountPrice()));
         }
-        paramObj.put("discountPrice", discountPrice);
-        paramObj.put("computeDiscountDtos", computeDiscountDtos);
+        paramObj.put("discountPrice", discountPrice.doubleValue());
+        paramObj.put("computeDiscountDtos", BeanConvertUtil.beanCovertJSONArray(computeDiscountDtos));
+        return discountPrice.doubleValue();
     }
 }

--
Gitblit v1.8.0