3 天以前 4cceee2ced8d9ed1d8e8d08213372cc8a77ac601
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -8,6 +8,12 @@
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.account.pojo.AccountExpense;
import com.ruoyi.account.pojo.AccountIncome;
import com.ruoyi.account.service.AccountIncomeService;
import com.ruoyi.approve.pojo.ApproveProcess;
import com.ruoyi.approve.service.IApproveProcessService;
import com.ruoyi.approve.vo.ApproveProcessVO;
import com.ruoyi.basic.mapper.CustomerMapper;
import com.ruoyi.basic.pojo.Customer;
import com.ruoyi.common.enums.FileNameType;
@@ -16,8 +22,12 @@
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.other.mapper.TempFileMapper;
import com.ruoyi.other.pojo.TempFile;
import com.ruoyi.production.mapper.SalesLedgerSchedulingMapper;
import com.ruoyi.production.pojo.SalesLedgerScheduling;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.mapper.SysDeptMapper;
import com.ruoyi.purchase.dto.PurchaseLedgerDto;
import com.ruoyi.purchase.pojo.PurchaseLedger;
import com.ruoyi.sales.dto.MonthlyAmountDto;
import com.ruoyi.sales.dto.SalesLedgerDto;
import com.ruoyi.sales.mapper.*;
@@ -31,6 +41,7 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -41,6 +52,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
@@ -60,8 +72,11 @@
@RequiredArgsConstructor
@Slf4j
public class SalesLedgerServiceImpl extends ServiceImpl<SalesLedgerMapper, SalesLedger> implements ISalesLedgerService {
    private final AccountIncomeService accountIncomeService;
    private final SalesLedgerMapper salesLedgerMapper;
    private final IApproveProcessService approveProcessService;
    private final CustomerMapper customerMapper;
@@ -74,6 +89,8 @@
    private final ReceiptPaymentMapper receiptPaymentMapper;
    private final InvoiceLedgerMapper invoiceLedgerMapper;
    private final SalesLedgerSchedulingMapper salesLedgerSchedulingMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
@@ -200,53 +217,77 @@
    }
    @Override
    public List<MonthlyAmountDto> getAmountHalfYear() {
    public List<MonthlyAmountDto> getAmountHalfYear(Integer type) {
        LocalDate now = LocalDate.now();
        YearMonth currentMonth = YearMonth.from(now);
        LocalDateTime currentDateTime = LocalDateTime.now();
        List<MonthlyAmountDto> monthlyAmounts = new ArrayList<>();
        // 根据 type 确定查询的时间间隔(天数)和总查询天数
        int daysPerPeriod;
        int totalDays;
        switch (type) {
            case 1:
                daysPerPeriod = 5;   // 每5天查一次
                totalDays = 30;       // 6次 × 5天 = 30天
                break;
            case 2:
                daysPerPeriod = 15;    // 每15天查一次
                totalDays = 90;       // 6次 × 15天 = 90天
                break;
            case 3:
                daysPerPeriod = 30;   // 每30天查一次
                totalDays = 180;      // 6次 × 30天 = 180天
                break;
            default:
                throw new IllegalArgumentException("Invalid type value: " + type);
        }
        List<MonthlyAmountDto> result = new ArrayList<>();
        // 循环6次,每次查询一个时间段的数据
        for (int i = 0; i < 6; i++) {
            YearMonth targetMonth = currentMonth.minusMonths(i);
            LocalDate firstDayOfMonth = targetMonth.atDay(1);
            LocalDate firstDayOfNextMonth = targetMonth.plusMonths(1).atDay(1);
            // 计算当前时间段的起始和结束时间
            LocalDateTime endTime = currentDateTime.minusDays(i * daysPerPeriod);
            LocalDateTime startTime = endTime.minusDays(daysPerPeriod);
            LocalDateTime startOfMonth = firstDayOfMonth.atStartOfDay();
            LocalDateTime startOfNextMonth = firstDayOfNextMonth.atStartOfDay();
            // 查询回款金额
            LambdaQueryWrapper<ReceiptPayment> receiptPaymentQuery = new LambdaQueryWrapper<>();
            receiptPaymentQuery
                    .ge(ReceiptPayment::getCreateTime, startTime)
                    .lt(ReceiptPayment::getCreateTime, endTime);
            List<ReceiptPayment> receiptPayments = receiptPaymentMapper.selectList(receiptPaymentQuery);
            LambdaQueryWrapper<ReceiptPayment> receiptPaymentLambdaQueryWrapper = new LambdaQueryWrapper<>();
            receiptPaymentLambdaQueryWrapper.ge(ReceiptPayment::getCreateTime, startOfMonth)
                    .lt(ReceiptPayment::getCreateTime, startOfNextMonth);
            // 查询开票金额
            LambdaQueryWrapper<InvoiceLedger> invoiceLedgerQuery = new LambdaQueryWrapper<>();
            invoiceLedgerQuery
                    .ge(InvoiceLedger::getCreateTime, startTime)
                    .lt(InvoiceLedger::getCreateTime, endTime);
            List<InvoiceLedger> invoiceLedgers = invoiceLedgerMapper.selectList(invoiceLedgerQuery);
            LambdaQueryWrapper<InvoiceLedger> invoiceLedgerLambdaQueryWrapper = new LambdaQueryWrapper<>();
            invoiceLedgerLambdaQueryWrapper.ge(InvoiceLedger::getCreateTime, startOfMonth)
                    .lt(InvoiceLedger::getCreateTime, startOfNextMonth);
            // 获取回款金额
            List<ReceiptPayment> receiptPaymentList = receiptPaymentMapper.selectList(receiptPaymentLambdaQueryWrapper);
            //开票金额
            List<InvoiceLedger> invoiceLedgerList = invoiceLedgerMapper.selectList(invoiceLedgerLambdaQueryWrapper);
            // 使用 Stream 求和
            BigDecimal invoiceAmount = invoiceLedgerList.stream()
                    .map(InvoiceLedger::getInvoiceTotal)
                    .filter(Objects::nonNull)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal receiptAmount = receiptPaymentList.stream()
            // 计算回款总额
            BigDecimal receiptAmount = receiptPayments.stream()
                    .map(ReceiptPayment::getReceiptPaymentAmount)
                    .filter(Objects::nonNull)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            MonthlyAmountDto monthlyAmount = new MonthlyAmountDto();
            monthlyAmount.setMonth(targetMonth.format(DateTimeFormatter.ofPattern("yyyy-MM")));
            monthlyAmount.setInvoiceAmount(invoiceAmount);
            monthlyAmount.setReceiptAmount(receiptAmount);
            // 计算开票总额
            BigDecimal invoiceAmount = invoiceLedgers.stream()
                    .map(InvoiceLedger::getInvoiceTotal)
                    .filter(Objects::nonNull)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            monthlyAmounts.add(monthlyAmount);
            // 构造返回的 DTO
            MonthlyAmountDto dto = new MonthlyAmountDto();
            dto.setMonth(startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " ~ " +
                    endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            dto.setReceiptAmount(receiptAmount);
            dto.setInvoiceAmount(invoiceAmount);
            result.add(dto);
        }
        Collections.reverse(monthlyAmounts);
        return monthlyAmounts;
        // 反转列表,使时间顺序从早到晚
        Collections.reverse(result);
        return result;
    }
    @Override
@@ -312,16 +353,38 @@
        List<Long> idList = Arrays.stream(ids)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(idList)) {
            return 0;
        }
        // 生产订单有待排产数据,台账不可删除
        LambdaQueryWrapper<SalesLedgerScheduling> salesLedgerSchedulingLambdaQueryWrapper = new LambdaQueryWrapper<SalesLedgerScheduling>()
                .in(SalesLedgerScheduling::getSalesLedgerId, idList);
        if (salesLedgerSchedulingMapper.selectCount(salesLedgerSchedulingLambdaQueryWrapper) > 0) {
            throw new BaseException("有排产数据,不可删除");
        }
        // 1. 先删除子表数据
        LambdaQueryWrapper<SalesLedgerProduct> productWrapper = new LambdaQueryWrapper<>();
        productWrapper.in(SalesLedgerProduct::getSalesLedgerId, idList);
        salesLedgerProductMapper.delete(productWrapper);
        // 删除生产订单数据
        LambdaQueryWrapper<SalesLedgerScheduling> in = new LambdaQueryWrapper<SalesLedgerScheduling>()
                .in(SalesLedgerScheduling::getSalesLedgerId, idList);
        salesLedgerSchedulingMapper.delete(in);
        // 2. 再删除主表数据
        // 2. 删除关联的审批表
        List<String> strings = salesLedgerMapper.selectBatchIds(idList).stream().map(SalesLedger::getSalesContractNo).distinct().collect(Collectors.toList());
        List<ApproveProcess> list = approveProcessService.list(Wrappers.<ApproveProcess>lambdaQuery()
                .eq(ApproveProcess::getApproveType, 6)
                .in(ApproveProcess::getApproveReason, strings));
        if (CollectionUtils.isNotEmpty(list)){
            List<String> approveIds = list.stream().map(ApproveProcess::getApproveId).distinct().collect(Collectors.toList());
            Long[] ides = approveIds.stream()
                    .filter(s -> s != null && !s.isEmpty()) // 过滤空字符串
                    .map(Long::valueOf)
                    .toArray(Long[]::new);
            approveProcessService.delApprove(ides);
        }
        // 3. 再删除主表数据
        return salesLedgerMapper.deleteBatchIds(idList);
    }
@@ -340,13 +403,38 @@
            salesLedger.setCustomerName(customer.getCustomerName());
            salesLedger.setTenantId(customer.getTenantId());
            AccountIncome accountIncome = new AccountIncome();
            accountIncome.setIncomeDate(salesLedger.getEntryDate());
            accountIncome.setIncomeType("0");
            accountIncome.setCustomerName(customer.getCustomerName());
            accountIncome.setIncomeMoney(salesLedger.getContractAmount());
            accountIncome.setIncomeMethod("0");
            accountIncome.setInputTime(new Date());
            accountIncome.setInputUser(salesLedger.getEntryPerson());
            // 3. 新增或更新主表
            if (salesLedger.getId() == null) {
                String contractNo = generateSalesContractNo();
                salesLedger.setSalesContractNo(contractNo);
                salesLedgerMapper.insert(salesLedger);
                accountIncome.setIncomeDescribed("销售合同:" + salesLedger.getSalesContractNo());
                accountIncome.setInvoiceNumber(salesLedger.getSalesContractNo());
                accountIncomeService.save(accountIncome);
            } else {
                salesLedgerMapper.updateById(salesLedger);
                SalesLedger salesLedgerDB = salesLedgerMapper.selectById(salesLedger.getId());
                List<AccountIncome> accountIncomeDBs = accountIncomeService.getByInvoiceNumberList(salesLedger.getSalesContractNo());
                if (!CollectionUtils.isEmpty(accountIncomeDBs)) {
                    accountIncomeDBs.forEach(accountIncomeDB ->{
                        accountIncomeDB.setCustomerName(salesLedgerDB.getCustomerName());
                        accountIncomeDB.setIncomeMoney(salesLedgerDB.getContractAmount());
                        accountIncomeDB.setIncomeDescribed("销售合同:" + salesLedgerDB.getSalesContractNo());
                        accountIncomeDB.setInvoiceNumber(salesLedgerDB.getSalesContractNo());
                        accountIncomeDB.setInputTime(new Date());
                        accountIncomeDB.setInputUser(salesLedgerDB.getEntryPerson());
                        accountIncomeService.updateById(accountIncomeDB);
                    });
                }
            }
            // 4. 处理子表数据
@@ -366,10 +454,58 @@
            if (salesLedgerDto.getTempFileIds() != null && !salesLedgerDto.getTempFileIds().isEmpty()) {
                migrateTempFilesToFormal(salesLedger.getId(), salesLedgerDto.getTempFileIds());
            }
            //6.销售订单按紧急程度分类,如普通订单以及紧急订单;普通订单需流转至业务单据进行审批,紧急订单无需审批。
            if (salesLedger.getSalesType().equals("普通")){
                //新增审批数据approve_process
                ApproveProcessVO approveProcessVO = new ApproveProcessVO();
                approveProcessVO.setApproveDeptId(salesLedger.getTenantId());
                approveProcessVO.setApproveReason(salesLedger.getSalesContractNo());//审批理由是销售合同号用这个来关联
                DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                approveProcessVO.setApproveTime(LocalDate.now().format(dateFormat));
                approveProcessVO.setApproveType(6);//6是销售台账
                approveProcessVO.setApproveUser(Long.parseLong(salesLedger.getEntryPerson()));//录入人=申请人
                approveProcessVO.setApproveUserIds(salesLedgerDto.getApproveUserIds());//审批人
                try {
                    approveProcessService.addApprove(approveProcessVO);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else {
                //紧急默认通过
                salesLedger.setApprovalStatus(2);
                salesLedgerMapper.updateById(salesLedger);
            }
            return 1;
        } catch (IOException e) {
            throw new BaseException("文件迁移失败: " + e.getMessage());
        }
    }
    @Override
    public SalesLedgerDto getSalesByCode(SalesLedgerDto salesLedgerDto) {
        // 1. 查询主表
        SalesLedger salesLedger = salesLedgerMapper.selectOne(new LambdaQueryWrapper<SalesLedger>()
                .eq(SalesLedger::getSalesContractNo, salesLedgerDto.getSalesContractNo())
                .last("LIMIT 1"));
        if (salesLedger == null) {
            throw new BaseException("销售台账不存在");
        }
        // 2. 查询子表
        LambdaQueryWrapper<SalesLedgerProduct> productWrapper = new LambdaQueryWrapper<>();
        productWrapper.eq(SalesLedgerProduct::getSalesLedgerId, salesLedger.getId())
                .eq(SalesLedgerProduct::getType, 1);
        List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(productWrapper);
        // 4. 转换 DTO
        SalesLedgerDto resultDto = new SalesLedgerDto();
        BeanUtils.copyProperties(salesLedger, resultDto);
        if (!products.isEmpty()) {
            resultDto.setHasChildren(true);
            resultDto.setProductData(products);
        }
        return resultDto;
    }
    // 文件迁移方法
@@ -416,12 +552,15 @@
            try {
                // 执行文件迁移(使用原子操作确保安全性)
                Files.move(
                        Paths.get(tempFile.getTempPath()),
                        formalFilePath,
                        StandardCopyOption.REPLACE_EXISTING,
                        StandardCopyOption.ATOMIC_MOVE
                );
//                Files.move(
//                        Paths.get(tempFile.getTempPath()),
//                        formalFilePath,
//                        StandardCopyOption.REPLACE_EXISTING,
//                        StandardCopyOption.ATOMIC_MOVE
//                );
                // 原子移动失败,使用复制+删除
                Files.copy(Paths.get(tempFile.getTempPath()), formalFilePath, StandardCopyOption.REPLACE_EXISTING);
                Files.deleteIfExists(Paths.get(tempFile.getTempPath()));
                log.info("文件迁移成功: {} -> {}", tempFile.getTempPath(), formalFilePath);
                // 更新文件记录(关联到业务ID)