无关风月
2025-02-13 b0b9a8f3c2840d25c4cfe336bbefaaa56e11b69f
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TContractServiceImpl.java
@@ -1,10 +1,38 @@
package com.ruoyi.system.service.impl;
import com.ruoyi.system.model.TContract;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.constant.DictConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.dto.SignContractDTO;
import com.ruoyi.system.dto.TerminateContractDTO;
import com.ruoyi.system.mapper.TBillMapper;
import com.ruoyi.system.mapper.TCheckAcceptRecordMapper;
import com.ruoyi.system.mapper.TContractMapper;
import com.ruoyi.system.mapper.THouseMapper;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.TContractAppletQuery;
import com.ruoyi.system.query.TContractBillQuery;
import com.ruoyi.system.query.TContractQuery;
import com.ruoyi.system.service.TBillService;
import com.ruoyi.system.service.TContractRentTypeService;
import com.ruoyi.system.service.TContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.vo.BillVO;
import com.ruoyi.system.vo.CheckAcceptRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
 * <p>
@@ -16,5 +44,251 @@
 */
@Service
public class TContractServiceImpl extends ServiceImpl<TContractMapper, TContract> implements TContractService {
    @Resource
    private TCheckAcceptRecordMapper checkAcceptRecordMapper;
    @Resource
    private THouseMapper houseMapper;
    @Resource
    private TBillMapper billMapper;
    @Resource
    private TBillService billService;
    @Resource
    private TContractMapper contractMapper;
    @Override
    public PageInfo<TContract> contractList(TContractQuery query) {
        PageInfo<TContract> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<TContract> list = this.baseMapper.contractList(query,pageInfo);
        for (TContract tContract : list) {
            tContract.setPayType(DictUtils.getDictLabel(DictConstants.DICT_TYPE_CONTRACT_PAY_TYPE,tContract.getPayType()));
            tContract.setStatus(DictUtils.getDictLabel(DictConstants.DICT_TYPE_CONTRACT_STATUS,tContract.getStatus()));
        }
        pageInfo.setRecords(list);
        return pageInfo;
    }
    @Override
    public PageInfo<TContract> contractAppletList(TContractAppletQuery query) {
        PageInfo<TContract> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<TContract> list = this.baseMapper.contractAppletList(query,pageInfo);
        for (TContract tContract : list) {
            tContract.setPayType(DictUtils.getDictLabel(DictConstants.DICT_TYPE_CONTRACT_PAY_TYPE,tContract.getPayType()));
            tContract.setStatus(DictUtils.getDictLabel(DictConstants.DICT_TYPE_CONTRACT_STATUS,tContract.getStatus()));
        }
        pageInfo.setRecords(list);
        return pageInfo;
    }
    @Override
    public PageInfo<BillVO> contractBillList(TContractBillQuery query) {
        PageInfo<BillVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<BillVO> list = this.baseMapper.contractBillList(query,pageInfo);
        for (BillVO billVO : list) {
            if (billVO.getPayFeesStatus().equals("4")){
                billVO.setPayFeesMoneyString((billVO.getPayFeesMoney().add(billVO.getPayableFeesPenalty()))+"【"+billVO.getPayFeesMoney()+"+"+billVO.getPayableFeesPenalty()+"】");
            }
            billVO.setPayFeesStatus(DictUtils.getDictLabel(DictConstants.DICT_TYPE_PAY_FEES_STATUS,billVO.getPayFeesStatus()));
        }
        pageInfo.setRecords(list);
        return pageInfo;
    }
    @Override
    public void terminateContract(TerminateContractDTO dto) {
        TContract contract = this.baseMapper.selectById(dto.getId());
        contract.setTerminateRemark(dto.getTerminateRemark());
        contract.setStatus("4");
        this.baseMapper.updateById(contract);
        // 生成验收记录
        TCheckAcceptRecord tCheckAcceptRecord = new TCheckAcceptRecord();
        tCheckAcceptRecord.setContractId(dto.getId());
        tCheckAcceptRecord.setHouseId(contract.getHouseId());
        tCheckAcceptRecord.setLeaseReason("后台终止");
        tCheckAcceptRecord.setStatus("1");
        checkAcceptRecordMapper.insert(tCheckAcceptRecord);
        // 将所有未缴费账单设置未已失效
        List<TBill> tBills = billMapper.selectList(new LambdaQueryWrapper<TBill>()
                .in(TBill::getPayFeesStatus, Arrays.asList("1,4"))
                .eq(TBill::getContractId, dto.getId()));
        for (TBill tBill : tBills) {
            tBill.setPayFeesStatus("5");
        }
        billService.updateBatchById(tBills);
    }
    @Override
    public CheckAcceptRecordVO getCheckByContractId(String id) {
        CheckAcceptRecordVO checkAcceptRecordVO = new CheckAcceptRecordVO();
        TCheckAcceptRecord tCheckAcceptRecord = checkAcceptRecordMapper.selectOne(new LambdaQueryWrapper<TCheckAcceptRecord>()
                .eq(TCheckAcceptRecord::getContractId, id));
        if (tCheckAcceptRecord==null)return checkAcceptRecordVO;
        BeanUtils.copyProperties(tCheckAcceptRecord,checkAcceptRecordVO);
        THouse tHouse = houseMapper.selectById(tCheckAcceptRecord.getHouseId());
        tHouse.setLeaseStatus(DictUtils.getDictLabel(DictConstants.DICT_TYPE_LEASE_STATUS,tHouse.getLeaseStatus()));
        tHouse.setBusinessAttributes(DictUtils.getDictLabel(DictConstants.DICT_TYPE_BUSINESS_ATTRIBUTES,tHouse.getBusinessAttributes()));
        checkAcceptRecordVO.setHouse(tHouse);
        return checkAcceptRecordVO;
    }
    @Autowired
    private TContractRentTypeService contractRentTypeService;
    @Override
    public R signContract(SignContractDTO dto) {
        TContract contract = contractMapper.selectById(dto.getId());
        if (contract==null)return R.fail("合同不存在");
        if (contract.getStatus().equals("4"))return R.fail("该合同已签订");
        contract.setSignature(dto.getSignature());
        contract.setStatus("2");
        contractMapper.updateById(contract);
        contract.setFirstPayTime(contract.getStartTime().plusDays(10));
        List<TBill> bills = new ArrayList<>();
        List<TContractRentType> contractRentTypes = contractRentTypeService.list();
        // 第一次应缴费日期
        LocalDateTime firstPayTime = contract.getStartTime().plusDays(10).withHour(0).withMinute(0).withSecond(0);
        LocalDate localDate = contract.getStartTime().plusDays(10).toLocalDate();
        LocalDate now = LocalDate.now();
        TBill rentBill = new TBill();
        rentBill.setContractId(contract.getId());
        rentBill.setContractNumber(contract.getContractNumber());
        LocalDateTime startPayTime = contract.getStartPayTime();
        LocalDateTime endTime1 = contract.getEndTime();
//                // 计算两个时间相差多少天
//                // 如果时间小于30天 需要计算每日租金
//                if (days<30){
//                    rentBill.setPayableFeesMoney(contract.getMonthRent().divide(new BigDecimal("30"),2,BigDecimal.ROUND_DOWN).multiply(new BigDecimal(days)));
//                }else{
//                    rentBill.setPayableFeesMoney(contract.getPayType().equals("1")?contract.getMonthRent():
//                            contract.getPayType().equals("2")?contract.getMonthRent().multiply(new BigDecimal("3")):contract.getMonthRent().multiply(new BigDecimal("12")).setScale(2,BigDecimal.ROUND_DOWN));
//                }
        rentBill.setPayableFeesTime(firstPayTime);
        rentBill.setPayFeesStatus("1");
        rentBill.setBillType("1");
        rentBill.setStartTime(contract.getStartPayTime());
        TContractRentType tContractRentType = contractRentTypes.stream().filter(e -> e.getContractId().equals(contract.getId())).findFirst().orElse(null);
        if (tContractRentType!=null && contract.getStartPayTime().plusMonths(contract.getPayType().equals("1")? 1:contract.getPayType().equals("2")? 3:12).isAfter(tContractRentType.getChangeTime())){
            // 计算租金变动的天数
            long moneyDays = ChronoUnit.DAYS.between(tContractRentType.getChangeTime(), contract.getStartPayTime().plusMonths(contract.getPayType().equals("1")? 1:contract.getPayType().equals("2")? 3:12))+1L;
            contract.setChangeTime(LocalDateTime.now());
            // 递增递减的租金
            BigDecimal contractRentTypeMoney = new BigDecimal("0");
            // 不递增递减的租金
            BigDecimal originalMoney = new BigDecimal("0");
            // 原租金
            switch (tContractRentType.getIncreasingDecreasingType()){
                case 1:
                    switch (tContractRentType.getIncreasingDecreasing()){
                        case 1:
                            contractRentTypeMoney =contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).add(tContractRentType.getNumericalValue())).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(moneyDays)));
                            contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).add(tContractRentType.getNumericalValue())).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)));
                            break;
                        case 2:
                            contractRentTypeMoney = contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).subtract(tContractRentType.getNumericalValue())).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(moneyDays)));
                            contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).subtract(tContractRentType.getNumericalValue())).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)));
                            break;
                    }
                    break;
                case 2:
                    switch (tContractRentType.getIncreasingDecreasing()){
                        case 1:
                            contractRentTypeMoney =contractRentTypeMoney.add(contract.getChangeRent().add(tContractRentType.getNumericalValue())).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(moneyDays));
                            contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().add(tContractRentType.getNumericalValue())));
                            break;
                        case 2:
                            contractRentTypeMoney = contractRentTypeMoney.add(contract.getChangeRent().subtract(tContractRentType.getNumericalValue())).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(moneyDays));
                            contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().subtract(tContractRentType.getNumericalValue())));
                            break;
                    }
                    break;
            }
            // 不需要涨租金的时间段
            long originalDays = ChronoUnit.DAYS.between(contract.getFirstPayTime(), tContractRentType.getChangeTime());
            originalMoney=originalMoney.add(contract.getMonthRent().divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN))
                    .multiply(new BigDecimal(originalDays));
            rentBill.setPayableFeesMoney(contractRentTypeMoney.add(originalMoney));
            rentBill.setOutstandingMoney(rentBill.getPayableFeesMoney());
            if (contract.getFirstPayTime().plusMonths(contract.getPayType().equals("1")? 1:contract.getPayType().equals("2")? 3:12).isAfter(contract.getEndTime())){
                rentBill.setEndTime(contract.getFirstPayTime().plusMonths(contract.getPayType().equals("1")? 1:contract.getPayType().equals("2")? 3:12));
            }else{
                rentBill.setEndTime(contract.getEndTime());
            }
        }else{
            if (contract.getFirstPayTime().plusMonths(contract.getPayType().equals("1")? 1:contract.getPayType().equals("2")? 3:12).isAfter(contract.getEndTime())){
                rentBill.setEndTime(contract.getFirstPayTime().plusMonths(contract.getPayType().equals("1")? 1:contract.getPayType().equals("2")? 3:12));
            }else{
                rentBill.setEndTime(contract.getEndTime());
            }
            // 不走递增递减
            long allDays = ChronoUnit.DAYS.between(contract.getFirstPayTime(), rentBill.getEndTime());
            rentBill.setPayableFeesMoney(contract.getMonthRent().divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(allDays)));
            rentBill.setOutstandingMoney(rentBill.getPayableFeesMoney());
        }
        // 租金账单
        bills.add(rentBill);
        // 押金账单
        TBill depositBill = new TBill();
        depositBill.setContractId(contract.getId());
        depositBill.setContractNumber(contract.getContractNumber());
        depositBill.setPayableFeesMoney(contract.getDeposit());
        depositBill.setOutstandingMoney(depositBill.getPayableFeesMoney());
        depositBill.setPayableFeesTime(firstPayTime);
        depositBill.setPayFeesStatus("1");
        depositBill.setBillType("2");
        bills.add(depositBill);
        this.updateById(contract);
        billService.saveBatch(bills);
        // 用户签订合同后 生成第一批账单包含租金账单和押金账单 后续账单通过定时任务生成
//        TBill rent = new TBill();
//        rent.setContractId(contract.getId());
//        // 应缴费租金
//        BigDecimal payableFeesMoney = new BigDecimal("0");
//        LocalDateTime startTime = contract.getStartTime();
//        LocalDateTime endTime = contract.getEndTime();
//        // 计算相差多少天
//        long days = ChronoUnit.DAYS.between(startTime, endTime);
//        // 计算相差多少个月
//        long months = ChronoUnit.MONTHS.between(startTime, endTime);
//        if (months<=31){
//            // 小于等于一个月 合计租金就是首笔账单金额
//            payableFeesMoney = contract.getTotalRent();
//        }else{
//            switch (contract.getPayType()){
//                case "1":
//                    break;
//                case "2":
//                    break;
//                case "3":
//                    break;
//            }
//        }
//
//        rent.setPayableFeesMoney(payableFeesMoney);
//        rent.setPayableFeesTime(contract.getFirstPayTime());
//        rent.setPayFeesStatus("1");
//        rent.setBillType("1");
//
//        TBill deposit = new TBill();
//        deposit.setContractId(contract.getId());
//        deposit.setPayableFeesMoney(contract.getDeposit());
//        deposit.setPayableFeesTime(contract.getFirstPayTime());
//        deposit.setPayFeesStatus("1");
//        deposit.setBillType("2");
        return R.ok();
    }
    @Override
    public void export(TContractQuery query) {
    }
    @Override
    public List<TContract> contractExportList(TContractQuery query) {
        List<TContract> list = this.baseMapper.contractExportList(query);
        return list;
    }
}