2
0

ExcelController.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. package com.imed.costaccount.web;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.io.IoUtil;
  5. import cn.hutool.poi.excel.ExcelReader;
  6. import cn.hutool.poi.excel.ExcelUtil;
  7. import cn.hutool.poi.excel.ExcelWriter;
  8. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  9. import com.imed.costaccount.common.exception.CostException;
  10. import com.imed.costaccount.common.util.Result;
  11. import com.imed.costaccount.common.util.UserContext;
  12. import com.imed.costaccount.constants.NumberConstant;
  13. import com.imed.costaccount.model.*;
  14. import com.imed.costaccount.service.*;
  15. import com.imed.costaccount.service.impl.DepartmentServiceImpl;
  16. import com.imed.costaccount.service.impl.ProductServiceImpl;
  17. import io.swagger.annotations.Api;
  18. import io.swagger.annotations.ApiOperation;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.apache.poi.ss.usermodel.Sheet;
  21. import org.apache.shiro.SecurityUtils;
  22. import org.jetbrains.annotations.NotNull;
  23. import org.springframework.util.CollectionUtils;
  24. import org.springframework.web.bind.annotation.*;
  25. import org.springframework.web.multipart.MultipartFile;
  26. import javax.servlet.ServletOutputStream;
  27. import javax.servlet.http.HttpServletResponse;
  28. import java.io.File;
  29. import java.io.IOException;
  30. import java.io.InputStream;
  31. import java.util.*;
  32. import java.util.stream.Collectors;
  33. /**
  34. * 相关导入导出操作
  35. */
  36. @Slf4j
  37. @Api(tags = "excel导入导出")
  38. @RestController
  39. @RequestMapping("/costAccount/excel")
  40. public class ExcelController {
  41. private final UserService userService;
  42. private final DepartmentServiceImpl departmentService;
  43. private final ProductServiceImpl productService;
  44. private final AccountingService accountingService;
  45. private final AccountingProductService accountingProductService;
  46. private final ResponsibilityDepartmentService responsibilityDepartmentService;
  47. private final CostShareParamService costShareParamService;
  48. private final CostIncomeGroupService costIncomeGroupService;
  49. public ExcelController(UserService userService, DepartmentServiceImpl departmentService, ProductServiceImpl productService, AccountingService accountingService, AccountingProductService accountingProductService, ResponsibilityDepartmentService responsibilityDepartmentService, CostShareParamService costShareParamService, CostIncomeGroupService costIncomeGroupService) {
  50. this.userService = userService;
  51. this.departmentService = departmentService;
  52. this.productService = productService;
  53. this.accountingService = accountingService;
  54. this.accountingProductService = accountingProductService;
  55. this.responsibilityDepartmentService = responsibilityDepartmentService;
  56. this.costShareParamService = costShareParamService;
  57. this.costIncomeGroupService = costIncomeGroupService;
  58. }
  59. @ApiOperation("用户导出模板设置")
  60. @GetMapping("/getcurrentTemplate")
  61. public void getImportUserTemplate(HttpServletResponse response) throws IOException {
  62. User user = (User) SecurityUtils.getSubject().getPrincipal();
  63. // TODO: 2021/7/26 暂时没有登录
  64. String uuid = UUID.randomUUID().toString();
  65. String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
  66. FileUtil.del(FileUtil.file(url));
  67. ExcelWriter writer = new ExcelWriter(url);
  68. // 样式
  69. Sheet sheet = writer.getSheet();
  70. // 内容
  71. writer.merge(0, 1, 0, 4, "为了保证成功导入,请勿修改模板格式", false);
  72. writer.passCurrentRow();
  73. writer.passCurrentRow();
  74. writer.merge(2, 2, 0, 4, "测试医院用户导入", false);
  75. writer.passCurrentRow();
  76. writer.writeRow(Arrays.asList("院区","姓名", "工号", "密码","手机号码"));
  77. // 写入响应
  78. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  79. response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
  80. ServletOutputStream out = null;
  81. out = response.getOutputStream();
  82. writer.flush(out, true);
  83. writer.close();
  84. IoUtil.close(out);
  85. }
  86. @PostMapping("/importUser")
  87. @ApiOperation("导入用户")
  88. public Result importUser(@RequestParam("file") MultipartFile file) {
  89. InputStream in;
  90. try {
  91. in = file.getInputStream();
  92. ExcelReader reader = ExcelUtil.getReader(in);
  93. List<List<Object>> read = reader.read();
  94. log.info("最开始:read={}",read);
  95. log.info("-------------------------------假装我是个分割线------------------------------------");
  96. User user = (User) SecurityUtils.getSubject().getPrincipal();
  97. return userService.importUser(read, user);
  98. } catch (IOException e) {
  99. e.printStackTrace();
  100. throw new CostException(500, "导入失败");
  101. }
  102. }
  103. /**
  104. * 科室模板导出功能设置
  105. */
  106. @ApiOperation("科室导出模板设置")
  107. @GetMapping("/getDepartmentTemplate")
  108. public void getImportDepartmentTemplate(HttpServletResponse response) throws IOException {
  109. User user = (User) SecurityUtils.getSubject().getPrincipal();
  110. String uuid = UUID.randomUUID().toString();
  111. String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
  112. FileUtil.del(FileUtil.file(url));
  113. ExcelWriter writer = new ExcelWriter(url);
  114. // 样式
  115. Sheet sheet = writer.getSheet();
  116. // 内容
  117. writer.merge(0, 1, 0, 2, "为了保证成功导入,请勿修改模板格式", false);
  118. writer.passCurrentRow();
  119. writer.passCurrentRow();
  120. writer.merge(2, 2, 0, 2, "医院科室批量导入", false);
  121. writer.passCurrentRow();
  122. writer.setColumnWidth(0,20);
  123. writer.setColumnWidth(1,15);
  124. writer.setColumnWidth(2,15);
  125. writer.writeRow(Arrays.asList("院区","科室名称", "科室代码"));
  126. // 写入响应
  127. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  128. response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
  129. ServletOutputStream out = null;
  130. out = response.getOutputStream();
  131. writer.flush(out, true);
  132. writer.close();
  133. IoUtil.close(out);
  134. }
  135. /**
  136. * 批量导入科室信息
  137. */
  138. @PostMapping("/importDepartment")
  139. @ApiOperation("导入科室信息")
  140. public Result importDepartment(@RequestParam("file") MultipartFile file){
  141. InputStream in;
  142. try {
  143. in = file.getInputStream();
  144. ExcelReader reader = ExcelUtil.getReader(in);
  145. List<List<Object>> read = reader.read();
  146. log.info("最开始:read={}",read);
  147. log.info("-------------------------------------------------------------------");
  148. User user = (User) SecurityUtils.getSubject().getPrincipal();
  149. Long hospId = user.getHospId();
  150. return departmentService.importDepartment(read, hospId);
  151. }catch (IOException e){
  152. e.printStackTrace();;
  153. throw new CostException(500, "导入失败");
  154. }
  155. }
  156. /**
  157. * 收入成本项目批量导入模板
  158. */
  159. @ApiOperation("成本项目导出模板设置")
  160. @GetMapping("/getImportProductTemplate")
  161. public void getImportProductTemplate(HttpServletResponse response) throws IOException {
  162. User user = (User) SecurityUtils.getSubject().getPrincipal();
  163. String uuid = UUID.randomUUID().toString();
  164. String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
  165. FileUtil.del(FileUtil.file(url));
  166. ExcelWriter writer = new ExcelWriter(url);
  167. // 样式
  168. Sheet sheet = writer.getSheet();
  169. // 内容
  170. writer.merge(0, 1, 0, 2, "为了保证成功导入,请勿修改模板格式", false);
  171. writer.passCurrentRow();
  172. writer.passCurrentRow();
  173. writer.merge(2, 2, 0, 2, "医院成本收入批量导入", false);
  174. writer.passCurrentRow();
  175. writer.setColumnWidth(0,20);
  176. writer.setColumnWidth(1,15);
  177. writer.setColumnWidth(2,15);
  178. writer.writeRow(Arrays.asList("院区","成本项目名", "成本项目编号"));
  179. // 写入响应
  180. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  181. response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
  182. ServletOutputStream out = null;
  183. out = response.getOutputStream();
  184. writer.flush(out, true);
  185. writer.close();
  186. IoUtil.close(out);
  187. }
  188. /**
  189. * 收入成本项目批量导入
  190. */
  191. @PostMapping("/importProduct")
  192. @ApiOperation("批量导入成本项目信息")
  193. public Result importProduct(@RequestParam("file") MultipartFile file){
  194. InputStream in;
  195. try {
  196. in = file.getInputStream();
  197. ExcelReader reader = ExcelUtil.getReader(in);
  198. List<List<Object>> read = reader.read();
  199. log.info("最开始:read={}",read);
  200. log.info("-------------------------------------------------------------------");
  201. User user = (User) SecurityUtils.getSubject().getPrincipal();
  202. Long hospId = user.getHospId();
  203. return productService.importProduct(read, hospId);
  204. }catch (IOException e){
  205. e.printStackTrace();;
  206. throw new CostException(500, "导入失败");
  207. }
  208. }
  209. /**
  210. * 收入成本数据导出模板
  211. */
  212. @ApiOperation("收入数据导出模板设置")
  213. @GetMapping("/getImportIncomeProductAccountTemplate")
  214. public void getImportProductAccountTemplate(HttpServletResponse response) throws IOException {
  215. Long hospId = UserContext.getHospId();
  216. String uuid = UUID.randomUUID().toString();
  217. String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
  218. FileUtil.del(FileUtil.file(url));
  219. ExcelWriter writer = new ExcelWriter(url);
  220. // 样式
  221. Sheet sheet = writer.getSheet();
  222. // 内容
  223. writer.merge(0, 1, 0, 6, "为了保证成功导入,请勿修改模板格式", false);
  224. writer.passCurrentRow();
  225. writer.passCurrentRow();
  226. writer.merge(2, 2, 0, 6, "医院收入数据批量导入", false);
  227. writer.passCurrentRow();
  228. // 冻结前四行
  229. writer.setFreezePane(4);
  230. writer.writeRow(Arrays.asList("成本项目代码","成本项目名称","开单科室","开单科室代码", "执行科室","执行科室代码","金额"));
  231. int accountType=NumberConstant.ONE;
  232. int column=NumberConstant.FOUR;
  233. getProductByAccountType(hospId, writer,accountType,column);
  234. writer.setColumnWidth(0,20);
  235. writer.setColumnWidth(1,20);
  236. writer.setColumnWidth(2,20);
  237. writer.setColumnWidth(3,20);
  238. writer.setColumnWidth(4,20);
  239. writer.setColumnWidth(5,20);
  240. writer.setColumnWidth(6,20);
  241. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  242. response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
  243. ServletOutputStream out = null;
  244. out = response.getOutputStream();
  245. writer.flush(out, true);
  246. writer.close();
  247. IoUtil.close(out);
  248. }
  249. /**
  250. * 成本数据导出模板
  251. */
  252. @GetMapping("/getImportCostProductAccountTemplate")
  253. @ApiOperation("成本数据导出模板设置")
  254. public void getImportCostProductAccountTemplate(HttpServletResponse response) throws IOException {
  255. Long hospId = UserContext.getHospId();
  256. String uuid = UUID.randomUUID().toString();
  257. String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
  258. FileUtil.del(FileUtil.file(url));
  259. ExcelWriter writer = new ExcelWriter(url);
  260. // 样式
  261. Sheet sheet = writer.getSheet();
  262. writer.merge(0, 1, 0, 6, "为了保证成功导入,请勿修改模板格式", false);
  263. writer.passCurrentRow();
  264. writer.passCurrentRow();
  265. writer.merge(2, 2, 0, 6, "医院成本数据批量导入", false);
  266. writer.passCurrentRow();
  267. // 所有科室里面对应存在责任中心对应的科室
  268. List<Department> departmentLinkedList = getDepartments(hospId);
  269. // 设置科室的代码集合
  270. List<String> departmentCodeList = departmentLinkedList.stream().map(Department::getDepartmentCode).collect(Collectors.toList());
  271. List<String> departmentNameList = departmentLinkedList.stream().map(Department::getDepartmentName).collect(Collectors.toList());
  272. departmentCodeList.add(0,null);
  273. departmentCodeList.add(1,null);
  274. writer.writeRow(departmentCodeList);
  275. departmentNameList.add(0,"成本项目代码");
  276. departmentNameList.add(1,"成本项目名称");
  277. writer.writeRow(departmentNameList);
  278. // 设置科室名称的集合
  279. writer.setFreezePane(5);
  280. int accountType=NumberConstant.TWO;
  281. int column=NumberConstant.FIVE;
  282. getProductByAccountType(hospId, writer,accountType,column);
  283. writer.setColumnWidth(0,15);
  284. writer.setColumnWidth(1,15);
  285. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  286. response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
  287. ServletOutputStream out = null;
  288. out = response.getOutputStream();
  289. writer.flush(out, true);
  290. writer.close();
  291. IoUtil.close(out);
  292. }
  293. /**
  294. * 获取执行类型的成本项目
  295. * @param hospId 医院Id
  296. * @param writer 操作流
  297. * @param accountType 会计科目类型 1 收入 2支出(成本)
  298. * @param column 控制第几列
  299. */
  300. private void getProductByAccountType(Long hospId, ExcelWriter writer,Integer accountType,Integer column) {
  301. // 所有的成本项目
  302. List<Product> productList = productService.list(new QueryWrapper<Product>().lambda().eq(Product::getHospId, hospId));
  303. // 所有成本会计对照数据
  304. List<AccountingProduct> accountingProductList = accountingProductService.list(new QueryWrapper<AccountingProduct>().lambda().eq(AccountingProduct::getHospId, hospId));
  305. // 所有会计科目列表数据
  306. List<Accounting> accountingList = accountingService.list(new QueryWrapper<Accounting>().lambda().eq(Accounting::getHospId, hospId));
  307. List<Product> products = new ArrayList<>();
  308. Map<Long, List<AccountingProduct>> accountProductMap = accountingProductList.stream().collect(Collectors.groupingBy(AccountingProduct::getProductId));
  309. Map<Long, List<Accounting>> accountMap = accountingList.stream().collect(Collectors.groupingBy(Accounting::getId));
  310. productList.forEach(i->{
  311. Long productId = i.getId();
  312. List<AccountingProduct> accountingProducts = accountProductMap.get(productId);
  313. if (CollUtil.isNotEmpty(accountingProducts)){
  314. Long accountingId = accountingProducts.get(0).getAccountingId();
  315. List<Accounting> accountings = accountMap.get(accountingId);
  316. if (CollUtil.isNotEmpty(accountings) && accountType.equals(accountings.get(0).getAccountingType())){
  317. products.add(i);
  318. }
  319. }
  320. });
  321. // 写入响应第一列 第二列的数据
  322. for (int j = 0; j<products.size(); j++){
  323. writer.writeCellValue(0,j+column,productList.get(j).getProductCode());
  324. writer.writeCellValue(1,j+column,productList.get(j).getProductName());
  325. }
  326. }
  327. /**
  328. * 成本分摊参数导出模板
  329. */
  330. @GetMapping("/getShareParamTemplate")
  331. @ApiOperation("成本分摊参数导出模板")
  332. public void getShareParamTemplate(HttpServletResponse response) throws IOException{
  333. Long hospId = UserContext.getHospId();
  334. String uuid = UUID.randomUUID().toString();
  335. String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
  336. FileUtil.del(FileUtil.file(url));
  337. ExcelWriter writer = new ExcelWriter(url);
  338. // 样式
  339. Sheet sheet = writer.getSheet();
  340. writer.merge(0, 1, 0, 6, "为了保证成功导入,请勿修改模板格式", false);
  341. writer.passCurrentRow();
  342. writer.passCurrentRow();
  343. writer.merge(2, 2, 0, 6, "医院成本分摊参数数据批量导入", false);
  344. writer.passCurrentRow();
  345. // 所有科室里面对应存在责任中心对应的科室进行显示
  346. List<Department> departmentLinkedList = getDepartments(hospId);
  347. // 设置科室的代码集合
  348. List<String> departmentCodeList = departmentLinkedList.stream().map(Department::getDepartmentCode).collect(Collectors.toList());
  349. List<String> departmentNameList = departmentLinkedList.stream().map(Department::getDepartmentName).collect(Collectors.toList());
  350. departmentCodeList.add(0,null);
  351. departmentCodeList.add(1,null);
  352. writer.writeRow(departmentCodeList);
  353. departmentNameList.add(0,"成本分摊参数代码");
  354. departmentNameList.add(1,"成本分摊参数名称");
  355. writer.writeRow(departmentNameList);
  356. // 设置科室名称的集合
  357. writer.setFreezePane(5);
  358. // 为第一列和第二列设置成本分摊参数的列表数据
  359. List<CostShareParam> costShareParamList = costShareParamService.list(new QueryWrapper<CostShareParam>().lambda().eq(CostShareParam::getHospId, hospId));
  360. for (int j = 0; j<costShareParamList.size(); j++){
  361. writer.writeCellValue(0,j+5,costShareParamList.get(j).getShareParamCode());
  362. writer.writeCellValue(1,j+5,costShareParamList.get(j).getShareParamName());
  363. }
  364. writer.setColumnWidth(0,15);
  365. writer.setColumnWidth(1,15);
  366. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  367. response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
  368. ServletOutputStream out = null;
  369. out = response.getOutputStream();
  370. writer.flush(out, true);
  371. writer.close();
  372. IoUtil.close(out);
  373. }
  374. @NotNull
  375. private List<Department> getDepartments(Long hospId) {
  376. List<Department> departmentLinkedList = new LinkedList<>();
  377. List<Department> departmentList = departmentService.list(new QueryWrapper<Department>().lambda().eq(Department::getHospId, hospId).orderByDesc(Department::getCreateTime));
  378. List<ResponsibilityDepartment> responsibilityDepartmentList = responsibilityDepartmentService.list(new QueryWrapper<ResponsibilityDepartment>().lambda()
  379. .eq(ResponsibilityDepartment::getHospId, hospId));
  380. Map<Long, List<ResponsibilityDepartment>> responsibilityDepartmentMap = responsibilityDepartmentList.stream().collect(Collectors.groupingBy(ResponsibilityDepartment::getDepartmentId));
  381. departmentList.forEach(i -> {
  382. Long id = i.getId();
  383. if (!CollectionUtils.isEmpty(responsibilityDepartmentMap.get(id))) {
  384. // TODO 暂时先不考虑关联的责任中心是否存在
  385. departmentLinkedList.add(i);
  386. }
  387. });
  388. return departmentLinkedList;
  389. }
  390. /**
  391. * 导入 收入数据导入
  392. * @param file 导入的文件
  393. */
  394. @PostMapping("/importDataByFileType")
  395. @ApiOperation("批量导入指定类型数据信息")
  396. public Result importProductAccount(@RequestParam("file") MultipartFile file,Integer year,Integer month){
  397. if (Objects.isNull(file)){
  398. throw new CostException(500,"请选择文件");
  399. }
  400. InputStream in;
  401. // 导入的是收入数据
  402. try {
  403. in = file.getInputStream();
  404. ExcelReader reader = ExcelUtil.getReader(in);
  405. List<List<Object>> read = reader.read();
  406. log.info("最开始:read={}",read);
  407. log.info("-------------------------------------------------------------------");
  408. Long hospId = UserContext.getHospId();
  409. return costIncomeGroupService.importIncomeGroup(read, hospId,file,year,month);
  410. }catch (IOException e){
  411. e.printStackTrace();;
  412. throw new CostException(500, "导入失败");
  413. }
  414. }
  415. }