From 949373691bb795099503ec2d9a9d8cffd8d2669c Mon Sep 17 00:00:00 2001
From: chenjiahe <763432473@qq.com>
Date: 星期五, 16 六月 2023 19:24:16 +0800
Subject: [PATCH] 卡项退款-提交

---
 phi_platform_user/src/main/java/com/hx/phip/tool/refund/PartialRefundUtil.java             |   27 ++
 phi_platform_user/src/main/java/com/hx/phip/service/order/impl/OrderRefundServiceImpl.java |    6 
 phi_platform_common/src/main/java/com/hx/phip/tool/refund/RefundTool.java                  |  383 ++++++++++++++++++++++++++++++++++++------
 phi_platform_common/src/main/java/com/hx/phip/vo/order/refund/RefundCardVo.java            |   25 ++
 phi_platform_common/src/main/java/com/hx/phip/tool/user/UserCardTool.java                  |   42 ++++
 5 files changed, 423 insertions(+), 60 deletions(-)

diff --git a/phi_platform_common/src/main/java/com/hx/phip/tool/refund/RefundTool.java b/phi_platform_common/src/main/java/com/hx/phip/tool/refund/RefundTool.java
index e6bc4a2..f061239 100644
--- a/phi_platform_common/src/main/java/com/hx/phip/tool/refund/RefundTool.java
+++ b/phi_platform_common/src/main/java/com/hx/phip/tool/refund/RefundTool.java
@@ -5,6 +5,7 @@
 import com.hx.mybatisTool.SqlSentence;
 import com.hx.phiappt.common.OrderItemConstants;
 import com.hx.phiappt.common.RefundStatus;
+import com.hx.phiappt.model.BaseEntity;
 import com.hx.phiappt.model.cardItem.CardEquity;
 import com.hx.phiappt.model.cardItem.CardItem;
 import com.hx.phiappt.model.cardItem.CardItemInfo;
@@ -17,12 +18,14 @@
 import com.hx.phip.tool.user.UserCardTool;
 import com.hx.phip.vo.order.payment.PayMethodVo;
 import com.hx.phip.vo.order.refund.DistributionRedundVo;
+import com.hx.phip.vo.order.refund.RefundCardVo;
 import com.hx.phip.vo.user.UserCardItemInfoVo;
 import com.hx.util.StringUtils;
 import com.hz.his.vo.order.refund.RefundUserCardItemVo;
 import com.hz.his.vo.order.refund.RefundUserCardVo;
 import com.platform.exception.PlatTipsException;
 import com.platform.resultTool.PlatformCode;
+import org.springframework.beans.BeanUtils;
 
 import java.math.BigDecimal;
 import java.math.RoundingMode;
@@ -77,39 +80,42 @@
     }
 
     /**鑾峰彇涓�绾ч��娆惧瓙鍗曞凡閫�閲戦
+     * 涓昏鏌ヨ锛屽崟涓暟閲忓垎閰嶇殑閫�娆鹃噾棰濓紝鍥犱负鐩墠鏄寜鐓ф暟閲忔潵鍒嗛厤閲戦閫�娆剧殑
      * @param orderItemId 涓�绾ч��娆惧瓙鍗曞叧鑱旀爣璇�
      * @param goodsType 鍟嗗搧绫诲瀷
      * @param commonService 鏄犲皠
      * @return 宸查��閲戦
      */
     public static RefundRecordItem getOneAlreadyRefundTotal(String orderItemId,String goodsType, CommonService commonService ){
+
+        //鏌ユ壘姝e父閫�娆剧殑
         SqlSentence sqlSentence = new SqlSentence();
         Map<String,Object> values = new HashMap<>();
-
-        //鑾峰彇宸查��娆剧殑閲戦
+        StringBuilder sql = new StringBuilder();
+        //鑾峰彇宸查��娆剧殑閲戦锛屽墧闄ゆ帀鐢ㄦ埛鍗¢」鐨勯儴鍒嗛��
         values.put("orderItemId",orderItemId);
-        sqlSentence.sqlSentence("SELECT ROUND(SUM(rri.occupyRefundTotal),2) AS occupyRefundTotal,ROUND(SUM(rri.occupyRefundIntegral),2) AS occupyRefundIntegral FROM refund_record_item rri" +
-                " JOIN refund_record rr ON rr.id = rri.refundRecordId AND rr.isDel = 0 AND rr.refundStatus IN(0,2)" +
-                " WHERE rri.isDel = 0 AND rri.orderItemId = #{m.orderItemId} AND rri.userCardId IS NULL",values);
+        sql.append("SELECT ROUND(SUM(rri.occupyRefundTotal),2) AS occupyRefundTotal,ROUND(SUM(rri.occupyRefundIntegral),2) AS occupyRefundIntegral");
+        sql.append(" FROM refund_record_item rri");
+        sql.append(" JOIN refund_record rr ON rr.id = rri.refundRecordId AND rr.isDel = 0 AND rr.refundStatus IN(0,2)");
+        sql.append(" WHERE rri.isDel = 0 AND rri.orderItemId = #{m.orderItemId} AND rri.userCardId IS NULL");
+        sqlSentence.sqlSentence(sql.toString(),values);
         RefundRecordItem refundRecordItem = commonService.selectOne(RefundRecordItemMapper.class,sqlSentence);
-
-        if(refundRecordItem == null || refundRecordItem.getOccupyRefundTotal() == null){
+        if(refundRecordItem == null){
+            //璁剧疆娌℃湁鎵惧埌
+            refundRecordItem.setIsDel(BaseEntity.YES);
             refundRecordItem = new RefundRecordItem();
         }
-        //鐢ㄦ埛鍗″寘閫�娆撅紝绯荤粺閫�娆炬�婚噾棰濓紝鎸夌収閫�娆炬暟閲忔潵绠楃殑锛岃褰曞埌鐢ㄦ埛鍗″寘閲岄潰浜�
+
+        //鐢ㄦ埛鍗″寘閫�娆撅紝绯荤粺閫�娆炬�婚噾棰濓紝鎸夌収閫�娆炬暟閲忔潵绠楃殑锛岀敤鎴峰崱鍖呴儴鍒嗛��锛岃鐗规畩鏌ユ壘
         if(OrderItemConstants.TYPE_CARD.equals(goodsType)){
             //鏌ユ壘鏈夋病鏈夎繖涓崱鍖呴儴鍒嗛��娆剧殑璁板綍
-            values.clear();
-            values.put("orderItemId",orderItemId);
-            sqlSentence.sqlSentence("SELECT uc.* FROM refund_record_item rri" +
-                    " JOIN refund_record rr ON rr.id = rri.refundRecordId" +
-                    " JOIN user_card uc ON uc.id = rri.userCardId" +
-                    " AND rr.isDel = 0 AND rr.refundStatus IN(0,2)" +
-                    " WHERE rri.isDel = 0 AND rri.orderItemId = #{m.orderItemId} AND rri.userCardId IS NOT NULL",values);
-            List<UserCard> userCardList = commonService.selectList(UserCardMapper.class,sqlSentence);
-            for(UserCard userCard:userCardList){
-                refundRecordItem.setOccupyRefundTotal(refundRecordItem.getOccupyRefundTotal().add(userCard.getOccupyRefundTotal()));
-                refundRecordItem.setOccupyRefundIntegral(refundRecordItem.getOccupyRefundIntegral().add(userCard.getOccupyRefundIntegral()));
+            List<RefundRecordItem> refundRecordItemList = findRefundUserCard(orderItemId,null,commonService);
+            if(refundRecordItemList.size() > 0){
+                refundRecordItem.setIsDel(BaseEntity.NO);
+            }
+            for(RefundRecordItem refundRecordItem1:refundRecordItemList){
+                refundRecordItem.setOccupyRefundTotal(refundRecordItem.getOccupyRefundTotal().add(refundRecordItem1.getOccupyRefundTotal()));
+                refundRecordItem.setOccupyRefundIntegral(refundRecordItem.getOccupyRefundIntegral().add(refundRecordItem1.getOccupyRefundIntegral()));
             }
         }
 
@@ -117,22 +123,27 @@
     }
 
     /**鑾峰彇浜岀骇閫�娆惧瓙鍗曞凡閫�閲戦
-     * @param orderItemSonId 浜岀骇閫�娆惧瓙鍗曞叧鑱旀爣璇�
+     * @param orderItemSonId 浜岀骇閫�娆惧瓙鍗曞叧鑱旀爣璇�/鍏宠仈鍗¢」鏉$洰鐨刬d
      * @param commonService 鏄犲皠
      * @return 宸查��閲戦
      */
     public static RefundRecordItemSource getTwoAlreadyRefundTotal(String orderItemSonId, CommonService commonService ){
         SqlSentence sqlSentence = new SqlSentence();
         Map<String,Object> values = new HashMap<>();
+        StringBuilder sql = new StringBuilder();
 
-        //鑾峰彇宸查��娆剧殑閲戦
         values.put("orderItemSonId",orderItemSonId);
-        sqlSentence.sqlSentence("SELECT ROUND(SUM(rris.occupyRefundTotal),2) AS occupyRefundTotal,ROUND(SUM(rris.occupyRefundIntegral),2) AS occupyRefundIntegral FROM refund_record_item_source rris" +
-                " JOIN refund_record rr ON rr.id = rris.refundRecordId AND rr.isDel = 0 AND rr.refundStatus IN(0,2)" +
-                " WHERE rris.isDel = 0 AND rris.orderItemSonId = #{m.orderItemSonId}",values);
+        sql.append("SELECT ROUND(SUM(rris.occupyRefundTotal),2) AS occupyRefundTotal,ROUND(SUM(rris.occupyRefundIntegral),2) AS occupyRefundIntegral");
+        sql.append(" FROM refund_record_item_source rris");
+        sql.append(" JOIN refund_record rr ON rr.id = rris.refundRecordId AND rr.isDel = 0 AND rr.refundStatus IN(0,2)");
+        sql.append(" WHERE rris.isDel = 0 AND rris.orderItemSonId = #{m.orderItemSonId}");
+        //鑾峰彇宸查��娆剧殑閲戦
+        sqlSentence.sqlSentence(sql.toString(),values);
         RefundRecordItemSource refundRecordItemSource = commonService.selectOne(RefundRecordItemSourceMapper.class,sqlSentence);
 
-        if(refundRecordItemSource == null || refundRecordItemSource.getOccupyRefundTotal() == null){
+        if(refundRecordItemSource == null){
+            //璁剧疆鏄病鏈夋壘鍒�
+            refundRecordItemSource.setIsDel(BaseEntity.YES);
             return new RefundRecordItemSource();
         }else{
             return refundRecordItemSource;
@@ -141,9 +152,15 @@
 
     /**璁$畻鐢ㄦ埛鍗″寘鏉$洰閫�娆鹃噾棰�*/
     public static void userCardRefundCount(UserCard userCard,RefundUserCardVo refundUserCardVo, CommonService commonService, RefundMapper refundMapper){
+
         ////绠楀嚭閲戦锛�
-        //1銆佺畻鍑衡�滃崟娆℃姷鎵i噾棰濃�濓紝鍐嶄箻浠ユ暟閲忓緱鍑衡�滄姷鎵f�婚噾棰濃��
-        //2銆佺敤鈥滄姷鎵f�婚噾棰濃�濈畻鍑哄崰鐢ㄦ暣涓崱椤规�婚噾棰濈殑鈥滄瘮渚嬧��
+        //1銆佺畻鍑鸿鍗″彲閫�鎬婚噾棰�
+        //2銆佺畻鍑哄崱椤规潯鐩�婚噾棰濓紝灏辨槸鍗犺鍗曟敮浠橀噾棰濈殑閽�
+        //3銆佺畻鍑哄崟鏉$洰鐨勬潈鐩婂崰鐢ㄩ噾棰�
+        //4銆佽幏鍙栬鏉$洰宸查��娆鹃噾棰�
+        //5銆佺畻鍑衡�滃崟娆℃姷鎵i噾棰濃�濓紝鍐嶄箻浠ユ暟閲忓緱鍑衡�滄姷鎵f�婚噾棰濃��
+        //6銆佺敤鈥滄姷鎵f�婚噾棰濃�濈畻鍑哄崰鐢ㄦ暣涓崱椤规�婚噾棰濈殑鈥滄瘮渚嬧��
+        //7銆佹瘮杈冮噾棰濓紝寰楀嚭鍙��閲戦锛屾敞鎰忔渶鍚庝竴娆¤鍏ㄩ儴閫�鎺�
 
         //鑾峰彇鍗¢」
         CardItem cardItem = commonService.selectOneByKey(CardItemMapper.class,userCard.getCardItemId());
@@ -157,37 +174,28 @@
             throw new TipsException("鑾峰彇璁㈠崟瀛愬崟澶辫触[210]锛�");
         }
 
-        //鏁翠釜鐢ㄦ埛鍗″寘鍙��娆剧粨鏋勪綋
-        DistributionRedundVo distributionRedundVo;
+        //鍏敤瀵硅薄
+        RefundRecordItemSource itemSource;
 
-        SqlSentence sqlSentence = new SqlSentence();
-        Map<String,Object> values = new HashMap<>();
-
-        //--------鑾峰彇鐢ㄦ埛鍗″寘鐨勬�婚噾棰�
-        //鏌ユ壘鏈夋病鏈夎繖涓崱鍖呴儴鍒嗛��娆剧殑璁板綍
-        values.clear();
-        values.put("orderItemId",orderItem.getId());
-        values.put("userCardId",userCard.getId());
-        sqlSentence.sqlSentence("SELECT uc.* FROM refund_record_item rri" +
-                " JOIN refund_record rr ON rr.id = rri.refundRecordId" +
-                " JOIN user_card uc ON uc.id = rri.userCardId" +
-                " AND rr.isDel = 0 AND rr.refundStatus IN(0,2)" +
-                " WHERE rri.isDel = 0 AND rri.orderItemId = #{m.orderItemId} AND rri.userCardId IS NOT NULL LIMIT 1",values);
-        UserCard oldUserCard = commonService.selectOne(UserCardMapper.class,sqlSentence);
-        if(oldUserCard == null){
-            //鐢ㄦ埛鍗″寘娌℃湁閫�杩囨锛岄偅涔堝氨鎸夌収姝e父鐨勬祦绋嬭幏鍙栧彲閫�娆鹃噾棰�
-            distributionRedundVo = UserCardTool.userCardPay(userCard,orderItem,commonService,refundMapper);
-        }else{
-            distributionRedundVo = new DistributionRedundVo();
-            distributionRedundVo.setRefundTotal(oldUserCard.getOccupyRefundTotal());
-            distributionRedundVo.setRefundIntegral(oldUserCard.getOccupyRefundTotal());
-        }
+        //鐢ㄦ埛鍗¢」鍒嗛厤鍙��鎬婚噾棰�
+        RefundCardVo refundCard = new RefundCardVo();
 
         CardItemInfo cardItemInfo;
         CardEquity cardEquity;
         //鏉$洰閲戦璁$畻
         BigDecimal sumCardBagMoney;
+        //鏉$洰绉垎璁$畻
+        BigDecimal sumCardBagIntegral;
+
+        //鑾峰彇鍗¢」鐨勬潈鐩婃暟閲�
+        int cardEquityNum = UserCardTool.getCardEquityCount(userCard.getCardItemId(),commonService);
+
+        //瀛樺偍鏉冪泭鍒嗛厤鐨勯��娆鹃噾棰�
+        Map<String,RefundCardVo> cardEquityMap = new HashMap<>();
+
         //姣斾緥
+        RefundCardVo refundCardEquity;
+        RefundCardVo refundCardInfo;
         BigDecimal proportion;
         for(RefundUserCardItemVo refundUserCardItemVo:refundUserCardVo.getRefundUserCardItemVoList()){
             if(StringUtils.isEmpty(refundUserCardItemVo.getCardItemInfoId())){
@@ -204,7 +212,7 @@
             if(cardEquity == null){
                 throw new TipsException("鑾峰彇鍗¢」鏉冪泭淇℃伅澶辫触[210]锛�");
             }
-
+            //-----鏍¢獙
             //鑾峰彇鍙敤鍓╀綑浣跨敤娆℃暟
             UserCardItemInfoVo uerCardItemInfoVo = UserCardTool.getSurplusNumber(userCard,cardItemInfo,cardEquity,commonService);
             //鑾峰彇褰撳墠闇�瑕佹姷鎵g殑娆℃暟
@@ -216,11 +224,38 @@
                 throw new PlatTipsException(PlatformCode.ERROR_TIPS,"鍗¢」閫�娆炬彁绀�:鍗¢」鐨勩��"+cardItemInfo.getCommonName()+"銆戞鏁颁笉澶�");
             }
 
+            refundCardInfo = new RefundCardVo();
+
+            //-----绠楀嚭鏉冪泭鍒嗛厤鍒伴噾棰�
+            //鏌ョ湅鏉冪泭鏄惁宸茬粡鍒嗛厤濂戒簡鐨�
+            refundCardEquity = getCardEquityData(orderItem,userCard,cardItem,cardEquity,refundCard,cardEquityNum,cardEquityMap,commonService,refundMapper);
+            //鍒ゆ柇鏄笉鏄渶鍚庝竴涓潯鐩�
+            if(CardEquity.EQUITY_TYPE_TIME_CARD.equals(cardEquity.getEquityType())){
+                //娆″崱锛屾潈鐩婃湁鎬绘鏁帮紝鎵�鏈夋潯鐩娇鐢ㄧ殑鎬绘鏁颁笉鑳藉ぇ浜庢潈鐩婄殑鎬绘鏁�
+                //鍒ゆ柇鏁伴噺
+                if(uerCardItemInfoVo.getSurplusNum() < refundUserCardItemVo.getRefundQuantity()){
+                    //鏈�鍚庣殑娆℃暟
+                    refundCardInfo.setOccupyRefundTotal(refundCardEquity.getSurplusTotal());
+                    refundCardInfo.setOccupyRefundIntegral(refundCardEquity.getSurplusIntegral());
+                }else{
+
+                }
+                refundCardInfo.setSurplusTotal(refundCardEquity.getSurplusTotal());
+                refundCardInfo.setSurplusIntegral(refundCardEquity.getSurplusIntegral());
+            }else if(CardEquity.EQUITY_TYPE_N_M.equals(cardEquity.getEquityType())){
+                //鍒ゆ柇鏉$洰鏁伴噺锛屽啀鍒ゆ柇鏁伴噺
+
+
+            }else{
+                //鍥哄畾鎴栬�呭叾浠�
+                userCardItemInfoVo.setSurplusNum(cardItemInfo.getMaxNum() - cardItemInfoUseMap.computeIfAbsent(cardItemInfo.getId(),k->0));
+            }
+
+            //-----绠楀嚭鍗曟鍒掓墸閲戦
             //鑾峰彇鍗曟浠锋牸閲戦
             sumCardBagMoney = UserCardTool.countOneMoney(cardItemInfo);
             //璁$畻鎶垫墸鎬荤殑鍒嗘憡鏄庣粏
             sumCardBagMoney = sumCardBagMoney.multiply(BigDecimal.valueOf(refundUserCardItemVo.getRefundQuantity()).setScale(2,RoundingMode.DOWN));
-
             //绠楁瘮渚�
             if(cardItem.getTotal() != null && cardItem.getTotal().compareTo(BigDecimal.ZERO) > 0){
                 proportion = sumCardBagMoney.divide(cardItem.getTotal(),20,RoundingMode.HALF_UP);
@@ -228,10 +263,250 @@
                 proportion = BigDecimal.ZERO;
             }
 
+            //绠楀嚭鍗犵敤璐拱鍗¢」璁㈠崟鏃跺�欓噾棰濓紙鍒嗘憡妯″紡锛�
+            sumCardBagMoney = occupyRefundTotal.multiply(proportion);
+            sumCardBagIntegral = occupyRefundIntegral.multiply(proportion);
 
+            //鑾峰彇鍗¢」鏉$洰宸查��娆鹃噾棰�
+            refundRecordItemSource = getTwoAlreadyRefundTotal(cardItemInfo.getId(),null,null,commonService);
+
+            //
+
+            //涓嶈兘瓒呰繃鍗¢」鏉$洰鐨勫垎鎽婃�婚噾棰�
 
         }
 
     }
 
-}
+    /**鏌ユ壘閮ㄥ垎閫�娆剧殑鐢ㄦ埛鍗″寘淇℃伅锛屾牴鎹敤鎴峰崱鍖呮爣璇嗗幓閲嶄簡
+     * @param orderItemId 瀛愬崟鏍囪瘑
+     * @param userCardId 鐢ㄦ埛鍗″寘鏍囪瘑
+     * @param commonService 鏄犲皠
+     */
+    public static List<RefundRecordItem> findRefundUserCard(String orderItemId,String userCardId,CommonService commonService){
+        SqlSentence sqlSentence = new SqlSentence();
+        Map<String,Object> values = new HashMap<>();
+
+        StringBuilder sql = new StringBuilder();
+
+        //--------鑾峰彇鐢ㄦ埛鍗″寘鐨勬�婚噾棰�
+        //鏌ユ壘鏈夋病鏈夎繖涓崱鍖呴儴鍒嗛��娆剧殑璁板綍
+        sql.append("SELECT rri.occupyRefundTotal,rri.occupyRefundIntegral,rri.userCardId FROM refund_record_item rri");
+        sql.append(" JOIN refund_record rr ON rr.id = rri.refundRecordId");
+        sql.append(" JOIN user_card uc ON uc.id = rri.userCardId");
+        sql.append(" AND rr.isDel = 0 AND rr.refundStatus IN(0,2)");
+        sql.append(" WHERE rri.isDel = 0");
+        if(StringUtils.noNull(orderItemId)){
+            values.put("orderItemId",orderItemId);
+            sql.append(" AND rri.orderItemId = #{m.orderItemId}");
+        }
+        if(StringUtils.noNull(userCardId)){
+            values.put("userCardId",userCardId);
+            sql.append(" AND rri.userCardId = #{m.userCardId}");
+        }else{
+            sql.append(" AND rri.userCardId IS NOT NULL");
+        }
+        sql.append(" GROUP BY rri.userCardId");
+
+        sqlSentence.sqlSentence(sql.toString(),values);
+        return commonService.selectList(UserCardMapper.class,sqlSentence);
+    }
+
+    /**鑾峰彇鍗″寘宸插垎閰嶇殑閲戦锛堟潈鐩婂垎閰嶇殑閲戦锛�
+     * @param userCardId 鐢ㄦ埛鍗¢」鏍囪瘑锛屼富瑕佽幏鍙栫敤鎴峰崱椤瑰垎閰嶇殑閲戦
+     * @param commonService 鏄犲皠
+     * @return 宸查��閲戦
+     */
+    public static RefundRecordItemSource getCardRealRefund(String userCardId, CommonService commonService ){
+        SqlSentence sqlSentence = new SqlSentence();
+        Map<String,Object> values = new HashMap<>();
+        StringBuilder sql = new StringBuilder();
+
+        values.put("userCardId",userCardId);
+        sql.append("SELECT rris.occupyEqRefundTotal,rris.occupyEqRefundIntegral");
+        sql.append(" FROM refund_record_item_source rris");
+        sql.append(" JOIN refund_record rr ON rr.id = rris.refundRecordId AND rr.isDel = 0 AND rr.refundStatus IN(0,2)");
+        sql.append(" WHERE rris.isDel = 0 AND rris.userCardId = #{m.userCardId} GROUP BY rris.cardEquityId");
+
+        List<RefundRecordItemSource> refundRecordItemSourceList = commonService.selectList(RefundRecordItemSourceMapper.class,sqlSentence);
+
+        RefundRecordItemSource source = new RefundRecordItemSource();
+        if(refundRecordItemSourceList.size()==0){
+            source.setIsDel(BaseEntity.YES);
+            return source;
+        }
+        for(RefundRecordItemSource refundRecordItemSource:refundRecordItemSourceList){
+            //璁$畻鏈夊灏戜釜鏉冪泭鍙備笌浜嗛��娆�
+            refundRecordItemSource.setRealRefundNum(refundRecordItemSource.getRealRefundNum()+1);
+            source.setOccupyRefundTotal(source.getOccupyRefundTotal().add(refundRecordItemSource.getOccupyEqRefundTotal()));
+            source.setOccupyRefundIntegral(source.getOccupyRefundIntegral().add(refundRecordItemSource.getOccupyEqRefundIntegral()));
+        }
+        return source;
+    }
+
+    /**鑾峰彇鍗″寘鏉冪泭宸查��娆鹃噾棰濓紙鏉$洰鍒嗛厤鐨勯噾棰濓級
+     * @param cardEquityId 鍗¢」鏉冪泭鏍囪瘑锛屼富瑕佽幏鍙栧崱椤规潈鐩婂垎閰嶇殑閲戦
+     * @param commonService 鏄犲皠
+     * @return 宸查��閲戦
+     */
+    public static RefundRecordItemSource getCardEquityRealRefund(String cardEquityId, CommonService commonService ){
+        SqlSentence sqlSentence = new SqlSentence();
+        Map<String,Object> values = new HashMap<>();
+        StringBuilder sql = new StringBuilder();
+
+        values.put("cardEquityId",cardEquityId);
+        sql.append("SELECT rris.occupyItemRefundTotal,rris.occupyItemRefundIntegral,rris.occupyEqRefundTotal,rris.occupyEqRefundIntegral");
+        sql.append(" FROM refund_record_item_source rris");
+        sql.append(" JOIN refund_record rr ON rr.id = rris.refundRecordId AND rr.isDel = 0 AND rr.refundStatus IN(0,2)");
+        sql.append(" WHERE rris.isDel = 0 AND rris.cardEquityId = #{m.cardEquityId} GROUP BY orderItemSonId");
+
+        List<RefundRecordItemSource> refundRecordItemSourceList = commonService.selectList(RefundRecordItemSourceMapper.class,sqlSentence);
+
+        RefundRecordItemSource source = new RefundRecordItemSource();
+        if(refundRecordItemSourceList.size()==0){
+            source.setIsDel(BaseEntity.YES);
+            return source;
+        }
+        for(RefundRecordItemSource refundRecordItemSource:refundRecordItemSourceList){
+            //璁$畻鏈夊灏戜釜鏉$洰鍙備笌浜嗛��娆�
+            source.setRealRefundNum(refundRecordItemSource.getRealRefundNum()+1);
+            source.setRefundMoney(source.getRefundMoney().add(refundRecordItemSource.getOccupyItemRefundTotal()));
+            source.setRefundIntegral(source.getRefundIntegral().add(refundRecordItemSource.getOccupyItemRefundIntegral()));
+            source.setOccupyEqRefundTotal(refundRecordItemSource.getOccupyEqRefundTotal());
+            source.setOccupyEqRefundIntegral(refundRecordItemSource.getOccupyEqRefundIntegral());
+        }
+        return source;
+    }
+
+    /**鑾峰彇鍗″寘鏉$洰宸查��娆鹃噾棰濓紙鏉$洰鍒嗛厤鐨勯噾棰濓級
+     * @param cardInfoId 鍗¢」鏉$洰鏍囪瘑锛屼富瑕佽幏鍙栧崱椤规潯鐩垎閰嶇殑閲戦
+     * @param commonService 鏄犲皠
+     * @return 宸查��閲戦
+     */
+    public static RefundCardVo getCardInfoData(String cardInfoId,CommonService commonService){
+
+        SqlSentence sqlSentence = new SqlSentence();
+        Map<String,Object> values = new HashMap<>();
+        StringBuilder sql = new StringBuilder();
+
+        values.put("orderItemSonId",cardInfoId);
+        sql.append("SELECT SUM(rris.occupyRefundTotal) AS occupyRefundTotal,SUM(rris.occupyRefundIntegral) AS occupyRefundIntegral");
+        sql.append(",SUM(rris.realRefundNum) AS realRefundNum,rris.occupyItemRefundTotal,rris.occupyItemRefundIntegral");
+        sql.append(" FROM refund_record_item_source rris");
+        sql.append(" JOIN refund_record rr ON rr.id = rris.refundRecordId AND rr.isDel = 0 AND rr.refundStatus IN(0,2)");
+        sql.append(" WHERE rris.isDel = 0 AND rris.orderItemSonId = #{m.orderItemSonId} GROUP BY orderItemSonId");
+
+        RefundRecordItemSource itemSource = commonService.selectOne(RefundRecordItemSourceMapper.class,sqlSentence);
+
+        RefundCardVo refundCardInfo = new RefundCardVo();
+
+        if(itemSource != null){
+            refundCardInfo.setJoinRefund(true);
+            refundCardInfo.setRefundNum(itemSource.getRealRefundNum());
+            refundCardInfo.setOccupyRefundTotal(itemSource.getOccupyItemRefundTotal());
+            refundCardInfo.setOccupyRefundIntegral(itemSource.getOccupyItemRefundIntegral());
+            refundCardInfo.setSurplusTotal(itemSource.getOccupyItemRefundTotal().subtract(itemSource.getOccupyRefundTotal()));
+            refundCardInfo.setSurplusIntegral(itemSource.getOccupyItemRefundIntegral().subtract(itemSource.getOccupyRefundIntegral()));
+
+        }
+        return refundCardInfo;
+
+
+    }
+
+
+    /**鑾峰彇鐢ㄦ埛鍗¢」鐨勫弬鏁帮紝鍒嗛厤閲戦鍜屽墿浣欏彲閫�閲戦*/
+    public static RefundCardVo getUserCardData(OrderItem orderItem,UserCard userCard,CommonService commonService,RefundMapper refundMapper){
+
+        RefundCardVo refundCard = new RefundCardVo();
+        //--------鑾峰彇鐢ㄦ埛鍗″寘鍒嗛厤鐨勭殑鍙��娆炬�婚噾棰濓紙鍖呭惈宸茬粡閮ㄥ垎閫�鐨勯噾棰濆湪閲岄潰锛�
+        //鏌ユ壘鏈夋病鏈夎繖涓崱鍖呴儴鍒嗛��娆剧殑璁板綍
+        List<RefundRecordItem> refundRecordItemList = findRefundUserCard(orderItem.getId(),userCard.getId(),commonService);
+        if(refundRecordItemList.size() == 0){
+            //鐢ㄦ埛鍗″寘娌℃湁鍒嗛厤鎬婚噾棰濓紝閭d箞灏辨寜鐓ф甯哥殑娴佺▼鑾峰彇鍙��娆鹃噾棰�
+            DistributionRedundVo distributionRedundVo = UserCardTool.userCardPay(userCard,orderItem,commonService,refundMapper);
+            refundCard.setOccupyRefundTotal(distributionRedundVo.getRefundTotal());
+            refundCard.setOccupyRefundIntegral(distributionRedundVo.getRefundIntegral());
+        }else{
+            refundCard.setJoinRefund(true);
+            RefundRecordItem refundRecordItem = refundRecordItemList.get(0);
+            refundCard.setOccupyRefundTotal(refundRecordItem.getOccupyRefundTotal());
+            refundCard.setOccupyRefundIntegral(refundRecordItem.getOccupyRefundIntegral());
+        }
+
+        //-----璁$畻鍓╀綑鍙垎閰嶇粰鏉冪泭閫�娆剧殑閲戦
+        //鏌ユ壘璇ュ崱鍖呭凡閮ㄥ垎閫�娆剧殑閲戦
+        RefundRecordItemSource itemSource = getCardRealRefund(userCard.getId(),commonService);
+        //璁$畻璇ョ敤鎴峰崱鍖呭墿浣欏彲閫�閲戦鍜岀Н鍒嗭紝灏辨槸鍙垎閰嶇粰鏉冪泭閫�娆剧殑閲戦
+        refundCard.setSurplusTotal(refundCard.getOccupyRefundTotal().subtract(itemSource.getOccupyRefundTotal()));
+        refundCard.setSurplusIntegral(refundCard.getOccupyRefundIntegral().subtract(itemSource.getOccupyRefundIntegral()));
+        refundCard.setRefundNum(itemSource.getRealRefundNum());
+
+        return refundCard;
+    }
+
+    /**鑾峰彇鐢ㄦ埛鍗¢」鏉冪泭鐨勫弬鏁帮紝鍒嗛厤閲戦鍜屽墿浣欏彲閫�閲戦*/
+    public static RefundCardVo getCardEquityData(OrderItem orderItem,UserCard userCard,CardItem cardItem,CardEquity cardEquity,RefundCardVo refundCard,int cardEquityNum
+            ,Map<String,RefundCardVo> cardEquityMap,CommonService commonService,RefundMapper refundMapper){
+
+        //-----绠楀嚭鏉冪泭鍒嗛厤鍒伴噾棰�
+        //鏌ョ湅鏉冪泭鏄惁宸茬粡鍒嗛厤濂戒簡鐨�
+        BigDecimal proportion;
+        RefundCardVo refundCardEquity = cardEquityMap.get(cardEquity.getId());
+        if(refundCardEquity == null){
+            refundCardEquity = new RefundCardVo();
+
+            //鑾峰彇鍗¢」鏉冪泭宸查��娆鹃噾棰濆拰鍒嗛厤鐨勯噾棰�
+            RefundRecordItemSource cardEquityRealRefund = getCardEquityRealRefund(cardEquity.getId(),commonService);
+            if(cardEquityRealRefund.getIsDel().equals(BaseEntity.YES)){
+                //娌℃湁鎵惧埌宸茬粡鍒嗛厤濂界殑锛岃繘琛屾柊鐨勫垎閰�
+                //鑾峰彇鐢ㄦ埛鍗″寘鍒嗛厤閲戦
+                if(!refundCard.isJoinRefund()){
+                    BeanUtils.copyProperties(getUserCardData(orderItem,userCard,commonService,refundMapper),refundCard);
+                    refundCard.setJoinRefund(true);
+                }
+
+                if(cardEquityNum>=refundCard.getRefundNum()+1){
+                    //鏄渶鍚庝竴涓�
+                    refundCardEquity.setOccupyRefundTotal(refundCard.getSurplusTotal());
+                    refundCardEquity.setOccupyRefundIntegral(refundCard.getSurplusIntegral());
+                }else{
+                    //涓嶆槸鏈�鍚庝竴涓紝閫氳繃姣斾緥鏉ョ畻閲戦
+                    if(cardItem.getTotal().compareTo(BigDecimal.ZERO) > 0){
+                        proportion = cardEquity.getShareMoney().divide(cardItem.getTotal(),20,RoundingMode.HALF_UP);
+                    }else{
+                        proportion = BigDecimal.ZERO;
+                    }
+                    refundCardEquity.setOccupyRefundTotal(refundCard.getOccupyRefundTotal().multiply(proportion).setScale(2,RoundingMode.HALF_UP));
+                    refundCardEquity.setOccupyRefundIntegral(refundCard.getOccupyRefundIntegral().multiply(proportion).setScale(2,RoundingMode.HALF_UP));
+
+                    //鍒ゆ柇涓嶈兘澶т簬鏉冪泭璁剧疆鐨勯噾棰�
+                    if(refundCardEquity.getOccupyRefundTotal().compareTo(cardEquity.getShareMoney()) > 0){
+                        refundCardEquity.setOccupyRefundTotal(cardEquity.getShareMoney());
+                    }
+                    //鍒ゆ柇涓嶈兘澶т簬鍓╀笅鐨勫彲閫�閲戦
+                    if(refundCardEquity.getOccupyRefundTotal().compareTo(refundCard.getSurplusTotal()) > 0){
+                        refundCardEquity.setOccupyRefundTotal(refundCard.getSurplusTotal());
+                    }
+                    //鍒ゆ柇涓嶈兘澶т簬鍓╀笅鐨勫彲閫�绉垎
+                    if(refundCardEquity.getOccupyRefundIntegral().compareTo(refundCard.getSurplusIntegral()) > 0){
+                        refundCardEquity.setOccupyRefundIntegral(refundCard.getSurplusIntegral());
+                    }
+                }
+                refundCardEquity.setSurplusTotal(refundCardEquity.getOccupyRefundTotal());
+                refundCardEquity.setSurplusIntegral(refundCardEquity.getOccupyRefundIntegral());
+            }else{
+                refundCardEquity.setJoinRefund(true);
+                refundCardEquity.setRefundNum(cardEquityRealRefund.getRealRefundNum());
+                refundCardEquity.setOccupyRefundTotal(cardEquityRealRefund.getOccupyEqRefundTotal());
+                refundCardEquity.setOccupyRefundIntegral(cardEquityRealRefund.getOccupyEqRefundIntegral());
+                refundCardEquity.setSurplusTotal(cardEquityRealRefund.getOccupyEqRefundTotal().subtract(cardEquityRealRefund.getRefundMoney()));
+                refundCardEquity.setSurplusIntegral(cardEquityRealRefund.getOccupyEqRefundIntegral().subtract(cardEquityRealRefund.getRefundIntegral()));
+            }
+            cardEquityMap.put(cardEquity.getId(),refundCardEquity);
+        }
+        return refundCardEquity;
+    }
+
+
+}
\ No newline at end of file
diff --git a/phi_platform_common/src/main/java/com/hx/phip/tool/user/UserCardTool.java b/phi_platform_common/src/main/java/com/hx/phip/tool/user/UserCardTool.java
index ef2c60a..c12b7e3 100644
--- a/phi_platform_common/src/main/java/com/hx/phip/tool/user/UserCardTool.java
+++ b/phi_platform_common/src/main/java/com/hx/phip/tool/user/UserCardTool.java
@@ -208,6 +208,8 @@
         //璁$畻閫�娆炬柟寮忛噾棰�
         distributionRedundVo = PaymentCountTool.countMakeWay(orderItem.getId(),orderItem.getActualTotal(),refundRecordItem.getOccupyRefundTotal()
                 ,refundRecordItem.getOccupyRefundIntegral(),orderItem.getBuyNum(),userCardList.size(),null,1,payMethodVoList,commonService);
+        distributionRedundVo.setGoodsType(orderItem.getType());
+        distributionRedundVo.setGoodsName(orderItem.getGoodsName());
 
         return distributionRedundVo;
     }
@@ -222,8 +224,32 @@
         //鑾峰彇鐢ㄦ埛鍗¢」
         sqlMap.put("sourceId",sourceId);
         sqlMap.put("effectiveStatus", UserProjectConstants.EFF_STATUS_CANCEL);
-        sqlSentence.sqlSentence("SELECT * FROM user_card WHERE isDel = 0 AND sourceId = #{m.sourceId} AND effectiveStatus != #{m.effectiveStatus}",sqlMap);
-        return commonService.selectList(UserCardMapper.class,sqlSentence);
+        sqlSentence.sqlSentence("SELECT * FROM user_card WHERE isDel = 0 AND sourceId = #{m.sourceId} AND effectiveStatus != #{m.effectiveStatus} AND turnAddId IS NULL",sqlMap);
+        List<UserCard> userCardList = commonService.selectList(UserCardMapper.class,sqlSentence);
+        if(userCardList.size() == 0){
+            return userCardList;
+        }
+        //鑾峰彇宸茬粡閮ㄥ垎閫�鐨勭敤鎴峰崱鍖呮暟閲�
+        List<RefundRecordItem> refundRecordItemList = RefundTool.findRefundUserCard(sourceId,null,commonService);
+        //杩囨护鎺夋病鏈変娇鐢ㄤ絾鏈夐儴鍒嗛��娆�
+        if(refundRecordItemList.size() == 0){
+            return userCardList;
+        }
+
+        Map<String,UserCard> userCardMap = new HashMap<>();
+        for(UserCard userCard:userCardList){
+            userCardMap.put(userCard.getId(),userCard);
+        }
+        //鍘婚櫎鎺夊弬涓庨儴鍒嗛��娆剧殑鐢ㄦ埛鍗″寘
+        for(RefundRecordItem refundRecordItem:refundRecordItemList){
+            userCardMap.remove(refundRecordItem.getUserCardId());
+        }
+        userCardList = new ArrayList<>();
+        for (Map.Entry<String,UserCard> entry : userCardMap.entrySet()) {
+            userCardList.add(entry.getValue());
+        }
+
+        return userCardList;
     }
 
     /**璁$畻鍗″寘鏉$洰鍗曟鎶垫墸鐨勯噾棰�
@@ -244,5 +270,17 @@
         }
     }
 
+    /**鑾峰彇鍗¢」鐨勬潈鐩婃暟閲�*/
+    public static int getCardEquityCount(String cardId,CommonService commonService){
+        SqlSentence sqlSentence = new SqlSentence();
+        Map<String,Object> values = new HashMap<>();
+
+        values.put("cardItemId",cardId);
+        sqlSentence.sqlSentence("SELECT * FROM card_equity WHERE isDel = 0 AND cardItemId = #{m.cardItemId}" +
+                " AND equityType != 4",values);
+
+        return commonService.selectCountSql(CardEquityMapper.class,sqlSentence);
+    }
+
 
 }
diff --git a/phi_platform_common/src/main/java/com/hx/phip/vo/order/refund/RefundCardVo.java b/phi_platform_common/src/main/java/com/hx/phip/vo/order/refund/RefundCardVo.java
new file mode 100644
index 0000000..868467a
--- /dev/null
+++ b/phi_platform_common/src/main/java/com/hx/phip/vo/order/refund/RefundCardVo.java
@@ -0,0 +1,25 @@
+package com.hx.phip.vo.order.refund;
+
+import lombok.Data;
+
+import java.math.BigDecimal;
+
+@Data
+public class RefundCardVo {
+
+    /**鏄惁瀛樺湪閫�娆�*/
+    private boolean joinRefund = false;
+    /**宸插弬涓庝簡閫�娆炬暟閲�*/
+    private int refundNum = 0;
+
+    /**鍒嗛厤閲戦*/
+    private BigDecimal occupyRefundTotal = BigDecimal.ZERO;
+    /**鍒嗛厤绉垎*/
+    private BigDecimal occupyRefundIntegral = BigDecimal.ZERO;
+
+    /**鍓╀綑鍙垎閰嶆�婚噾棰�*/
+    private BigDecimal surplusTotal = BigDecimal.ZERO;
+    /**鍓╀綑鍙垎閰嶆�婚噾棰�*/
+    private BigDecimal surplusIntegral = BigDecimal.ZERO;
+
+}
diff --git a/phi_platform_user/src/main/java/com/hx/phip/service/order/impl/OrderRefundServiceImpl.java b/phi_platform_user/src/main/java/com/hx/phip/service/order/impl/OrderRefundServiceImpl.java
index c162c70..ae096ca 100644
--- a/phi_platform_user/src/main/java/com/hx/phip/service/order/impl/OrderRefundServiceImpl.java
+++ b/phi_platform_user/src/main/java/com/hx/phip/service/order/impl/OrderRefundServiceImpl.java
@@ -552,8 +552,8 @@
             if(orderItemRefundDto.getRefundNum() > surplusNum){
                 throw new TipsException("閫�娆炬暟閲忎笌鍙��鏁伴噺涓嶇[02]锛�");
             }
-            //鏇挎崲鎴愬瓙鍗曠殑鍓╀綑鏁伴噺
-            surplusNum = orderItem.getBuyNum() - orderItem.getHasReNum();
+            //鏇挎崲鎴愬瓙鍗曠殑鍓╀綑鏁伴噺锛�2023-06-15鍥犱负鍋氱敤鎴峰崱鍖呴��娆惧師鍥狅紝鍏堝睆鎺夎繖涓�
+            //surplusNum = orderItem.getBuyNum() - orderItem.getHasReNum();
         }else{
             buyNum = orderItem.getBuyNum();
             //鏇挎崲鎴愬瓙鍗曠殑鍓╀綑鏁伴噺
@@ -628,7 +628,7 @@
         List<PayMethodVo> payMethodVoList = refundMapper.getConsumePayTwoGroupByNumberNo(orderItemSon.getId());
 
         //鑾峰彇璇ュ瓙鍗曞凡閫�鐨勬�婚噾棰�
-        RefundRecordItemSource refundRecordItemSource = RefundTool.getTwoAlreadyRefundTotal(orderItemSon.getId(),commonService);
+        RefundRecordItemSource refundRecordItemSource = RefundTool.getTwoAlreadyRefundTotal(orderItemSon.getId(),null,null,commonService);
 
         //璁$畻閫�娆炬柟寮忛噾棰�
         DistributionRedundVo distributionRedundVo = PaymentCountTool.countMakeWay(orderItemSon.getId(),orderItemSon.getActualTotal(),refundRecordItemSource.getOccupyRefundTotal()
diff --git a/phi_platform_user/src/main/java/com/hx/phip/tool/refund/PartialRefundUtil.java b/phi_platform_user/src/main/java/com/hx/phip/tool/refund/PartialRefundUtil.java
index e43d379..f2f8c1d 100644
--- a/phi_platform_user/src/main/java/com/hx/phip/tool/refund/PartialRefundUtil.java
+++ b/phi_platform_user/src/main/java/com/hx/phip/tool/refund/PartialRefundUtil.java
@@ -1640,7 +1640,32 @@
         sqlMap.put("effectiveStatus",effectiveStatus);
         sqlSentence.sqlSentence("SELECT * FROM user_card WHERE isDel = 0 AND sourceId = #{m.sourceId} AND status = #{m.status}" +
                 " AND effectiveStatus = #{m.effectiveStatus} AND turnAddId IS NULL",sqlMap);
-        return commonService.selectList(UserCardMapper.class,sqlSentence);
+        List<UserCard> userCardList = commonService.selectList(UserCardMapper.class,sqlSentence);
+        if(userCardList.size() == 0){
+            return userCardList;
+        }
+        //鑾峰彇宸茬粡閮ㄥ垎閫�鐨勭敤鎴峰崱鍖呮暟閲�
+        List<RefundRecordItem> refundRecordItemList = RefundTool.findRefundUserCard(sourceId,null,commonService);
+        //杩囨护鎺夋病鏈変娇鐢ㄤ絾鏈夐儴鍒嗛��娆�
+        if(refundRecordItemList.size() == 0){
+            return userCardList;
+        }
+
+        Map<String,UserCard> userCardMap = new HashMap<>();
+        for(UserCard userCard:userCardList){
+            userCardMap.put(userCard.getId(),userCard);
+        }
+        //鍘婚櫎鎺夊弬涓庨儴鍒嗛��娆剧殑鐢ㄦ埛鍗″寘
+        for(RefundRecordItem refundRecordItem:refundRecordItemList){
+            userCardMap.remove(refundRecordItem.getUserCardId());
+        }
+
+        userCardList = new ArrayList<>();
+        for (Map.Entry<String,UserCard> entry : userCardMap.entrySet()) {
+            userCardList.add(entry.getValue());
+        }
+
+        return userCardList;
 
     }
 

--
Gitblit v1.8.0