gongchunyi
15 小时以前 18fa07479d3c7f5a9b683ab0f698528d9bd2a9ec
src/main/java/com/ruoyi/sales/controller/SalesLedgerController.java
@@ -1,46 +1,137 @@
package com.ruoyi.sales.controller;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.basic.pojo.Customer;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.sales.dto.SalesLedgerDto;
import com.ruoyi.sales.pojo.SalesLedger;
import com.ruoyi.sales.service.ISalesLedgerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.domain.R;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.sales.dto.*;
import com.ruoyi.sales.mapper.InvoiceLedgerMapper;
import com.ruoyi.sales.pojo.SalesLedger;
import com.ruoyi.sales.pojo.SalesLedgerProcessRoute;
import com.ruoyi.sales.service.ICommonFileService;
import com.ruoyi.sales.service.ISalesLedgerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
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.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;
/**
 * 销售台账Controller
 *
 *
 * @author ruoyi
 * @date 2025-05-08
 */
@RestController
@RequestMapping("/sales/ledger")
public class SalesLedgerController extends BaseController
{
    @Autowired
@AllArgsConstructor
@Api(tags = "销售台账")
@Slf4j
public class SalesLedgerController extends BaseController {
    private ISalesLedgerService salesLedgerService;
    private ICommonFileService commonFileService;
    @Autowired
    private InvoiceLedgerMapper invoiceLedgerMapper;
    /**
     * 导入销售台账
     */
    @Log(title = "导入销售台账", businessType = BusinessType.INSERT)
    @PostMapping("/import")
    @ApiOperation("导入销售台账")
    public AjaxResult importData(@RequestParam("file")
                                 @ApiParam(value = "Excel文件", required = true)
                                 MultipartFile file) {
        salesLedgerService.importData(file);
        return AjaxResult.success();
    }
    @ApiOperation("导出销售台账模板")
    @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);
            }
        }
    }
    /**
     * 查询销售台账列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SalesLedgerDto salesLedgerDto)
    {
    public TableDataInfo list(Page<?> page, SalesLedgerDto salesLedgerDto) {
        startPage();
        List<SalesLedger> list = salesLedgerService.selectSalesLedgerList(salesLedgerDto);
        // 计算已开票金额/未开票金额(已填写发票金额为准)
        if (CollectionUtils.isEmpty(list)) {
            return getDataTable(list);
        }
        List<Long> salesLedgerIds = list.stream().map(SalesLedger::getId).collect(Collectors.toList());
        List<InvoiceLedgerDto> invoiceLedgerDtoList = invoiceLedgerMapper.invoicedTotal(salesLedgerIds);
        if (CollectionUtils.isEmpty(invoiceLedgerDtoList)) {
            return getDataTable(list);
        }
        for (SalesLedger salesLedger : list) {
            for (InvoiceLedgerDto invoiceLedgerDto : invoiceLedgerDtoList) {
                if (salesLedger.getId().intValue() == invoiceLedgerDto.getSalesLedgerId()) {
                    BigDecimal noInvoiceAmountTotal = salesLedger.getContractAmount().subtract(invoiceLedgerDto.getInvoiceTotal());
                    salesLedger.setNoInvoiceAmountTotal(noInvoiceAmountTotal);
                }
            }
        }
        return getDataTable(list);
    }
@@ -48,8 +139,8 @@
     * 查询销售台账和产品父子列表
     */
    @GetMapping("/getSalesLedgerWithProducts")
    public SalesLedgerDto getSalesLedgerWithProducts(SalesLedgerDto salesLedgerDto){
        return  salesLedgerService.getSalesLedgerWithProducts(salesLedgerDto);
    public SalesLedgerDto getSalesLedgerWithProducts(SalesLedgerDto salesLedgerDto) {
        return salesLedgerService.getSalesLedgerWithProducts(salesLedgerDto);
    }
    /**
@@ -57,33 +148,205 @@
     */
    @Log(title = "销售台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SalesLedgerDto salesLedgerDto)
    {
        List<SalesLedger> list = salesLedgerService.selectSalesLedgerList(salesLedgerDto);
    public void export(HttpServletResponse response, SalesLedgerDto salesLedgerDto) {
        Page<?> page = new Page<>(-1, -1);
        IPage<SalesLedger> salesLedgerIPage = listPage(page, salesLedgerDto);
        ExcelUtil<SalesLedger> util = new ExcelUtil<SalesLedger>(SalesLedger.class);
        if (salesLedgerIPage == null) {
            util.exportExcel(response, new ArrayList<>(), "销售台账数据");
            return;
        }
        List<SalesLedger> list = salesLedgerIPage.getRecords();
        util.exportExcel(response, list, "销售台账数据");
    }
    /**
     * 导出开票登记列表
     */
    @Log(title = "导出开票登记列表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportOne")
    public void exportOne(HttpServletResponse response, SalesLedgerDto salesLedgerDto) {
        Page<?> page = new Page<>();
        page.setCurrent(-1);
        page.setSize(-1);
        IPage<SalesLedger> salesLedgerIPage = listPage(page, salesLedgerDto);
        ExcelUtil<SalesLedger> util = new ExcelUtil<SalesLedger>(SalesLedger.class);
        util.exportExcel(response, salesLedgerIPage == null ? new ArrayList<>() : salesLedgerIPage.getRecords(), "导出开票登记列表");
    }
    /**
     * 新增修改销售台账
     */
    @Log(title = "销售台账", businessType = BusinessType.INSERT)
    @PostMapping ("/addOrUpdateSalesLedger")
    public AjaxResult add(@RequestBody SalesLedgerDto salesLedgerDto)
    {
    @PostMapping("/addOrUpdateSalesLedger")
    public AjaxResult add(@RequestBody SalesLedgerDto salesLedgerDto) {
        return toAjax(salesLedgerService.addOrUpdateSalesLedger(salesLedgerDto));
    }
    /**
     * 销售订单绑定工艺路线
     */
    @PostMapping("/saleProcessBind")
    @ApiOperation("销售订单绑定工艺路线")
    public AjaxResult saleProcessBind(@RequestBody SalesLedgerProcessRoute salesLedgerProcessRoute) {
        salesLedgerService.saleProcessBind(salesLedgerProcessRoute);
        return AjaxResult.success();
    }
    /**
     * 删除销售台账
     */
    @Log(title = "销售台账", businessType = BusinessType.DELETE)
   @DeleteMapping("/delLedger")
    public AjaxResult remove(@RequestBody Long[] ids)
    {
    @DeleteMapping("/delLedger")
    public AjaxResult remove(@RequestBody Long[] ids) {
        if (ids == null || ids.length == 0) {
            return AjaxResult.error("请传入要删除的ID");
        }
        return toAjax(salesLedgerService.deleteSalesLedgerByIds(ids));
    }
    /**
     * 查询销售台账不分页
     *
     * @param salesLedgerDto
     * @return
     */
    @GetMapping("/listNoPage")
    public AjaxResult listNoPage(SalesLedgerDto salesLedgerDto) {
        List<SalesLedger> list = salesLedgerService.selectSalesLedgerList(salesLedgerDto);
        return AjaxResult.success(list);
    }
    /**
     * 销售台账附件删除
     */
    @Log(title = "销售台账附件删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/delLedgerFile")
    public AjaxResult delLedgerFile(@RequestBody Long[] ids) {
        if (ids == null || ids.length == 0) {
            return AjaxResult.error("请传入要删除的ID");
        }
        return toAjax(commonFileService.deleteSalesLedgerByIds(ids));
    }
    /**
     * 本月销售合同金额
     */
    @GetMapping("/getContractAmount")
    public AjaxResult getContractAmount() {
        try {
            BigDecimal contractAmount = salesLedgerService.getContractAmount();
            return AjaxResult.success(contractAmount != null ? contractAmount : BigDecimal.ZERO);
        } catch (Exception e) {
            return AjaxResult.error("获取合同金额失败:" + e.getMessage());
        }
    }
    /**
     * 客户合同金额TOP5统计
     */
    @GetMapping("/getTopFiveList")
    public AjaxResult getTopFiveList() {
        return AjaxResult.success(salesLedgerService.getTopFiveList());
    }
    /**
     * 近半年开票,回款金额
     */
    @GetMapping("/getAmountHalfYear")
    public AjaxResult getAmountHalfYear(@RequestParam(value = "type", defaultValue = "1") Integer type) {
        return AjaxResult.success(salesLedgerService.getAmountHalfYear(type));
    }
    /**
     * 查询销售台账列表
     */
    @GetMapping("/listPage")
    public IPage<SalesLedger> listPage(Page<?> page, SalesLedgerDto salesLedgerDto) {
        return salesLedgerService.selectSalesLedgerListPage(page, salesLedgerDto);
    }
    @ApiOperation("查询销售台账消耗物料信息")
    @GetMapping("/getSalesLedgerWithProductsLoss")
    public R getSalesLedgerWithProductsLoss(Long salesLedgerId) {
        return R.ok(salesLedgerService.getSalesLedgerWithProductsLoss(salesLedgerId));
    }
    @ApiOperation("获取销售订单绑定的工艺路线")
    @GetMapping("/salesProcess/{salesLedgerId}")
    public AjaxResult salesProcess(@PathVariable Long salesLedgerId) {
        SalesLedgerProcessRouteDto dto = salesLedgerService.salesProcess(salesLedgerId);
        return AjaxResult.success(dto);
    }
    @GetMapping("/processCard/{salesLedgerId}")
    @ApiOperation("打印生产流程卡")
    public AjaxResult processCard(@PathVariable Long salesLedgerId) {
        SalesProcessCardDto dto = salesLedgerService.processCard(salesLedgerId);
        return AjaxResult.success(dto);
    }
    @GetMapping("/salesOrders/{salesLedgerId}")
    @ApiOperation("打印销售订单")
    public AjaxResult salesOrders(@PathVariable Long salesLedgerId) {
        SalesOrdersDto salesOrdersDto = salesLedgerService.salesOrders(salesLedgerId);
        return AjaxResult.success(salesOrdersDto);
    }
    @PostMapping("/salesInvoices")
    @ApiOperation("打印销售发货单")
    public AjaxResult salesInvoices(@RequestBody List<Long> salesLedgerIds) {
        SalesInvoicesDto dto = salesLedgerService.salesInvoices(salesLedgerIds);
        return AjaxResult.success(dto);
    }
    @GetMapping("/salesLabel/{salesLedgerId}")
    @ApiOperation("打印订单标签")
    public AjaxResult salesLabel(@PathVariable Long salesLedgerId) {
        List<SalesLabelDto> list = salesLedgerService.salesLabel(salesLedgerId);
        return AjaxResult.success(list);
    }
    @PostMapping("/salesStock")
    @ApiOperation("销售台账产品入库")
    public AjaxResult salesStock(@RequestBody SalesProductStockDto dto) {
        salesLedgerService.salesStock(dto);
        return AjaxResult.success();
    }
    @GetMapping("/shippedCustomers")
    @ApiOperation("已发货客户名单")
    public AjaxResult shippedCustomers() {
        List<Customer> list = salesLedgerService.shippedCustomers();
        return AjaxResult.success(list);
    }
    @PostMapping("/scanInbound")
    @ApiOperation("销售订单扫码-合格入库")
    public AjaxResult scanInbound(@RequestBody SalesScanInboundDto dto) {
        salesLedgerService.scanInbound(dto);
        return AjaxResult.success();
    }
    @PostMapping("/scanInboundUnqualified")
    @ApiOperation("销售订单扫码-不合格入库")
    public AjaxResult scanInboundUnqualified(@RequestBody SalesScanInboundDto dto) {
        salesLedgerService.scanInboundUnqualified(dto);
        return AjaxResult.success();
    }
    @PostMapping("/scanOutbound")
    @ApiOperation("销售订单扫码-合格出库")
    public AjaxResult scanOutbound(@RequestBody SalesScanInboundDto dto) {
        salesLedgerService.scanOutbound(dto);
        return AjaxResult.success();
    }
    @PostMapping("/scanOutboundUnqualified")
    @ApiOperation("销售订单扫码-不合格出库")
    public AjaxResult scanOutboundUnqualified(@RequestBody SalesScanInboundDto dto) {
        salesLedgerService.scanOutboundUnqualified(dto);
        return AjaxResult.success();
    }
}