package com.imed.costaccount.web; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.IoUtil; import cn.hutool.poi.excel.ExcelReader; import cn.hutool.poi.excel.ExcelUtil; import cn.hutool.poi.excel.ExcelWriter; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.imed.costaccount.common.exception.CostException; import com.imed.costaccount.common.util.Result; import com.imed.costaccount.common.util.UserContext; import com.imed.costaccount.constants.NumberConstant; import com.imed.costaccount.model.Accounting; import com.imed.costaccount.model.AccountingProduct; import com.imed.costaccount.model.Product; import com.imed.costaccount.model.User; import com.imed.costaccount.service.AccountingProductService; import com.imed.costaccount.service.AccountingService; import com.imed.costaccount.service.UserService; import com.imed.costaccount.service.impl.DepartmentServiceImpl; import com.imed.costaccount.service.impl.ProductServiceImpl; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.poi.ss.usermodel.Sheet; import org.apache.shiro.SecurityUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.*; import java.util.stream.Collectors; /** * 相关导入导出操作 */ @Slf4j @Api(tags = "excel导入导出") @RestController @RequestMapping("/costAccount/excel") public class ExcelController { private final UserService userService; private final DepartmentServiceImpl departmentService; private final ProductServiceImpl productService; private final AccountingService accountingService; private final AccountingProductService accountingProductService; public ExcelController(UserService userService, DepartmentServiceImpl departmentService, ProductServiceImpl productService, AccountingService accountingService, AccountingProductService accountingProductService) { this.userService = userService; this.departmentService = departmentService; this.productService = productService; this.accountingService = accountingService; this.accountingProductService = accountingProductService; } @ApiOperation("用户导出模板设置") @GetMapping("/getcurrentTemplate") public void getImportUserTemplate(HttpServletResponse response) throws IOException { User user = (User) SecurityUtils.getSubject().getPrincipal(); // TODO: 2021/7/26 暂时没有登录 String uuid = UUID.randomUUID().toString(); String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls"; FileUtil.del(FileUtil.file(url)); ExcelWriter writer = new ExcelWriter(url); // 样式 Sheet sheet = writer.getSheet(); // 内容 writer.merge(0, 1, 0, 4, "为了保证成功导入,请勿修改模板格式", false); writer.passCurrentRow(); writer.passCurrentRow(); writer.merge(2, 2, 0, 4, "测试医院用户导入", false); writer.passCurrentRow(); writer.writeRow(Arrays.asList("院区","姓名", "工号", "密码","手机号码")); // 写入响应 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls"); ServletOutputStream out = null; out = response.getOutputStream(); writer.flush(out, true); writer.close(); IoUtil.close(out); } @PostMapping("/importUser") @ApiOperation("导入用户") public Result importUser(@RequestParam("file") MultipartFile file) { InputStream in; try { in = file.getInputStream(); ExcelReader reader = ExcelUtil.getReader(in); List> read = reader.read(); log.info("最开始:read={}",read); log.info("-------------------------------假装我是个分割线------------------------------------"); User user = (User) SecurityUtils.getSubject().getPrincipal(); return userService.importUser(read, user); } catch (IOException e) { e.printStackTrace(); throw new CostException(500, "导入失败"); } } /** * 科室模板导出功能设置 */ @ApiOperation("科室导出模板设置") @GetMapping("/getDepartmentTemplate") public void getImportDepartmentTemplate(HttpServletResponse response) throws IOException { User user = (User) SecurityUtils.getSubject().getPrincipal(); String uuid = UUID.randomUUID().toString(); String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls"; FileUtil.del(FileUtil.file(url)); ExcelWriter writer = new ExcelWriter(url); // 样式 Sheet sheet = writer.getSheet(); // 内容 writer.merge(0, 1, 0, 2, "为了保证成功导入,请勿修改模板格式", false); writer.passCurrentRow(); writer.passCurrentRow(); writer.merge(2, 2, 0, 2, "医院科室批量导入", false); writer.passCurrentRow(); writer.setColumnWidth(0,20); writer.setColumnWidth(1,15); writer.setColumnWidth(2,15); writer.writeRow(Arrays.asList("院区","科室名称", "科室代码")); // 写入响应 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls"); ServletOutputStream out = null; out = response.getOutputStream(); writer.flush(out, true); writer.close(); IoUtil.close(out); } /** * 批量导入科室信息 */ @PostMapping("/importDepartment") @ApiOperation("导入科室信息") public Result importDepartment(@RequestParam("file") MultipartFile file){ InputStream in; try { in = file.getInputStream(); ExcelReader reader = ExcelUtil.getReader(in); List> read = reader.read(); log.info("最开始:read={}",read); log.info("-------------------------------------------------------------------"); User user = (User) SecurityUtils.getSubject().getPrincipal(); Long hospId = user.getHospId(); return departmentService.importDepartment(read, hospId); }catch (IOException e){ e.printStackTrace();; throw new CostException(500, "导入失败"); } } /** * 收入成本项目批量导入模板 */ @ApiOperation("收入成本导出模板设置") @GetMapping("/getImportProductTemplate") public void getImportProductTemplate(HttpServletResponse response) throws IOException { User user = (User) SecurityUtils.getSubject().getPrincipal(); String uuid = UUID.randomUUID().toString(); String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls"; FileUtil.del(FileUtil.file(url)); ExcelWriter writer = new ExcelWriter(url); // 样式 Sheet sheet = writer.getSheet(); // 内容 writer.merge(0, 1, 0, 2, "为了保证成功导入,请勿修改模板格式", false); writer.passCurrentRow(); writer.passCurrentRow(); writer.merge(2, 2, 0, 2, "医院成本收入批量导入", false); writer.passCurrentRow(); writer.setColumnWidth(0,20); writer.setColumnWidth(1,15); writer.setColumnWidth(2,15); writer.writeRow(Arrays.asList("院区","成本项目名", "成本项目编号")); // 写入响应 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls"); ServletOutputStream out = null; out = response.getOutputStream(); writer.flush(out, true); writer.close(); IoUtil.close(out); } /** * 收入成本项目批量导入 */ @PostMapping("/importProduct") @ApiOperation("批量导入收入成本信息") public Result importProduct(@RequestParam("file") MultipartFile file){ InputStream in; try { in = file.getInputStream(); ExcelReader reader = ExcelUtil.getReader(in); List> read = reader.read(); log.info("最开始:read={}",read); log.info("-------------------------------------------------------------------"); User user = (User) SecurityUtils.getSubject().getPrincipal(); Long hospId = user.getHospId(); return productService.importProduct(read, hospId); }catch (IOException e){ e.printStackTrace();; throw new CostException(500, "导入失败"); } } /** * 收入成本数据导出模板 * @param accountType 1收入数据 2 成本数据(支出数据) */ @ApiOperation("收入数据导出模板设置") @GetMapping("/getImportProductAccountTemplate") public void getImportProductAccountTemplate(HttpServletResponse response,Integer accountType) throws IOException { Long hospId = UserContext.getHospId(); String uuid = UUID.randomUUID().toString(); String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls"; FileUtil.del(FileUtil.file(url)); ExcelWriter writer = new ExcelWriter(url); // 样式 Sheet sheet = writer.getSheet(); // 内容 writer.merge(0, 1, 0, 6, "为了保证成功导入,请勿修改模板格式", false); writer.passCurrentRow(); writer.passCurrentRow(); String stateType= NumberConstant.ONE.equals(accountType)?"医院收入数据批量导入":"医院成本数据批量导入"; writer.merge(2, 2, 0, 6, stateType, false); writer.passCurrentRow(); // 冻结前四行 writer.setFreezePane(4); writer.writeRow(Arrays.asList("","","开单科室","开单科室代码", "执行科室","执行科室代码","金额")); // 所有的成本项目 List productList = productService.list(new QueryWrapper().lambda().eq(Product::getHospId, hospId)); // 所有成本会计对照数据 List accountingProductList = accountingProductService.list(new QueryWrapper().lambda().eq(AccountingProduct::getHospId, hospId)); // 所有会计科目列表数据 List accountingList = accountingService.list(new QueryWrapper().lambda().eq(Accounting::getHospId, hospId)); List products = new ArrayList<>(); Map> accountProductMap = accountingProductList.stream().collect(Collectors.groupingBy(AccountingProduct::getProductId)); Map> accountMap = accountingList.stream().collect(Collectors.groupingBy(Accounting::getId)); productList.forEach(i->{ Long productId = i.getId(); List accountingProducts = accountProductMap.get(productId); if (CollUtil.isNotEmpty(accountingProducts)){ Long accountingId = accountingProducts.get(0).getAccountingId(); List accountings = accountMap.get(accountingId); if (CollUtil.isNotEmpty(accountings) && accountType.equals(accountings.get(0).getAccountingType())){ products.add(i); } } }); // 写入响应第一列 第二列的数据 for (int j = 0; j> read = reader.read(); log.info("最开始:read={}",read); log.info("-------------------------------------------------------------------"); Long hospId = UserContext.getHospId(); return productService.importProduct(read, hospId); }catch (IOException e){ e.printStackTrace();; throw new CostException(500, "导入失败"); } } }