package com.ruoyi.account.service.impl.purchase;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
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.bean.dto.purchase.AccountPaymentApplicationDto;
import com.ruoyi.account.bean.vo.purchase.AccountPaymentApplicationVo;
import com.ruoyi.account.bean.vo.purchase.PurchaseInboundVo;
import com.ruoyi.account.mapper.purchase.AccountPaymentApplicationMapper;
import com.ruoyi.account.mapper.purchase.AccountPurchasePaymentMapper;
import com.ruoyi.account.pojo.purchase.AccountPaymentApplication;
import com.ruoyi.account.pojo.purchase.AccountPurchasePayment;
import com.ruoyi.account.service.purchase.AccountPaymentApplicationService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
/**
*
* 财务管理--付款申请 服务实现类
*
*
* @author 芯导软件(江苏)有限公司
* @since 2026-05-19 03:44:22
*/
@Service
@RequiredArgsConstructor
public class AccountPaymentApplicationServiceImpl extends ServiceImpl implements AccountPaymentApplicationService {
private final AccountPaymentApplicationMapper accountPaymentApplicationMapper;
private final AccountPurchasePaymentMapper accountPurchasePaymentMapper;
private static final DateTimeFormatter CODE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyMMddHHmmss");
@Override
public IPage listPageAccountPaymentApplication(Page page, AccountPaymentApplicationDto accountPaymentApplicationDto) {
return accountPaymentApplicationMapper.listPageAccountPaymentApplication(page, accountPaymentApplicationDto);
}
@Override
public List getInboundBatchesBySupplier(Integer supplierId) {
return accountPaymentApplicationMapper.getInboundBatchesBySupplier(supplierId);
}
@Override
public boolean addAccountPaymentApplication(AccountPaymentApplication accountPaymentApplication) {
if (StringUtils.isEmpty(accountPaymentApplication.getInvoiceApplicationNo())) {
accountPaymentApplication.setInvoiceApplicationNo(genPaymentApplicationNo());
}
String stockInRecordIds= accountPaymentApplication.getStockInRecordIds();
if (stockInRecordIds != null && !stockInRecordIds.isEmpty()) {
List ids = Arrays.stream(stockInRecordIds.split(","))
.map(Long::valueOf)
.toList();
if (accountPaymentApplicationMapper.existsByStockInRecordId(ids)){
throw new ServiceException("存在重复的入库单");
}
}
return save(accountPaymentApplication);
}
@Override
public boolean deleteAccountPaymentApplication(List ids) {
if (ids == null || ids.isEmpty()) {
throw new ServiceException("删除失败,请选择要删除的数据");
}
//判断是否已经有对应的付款单,如果有则无法删除
List accountPurchasePayments = accountPurchasePaymentMapper.selectList(Wrappers.lambdaQuery().in(AccountPurchasePayment::getAccountPaymentApplicationId, ids));
if (CollectionUtils.isNotEmpty(accountPurchasePayments)){
throw new ServiceException("删除失败,已经有关联的付款单");
}
//删除开票申请
return removeBatchByIds(ids);
}
@Override
public void exportAccountPaymentApplication(HttpServletResponse response, AccountPaymentApplicationDto accountPaymentApplicationDto) {
List list = accountPaymentApplicationMapper.listPageAccountPaymentApplication(new Page(1,-1),accountPaymentApplicationDto).getRecords();
ExcelUtil util = new ExcelUtil<>(AccountPaymentApplicationVo.class);
util.exportExcel(response, list , "付款申请");
}
private String genPaymentApplicationNo() {
return "FK" + LocalDateTime.now().format(CODE_TIME_FORMATTER) + new Random().nextInt(10);
}
}