shiyunteng
2026-05-27 0527f90adf2aea086af681fb8f3dbf49c0a5ed31
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
package com.by4cloud.platformx.business.service.impl;
 
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.by4cloud.platformx.business.dto.MeterReadRecordUpdateDTO;
import com.by4cloud.platformx.business.entity.Contract;
import com.by4cloud.platformx.business.entity.ContractSubjectMatter;
import com.by4cloud.platformx.business.entity.MeterReadRecord;
import com.by4cloud.platformx.business.entity.PaymentConfirm;
import com.by4cloud.platformx.business.mapper.ContractMapper;
import com.by4cloud.platformx.business.mapper.ContractSubjectMatterMapper;
import com.by4cloud.platformx.business.mapper.MeterReadRecordMapper;
import com.by4cloud.platformx.business.mapper.PaymentConfirmMapper;
import com.by4cloud.platformx.business.service.MeterReadRecordService;
import com.by4cloud.platformx.common.core.util.R;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
 
/**
 * 水电抄表
 *
 * @author syt
 * @date 2026-05-27 13:58:57
 */
@Service
@RequiredArgsConstructor
public class MeterReadRecordServiceImpl extends ServiceImpl<MeterReadRecordMapper, MeterReadRecord> implements MeterReadRecordService {
 
    private final ContractMapper contractMapper;
    private final ContractSubjectMatterMapper contractSubjectMatterMapper;
    private final PaymentConfirmMapper paymentConfirmMapper;
 
    @Override
    public R edit(MeterReadRecordUpdateDTO updateDTO) {
        MeterReadRecord record = baseMapper.selectById(updateDTO.getId());
        MeterReadRecord lastRecord = baseMapper.selectOne(Wrappers.<MeterReadRecord>lambdaQuery().eq(MeterReadRecord::getContractId,updateDTO.getContractId())
                .eq(MeterReadRecord::getMeterReadCode,updateDTO.getMeterReadCode()).lt(MeterReadRecord::getCreateTime,record.getCreateTime())
                .orderByDesc(MeterReadRecord::getCreateTime).last("limit 1"));
        if(ObjUtil.isNull(lastRecord)){
            return R.failed("无最近抄号记录");
        }
        if (updateDTO.getMeterReadNum().compareTo(lastRecord.getMeterReadNum())<0){
            return R.failed("抄号数字不能比上次少");
        }
        record.setMeterReadTime(updateDTO.getMeterReadTime());
        record.setMeterReadNum(updateDTO.getMeterReadNum());
        record.setMeterReadAttNames(updateDTO.getMeterReadAttNames());
        record.setMeterReadAttPaths(updateDTO.getMeterReadAttPaths());
        baseMapper.updateById(record);
        //房屋租赁生成应收
        Contract contract = contractMapper.selectById(updateDTO.getContractId());
        ContractSubjectMatter subjectMatter = contractSubjectMatterMapper.selectById(updateDTO.getMatterId());
        if(ObjUtil.isNull(lastRecord)){
            R.failed("无合同信息,请联系技术人员");
        }
        PaymentConfirm confirm = new PaymentConfirm();
        confirm.setBusinessType(subjectMatter.getMaterialInternalName()+"应收");
        confirm.setBusGuestId(contract.getPartyAId());
        confirm.setBusGuestName(contract.getPartyA());
        confirm.setContractId(contract.getId());
        confirm.setContractName(contract.getContractName());
        confirm.setContractNo(contract.getContractNo());
        confirm.setConfirmTime(updateDTO.getMeterReadTime());
        confirm.setTransationAmount(subjectMatter.getUnitPrice().multiply(updateDTO.getMeterReadNum().subtract(lastRecord.getMeterReadNum())));
        confirm.setTotalAmount(confirm.getTransationAmount().multiply(new BigDecimal("-1")));
        PaymentConfirm lastConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
                .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
        if (ObjUtil.isNotNull(lastConfirm)) {
            BigDecimal total = lastConfirm.getTotalAmount().add(confirm.getTotalAmount());
            if (total.compareTo(new BigDecimal("0")) > 0) {
                confirm.setAdvanceAmount(total);
                confirm.setTotalAmount(total);
            } else if (total.compareTo(new BigDecimal("0")) == 0) {
                confirm.setTotalAmount(total);
            } else {
                confirm.setReceivableAmount(total.multiply(new BigDecimal("-1")));
                confirm.setTotalAmount(total);
            }
            confirm.setCompId(contract.getCompId());
            paymentConfirmMapper.insert(confirm);
        } else {
            confirm.setReceivableAmount(confirm.getTransationAmount());
            confirm.setTotalAmount(confirm.getReceivableAmount().multiply(new BigDecimal("-1")));
            confirm.setCompId(contract.getCompId());
            paymentConfirmMapper.insert(confirm);
        }
        return R.ok();
    }
}