wujianwei
2025-08-18 82a1cf87de473e970185d6a60291f8087efa5afd
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package com.ruoyi.cwgl.service.impl;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
 
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import javax.annotation.Resource;
 
import com.ruoyi.common.utils.RandomUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.cwgl.domain.EstimatedReceivableBill;
import com.ruoyi.cwgl.domain.PendingSettlementBusiness;
import com.ruoyi.cwgl.mapper.EstimatedReceivableBillMapper;
import com.ruoyi.cwgl.mapper.EstimatedReceivableLogMapper;
import com.ruoyi.cwgl.mapper.PendingSettlementBusinessMapper;
import com.ruoyi.cwgl.service.IEstimatedReceivableLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.scheduling.annotation.Async;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.core.service.BaseService;
 
import com.ruoyi.cwgl.mapper.EstimatedReceivableMapper;
import com.ruoyi.cwgl.domain.EstimatedReceivable;
import com.ruoyi.cwgl.service.IEstimatedReceivableService;
import com.ruoyi.common.core.text.Convert;
 
/**
 * 预估应收管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-07
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class EstimatedReceivableServiceImpl  extends BaseService<EstimatedReceivableMapper, EstimatedReceivable> implements IEstimatedReceivableService
{
    protected final Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private EstimatedReceivableMapper estimatedReceivableMapper;
    @Autowired
    private IEstimatedReceivableLogService logService;
    @Resource
    private PendingSettlementBusinessMapper pendingSettlementBusinessMapper;
    @Resource
    private EstimatedReceivableBillMapper estimatedReceivableBillMapper;
 
    /**
     * 查询预估应收管理
     *
     * @param id 预估应收管理ID
     * @return 预估应收管理
     */
    @DataSource(DataSourceType.SLAVE)
    @Override
    public EstimatedReceivable selectEstimatedReceivableById(Integer id)
    {
        return estimatedReceivableMapper.selectEstimatedReceivableById(id);
    }
 
    /**
     * 查询预估应收管理 记录数
     *
     * @param estimatedReceivable 预估应收管理
     * @return 预估应收管理集合
     */
    @DataSource(DataSourceType.SLAVE)
    @Override
    public int selectEstimatedReceivableCount(EstimatedReceivable estimatedReceivable)
    {
        return estimatedReceivableMapper.selectEstimatedReceivableCount(estimatedReceivable);
    }
 
    /**
     * 查询预估应收管理列表
     *
     * @param estimatedReceivable 预估应收管理
     * @return 预估应收管理
     */
    @DataSource(DataSourceType.SLAVE)
    @Override
    public List<EstimatedReceivable> selectEstimatedReceivableList(EstimatedReceivable estimatedReceivable)
    {
        return estimatedReceivableMapper.selectEstimatedReceivableList(estimatedReceivable);
    }
 
    /**
     * 查询预估应收管理列表 异步 导出
     *
     * @param estimatedReceivable 预估应收管理
     * @param exportKey 导出功能的唯一标识
     * @return 预估应收管理集合
     */
    @DataSource(DataSourceType.SLAVE)
    @Async
    @Override
    public void export(EstimatedReceivable estimatedReceivable,String exportKey) {
 
        super.export(EstimatedReceivable.class,exportKey,"estimatedReceivableData",(pageNum)->{
            PageUtils.startPage(pageNum, Constants.EXPORT_PATE_SIZE);
            return selectEstimatedReceivableList(estimatedReceivable);
        });
    }
 
 
    /**
     * 新增预估应收管理
     *
     * @param estimatedReceivable 预估应收管理
     * @return 结果
     */
    @Override
    public int insertEstimatedReceivable(EstimatedReceivable estimatedReceivable)
    {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd");
 
        Date nowDate = DateUtils.getNowDate();
        String datePart = dateFormat.format(nowDate);
        estimatedReceivable.setCreateTime(nowDate);
        estimatedReceivable.setFeeSystemNo("YF"+datePart+ RandomUtils.random(5));
        estimatedReceivable.setFeeType(0);
        PendingSettlementBusiness pendingSettlementBusiness = pendingSettlementBusinessMapper.selectPendingSettlementBusinessByNo(estimatedReceivable.getDispatchNo());
        if (pendingSettlementBusiness == null){
            throw new ServiceException("调度单不存在");
 
        }
        Integer relatedBillStatus = pendingSettlementBusiness.getRelatedBillStatus();
        if (relatedBillStatus.equals(2)||relatedBillStatus.equals(3)){
            throw new ServiceException("当前调度单已结算或部分结算无法新增明细");
        }
        return estimatedReceivableMapper.insertEstimatedReceivable(estimatedReceivable);
    }
 
    /**
     * 新增预估应收管理[批量]
     *
     * @param estimatedReceivables 预估应收管理
     * @return 结果
     */
    @Override
    public int insertEstimatedReceivableBatch(List<EstimatedReceivable> estimatedReceivables)
    {
        int rows = estimatedReceivableMapper.insertEstimatedReceivableBatch(estimatedReceivables);
        return rows;
    }
 
    /**
     * 修改预估应收管理
     *
     * @param estimatedReceivable 预估应收管理
     * @return 结果
     */
    @Override
    public int updateEstimatedReceivable(EstimatedReceivable estimatedReceivable)
    {
        Integer id = estimatedReceivable.getId();
        EstimatedReceivable estimatedReceivable1 = estimatedReceivableMapper.selectEstimatedReceivableById(id);
        if (estimatedReceivable1.getRelatedBillStatus().equals(2)||estimatedReceivable1.getRelatedBillStatus().equals(3)){
            throw new ServiceException("结算中或已结算无法修改");
        }
        //原本金额
        BigDecimal oldAmount = estimatedReceivable1.getEstimatedAmount();
        //新金额
        BigDecimal newAmount = estimatedReceivable.getEstimatedAmount();
        if (!oldAmount.equals(newAmount)) {
 
              PendingSettlementBusiness pendingSettlementBusiness = pendingSettlementBusinessMapper.selectPendingSettlementBusinessByNo(estimatedReceivable.getDispatchNo());
              if (pendingSettlementBusiness!=null) {
                  BigDecimal estimatedTotalIncome = pendingSettlementBusiness.getEstimatedTotalIncome();
                  pendingSettlementBusiness.setEstimatedTotalIncome(estimatedTotalIncome.subtract(oldAmount).add(newAmount));
                  pendingSettlementBusinessMapper.updatePendingSettlementBusiness(pendingSettlementBusiness);
                  if (pendingSettlementBusiness.getBillId() != null) {
                      EstimatedReceivableBill estimatedReceivableBill = estimatedReceivableBillMapper.selectEstimatedReceivableBillById(pendingSettlementBusiness.getBillId());
                            estimatedReceivableBill.setTotalAmount(estimatedReceivableBill.getTotalAmount().subtract( oldAmount).add( newAmount));
                            estimatedReceivableBillMapper.updateEstimatedReceivableBill(estimatedReceivableBill);
                  }
              }
        }
        estimatedReceivable.setUpdateTime(DateUtils.getNowDate());
        String username = SecurityUtils.getUsername();
        logService.insertEstimatedReceivableLog("修改应收",estimatedReceivable.getId(),username);
        return estimatedReceivableMapper.updateEstimatedReceivable(estimatedReceivable);
    }
 
    /**
     * 修改预估应收管理[批量]
     *
     * @param estimatedReceivables 预估应收管理
     * @return 结果
     */
    @Override
    public int updateEstimatedReceivableBatch(List<EstimatedReceivable> estimatedReceivables){
        return estimatedReceivableMapper.updateEstimatedReceivableBatch(estimatedReceivables);
    }
 
    /**
     * 删除预估应收管理对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteEstimatedReceivableByIds(String ids)
    {
        return deleteEstimatedReceivableByIds(Convert.toIntArray(ids));
    }
 
    /**
     * 删除预估应收管理对象
     *
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteEstimatedReceivableByIds(Integer[] ids)
    {
        return estimatedReceivableMapper.deleteEstimatedReceivableByIds(ids);
    }
 
    /**
     * 删除预估应收管理信息
     *
     * @param id 预估应收管理ID
     * @return 结果
     */
    @Override
    public int deleteEstimatedReceivableById(Integer id)
    {
        return estimatedReceivableMapper.deleteEstimatedReceivableById(id);
    }
 
    @Override
    public int confirm(Integer id) {
        EstimatedReceivable estimatedReceivable = estimatedReceivableMapper.selectEstimatedReceivableById(id);
        if (estimatedReceivable==null){
            throw new ServiceException("数据不存在");
        }
        if (estimatedReceivable.getIsConfirmed().equals(2)) {
            throw new ServiceException("该数据已作废");
 
        }
        if (estimatedReceivable.getIsConfirmed().equals(1)) {
            throw new ServiceException("无需重复确认");
 
        }
        PendingSettlementBusiness pendingSettlementBusiness = pendingSettlementBusinessMapper.selectPendingSettlementBusinessByNo(estimatedReceivable.getDispatchNo());
        if (pendingSettlementBusiness == null){
            throw new ServiceException("调度单不存在");
 
        }
        Integer relatedBillStatus = pendingSettlementBusiness.getRelatedBillStatus();
        if (relatedBillStatus.equals(2)||relatedBillStatus.equals(3)){
            throw new ServiceException("当前调度单已结算或部分结算无法确认");
        }
        if (pendingSettlementBusiness.getBillId()!=null){
            EstimatedReceivableBill estimatedReceivableBill = estimatedReceivableBillMapper.selectEstimatedReceivableBillById(pendingSettlementBusiness.getBillId());
            BigDecimal totalAmount = estimatedReceivableBill.getTotalAmount();
            estimatedReceivableBill.setTotalAmount(totalAmount.add(estimatedReceivable.getEstimatedAmount()));
            estimatedReceivableBillMapper.updateEstimatedReceivableBill(estimatedReceivableBill);
            pendingSettlementBusiness.setEstimatedTotalIncome(pendingSettlementBusiness.getEstimatedTotalIncome().add(estimatedReceivable.getEstimatedAmount()));
            pendingSettlementBusinessMapper.updatePendingSettlementBusiness(pendingSettlementBusiness);
            estimatedReceivable.setRelatedBillStatus(1);
        }
        String username = SecurityUtils.getUsername();
 
        logService.insertEstimatedReceivableLog("确认应收",id,username);
        estimatedReceivable.setIsConfirmed(1);
        estimatedReceivable.setConfirmBy(username);
        estimatedReceivable.setConfirmTime(new Date());
        return estimatedReceivableMapper.updateEstimatedReceivable(estimatedReceivable);
    }
 
    @Override
    public int cancel(Integer id) {
        EstimatedReceivable estimatedReceivable = estimatedReceivableMapper.selectEstimatedReceivableById(id);
        if (estimatedReceivable==null){
            throw new ServiceException("数据不存在");
        }
        if (estimatedReceivable.getIsConfirmed().equals(2)) {
            throw new ServiceException("该数据已作废");
 
        }
        if (estimatedReceivable.getIsConfirmed().equals(0)) {
            throw new ServiceException("无需重复取消");
 
        }
        PendingSettlementBusiness pendingSettlementBusiness = pendingSettlementBusinessMapper.selectPendingSettlementBusinessByNo(estimatedReceivable.getDispatchNo());
        if (pendingSettlementBusiness == null){
            throw new ServiceException("调度单不存在");
 
        }
        Integer relatedBillStatus = pendingSettlementBusiness.getRelatedBillStatus();
        if (relatedBillStatus.equals(2)||relatedBillStatus.equals(3)){
            throw new ServiceException("当前调度单已结算或部分结算无法取消");
        }
        if (pendingSettlementBusiness.getBillId()!=null){
            EstimatedReceivableBill estimatedReceivableBill = estimatedReceivableBillMapper.selectEstimatedReceivableBillById(pendingSettlementBusiness.getBillId());
            BigDecimal totalAmount = estimatedReceivableBill.getTotalAmount();
            estimatedReceivableBill.setTotalAmount(totalAmount.subtract(estimatedReceivable.getEstimatedAmount()));
            estimatedReceivableBillMapper.updateEstimatedReceivableBill(estimatedReceivableBill);
            pendingSettlementBusiness.setEstimatedTotalIncome(pendingSettlementBusiness.getConfirmedTotalIncome().subtract(estimatedReceivable.getEstimatedAmount()));
            pendingSettlementBusinessMapper.updatePendingSettlementBusiness(pendingSettlementBusiness);
            estimatedReceivable.setRelatedBillStatus(0);
        }
        String username = SecurityUtils.getUsername();
 
        logService.insertEstimatedReceivableLog("取消应收",id,username);
        estimatedReceivable.setIsConfirmed(0);
 
        return estimatedReceivableMapper.updateEstimatedReceivable(estimatedReceivable);
    }
 
    @Override
    public int invalid(Integer id) {
        EstimatedReceivable estimatedReceivable = estimatedReceivableMapper.selectEstimatedReceivableById(id);
        if (estimatedReceivable==null){
            throw new ServiceException("数据不存在");
        }
        if (estimatedReceivable.getIsConfirmed().equals(2)) {
            throw new ServiceException("无需重复作废");
 
        }
        PendingSettlementBusiness pendingSettlementBusiness = pendingSettlementBusinessMapper.selectPendingSettlementBusinessByNo(estimatedReceivable.getDispatchNo());
        if (pendingSettlementBusiness == null){
            throw new ServiceException("调度单不存在");
 
        }
        Integer relatedBillStatus = pendingSettlementBusiness.getRelatedBillStatus();
        if (relatedBillStatus.equals(2)||relatedBillStatus.equals(3)){
            throw new ServiceException("当前调度单已结算或部分结算无法作废");
        }
        //确认状态下作废才执行
        if (estimatedReceivable.getIsConfirmed().equals(1)) {
        if (pendingSettlementBusiness.getBillId()!=null){
            EstimatedReceivableBill estimatedReceivableBill = estimatedReceivableBillMapper.selectEstimatedReceivableBillById(pendingSettlementBusiness.getBillId());
            BigDecimal totalAmount = estimatedReceivableBill.getTotalAmount();
            estimatedReceivableBill.setTotalAmount(totalAmount.subtract(estimatedReceivable.getEstimatedAmount()));
            estimatedReceivableBillMapper.updateEstimatedReceivableBill(estimatedReceivableBill);
            pendingSettlementBusiness.setEstimatedTotalIncome(pendingSettlementBusiness.getConfirmedTotalIncome().subtract(estimatedReceivable.getEstimatedAmount()));
            pendingSettlementBusinessMapper.updatePendingSettlementBusiness(pendingSettlementBusiness);
            estimatedReceivable.setRelatedBillStatus(0);
        }
        }
        String username = SecurityUtils.getUsername();
 
        logService.insertEstimatedReceivableLog("作废应收",id,username);
        estimatedReceivable.setIsConfirmed(2);
        return estimatedReceivableMapper.updateEstimatedReceivable(estimatedReceivable);
    }
}