liyong
6 天以前 9d66bfbfcda297f628e6a857e343f98422f4534a
src/main/java/com/ruoyi/purchase/controller/PurchaseLedgerController.java
@@ -1,8 +1,6 @@
package com.ruoyi.purchase.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
@@ -17,20 +15,23 @@
import com.ruoyi.purchase.pojo.PurchaseLedgerTemplate;
import com.ruoyi.purchase.pojo.SalesLedgerProductTemplate;
import com.ruoyi.purchase.service.IPurchaseLedgerService;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import com.ruoyi.sales.service.ISalesLedgerProductService;
import com.ruoyi.sales.service.ISalesLedgerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
 * 采购台账Controller
@@ -41,7 +42,8 @@
@RestController
@RequestMapping("/purchase/ledger")
@AllArgsConstructor
@Api(tags = "采购台账")
@Tag(name = "采购台账")
@Slf4j
public class PurchaseLedgerController extends BaseController {
    private IPurchaseLedgerService purchaseLedgerService;
@@ -51,6 +53,57 @@
    private PurchaseLedgerTemplateMapper purchaseLedgerTemplateMapper;
    private SalesLedgerProductTemplateMapper salesLedgerProductTemplateMapper;
    /**
     * 导入采购台账
     */
    @Log(title = "导入采购台账", businessType = BusinessType.INSERT)
    @PostMapping("/import")
    @Operation(summary = "导入采购台账")
    public AjaxResult importData(@RequestParam("file")
                                 @ApiParam(value = "Excel文件", required = true)
                                         MultipartFile file) {
        return purchaseLedgerService.importData(file);
    }
    @Operation(summary = "导出采购台账模板")
    @PostMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response) {
        // 1. 模板文件在resources/static下的路径
        String templatePath = "static/采购台账导入模板.xlsx";
        // 2. 获取模板文件的输入流
        try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(templatePath)) {
            if (inputStream == null) {
                throw new FileNotFoundException("模板文件不存在:" + templatePath);
            }
            // 3. 设置响应头,触发浏览器下载
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("采购台账导入模板.xlsx", "utf-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName);
            // 4. 将模板文件写入响应输出流
            try (OutputStream outputStream = response.getOutputStream()) {
                byte[] buffer = new byte[1024];
                int len;
                while ((len = inputStream.read(buffer)) > 0) {
                    outputStream.write(buffer, 0, len);
                }
                outputStream.flush();
            }
        } catch (IOException e) {
            log.error("导出销售台账模板失败", e);
            // 若模板文件读取失败,返回错误提示
            try {
                response.getWriter().write("模板导出失败:" + e.getMessage());
            } catch (IOException ex) {
                log.error("响应输出错误", ex);
            }
        }
    }
    /**
     * 查询采购台账列表
@@ -73,19 +126,6 @@
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }
    /**
     * 导出来票登记列表
     */
    @Log(title = "导出来票登记列表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportOne")
    public void exportOne(HttpServletResponse response, PurchaseLedger purchaseLedger) {
        Page page = new Page();
        page.setCurrent(-1);
        page.setSize(-1);
        IPage<PurchaseLedgerDto> purchaseLedgerDtoIPage = purchaseLedgerService.selectPurchaseLedgerListPage(page, new PurchaseLedgerDto());
        ExcelUtil<PurchaseLedgerDto> util = new ExcelUtil<PurchaseLedgerDto>(PurchaseLedgerDto.class);
        util.exportExcel(response, purchaseLedgerDtoIPage.getRecords(), "导出来票登记列表");
    }
    /**
     * 新增修改采购台账
@@ -99,7 +139,7 @@
    /**
     * 查询采购模板
     */
    @ApiOperation("/查询采购模板")
    @Operation(summary = "/查询采购模板")
    @GetMapping("/getPurchaseTemplateList")
    public AjaxResult getPurchaseTemplateList() {
        List<PurchaseLedgerTemplate>  purchaseLedgers = purchaseLedgerTemplateMapper.selectList(null);
@@ -130,6 +170,14 @@
    }
    /**
     * 查询采购台账和产品父子列表
     */
    @GetMapping("/getPurchaseByCode")
    public PurchaseLedgerDto getPurchaseByCode(PurchaseLedgerDto purchaseLedgerDto) {
        return purchaseLedgerService.getPurchaseByCode(purchaseLedgerDto);
    }
    /**
     * 删除采购台账
     */
    @Log(title = "采购台账", businessType = BusinessType.DELETE)
@@ -152,7 +200,7 @@
     */
    @GetMapping("/getProductBySalesNo")
    public AjaxResult getProductBySalesNo(Long id) {
        return AjaxResult.success(purchaseLedgerService.getProductBySalesNo(id));
        return AjaxResult.success();
    }
    /**
@@ -195,7 +243,7 @@
        return AjaxResult.success(purchaseLedgerService.selectPurchaseLedgerListPage(page, purchaseLedger));
    }
    @ApiOperation("生成采购序列号")
    @Operation(summary = "生成采购序列号")
    @GetMapping("/createPurchaseNo")
    @Log(title = "生成采购序列号", businessType = BusinessType.OTHER)
    public AjaxResult createPurchaseNo() {