chenjiahe
2023-08-14 f3ff4ed544dab12f9471a738e106379a466bb5e2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package com.hx.phip.tool.payment;
 
import com.hx.common.service.CommonService;
import com.hx.mybatisTool.SqlSentence;
import com.hx.phiappt.model.PaymentMethod;
import com.hx.phiappt.model.consume.ConsumeNotify;
import com.hx.phiappt.model.consume.ConsumePay;
import com.hx.phip.dao.mapper.ConsumeNotifyMapper;
import com.hx.phip.dao.mapper.PaymentMethodMapper;
import com.hx.util.StringUtils;
 
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**消费记录工具
 *
 * @author CJH
 */
public class ConsumeTool {
 
    /**通过支付编号或者作废流水号获取到支付回调信息
     * @param orderId 支付回调信息关联标识,可不传
     * @param outOrderNo 支付编号
     * @param commonService 映射
     * @return 支付回调信息
     */
    public static ConsumeNotify selectConsumeNotifyByOrderNo(String orderId,String outOrderNo,String platTrxNo, CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        StringBuilder sql = new StringBuilder();
 
        values.put("outOrderNo",outOrderNo);
        values.put("platTrxNo",platTrxNo);
        sql.append("SELECT * FROM consume_notify WHERE isDel = 0 AND (outOrderNo = #{m.outOrderNo} OR platTrxNo = #{m.platTrxNo})");
        if(StringUtils.noNull(orderId)){
            values.put("orderId",orderId);
            sql.append(" AND orderId = #{m.orderId}");
        }
 
        sqlSentence.sqlSentence(sql.toString(),values);
        return commonService.selectOne(ConsumeNotifyMapper.class,sqlSentence);
    }
 
    /**绑定支付信息,没有绑定的,单个
     * 没有关联到支付记录的支付信息
     * @param orderId 支付回调信息关联标识
     * @param paymentNo 支付方式编号
     * @param payAmount 支付金额
     * @param consumePayId 支付记录标识
     * @param commonService 映射
     * @return 支付回调信息
     */
    public static void bindConsume(String orderId,String paymentNo,BigDecimal payAmount,String consumePayId, CommonService commonService){
        ConsumeNotify consumeNotify = selectNoBind(orderId,paymentNo,payAmount,commonService);
        if(consumeNotify == null){
            return;
        }
        bindConsumeNotify(consumeNotify,consumePayId,commonService);
    }
 
    /**绑定支付信息集合,没有绑定的
     * 没有关联到支付记录的支付信息
     * @param orderId 支付回调信息关联标识
     * @param consumePayList 支付方式编号
     * @param commonService 映射
     * @return 支付回调信息
     */
    public static void bindConsumeList(String orderId,List<ConsumePay> consumePayList, CommonService commonService){
 
        Map<String,Integer> consumePayCount = new HashMap<>();
        Integer count;
        for(ConsumePay consumePay:consumePayList){
            count = consumePayCount.getOrDefault(consumePay.getNumberNo(),0);
            count++;
            consumePayCount.put(consumePay.getNumberNo(),count);
        }
        List<ConsumeNotify> consumeNotifyList;
        for(ConsumePay consumePay:consumePayList){
            count = consumePayCount.get(consumePay.getNumberNo());
            if(count == 1){
                //单条,绑定所有的支付回调信息
                consumeNotifyList = selectNoBindList(orderId,consumePay.getNumberNo(),null,commonService);
                for(ConsumeNotify consumeNotify:consumeNotifyList){
                    bindConsumeNotify(consumeNotify,consumePay.getId(),commonService);
                }
            }else if(count > 1){
                //多条,绑定金额一样的回调信息
                bindConsume(orderId,consumePay.getNumberNo(),consumePay.getActualTotal(),consumePay.getId(),commonService);
            }
        }
    }
 
    /**绑定回调记录*/
    public static void bindConsumeNotify(ConsumeNotify consumeNotify,String consumePayId,CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
 
        values.put("id",consumeNotify.getId());
        values.put("consumePayId",consumePayId);
        sqlSentence.sqlUpdate("consumePayId = #{m.consumePayId} WHERE id = #{m.id} AND consumePayId IS NULL",values);
        commonService.updateWhere(ConsumeNotifyMapper.class,sqlSentence);
    }
 
    /**通过支付方式编号和金额获取到支付回调信息,获取单条
     * 没有关联到支付记录的支付信息
     * @param orderId 支付回调信息关联标识
     * @param paymentNo 支付方式编号
     * @param payAmount 支付金额
     * @param commonService 映射
     * @return 支付回调信息
     */
    public static ConsumeNotify selectNoBind(String orderId,String paymentNo,BigDecimal payAmount, CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        values.put("orderId",orderId);
        values.put("paymentNo",paymentNo);
        values.put("payAmount",payAmount);
        values.put("refundStatus",ConsumeNotify.REFUND_STATUS_NO);
        sqlSentence.sqlSentence("SELECT * FROM consume_notify WHERE isDel = 0 AND paymentNo = #{m.paymentNo} AND orderId = #{m.orderId}" +
                " AND payAmount = #{m.payAmount} AND refundStatus = #{m.refundStatus} AND consumePayId IS NULL LIMIT 1",values);
        return commonService.selectOne(ConsumeNotifyMapper.class,sqlSentence);
    }
 
    /**通过支付方式编号和金额获取到支付回调信息,获取多条
     * 没有关联到支付记录的支付信息
     * @param orderId 支付回调信息关联标识
     * @param paymentNo 支付方式编号
     * @param payAmount 支付金额
     * @param commonService 映射
     * @return 支付回调信息
     */
    public static List<ConsumeNotify> selectNoBindList(String orderId,String paymentNo,BigDecimal payAmount, CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder();
 
        values.put("orderId",orderId);
        values.put("paymentNo",paymentNo);
        values.put("refundStatus",ConsumeNotify.REFUND_STATUS_NO);
        stringBuilder.append("SELECT * FROM consume_notify WHERE isDel = 0 AND paymentNo = #{m.paymentNo} AND orderId = #{m.orderId}");
        if(payAmount != null){
            values.put("payAmount",payAmount);
            stringBuilder.append(" AND payAmount = #{m.payAmount}");
        }
        stringBuilder.append(" AND refundStatus = #{m.refundStatus} AND consumePayId IS NULL");
        sqlSentence.sqlSentence(stringBuilder.toString(),values);
        return commonService.selectList(ConsumeNotifyMapper.class,sqlSentence);
    }
 
    /**通过支付方式编号和金额获取到支付回调信息,获取多条,升序排序
     * @param orderId 支付回调信息关联标识
     * @param paymentNo 支付方式编号
     * @param payAmount 支付金额
     * @param commonService 映射
     * @return 支付回调信息
     */
    public static List<ConsumeNotify> selectList(String orderId,String paymentNo,BigDecimal payAmount, CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder();
 
        values.put("orderId",orderId);
        stringBuilder.append("SELECT * FROM consume_notify WHERE isDel = 0 AND orderId = #{m.orderId}");
        if(StringUtils.noNull(paymentNo)){
            values.put("paymentNo",paymentNo);
            stringBuilder.append(" AND paymentNo = #{m.paymentNo}");
        }
        if(payAmount != null){
            values.put("payAmount",payAmount);
            stringBuilder.append(" AND payAmount = #{m.payAmount}");
        }
        stringBuilder.append(" ORDER BY payAmount ASC");
        sqlSentence.sqlSentence(stringBuilder.toString(),values);
        return commonService.selectList(ConsumeNotifyMapper.class,sqlSentence);
    }
 
    /**更新支付回调的退款金额信息*/
    public static void updateRefundTotal(ConsumeNotify consumeNotify,BigDecimal refundTotal,int refundStatus,CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
 
        values.put("refundTotal",refundTotal);
        values.put("id",consumeNotify.getId());
        values.put("refundStatus",refundStatus);
        sqlSentence.sqlUpdate("refundTotal = refundTotal+#{m.refundTotal},refundStatus = #{m.refundStatus} WHERE id = #{m.id}",values);
        commonService.updateWhere(ConsumeNotifyMapper.class,sqlSentence);
    }
 
 
    /**
     * 获取支付方式
     * @param numberNo 支付编号
     * @param commonService 映射
     * @return 支付方式
     */
    public static PaymentMethod getPaymentMethod(String numberNo,CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
 
        values.put("numberNo",numberNo);
        sqlSentence.sqlSentence("SELECT * FROM payment_method WHERE isDel = 0 AND isUp = 1 AND numberNo = #{m.numberNo}",values);
        return commonService.selectOne(PaymentMethodMapper.class,sqlSentence);
    }
 
 
 
}