ExcelController.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. package com.kcim.web;
  2. import cn.afterturn.easypoi.excel.ExcelExportUtil;
  3. import cn.afterturn.easypoi.excel.entity.ExportParams;
  4. import cn.hutool.core.collection.CollUtil;
  5. import cn.hutool.core.date.DateUtil;
  6. import cn.hutool.core.io.FileUtil;
  7. import cn.hutool.core.io.IoUtil;
  8. import cn.hutool.poi.excel.ExcelReader;
  9. import cn.hutool.poi.excel.ExcelUtil;
  10. import cn.hutool.poi.excel.ExcelWriter;
  11. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  12. import com.kcim.common.exception.CostException;
  13. import com.kcim.common.util.Result;
  14. import com.kcim.common.util.UserContext;
  15. import com.kcim.common.constants.NumberConstant;
  16. import com.kcim.common.util.excel.ExcelStyleUtil;
  17. import com.kcim.common.util.excel.entity.ProductTemplateEntity;
  18. import com.kcim.dao.model.*;
  19. import com.kcim.service.impl.DepartmentServiceImpl;
  20. import com.kcim.service.impl.ProductServiceImpl;
  21. import com.kcim.service.*;
  22. import io.swagger.annotations.Api;
  23. import io.swagger.annotations.ApiImplicitParam;
  24. import io.swagger.annotations.ApiImplicitParams;
  25. import io.swagger.annotations.ApiOperation;
  26. import lombok.extern.slf4j.Slf4j;
  27. import org.apache.poi.ss.usermodel.Sheet;
  28. import org.apache.poi.ss.usermodel.Workbook;
  29. import org.springframework.util.CollectionUtils;
  30. import org.springframework.web.bind.annotation.*;
  31. import org.springframework.web.multipart.MultipartFile;
  32. import javax.servlet.ServletOutputStream;
  33. import javax.servlet.http.HttpServletResponse;
  34. import java.io.*;
  35. import java.net.URLEncoder;
  36. import java.util.*;
  37. import java.util.stream.Collectors;
  38. /**
  39. * 相关导入导出操作
  40. */
  41. @Slf4j
  42. @Api(tags = "excel导入导出")
  43. @RestController
  44. @RequestMapping("excel")
  45. public class ExcelController extends AbstractController{
  46. // @Value("${file.filelocal}")
  47. // private String hospProfitReportUrl;
  48. //
  49. // @Value("${file.serverUrl}")
  50. // private String serverUrl;
  51. private final UserService userService;
  52. private final DepartmentServiceImpl departmentService;
  53. private final ProductServiceImpl productService;
  54. private final AccountingService accountingService;
  55. private final AccountingProductService accountingProductService;
  56. private final ResponsibilityDepartmentService responsibilityDepartmentService;
  57. private final CostShareParamService costShareParamService;
  58. private final CostIncomeGroupService costIncomeGroupService;
  59. private final CostCostingGroupService costCostingGroupService;
  60. private final ShareParamValueService shareParamValueService;
  61. private final CostCostingCollectionService costCostingCollectionService;
  62. private final AllocationService allocationService;
  63. private final CostDepartmentProfitService costDepartmentProfitService;
  64. private final CostShareLevelService shareLevelService;
  65. public ExcelController(UserService userService, DepartmentServiceImpl departmentService, ProductServiceImpl productService,
  66. AccountingService accountingService, AccountingProductService accountingProductService,
  67. ResponsibilityDepartmentService responsibilityDepartmentService,
  68. CostShareParamService costShareParamService, CostIncomeGroupService costIncomeGroupService,
  69. CostCostingGroupService costCostingGroupService, ShareParamValueService shareParamValueService,
  70. CostCostingCollectionService costCostingCollectionService, AllocationService allocationService,
  71. CostDepartmentProfitService costDepartmentProfitService, CostShareLevelService shareLevelService) {
  72. this.userService = userService;
  73. this.departmentService = departmentService;
  74. this.productService = productService;
  75. this.accountingService = accountingService;
  76. this.accountingProductService = accountingProductService;
  77. this.responsibilityDepartmentService = responsibilityDepartmentService;
  78. this.costShareParamService = costShareParamService;
  79. this.costIncomeGroupService = costIncomeGroupService;
  80. this.costCostingGroupService = costCostingGroupService;
  81. this.shareParamValueService = shareParamValueService;
  82. this.costCostingCollectionService = costCostingCollectionService;
  83. this.allocationService = allocationService;
  84. this.costDepartmentProfitService = costDepartmentProfitService;
  85. this.shareLevelService = shareLevelService;
  86. }
  87. @ApiOperation("用户导出模板设置")
  88. @GetMapping("/getcurrentTemplate")
  89. public void getImportUserTemplate(HttpServletResponse response) throws IOException {
  90. // TODO: 2021/7/26 暂时没有登录
  91. String uuid = UUID.randomUUID().toString();
  92. String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
  93. FileUtil.del(FileUtil.file(url));
  94. ExcelWriter writer = new ExcelWriter(url);
  95. // 样式
  96. Sheet sheet = writer.getSheet();
  97. // 内容
  98. writer.merge(0, 1, 0, 4, "为了保证成功导入,请勿修改模板格式", false);
  99. writer.passCurrentRow();
  100. writer.passCurrentRow();
  101. writer.merge(2, 2, 0, 4, "测试医院用户导入", false);
  102. writer.passCurrentRow();
  103. writer.writeRow(Arrays.asList("院区", "姓名", "工号", "密码", "手机号码"));
  104. // 写入响应
  105. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  106. response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
  107. ServletOutputStream out = null;
  108. out = response.getOutputStream();
  109. writer.flush(out, true);
  110. writer.close();
  111. IoUtil.close(out);
  112. }
  113. @PostMapping("/importUser")
  114. @ApiOperation("导入用户")
  115. public Result importUser(@RequestParam("file") MultipartFile file) {
  116. // InputStream in;
  117. // try {
  118. // in = file.getInputStream();
  119. // ExcelReader reader = ExcelUtil.getReader(in);
  120. // List<List<Object>> read = reader.read();
  121. // log.info("最开始:read={}", read);
  122. // log.info("-------------------------------假装我是个分割线------------------------------------");
  123. // return userService.importUser(read);
  124. // } catch (IOException e) {
  125. // e.printStackTrace();
  126. // throw new CostException(500, "导入失败");
  127. // }
  128. return null;
  129. }
  130. /**
  131. * 科室模板导出功能设置
  132. */
  133. @ApiOperation("科室导出模板设置")
  134. @GetMapping("/getDepartmentTemplate")
  135. public void getImportDepartmentTemplate(HttpServletResponse response) throws IOException {
  136. String uuid = UUID.randomUUID().toString();
  137. String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
  138. FileUtil.del(FileUtil.file(url));
  139. ExcelWriter writer = new ExcelWriter(url);
  140. // 样式
  141. Sheet sheet = writer.getSheet();
  142. // 内容
  143. writer.merge(0, 1, 0, 2, "为了保证成功导入,请勿修改模板格式", false);
  144. writer.passCurrentRow();
  145. writer.passCurrentRow();
  146. writer.merge(2, 2, 0, 2, "医院科室批量导入", false);
  147. writer.passCurrentRow();
  148. writer.setColumnWidth(0, 20);
  149. writer.setColumnWidth(1, 15);
  150. writer.setColumnWidth(2, 15);
  151. writer.writeRow(Arrays.asList("院区", "科室名称", "科室代码"));
  152. // 写入响应
  153. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  154. response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
  155. ServletOutputStream out = null;
  156. out = response.getOutputStream();
  157. writer.flush(out, true);
  158. writer.close();
  159. IoUtil.close(out);
  160. }
  161. /**
  162. * 批量导入科室信息
  163. */
  164. @PostMapping("/importDepartment")
  165. @ApiOperation("导入科室信息")
  166. public Result importDepartment(@RequestParam("file") MultipartFile file) {
  167. // InputStream in;
  168. // try {
  169. // in = file.getInputStream();
  170. // ExcelReader reader = ExcelUtil.getReader(in);
  171. // List<List<Object>> read = reader.read();
  172. // log.info("最开始:read={}", read);
  173. // log.info("-------------------------------------------------------------------");
  174. // User user = getUser();
  175. // Long hospId = user.getHospId();
  176. // return departmentService.importDepartment(read, hospId);
  177. // } catch (IOException e) {
  178. // e.printStackTrace();
  179. // throw new CostException(500, "导入失败");
  180. // }
  181. return null;
  182. }
  183. /**
  184. * 收入成本项目批量导入模板
  185. */
  186. @ApiOperation("成本项目导出模板设置")
  187. @GetMapping("/getImportProductTemplate")
  188. public Object getImportProductTemplate(HttpServletResponse response) throws IOException {
  189. // String uuid = UUID.randomUUID().toString();
  190. // String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
  191. // FileUtil.del(FileUtil.file(url));
  192. //
  193. // ExcelWriter writer = new ExcelWriter(url);
  194. // // 样式
  195. // Sheet sheet = writer.getSheet();
  196. // // 内容
  197. // writer.merge(0, 1, 0, 2, "为了保证成功导入,请勿修改模板格式", false);
  198. // writer.passCurrentRow();
  199. // writer.passCurrentRow();
  200. // writer.merge(2, 2, 0, 2, "医院成本收入批量导入", false);
  201. // writer.passCurrentRow();
  202. // writer.setColumnWidth(0, 20);
  203. // writer.setColumnWidth(1, 15);
  204. // writer.setColumnWidth(2, 15);
  205. // writer.writeRow(Arrays.asList("院区", "成本项目名", "成本项目编号"));
  206. //
  207. // // 写入响应
  208. // response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  209. // response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
  210. // ServletOutputStream out = null;
  211. //
  212. // out = response.getOutputStream();
  213. // writer.flush(out, true);
  214. // writer.close();
  215. // IoUtil.close(out);
  216. try {
  217. //获取项目类别字典
  218. //配置excel 文件信息
  219. ExportParams params = new ExportParams();
  220. params.setTitle("说明:为了保证成功导入,请勿修改模板格式" );
  221. //设置导出样式
  222. params.setStyle(ExcelStyleUtil.class);
  223. params.setHeaderHeight(10);
  224. //设置sheetName
  225. params.setSheetName("会计收入项目");
  226. // params.setHeaderColor();
  227. // params.setFreezeCol(2);
  228. // FileOutputStream outputStream = new FileOutputStream("E:\\会计收入项目导入模版.xlsx");
  229. Workbook workbook = ExcelExportUtil.exportExcel(params, ProductTemplateEntity.class,new ArrayList<>());
  230. response.setCharacterEncoding("UTF-8");
  231. response.setContentType("application/vnd.ms-excel");
  232. String fileName ="会计收入项目导入模版";
  233. response.setHeader("Content-Disposition", URLEncoder.encode(fileName, "UTF-8"));
  234. workbook.write(response.getOutputStream());
  235. // workbook.write(outputStream);
  236. // outputStream.close();
  237. workbook.close();
  238. } catch (FileNotFoundException e) {
  239. throw new CostException(e.getMessage());
  240. } catch (IOException e) {
  241. throw new RuntimeException(e);
  242. }
  243. return response.getOutputStream();
  244. }
  245. /**
  246. * 收入成本项目批量导入
  247. */
  248. @PostMapping("/importProduct")
  249. @ApiOperation("批量导入成本项目信息")
  250. public Result importProduct(@RequestParam("file") MultipartFile file) {
  251. // InputStream in;
  252. // try {
  253. //// in = file.getInputStream();
  254. //// ExcelReader reader = ExcelUtil.getReader(in);
  255. //// List<List<Object>> read = reader.read();
  256. //// log.info("最开始:read={}", read);
  257. //// log.info("-------------------------------------------------------------------");
  258. //// Long hospId = UserContext.getHospId();
  259. //// return productService.importProduct(read, hospId);
  260. // return productService.importProduct(file);
  261. // } catch (IOException e) {
  262. // e.printStackTrace();
  263. // throw new CostException(500, "导入失败");
  264. // }
  265. return productService.importProduct(file);
  266. }
  267. /**
  268. * 收入成本数据导出模板
  269. */
  270. @ApiOperation("收入数据导出模板设置")
  271. @GetMapping("/getImportIncomeProductAccountTemplate")
  272. public void getImportProductAccountTemplate(HttpServletResponse response,String token) throws IOException {
  273. Long hospId = UserContext.getHospId();
  274. // String uuid = UUID.randomUUID().toString();
  275. // String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
  276. // FileUtil.del(FileUtil.file(url));
  277. ExcelWriter writer = new ExcelWriter();
  278. // 样式
  279. Sheet sheet = writer.getSheet();
  280. // 内容
  281. writer.merge(0, 1, 0, 16, "为了保证成功导入,请勿修改模板格式", false);
  282. writer.passCurrentRow();
  283. writer.passCurrentRow();
  284. writer.merge(2, 2, 0, 16, "医院收入数据批量导入", false);
  285. writer.passCurrentRow();
  286. // 冻结前四行
  287. writer.setFreezePane(4);
  288. writer.writeRow(Arrays.asList("收入项目代码", "收入项目名称", "开单科室", "开单科室代码", "执行科室", "执行科室代码", "医生编码","开单医生",
  289. "病人ID", "住院号/门诊号", "患者姓名", "病人费别", "收据费别", "数量", "单位", "金额", "费用发生时间"));
  290. int accountType = NumberConstant.ONE;
  291. int column = NumberConstant.FOUR;
  292. // FileOutputStream outputStream = new FileOutputStream("E:\\收入数据导出模板设置.xlsx");
  293. // getProductByAccountType(hospId, writer, accountType, column);
  294. writer.setColumnWidth(0, 20);
  295. writer.setColumnWidth(1, 20);
  296. writer.setColumnWidth(2, 10);
  297. writer.setColumnWidth(3, 13);
  298. writer.setColumnWidth(4, 10);
  299. writer.setColumnWidth(5, 13);
  300. writer.setColumnWidth(6, 10);
  301. writer.setColumnWidth(7, 10);
  302. writer.setColumnWidth(8, 10);
  303. writer.setColumnWidth(9, 15);
  304. writer.setColumnWidth(10, 10);
  305. writer.setColumnWidth(11, 10);
  306. writer.setColumnWidth(12, 10);
  307. writer.setColumnWidth(13, 10);
  308. writer.setColumnWidth(14, 10);
  309. writer.setColumnWidth(15, 10);
  310. writer.setColumnWidth(16, 15);
  311. response.setCharacterEncoding("UTF-8");
  312. response.setContentType("application/vnd.ms-excel");
  313. String fileName ="收入数据导入模板";
  314. response.setHeader("Content-Disposition", URLEncoder.encode(fileName, "UTF-8"));
  315. // response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  316. // response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
  317. ServletOutputStream out = null;
  318. out = response.getOutputStream();
  319. writer.flush(out, true);
  320. writer.close();
  321. IoUtil.close(out);
  322. }
  323. /**
  324. * 成本数据导出模板
  325. */
  326. @GetMapping("/getImportCostProductAccountTemplate")
  327. @ApiOperation("成本数据导出模板设置")
  328. public void getImportCostProductAccountTemplate(HttpServletResponse response,String token) throws IOException {
  329. // int userId = jwtUtil.getUserId(token);
  330. // User user = userService.getById(userId);
  331. // if (Objects.isNull(user)){
  332. // throw new CostException(500,"用户不存在");
  333. // }
  334. Long hospId = UserContext.getHospId();
  335. // String uuid = UUID.randomUUID().toString();
  336. // String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
  337. // FileUtil.del(FileUtil.file(url));
  338. ExcelWriter writer = new ExcelWriter();
  339. // 样式
  340. // 所有科室里面对应存在责任中心对应的科室
  341. List<Department> departmentLinkedList = getDepartments(hospId);
  342. // 设置科室的代码集合
  343. List<String> departmentCodeList = departmentLinkedList.stream().map(Department::getDepartmentCode).collect(Collectors.toList());
  344. List<String> departmentNameList = departmentLinkedList.stream().map(Department::getDepartmentName).collect(Collectors.toList());
  345. Sheet sheet = writer.getSheet();
  346. writer.merge(0, 1, 0, departmentCodeList.size()==0?3:departmentCodeList.size()+2, "为了保证成功导入,请勿修改模板格式", false);
  347. writer.passCurrentRow();
  348. writer.passCurrentRow();
  349. writer.merge(2, 2, 0, departmentCodeList.size()==0?3:departmentCodeList.size()+2, "医院成本数据批量导入", false);
  350. writer.passCurrentRow();
  351. writer.merge(3,3,2,departmentCodeList.size()==0?3:departmentCodeList.size()+2,"科室",false);
  352. writer.passCurrentRow();
  353. departmentCodeList.add(0, null);
  354. departmentCodeList.add(1, null);
  355. departmentCodeList.add("-1");
  356. writer.writeRow(departmentCodeList);
  357. departmentNameList.add(0, "成本项目代码");
  358. departmentNameList.add(1, "成本项目名称");
  359. departmentNameList.add("总计");
  360. writer.writeRow(departmentNameList);
  361. // 设置科室名称的集合
  362. writer.setFreezePane(5);
  363. int accountType = NumberConstant.TWO;
  364. int column = NumberConstant.SIX;
  365. getProductByAccountType(hospId, writer, accountType, column);
  366. writer.setColumnWidth(0, 15);
  367. writer.setColumnWidth(1, 15);
  368. // response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  369. // response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
  370. response.setCharacterEncoding("UTF-8");
  371. response.setContentType("application/vnd.ms-excel");
  372. String fileName ="成本数据导入模板";
  373. response.setHeader("Content-Disposition", URLEncoder.encode(fileName, "UTF-8"));
  374. ServletOutputStream out = null;
  375. out = response.getOutputStream();
  376. writer.flush(out, true);
  377. writer.close();
  378. IoUtil.close(out);
  379. }
  380. // /**
  381. // * 测试模板数据导出
  382. // */
  383. // @ApiOperation("分摊报表输出")
  384. // @GetMapping("/getShareReportTemplateTwo")
  385. // public void getShareReportTemplateTwo(HttpServletResponse response,Integer shareNumber,String token) throws IOException {
  386. // int userId = jwtUtil.getUserId(token);
  387. // User user = userService.getById(userId);
  388. // if (Objects.isNull(user)){
  389. // throw new CostException(500,"用户不存在");
  390. // }
  391. // Long hospId = user.getHospId();
  392. // String uuid = UUID.randomUUID().toString();
  393. // String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
  394. // FileUtil.del(FileUtil.file(url));
  395. // ExcelWriter writer = new ExcelWriter(url);
  396. // Sheet sheet = writer.getSheet();
  397. // // 第几次分摊
  398. // writer= costCostingCollectionService.getShareReportTemplate(writer,shareNumber,sheet);
  399. // response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  400. // response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
  401. // ServletOutputStream out = null;
  402. // out = response.getOutputStream();
  403. // writer.flush(out, true);
  404. // writer.close();
  405. // IoUtil.close(out);
  406. //
  407. // }
  408. @ApiOperation("分摊报表输出2.0")
  409. @GetMapping("/getShareReportTemplate")
  410. public void getTemplate(HttpServletResponse response,Integer levelSort,String token,Integer year,Integer month,Long shareLevelId) throws IOException {
  411. // int userId = jwtUtil.getUserId(token);
  412. // User user = userService.getById(userId);
  413. // if (Objects.isNull(user)){
  414. // throw new CostException(500,"用户不存在");
  415. // }
  416. Long hospId = UserContext.getHospId();
  417. // String uuid = UUID.randomUUID().toString();
  418. // String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
  419. // FileUtil.del(FileUtil.file(url));
  420. ExcelWriter writer = new ExcelWriter();
  421. Sheet sheet = writer.getSheet();
  422. CostShareLevel service = shareLevelService.getById(shareLevelId); // 第几次分摊
  423. writer= allocationService.getShareReportTemplate(writer,levelSort,sheet,year,month,shareLevelId);
  424. // response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  425. // response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
  426. response.setCharacterEncoding("UTF-8");
  427. response.setContentType("application/vnd.ms-excel");
  428. String fileName = service.getShareName()+"分摊";
  429. response.setHeader("Content-Disposition", URLEncoder.encode(fileName, "UTF-8"));
  430. ServletOutputStream out = null;
  431. out = response.getOutputStream();
  432. writer.flush(out, true);
  433. writer.close();
  434. IoUtil.close(out);
  435. }
  436. /**
  437. * 获取执行类型的成本项目
  438. *
  439. * @param hospId 医院Id
  440. * @param writer 操作流
  441. * @param accountType 会计科目类型 1 收入 2支出(成本)
  442. * @param column 控制第几列
  443. */
  444. private void getProductByAccountType(Long hospId, ExcelWriter writer, Integer accountType, Integer column) {
  445. // 所有的成本项目
  446. List<Product> productList = productService.list(new QueryWrapper<Product>().lambda().eq(Product::getHospId, hospId));
  447. // 所有成本会计对照数据
  448. List<AccountingProduct> accountingProductList = accountingProductService.list(new QueryWrapper<AccountingProduct>().lambda().eq(AccountingProduct::getHospId, hospId));
  449. // 所有会计科目列表数据
  450. List<Accounting> accountingList = accountingService.list(new QueryWrapper<Accounting>().lambda().eq(Accounting::getHospId, hospId));
  451. List<Product> products = new ArrayList<>();
  452. Map<Long, List<AccountingProduct>> accountProductMap = accountingProductList.stream().collect(Collectors.groupingBy(AccountingProduct::getProductId));
  453. Map<Long, List<Accounting>> accountMap = accountingList.stream().collect(Collectors.groupingBy(Accounting::getId));
  454. productList.forEach(i -> {
  455. Long productId = i.getId();
  456. List<AccountingProduct> accountingProducts = accountProductMap.get(productId);
  457. if (CollUtil.isNotEmpty(accountingProducts)) {
  458. Long accountingId = accountingProducts.get(0).getAccountingId();
  459. List<Accounting> accountings = accountMap.get(accountingId);
  460. if (CollUtil.isNotEmpty(accountings) && accountType.equals(accountings.get(0).getAccountingType())) {
  461. products.add(i);
  462. }
  463. }
  464. });
  465. // 写入响应第二列 第三列的数据
  466. for (int j = 0; j < products.size(); j++) {
  467. writer.writeCellValue(0, j + column, products.get(j).getProductCode());
  468. writer.writeCellValue(1, j + column, products.get(j).getProductName());
  469. }
  470. }
  471. /**
  472. * 科室损益计算导出
  473. */
  474. @GetMapping("/getDepartmentProfit")
  475. @ApiOperation("科室损益计算导出")
  476. public void getDepartmentProfit(HttpServletResponse response,String token,String date) throws IOException {
  477. // int userId = jwtUtil.getUserId(token);
  478. // User user = userService.getById(userId);
  479. // if (Objects.isNull(user)){
  480. // throw new CostException(500,"用户不存在");
  481. // }
  482. // Long hospId = user.getHospId();
  483. String uuid = UUID.randomUUID().toString();
  484. String time = DateUtil.format(DateUtil.date(), "yyyy年MM月dd日HH时mm分ss秒");
  485. //todo:后面调整为上传到文件服务器,先启动确认环境
  486. String fileName = "全院损益" + time + ".xlsx";
  487. ExcelWriter writer = ExcelUtil.getWriter(fileName);
  488. Sheet sheet = writer.getSheet();
  489. costDepartmentProfitService.getDepartmentProfit(writer,sheet,date);
  490. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  491. response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
  492. ServletOutputStream out = null;
  493. out = response.getOutputStream();
  494. writer.flush(out, true);
  495. writer.close();
  496. IoUtil.close(out);
  497. }
  498. /**
  499. * 成本分摊参数导出模板
  500. */
  501. @GetMapping("/getShareParamTemplate")
  502. @ApiOperation("成本分摊参数值导出模板")
  503. public void getShareParamTemplate(HttpServletResponse response,String token) throws IOException {
  504. // int userId = jwtUtil.getUserId(token);
  505. // User user = userService.getById(userId);
  506. // if (Objects.isNull(user)){
  507. // throw new CostException(500,"用户不存在");
  508. // }
  509. Long hospId = UserContext.getHospId();
  510. String uuid = UUID.randomUUID().toString();
  511. String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
  512. FileUtil.del(FileUtil.file(url));
  513. ExcelWriter writer = new ExcelWriter(url);
  514. // 样式
  515. // 所有科室里面对应存在责任中心对应的科室进行显示
  516. List<Department> departmentLinkedList = getDepartments(hospId);
  517. // 设置科室的代码集合
  518. List<String> departmentCodeList = departmentLinkedList.stream().map(Department::getDepartmentCode).collect(Collectors.toList());
  519. List<String> departmentNameList = departmentLinkedList.stream().map(Department::getDepartmentName).collect(Collectors.toList());
  520. List<CostShareParam> costShareParamList = costShareParamService.list(new QueryWrapper<CostShareParam>().lambda().eq(CostShareParam::getHospId, hospId));
  521. Sheet sheet = writer.getSheet();
  522. writer.merge(0, 1, 0, departmentCodeList.size()==0?3:departmentCodeList.size()+1, "为了保证成功导入,请勿修改模板格式", false);
  523. writer.passCurrentRow();
  524. writer.passCurrentRow();
  525. writer.merge(2, 2, 0, departmentCodeList.size()==0?3:departmentCodeList.size()+1, "医院成本分摊参数数据批量导入", false);
  526. writer.passCurrentRow();
  527. writer.merge(3,3,2,departmentCodeList.size()==0?3:departmentCodeList.size()+1,"科室",true);
  528. writer.passCurrentRow();
  529. departmentCodeList.add(0, null);
  530. departmentCodeList.add(1, null);
  531. writer.writeRow(departmentCodeList);
  532. departmentNameList.add(0, "成本分摊参数代码");
  533. departmentNameList.add(1, "成本分摊参数名称");
  534. writer.writeRow(departmentNameList);
  535. // 设置科室名称的集合
  536. writer.setFreezePane(5);
  537. // 为第一列和第二列设置成本分摊参数的列表数据
  538. for (int j = 0; j < costShareParamList.size(); j++) {
  539. writer.writeCellValue(0, j + 6, costShareParamList.get(j).getShareParamCode());
  540. writer.writeCellValue(1, j + 6, costShareParamList.get(j).getShareParamName());
  541. }
  542. writer.setColumnWidth(0, 15);
  543. writer.setColumnWidth(1, 22);
  544. // response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  545. // response.setHeader("Content-Disposition", "attachment;filename=" + "成本分摊参数值导出模板" + ".xls");
  546. response.setCharacterEncoding("UTF-8");
  547. response.setContentType("application/vnd.ms-excel");
  548. String fileName ="成本分摊参数值导入模板";
  549. response.setHeader("Content-Disposition", URLEncoder.encode(fileName, "UTF-8"));
  550. ServletOutputStream out = null;
  551. // FileOutputStream outputStream = new FileOutputStream("E:\\患者信息导入模版.xlsx");
  552. out = response.getOutputStream();
  553. writer.flush(out, true);
  554. writer.close();
  555. IoUtil.close(out);
  556. }
  557. private List<Department> getDepartments(Long hospId) {
  558. List<Department> departmentLinkedList = new LinkedList<>();
  559. List<Department> departmentList = departmentService.getDepartments();
  560. // List<Department> departmentList = departmentService.list(new QueryWrapper<Department>().lambda().eq(Department::getHospId, hospId).orderByDesc(Department::getCreateTime));
  561. List<ResponsibilityDepartment> responsibilityDepartmentList = responsibilityDepartmentService.list(new QueryWrapper<ResponsibilityDepartment>().lambda()
  562. .eq(ResponsibilityDepartment::getHospId, hospId));
  563. Map<Long, List<ResponsibilityDepartment>> responsibilityDepartmentMap = responsibilityDepartmentList.stream().collect(Collectors.groupingBy(ResponsibilityDepartment::getDepartmentId));
  564. departmentList.forEach(i -> {
  565. Long id = i.getId();
  566. if (!CollectionUtils.isEmpty(responsibilityDepartmentMap.get(id))) {
  567. // TODO 暂时先不考虑关联的责任中心是否存在
  568. departmentLinkedList.add(i);
  569. }
  570. });
  571. return departmentLinkedList;
  572. }
  573. /**
  574. * 导入 收入数据导入
  575. *
  576. * @param
  577. */
  578. @PostMapping("/importDataByFileType")
  579. @ApiOperation("收入数据导入")
  580. @ApiImplicitParams({
  581. @ApiImplicitParam(name = "fileType", value = "文件类型 1 成本分摊参数值 2收入 3成本数据"),
  582. @ApiImplicitParam(name = "dateTime", value = "年月yyyy-MM")}
  583. )
  584. public Result importProductAccount(@RequestParam("file") MultipartFile file ,Integer fileType,String dateTime) {
  585. if (Objects.isNull(file)) {
  586. throw new CostException(500, "请选择文件");
  587. }
  588. InputStream in;
  589. // 导入的是收入数据
  590. try {
  591. in = file.getInputStream();
  592. ExcelReader reader = ExcelUtil.getReader(in);
  593. List<List<Object>> read = reader.read();
  594. log.info("最开始:read={}", read);
  595. log.info("-------------------------------------------------------------------");
  596. return costIncomeGroupService.importIncomeGroup(read, file, dateTime,fileType);
  597. } catch (IOException e) {
  598. e.printStackTrace();
  599. ;
  600. throw new CostException(500, "导入失败");
  601. }
  602. }
  603. @PostMapping("/importCostingGroup")
  604. @ApiOperation("成本数据导入")
  605. @ApiImplicitParams({
  606. @ApiImplicitParam(name = "fileType", value = "文件类型 1 成本分摊参数值 2收入 3成本数据"),
  607. @ApiImplicitParam(name = "dateTime", value = "年月yyyy-MM")}
  608. )
  609. public Result importCostingGroup(@RequestParam("file") MultipartFile file ,Integer fileType,String dateTime) {
  610. if (Objects.isNull(file)) {
  611. throw new CostException(500, "请选择文件");
  612. }
  613. InputStream in;
  614. // 导入的是收入数据
  615. try {
  616. in = file.getInputStream();
  617. ExcelReader reader = ExcelUtil.getReader(in);
  618. List<List<Object>> read = reader.read();
  619. log.info("最开始:read={}", read);
  620. log.info("-------------------------------------------------------------------");
  621. return costCostingGroupService.importCostingGroup(read, file, dateTime,fileType);
  622. } catch (IOException e) {
  623. e.printStackTrace();
  624. ;
  625. throw new CostException(500, "导入失败");
  626. }
  627. }
  628. @PostMapping("/importShareParamGroup")
  629. @ApiOperation("成本分摊参数值导入")
  630. @ApiImplicitParams({
  631. @ApiImplicitParam(name = "fileType", value = "文件类型 1 成本分摊参数值 2收入 3成本数据"),
  632. @ApiImplicitParam(name = "dateTime", value = "年月yyyy-MM")}
  633. )
  634. public Result importShareParamGroup(@RequestParam("file") MultipartFile file ,Integer fileType,String dateTime){
  635. if (Objects.isNull(file)) {
  636. throw new CostException(500, "请选择文件");
  637. }
  638. InputStream in;
  639. // 导入的是收入数据
  640. try {
  641. in = file.getInputStream();
  642. ExcelReader reader = ExcelUtil.getReader(in);
  643. List<List<Object>> read = reader.read();
  644. log.info("最开始:read={}", read);
  645. log.info("-------------------------------------------------------------------");
  646. return shareParamValueService.importShareParamGroup(read, file, dateTime,fileType);
  647. } catch (IOException e) {
  648. e.printStackTrace();
  649. ;
  650. throw new CostException(500, "导入失败");
  651. }
  652. }
  653. }