java110
2023-05-17 d6c57faee4911e3dc6bcb224a071796904c74e14
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package com.java110.fee.feeMonth;
 
import com.java110.core.smo.IComputeFeeSMO;
import com.java110.dto.fee.FeeAttrDto;
import com.java110.dto.fee.FeeDetailDto;
import com.java110.dto.fee.FeeDto;
import com.java110.dto.fee.MonthFeeDetailDto;
import com.java110.dto.payFeeDetailMonth.PayFeeMonthOwnerDto;
import com.java110.intf.community.IRoomInnerServiceSMO;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.util.*;
 
@Service
public class PayFeeMonthHelp implements IPayFeeMonthHelp {
 
    @Autowired
    private IRoomInnerServiceSMO roomInnerServiceSMOImpl;
 
    @Autowired
    private IComputeFeeSMO computeFeeSMOImpl;
 
 
    public PayFeeMonthOwnerDto generatorOwnerRoom(FeeDto feeDto) {
 
        PayFeeMonthOwnerDto payFeeMonthOwnerDto = new PayFeeMonthOwnerDto();
        payFeeMonthOwnerDto.setOwnerId(FeeAttrDto.getFeeAttrValue(feeDto, FeeAttrDto.SPEC_CD_OWNER_ID));
        payFeeMonthOwnerDto.setOwnerName(FeeAttrDto.getFeeAttrValue(feeDto, FeeAttrDto.SPEC_CD_OWNER_NAME));
        payFeeMonthOwnerDto.setLink(FeeAttrDto.getFeeAttrValue(feeDto, FeeAttrDto.SPEC_CD_OWNER_LINK));
        payFeeMonthOwnerDto.setObjName(FeeAttrDto.getFeeAttrValue(feeDto, FeeAttrDto.SPEC_CD_PAY_OBJECT_NAME));
        payFeeMonthOwnerDto.setObjId(feeDto.getPayerObjId());
        return payFeeMonthOwnerDto;
    }
 
    /**
     * 计算每月单价
     *
     * @param feeDto
     * @return
     */
    public Double getMonthFeePrice(FeeDto feeDto) {
        Map feePriceAll = computeFeeSMOImpl.getFeePrice(feeDto);
 
        Double feePrice = Double.parseDouble(feePriceAll.get("feePrice").toString());
        //todo 如果是一次性费用 除以
        if (!FeeDto.FEE_FLAG_ONCE.equals(feeDto.getPayerObjType())) {
            return feePrice;
        }
        List<String> months = DateUtil.getMonthBetweenDate(feeDto.getStartTime(), feeDto.getEndTime());
        //double maxMonth = Math.ceil(computeFeeSMOImpl.dayCompare(feeDto.getStartTime(), feeDto.getEndTime()));
        if (months == null || months.size() <= 0) {
            return feePrice;
 
        }
        BigDecimal feePriceDec = new BigDecimal(feePrice).divide(new BigDecimal(months.size()), 4, BigDecimal.ROUND_HALF_UP);
        feePrice = feePriceDec.doubleValue();
        return feePrice;
    }
 
 
    public Double getReceivableAmount(List<FeeDetailDto> feeDetailDtos, Map<String, MonthFeeDetailDto> monthFeeDetailDtos, Double feePrice, Date curDate, FeeDto feeDto) {
        MonthFeeDetailDto monthFeeDetailDto = getCurMonthFeeDetail(monthFeeDetailDtos, curDate);
        if (monthFeeDetailDto == null && curDate.getTime() < feeDto.getEndTime().getTime()) {
            return 0.00;
        }
        return feePrice;
    }
 
 
    @Override
    public Double getReceivedAmount(List<FeeDetailDto> feeDetailDtos, Map<String, MonthFeeDetailDto> monthFeeDetailDtos, Double feePrice, Date curDate, FeeDto feeDto) {
        MonthFeeDetailDto monthFeeDetailDto = getCurMonthFeeDetail(monthFeeDetailDtos, curDate);
        if (monthFeeDetailDto == null) {
            return 0.0;
        }
        return monthFeeDetailDto.getReceivedAmount();
    }
 
    @Override
    public Double getDiscountAmount(Double feePrice, double receivedAmount, Date curDate, FeeDto feeDto) {
 
        //todo 这种情况下应该 优惠为0
        if (curDate.getTime() >= feeDto.getEndTime().getTime()) {
            return 0.00;
        }
 
        BigDecimal discountAmountDec = new BigDecimal(feePrice).subtract(new BigDecimal(receivedAmount)).setScale(4, BigDecimal.ROUND_HALF_UP);
        return discountAmountDec.doubleValue();
    }
 
    @Override
    public String getFeeDetailId(List<FeeDetailDto> feeDetailDtos, Date curDate) {
        FeeDetailDto feeDetailDto = getCurFeeDetail(feeDetailDtos, curDate);
 
        if (feeDetailDto == null) {
            return "-1";
        }
        return feeDetailDto.getDetailId();
    }
 
    @Override
    public String getFeeFeeTime(List<FeeDetailDto> feeDetailDtos, String detailId) {
 
        if (feeDetailDtos == null || feeDetailDtos.size() < 1) {
            return null;
        }
        for (FeeDetailDto feeDetailDto : feeDetailDtos) {
            if (feeDetailDto.getDetailId().equals(detailId)) {
                return DateUtil.getFormatTimeStringA(feeDetailDto.getCreateTime());
            }
        }
        return null;
    }
 
 
    /**
     * 获取当前缴费记录
     *
     * @param feeDetailDtos
     * @param curDate
     * @return
     */
    private FeeDetailDto getCurFeeDetail(List<FeeDetailDto> feeDetailDtos, Date curDate) {
        if (feeDetailDtos == null || feeDetailDtos.size() < 1) {
            return null;
        }
        List<FeeDetailDto> tFeeDetailDtos = new ArrayList<>();
        for (FeeDetailDto feeDetailDto : feeDetailDtos) {
            if (feeDetailDto.getStartTime().getTime() <= curDate.getTime() && feeDetailDto.getEndTime().getTime() > curDate.getTime()) {
                tFeeDetailDtos.add(BeanConvertUtil.covertBean(feeDetailDto, FeeDetailDto.class));
            }
        }
 
        if (tFeeDetailDtos.size() < 1) {
            return null;
        }
        if (tFeeDetailDtos.size() == 1) {
            return tFeeDetailDtos.get(0);
        }
        //todo 这种应该是数据异常 也就是一个月费用变更后重复缴费
        BigDecimal cReceivedAmount = new BigDecimal(0.00);
        for (FeeDetailDto feeDetailDto : tFeeDetailDtos) {
            cReceivedAmount = cReceivedAmount.add(new BigDecimal(Double.parseDouble(feeDetailDto.getReceivedAmount()))).setScale(4, BigDecimal.ROUND_HALF_UP);
        }
 
        tFeeDetailDtos.get(0).setReceivedAmount(cReceivedAmount.doubleValue() + "");
 
        return tFeeDetailDtos.get(0);
    }
 
 
    @Override
    public Map<String, MonthFeeDetailDto> analysisMonthFeeDetail(List<FeeDetailDto> feeDetailDtos) {
 
        if (feeDetailDtos == null || feeDetailDtos.size() < 1) {
            return null;
        }
 
        Map<String, MonthFeeDetailDto> monthFeeDetailDtos = new HashMap<>();
 
        for (FeeDetailDto feeDetailDto : feeDetailDtos) {
            Date endTime =  feeDetailDto.getEndTime();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(endTime);
            calendar.add(Calendar.DAY_OF_MONTH,-1);
            if(feeDetailDto.getStartTime().getTime()< calendar.getTime().getTime()){
                endTime = calendar.getTime();
            }
            //计算两个日期包含的月份
            List<String> months = DateUtil.getMonthBetweenDate(feeDetailDto.getStartTime(), endTime);
 
            if (months == null || months.size() < 1) {
                putReceivedAmountToMonthFeeDetailDtos(monthFeeDetailDtos,
                        DateUtil.getFormatTimeString(feeDetailDto.getStartTime(), DateUtil.DATE_FORMATE_STRING_Q),
                        Double.parseDouble(feeDetailDto.getReceivedAmount()),
                        feeDetailDto);
                continue;
            }
 
            BigDecimal totalRecDec = new BigDecimal(feeDetailDto.getReceivedAmount());
            //每月平均值
            BigDecimal priRecDec = totalRecDec.divide(new BigDecimal(months.size()), 4, BigDecimal.ROUND_HALF_UP);
 
            for (String month : months) {
                putReceivedAmountToMonthFeeDetailDtos(monthFeeDetailDtos,
                        month,
                        priRecDec.doubleValue(),
                        feeDetailDto);
            }
 
        }
        return monthFeeDetailDtos;
    }
 
 
    /**
     * 月份存放起来
     *
     * @param monthFeeDetailDtos
     * @param month
     * @param receivedAmount
     */
    private void putReceivedAmountToMonthFeeDetailDtos(Map<String, MonthFeeDetailDto> monthFeeDetailDtos,
                                                       String month,
                                                       double receivedAmount,
                                                       FeeDetailDto feeDetailDto) {
 
        if (!monthFeeDetailDtos.containsKey(month)) {
            monthFeeDetailDtos.put(month, new MonthFeeDetailDto(receivedAmount, feeDetailDto));
            return;
        }
 
        MonthFeeDetailDto monthFeeDetailDto = monthFeeDetailDtos.get(month);
        BigDecimal recDec = new BigDecimal(monthFeeDetailDto.getReceivedAmount()).add(new BigDecimal(receivedAmount)).setScale(4, BigDecimal.ROUND_HALF_UP);
        monthFeeDetailDto.setReceivedAmount(recDec.doubleValue());
        monthFeeDetailDto.getFeeDetailDtos().add(feeDetailDto);
        monthFeeDetailDtos.put(month, monthFeeDetailDto);
    }
 
    /**
     * 月离散数据
     *
     * @param monthFeeDetailDtos
     * @param curDate
     * @return
     */
    private MonthFeeDetailDto getCurMonthFeeDetail(Map<String, MonthFeeDetailDto> monthFeeDetailDtos, Date curDate) {
        String month = DateUtil.getFormatTimeString(curDate, DateUtil.DATE_FORMATE_STRING_M);
        if (monthFeeDetailDtos == null) {
            return null;
        }
        if (!monthFeeDetailDtos.containsKey(month)) {
            return null;
        }
        MonthFeeDetailDto monthFeeDetailDto = monthFeeDetailDtos.get(month);
        return monthFeeDetailDto;
    }
 
 
}