| package com.ruoyi.web.controller.api; | 
|   | 
| import cn.afterturn.easypoi.excel.ExcelExportUtil; | 
| import cn.afterturn.easypoi.excel.entity.ExportParams; | 
| import com.baomidou.mybatisplus.core.toolkit.Wrappers; | 
| import com.ruoyi.common.annotation.Log; | 
| import com.ruoyi.common.basic.PageInfo; | 
| import com.ruoyi.common.core.domain.R; | 
| import com.ruoyi.common.enums.BusinessType; | 
| import com.ruoyi.common.utils.WebUtils; | 
| import com.ruoyi.framework.web.service.TokenService; | 
| import com.ruoyi.system.export.PlatformLedgerReportExport; | 
| import com.ruoyi.system.export.ProcurementReportClinicExport; | 
| import com.ruoyi.system.export.ProcurementReportPlatformExport; | 
| import com.ruoyi.system.export.ProcurementReportSupplierExport; | 
| import com.ruoyi.system.model.TCrmClinic; | 
| import com.ruoyi.system.model.TCrmSupplier; | 
| import com.ruoyi.system.query.ClinicProcurementReportQuery; | 
| import com.ruoyi.system.query.DetectionPaymentReportQuery; | 
| import com.ruoyi.system.service.*; | 
| import com.ruoyi.system.vo.ClinicProcurementReportVO; | 
| import com.ruoyi.system.vo.DetectionPaymentReportVO; | 
| import io.swagger.annotations.Api; | 
| import io.swagger.annotations.ApiOperation; | 
| import org.apache.poi.ss.usermodel.Workbook; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.web.bind.annotation.PostMapping; | 
| import org.springframework.web.bind.annotation.RequestBody; | 
| import org.springframework.web.bind.annotation.RequestMapping; | 
| import org.springframework.web.bind.annotation.RestController; | 
|   | 
| import javax.servlet.ServletOutputStream; | 
| import javax.servlet.http.HttpServletResponse; | 
| import java.io.IOException; | 
| import java.net.URLEncoder; | 
| import java.util.List; | 
| import java.util.Objects; | 
|   | 
| @Api(tags = "财务报表") | 
| @RestController | 
| @RequestMapping("/financial-statements") | 
| public class FinancialStatementsController { | 
|   | 
|     private final TErpProcurementService erpProcurementService; | 
|     private final TokenService tokenService; | 
|     private final ISysUserService sysUserService; | 
|     private final TCrmSupplierService crmSupplierService; | 
|     private final TCrmClinicService crmClinicService; | 
|     private final TSysAppUserService sysAppUserService; | 
|     private final TCrmClinicPointsService crmClinicPointsService; | 
|     private final TSysOrderService sysOrderService; | 
|     private final TSysPayRecordService sysPayRecordService; | 
|   | 
|     @Autowired | 
|     public FinancialStatementsController(TErpProcurementService erpProcurementService, TokenService tokenService, ISysUserService sysUserService, TCrmSupplierService crmSupplierService, TCrmClinicService crmClinicService, TSysAppUserService sysAppUserService, TCrmClinicPointsService crmClinicPointsService, TSysOrderService sysOrderService, TSysPayRecordService sysPayRecordService) { | 
|         this.erpProcurementService = erpProcurementService; | 
|         this.tokenService = tokenService; | 
|         this.sysUserService = sysUserService; | 
|         this.crmSupplierService = crmSupplierService; | 
|         this.crmClinicService = crmClinicService; | 
|         this.sysAppUserService = sysAppUserService; | 
|         this.crmClinicPointsService = crmClinicPointsService; | 
|         this.sysOrderService = sysOrderService; | 
|         this.sysPayRecordService = sysPayRecordService; | 
|     } | 
|   | 
|     /** | 
|      * 诊所采购报表 | 
|      */ | 
|     @ApiOperation(value = "诊所采购报表-平台-供应商-诊所共用") | 
|     @PostMapping(value = "/clinicProcurementReport") | 
|     public R<PageInfo<ClinicProcurementReportVO>> clinicProcurementReport(@RequestBody ClinicProcurementReportQuery query) { | 
|         Long userId = tokenService.getLoginUser().getUserId(); | 
|         Integer roleType = tokenService.getLoginUser().getUser().getRoleType(); | 
|         query.setRoleType(roleType); | 
|         if (Objects.equals(roleType, 4)) { | 
|             TCrmSupplier crmSupplier = crmSupplierService.getOne(Wrappers.lambdaQuery(TCrmSupplier.class) | 
|                     .eq(TCrmSupplier::getUserId, userId) | 
|                     .last("LIMIT 1")); | 
|             query.setSupplierId(crmSupplier.getId()); | 
|         } | 
|         if (Objects.equals(roleType, 5)) { | 
|             TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class) | 
|                     .eq(TCrmClinic::getUserId, userId) | 
|                     .last("LIMIT 1")); | 
|             query.setClinicId(crmClinic.getId()); | 
|         } | 
|   | 
|         PageInfo<ClinicProcurementReportVO> pageInfo = erpProcurementService.clinicProcurementReport(query); | 
|         return R.ok(pageInfo); | 
|     } | 
|   | 
|     @Log(title = "财务报表-诊所采购报表导出-诊所", businessType = BusinessType.EXPORT) | 
|     @ApiOperation(value = "诊所采购报表导出-诊所") | 
|     @PostMapping("/exportProcurementReportClinic") | 
|     public void exportProcurementReportClinic(@RequestBody ClinicProcurementReportQuery query){ | 
|         Long userId = tokenService.getLoginUser().getUserId(); | 
|         Integer roleType = tokenService.getLoginUser().getUser().getRoleType(); | 
|         query.setRoleType(roleType); | 
|         if (Objects.equals(roleType, 4)) { | 
|             TCrmSupplier crmSupplier = crmSupplierService.getOne(Wrappers.lambdaQuery(TCrmSupplier.class) | 
|                     .eq(TCrmSupplier::getUserId, userId) | 
|                     .last("LIMIT 1")); | 
|             query.setSupplierId(crmSupplier.getId()); | 
|         } | 
|         if (Objects.equals(roleType, 5)) { | 
|             TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class) | 
|                     .eq(TCrmClinic::getUserId, userId) | 
|                     .last("LIMIT 1")); | 
|             query.setClinicId(crmClinic.getId()); | 
|         } | 
|         List<ProcurementReportClinicExport> procurementReportClinicExports = erpProcurementService.exportProcurementReportClinic(query); | 
|         Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), ProcurementReportClinicExport.class, procurementReportClinicExports); | 
|         HttpServletResponse response = WebUtils.response(); | 
|         assert response != null; | 
|         response.setContentType("application/vnd.ms-excel"); | 
|         response.setCharacterEncoding("utf-8"); | 
|         ServletOutputStream outputStream = null; | 
|         try { | 
|             String fileName = URLEncoder.encode("诊所采购报表.xls", "utf-8"); | 
|             response.setHeader("Content-Disposition", "attachment;filename=" + fileName); | 
|             response.setContentType("application/vnd.ms-excel;charset=UTF-8"); | 
|             response.setHeader("Pragma", "no-cache"); | 
|             response.setHeader("Cache-Control", "no-cache"); | 
|             outputStream = response.getOutputStream(); | 
|             workbook.write(outputStream); | 
|         } catch (IOException e) { | 
|             e.printStackTrace(); | 
|         } finally { | 
|             try { | 
|                 outputStream.close(); | 
|             } catch (IOException e) { | 
|                 e.printStackTrace(); | 
|             } | 
|         } | 
|     } | 
|   | 
|     @Log(title = "财务报表-诊所采购报表导出-供应商", businessType = BusinessType.EXPORT) | 
|     @ApiOperation(value = "诊所采购报表导出-供应商") | 
|     @PostMapping("/exportProcurementReportSupplier") | 
|     public void exportProcurementReportSupplier(@RequestBody ClinicProcurementReportQuery query){ | 
|         Long userId = tokenService.getLoginUser().getUserId(); | 
|         Integer roleType = tokenService.getLoginUser().getUser().getRoleType(); | 
|         query.setRoleType(roleType); | 
|         if (Objects.equals(roleType, 4)) { | 
|             TCrmSupplier crmSupplier = crmSupplierService.getOne(Wrappers.lambdaQuery(TCrmSupplier.class) | 
|                     .eq(TCrmSupplier::getUserId, userId) | 
|                     .last("LIMIT 1")); | 
|             query.setSupplierId(crmSupplier.getId()); | 
|         } | 
|         if (Objects.equals(roleType, 5)) { | 
|             TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class) | 
|                     .eq(TCrmClinic::getUserId, userId) | 
|                     .last("LIMIT 1")); | 
|             query.setClinicId(crmClinic.getId()); | 
|         } | 
|         List<ProcurementReportSupplierExport> procurementReportSupplierExports = erpProcurementService.exportProcurementReportSupplier(query); | 
|         Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), ProcurementReportSupplierExport.class, procurementReportSupplierExports); | 
|         HttpServletResponse response = WebUtils.response(); | 
|         assert response != null; | 
|         response.setContentType("application/vnd.ms-excel"); | 
|         response.setCharacterEncoding("utf-8"); | 
|         ServletOutputStream outputStream = null; | 
|         try { | 
|             String fileName = URLEncoder.encode("诊所采购报表.xls", "utf-8"); | 
|             response.setHeader("Content-Disposition", "attachment;filename=" + fileName); | 
|             response.setContentType("application/vnd.ms-excel;charset=UTF-8"); | 
|             response.setHeader("Pragma", "no-cache"); | 
|             response.setHeader("Cache-Control", "no-cache"); | 
|             outputStream = response.getOutputStream(); | 
|             workbook.write(outputStream); | 
|         } catch (IOException e) { | 
|             e.printStackTrace(); | 
|         } finally { | 
|             try { | 
|                 outputStream.close(); | 
|             } catch (IOException e) { | 
|                 e.printStackTrace(); | 
|             } | 
|         } | 
|     } | 
|   | 
|     @Log(title = "财务报表-诊所采购报表导出-平台", businessType = BusinessType.EXPORT) | 
|     @ApiOperation(value = "诊所采购报表导出-平台") | 
|     @PostMapping("/exportProcurementReportPlatform") | 
|     public void exportProcurementReportPlatform(@RequestBody ClinicProcurementReportQuery query){ | 
|         Long userId = tokenService.getLoginUser().getUserId(); | 
|         Integer roleType = tokenService.getLoginUser().getUser().getRoleType(); | 
|         query.setRoleType(roleType); | 
|         if (Objects.equals(roleType, 4)) { | 
|             TCrmSupplier crmSupplier = crmSupplierService.getOne(Wrappers.lambdaQuery(TCrmSupplier.class) | 
|                     .eq(TCrmSupplier::getUserId, userId) | 
|                     .last("LIMIT 1")); | 
|             query.setSupplierId(crmSupplier.getId()); | 
|         } | 
|         if (Objects.equals(roleType, 5)) { | 
|             TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class) | 
|                     .eq(TCrmClinic::getUserId, userId) | 
|                     .last("LIMIT 1")); | 
|             query.setClinicId(crmClinic.getId()); | 
|         } | 
|         List<ProcurementReportPlatformExport> procurementReportPlatformExports = erpProcurementService.exportProcurementReportPlatform(query); | 
|         Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), ProcurementReportPlatformExport.class, procurementReportPlatformExports); | 
|         HttpServletResponse response = WebUtils.response(); | 
|         assert response != null; | 
|         response.setContentType("application/vnd.ms-excel"); | 
|         response.setCharacterEncoding("utf-8"); | 
|         ServletOutputStream outputStream = null; | 
|         try { | 
|             String fileName = URLEncoder.encode("诊所采购报表.xls", "utf-8"); | 
|             response.setHeader("Content-Disposition", "attachment;filename=" + fileName); | 
|             response.setContentType("application/vnd.ms-excel;charset=UTF-8"); | 
|             response.setHeader("Pragma", "no-cache"); | 
|             response.setHeader("Cache-Control", "no-cache"); | 
|             outputStream = response.getOutputStream(); | 
|             workbook.write(outputStream); | 
|         } catch (IOException e) { | 
|             e.printStackTrace(); | 
|         } finally { | 
|             try { | 
|                 outputStream.close(); | 
|             } catch (IOException e) { | 
|                 e.printStackTrace(); | 
|             } | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * AI检测支付报表 | 
|      */ | 
|     @ApiOperation(value = "AI检测支付报表") | 
|     @PostMapping(value = "/detectionPaymentReport") | 
|     public R<PageInfo<DetectionPaymentReportVO>> detectionPaymentReport(@RequestBody DetectionPaymentReportQuery query) { | 
|         PageInfo<DetectionPaymentReportVO> pageInfo = sysPayRecordService.detectionPaymentReport(query); | 
|         return R.ok(pageInfo); | 
|     } | 
|   | 
|     @Log(title = "财务报表-AI检测支付报表导出", businessType = BusinessType.EXPORT) | 
|     @ApiOperation(value = "AI检测支付报表导出") | 
|     @PostMapping("/exportDetectionPaymentReport") | 
|     public void exportDetectionPaymentReport(@RequestBody DetectionPaymentReportQuery query){ | 
|         List<DetectionPaymentReportVO> list = sysPayRecordService.exportDetectionPaymentReport(query); | 
|         Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), DetectionPaymentReportVO.class, list); | 
|         HttpServletResponse response = WebUtils.response(); | 
|         assert response != null; | 
|         response.setContentType("application/vnd.ms-excel"); | 
|         response.setCharacterEncoding("utf-8"); | 
|         ServletOutputStream outputStream = null; | 
|         try { | 
|             String fileName = URLEncoder.encode("AI检测支付报表.xls", "utf-8"); | 
|             response.setHeader("Content-Disposition", "attachment;filename=" + fileName); | 
|             response.setContentType("application/vnd.ms-excel;charset=UTF-8"); | 
|             response.setHeader("Pragma", "no-cache"); | 
|             response.setHeader("Cache-Control", "no-cache"); | 
|             outputStream = response.getOutputStream(); | 
|             workbook.write(outputStream); | 
|         } catch (IOException e) { | 
|             e.printStackTrace(); | 
|         } finally { | 
|             try { | 
|                 outputStream.close(); | 
|             } catch (IOException e) { | 
|                 e.printStackTrace(); | 
|             } | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 平台分账报表 | 
|      */ | 
|     @ApiOperation(value = "平台分账报表") | 
|     @PostMapping(value = "/platformLedgerReport") | 
|     public R<PageInfo<ClinicProcurementReportVO>> platformLedgerReport(@RequestBody ClinicProcurementReportQuery query) { | 
|         PageInfo<ClinicProcurementReportVO> pageInfo = erpProcurementService.platformLedgerReport(query); | 
|         return R.ok(pageInfo); | 
|     } | 
|   | 
|     @Log(title = "财务报表-平台分账报表导出", businessType = BusinessType.EXPORT) | 
|     @ApiOperation(value = "平台分账报表导出") | 
|     @PostMapping("/exportPlatformLedgerReport") | 
|     public void exportPlatformLedgerReport(@RequestBody ClinicProcurementReportQuery query){ | 
|         List<PlatformLedgerReportExport> list = erpProcurementService.exportPlatformLedgerReport(query); | 
|         Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), PlatformLedgerReportExport.class, list); | 
|         HttpServletResponse response = WebUtils.response(); | 
|         assert response != null; | 
|         response.setContentType("application/vnd.ms-excel"); | 
|         response.setCharacterEncoding("utf-8"); | 
|         ServletOutputStream outputStream = null; | 
|         try { | 
|             String fileName = URLEncoder.encode("平台分账报表.xls", "utf-8"); | 
|             response.setHeader("Content-Disposition", "attachment;filename=" + fileName); | 
|             response.setContentType("application/vnd.ms-excel;charset=UTF-8"); | 
|             response.setHeader("Pragma", "no-cache"); | 
|             response.setHeader("Cache-Control", "no-cache"); | 
|             outputStream = response.getOutputStream(); | 
|             workbook.write(outputStream); | 
|         } catch (IOException e) { | 
|             e.printStackTrace(); | 
|         } finally { | 
|             try { | 
|                 outputStream.close(); | 
|             } catch (IOException e) { | 
|                 e.printStackTrace(); | 
|             } | 
|         } | 
|     } | 
|   | 
| } |