Parcourir la source

优化手机缴费流程同时支持 账户扣款 优惠券抵消 和 优惠折扣

wuxw il y a 4 ans
Parent
commit
e2600948d7

+ 9 - 0
java110-bean/src/main/java/com/java110/dto/account/AccountDto.java

@@ -36,6 +36,7 @@ public class AccountDto extends PageDto implements Serializable {
     private String objId;
     private String [] objIds;
     private String acctId;
+    private String [] acctIds;
     private String acctName;
     private String objType;
     private String partId;
@@ -160,4 +161,12 @@ public class AccountDto extends PageDto implements Serializable {
     public void setObjIds(String[] objIds) {
         this.objIds = objIds;
     }
+
+    public String[] getAcctIds() {
+        return acctIds;
+    }
+
+    public void setAcctIds(String[] acctIds) {
+        this.acctIds = acctIds;
+    }
 }

+ 12 - 0
java110-db/src/main/resources/mapper/acct/AccountServiceDaoImplMapper.xml

@@ -119,6 +119,12 @@
                 #{item}
             </foreach>
         </if>
+        <if test="acctIds !=null ">
+            and t.acct_id in
+            <foreach collection="acctIds" item="item" open="(" close=")" separator=",">
+                #{item}
+            </foreach>
+        </if>
         <if test="acctId !=null and acctId != ''">
             and t.acct_id= #{acctId}
         </if>
@@ -196,6 +202,12 @@
         <if test="objId !=null and objId != ''">
             and t.obj_id= #{objId}
         </if>
+        <if test="acctIds !=null ">
+            and t.acct_id in
+            <foreach collection="acctIds" item="item" open="(" close=")" separator=",">
+                #{item}
+            </foreach>
+        </if>
         <if test="acctId !=null and acctId != ''">
             and t.acct_id= #{acctId}
         </if>

+ 2 - 2
java110-db/src/main/resources/mapper/fee/PayFeeDetailDiscountNewV1ServiceDaoImplMapper.xml

@@ -11,9 +11,9 @@
     <!-- 保存费用明细信息 add by wuxw 2018-07-03 -->
     <insert id="savePayFeeDetailDiscountNewInfo" parameterType="Map">
         insert into pay_fee_detail_discount(
-detail_discount_id,discount_price,detail_id,remark,community_id,discount_id,fee_id
+detail_discount_id,discount_price,detail_id,remark,community_id,discount_id,fee_id,b_id
 ) values (
-#{detailDiscountId},#{discountPrice},#{detailId},#{remark},#{communityId},#{discountId},#{feeId}
+#{detailDiscountId},#{discountPrice},#{detailId},#{remark},#{communityId},#{discountId},#{feeId},#{bId}
 )
     </insert>
 

+ 1 - 1
java110-db/src/main/resources/mapper/fee/PayFeeDetailNewV1ServiceDaoImplMapper.xml

@@ -10,7 +10,7 @@
         insert into pay_fee_detail(
         prime_rate,detail_id,receivable_amount,cycles,remark,start_time,received_amount,state,end_time,community_id,fee_id,b_id
         ) values (
-        #{primeRate},#{detailId},#{receivableAmount},#{cycles},#{remark},#{startTime},#{receivedAmount},#{state},#{endTime},#{communityId},#{feeId}#{bId}
+        #{primeRate},#{detailId},#{receivableAmount},#{cycles},#{remark},#{startTime},#{receivedAmount},#{state},#{endTime},#{communityId},#{feeId},#{bId}
         )
     </insert>
 

+ 1 - 1
service-acct/src/main/java/com/java110/acct/dao/impl/AccountServiceDaoImpl.java

@@ -148,7 +148,7 @@ public class AccountServiceDaoImpl extends BaseServiceDao implements IAccountSer
     public void saveAccount(Map info) {
         logger.debug("查询账户数据 入参 info : {}", info);
 
-         sqlSessionTemplate.update("accountServiceDaoImpl.saveAccount", info);
+        sqlSessionTemplate.update("accountServiceDaoImpl.saveAccount", info);
 
     }
 

+ 66 - 8
service-fee/src/main/java/com/java110/fee/cmd/fee/PayFeeConfirmCmd.java

@@ -8,6 +8,7 @@ import com.java110.core.context.ICmdDataFlowContext;
 import com.java110.core.event.cmd.AbstractServiceCmdListener;
 import com.java110.core.event.cmd.CmdEvent;
 import com.java110.core.factory.GenerateCodeFactory;
+import com.java110.dto.account.AccountDto;
 import com.java110.dto.app.AppDto;
 import com.java110.dto.couponUser.CouponUserDto;
 import com.java110.dto.fee.FeeAttrDto;
@@ -20,12 +21,14 @@ import com.java110.dto.repair.RepairDto;
 import com.java110.dto.repair.RepairUserDto;
 import com.java110.entity.order.Orders;
 import com.java110.fee.bmo.fee.IFeeBMO;
+import com.java110.intf.acct.IAccountInnerServiceSMO;
 import com.java110.intf.acct.ICouponUserDetailV1InnerServiceSMO;
 import com.java110.intf.acct.ICouponUserV1InnerServiceSMO;
 import com.java110.intf.community.IRepairUserInnerServiceSMO;
 import com.java110.intf.community.IRoomInnerServiceSMO;
 import com.java110.intf.fee.*;
 import com.java110.intf.user.IOwnerCarInnerServiceSMO;
+import com.java110.po.accountDetail.AccountDetailPo;
 import com.java110.po.applyRoomDiscount.ApplyRoomDiscountPo;
 import com.java110.po.car.OwnerCarPo;
 import com.java110.po.couponUser.CouponUserPo;
@@ -99,6 +102,9 @@ public class PayFeeConfirmCmd extends AbstractServiceCmdListener {
     @Autowired
     private ICouponUserDetailV1InnerServiceSMO couponUserDetailV1InnerServiceSMOImpl;
 
+    @Autowired
+    private IAccountInnerServiceSMO accountInnerServiceSMOImpl;
+
     //默认序列
     protected static final int DEFAULT_SEQ = 1;
 
@@ -125,20 +131,21 @@ public class PayFeeConfirmCmd extends AbstractServiceCmdListener {
             paramObj.put("remark", "线上小程序支付");
         }
 
+        //处理现金账户
+        dealAccount(paramObj);
+
+        //处理 优惠折扣
+        addDiscount(paramObj);
+
+        //修改已使用优惠卷信息
+        modifyCouponUser(paramObj);
+
         //添加单元信息
         feeBMOImpl.addFeePreDetail(paramObj);
         feeBMOImpl.modifyPreFee(paramObj);
 
-        double discountPrice = paramObj.getDouble("discountPrice");
-        if (discountPrice > 0) {
-            addDiscount(paramObj);
-        }
-
         dealOwnerCartEndTime(paramObj);
 
-        //修改已使用优惠卷信息
-        modifyCouponUser(paramObj);
-
         //判断是否有派单属性ID
         FeeAttrDto feeAttrDto = new FeeAttrDto();
         feeAttrDto.setCommunityId(paramObj.getString("communityId"));
@@ -228,10 +235,57 @@ public class PayFeeConfirmCmd extends AbstractServiceCmdListener {
         cmdDataFlowContext.setResponseEntity(ResultVo.success());
     }
 
+    private void dealAccount(JSONObject paramObj) {
+
+        if(!paramObj.containsKey("deductionAmount") || paramObj.getDouble("deductionAmount") <=0){
+            return ;
+        }
+
+        BigDecimal deductionAmount = new BigDecimal(paramObj.getDouble("deductionAmount"));
+
+        List<AccountDto> accountDtos = (List<AccountDto>) paramObj.get("selectUserAccount");
+        BigDecimal amount = null;
+        for(AccountDto accountDto : accountDtos){
+
+            amount = new BigDecimal(Double.parseDouble(accountDto.getAmount()));
+            AccountDetailPo accountDetailPo = new AccountDetailPo();
+            accountDetailPo.setAcctId(accountDto.getAcctId());
+            accountDetailPo.setObjId(accountDto.getObjId());
+            if(amount.doubleValue()< deductionAmount.doubleValue()){
+                accountDetailPo.setAmount(amount.doubleValue()+"");
+                deductionAmount = deductionAmount.subtract(amount).setScale(2,BigDecimal.ROUND_HALF_UP);
+            }else{
+                accountDetailPo.setAmount(deductionAmount.doubleValue()+"");
+                deductionAmount = deductionAmount.subtract(deductionAmount).setScale(2,BigDecimal.ROUND_HALF_UP);
+            }
+            int flag = accountInnerServiceSMOImpl.withholdAccount(accountDetailPo);
+
+            if(flag < 1){
+                throw  new CmdException("扣款失败");
+            }
+        }
+
+        if(deductionAmount.doubleValue()>0){
+            throw new CmdException("账户金额不足");
+        }
+
+    }
+
     private void modifyCouponUser(JSONObject paramObj) {
+        if(!paramObj.containsKey("couponPrice") || paramObj.getDouble("couponPrice")<=0){
+            return ;
+        }
         FeeDto feeInfo = (FeeDto) paramObj.get("feeInfo");
         List<CouponUserDto> couponUserDtos = (List<CouponUserDto>) paramObj.get("couponUserDtos");
+        CouponUserDto couponUserDto = null;
         for (CouponUserDto couponUser : couponUserDtos) {
+            couponUserDto = new CouponUserDto();
+            couponUserDto.setCouponId(couponUser.getCouponId());
+            couponUserDto.setState(CouponUserDto.COUPON_STATE_RUN);
+            List<CouponUserDto> couponUserDtos1 = couponUserV1InnerServiceSMOImpl.queryCouponUsers(couponUserDto);
+            if(couponUserDtos1==null || couponUserDtos1.size()<1){
+                throw new CmdException("优惠券被使用");
+            }
             CouponUserPo couponUserPo = new CouponUserPo();
             couponUserPo.setState(CouponUserDto.COUPON_STATE_STOP);
             couponUserPo.setCouponId(couponUser.getCouponId());
@@ -257,6 +311,10 @@ public class PayFeeConfirmCmd extends AbstractServiceCmdListener {
     }
 
     private void addDiscount(JSONObject paramObj) {
+
+        if (!paramObj.containsKey("discountPrice") || paramObj.getDouble("discountPrice") <=0) {
+            return ;
+        }
         List<ComputeDiscountDto> computeDiscountDtos = (List<ComputeDiscountDto>) paramObj.get("computeDiscountDtos");
         for (ComputeDiscountDto computeDiscountDto : computeDiscountDtos) {
             if (computeDiscountDto.getDiscountPrice() <= 0) {

+ 82 - 26
service-fee/src/main/java/com/java110/fee/cmd/fee/PayFeePreCmd.java

@@ -8,10 +8,12 @@ import com.java110.core.event.cmd.AbstractServiceCmdListener;
 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;
@@ -83,6 +85,9 @@ public class PayFeePreCmd extends AbstractServiceCmdListener {
     @Autowired
     private ICouponUserV1InnerServiceSMO couponUserV1InnerServiceSMOImpl;
 
+    @Autowired
+    private IAccountInnerServiceSMO accountInnerServiceSMOImpl;
+
     @Override
     public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) {
         Assert.jsonObjectHaveKey(reqJson, "communityId", "请求报文中未包含communityId节点");
@@ -102,17 +107,9 @@ public class PayFeePreCmd extends AbstractServiceCmdListener {
     public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {
         logger.debug("ServiceDataFlowEvent : {}", event);
 
-
-        //判断是否有折扣情况
-        judgeDiscount(reqJson);
-        //2.0 考虑账户抵消
-        judgeAccount(reqJson);
-        //3.0 考虑优惠卷
-        checkCouponUser(reqJson);
-
-
         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"));
@@ -123,20 +120,29 @@ public class PayFeePreCmd extends AbstractServiceCmdListener {
         feeDto = feeDtos.get(0);
 
         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);
+
+        //判断是否有折扣情况
+        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;
@@ -151,19 +157,67 @@ public class PayFeePreCmd extends AbstractServiceCmdListener {
      *  考虑账户抵消
      * @param reqJson
      */
-    private void judgeAccount(JSONObject 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",accountDtos);
+            return totalAccountAmount.doubleValue();
+        }
+        reqJson.put("deductionAmount",deductionAmount);
+        reqJson.put("selectUserAccount",accountDtos);
+        return deductionAmount;
     }
 
-    private void checkCouponUser(JSONObject paramObj) {
+    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);
+            paramObj.put("couponPrice", couponPrice.doubleValue());
             paramObj.put("couponUserDtos", new ArrayList<CouponUserDto>()); //这里考虑空
-            return;
+            return couponPrice.doubleValue();
         }
         for (int couponIndex = 0; couponIndex < couponList.size(); couponIndex++) {
             couponIds.add(couponList.getJSONObject(couponIndex).getString("couponId"));
@@ -172,8 +226,8 @@ public class PayFeePreCmd extends AbstractServiceCmdListener {
         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) {
             //不计算已过期购物券金额
@@ -181,12 +235,13 @@ public class PayFeePreCmd extends AbstractServiceCmdListener {
                 couponPrice.add(new BigDecimal(Double.parseDouble(couponUser.getActualPrice())));
             }
         }
-        paramObj.put("couponPrice", couponPrice);
+        paramObj.put("couponPrice", couponPrice.doubleValue());
         paramObj.put("couponUserDtos", 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"));
@@ -206,14 +261,15 @@ public class PayFeePreCmd extends AbstractServiceCmdListener {
         List<ComputeDiscountDto> computeDiscountDtos = feeDiscountInnerServiceSMOImpl.computeDiscount(feeDetailDto);
 
         if (computeDiscountDtos == null || computeDiscountDtos.size() < 1) {
-            paramObj.put("discountPrice", 0.0);
-            return;
+            paramObj.put("discountPrice",0.0);
+            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("discountPrice",discountPrice.doubleValue());
         paramObj.put("computeDiscountDtos", computeDiscountDtos);
+        return discountPrice.doubleValue();
     }
 }