ExcelController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.Accounting;
  14. import com.imed.costaccount.model.AccountingProduct;
  15. import com.imed.costaccount.model.Product;
  16. import com.imed.costaccount.model.User;
  17. import com.imed.costaccount.service.AccountingProductService;
  18. import com.imed.costaccount.service.AccountingService;
  19. import com.imed.costaccount.service.UserService;
  20. import com.imed.costaccount.service.impl.DepartmentServiceImpl;
  21. import com.imed.costaccount.service.impl.ProductServiceImpl;
  22. import io.swagger.annotations.Api;
  23. import io.swagger.annotations.ApiOperation;
  24. import lombok.extern.slf4j.Slf4j;
  25. import org.apache.poi.ss.usermodel.Sheet;
  26. import org.apache.shiro.SecurityUtils;
  27. import org.springframework.web.bind.annotation.*;
  28. import org.springframework.web.multipart.MultipartFile;
  29. import javax.servlet.ServletOutputStream;
  30. import javax.servlet.http.HttpServletResponse;
  31. import java.io.File;
  32. import java.io.IOException;
  33. import java.io.InputStream;
  34. import java.util.*;
  35. import java.util.stream.Collectors;
  36. /**
  37. * 相关导入导出操作
  38. */
  39. @Slf4j
  40. @Api(tags = "excel导入导出")
  41. @RestController
  42. @RequestMapping("/costAccount/excel")
  43. public class ExcelController {
  44. private final UserService userService;
  45. private final DepartmentServiceImpl departmentService;
  46. private final ProductServiceImpl productService;
  47. private final AccountingService accountingService;
  48. private final AccountingProductService accountingProductService;
  49. public ExcelController(UserService userService, DepartmentServiceImpl departmentService, ProductServiceImpl productService, AccountingService accountingService, AccountingProductService accountingProductService) {
  50. this.userService = userService;
  51. this.departmentService = departmentService;
  52. this.productService = productService;
  53. this.accountingService = accountingService;
  54. this.accountingProductService = accountingProductService;
  55. }
  56. @ApiOperation("用户导出模板设置")
  57. @GetMapping("/getcurrentTemplate")
  58. public void getImportUserTemplate(HttpServletResponse response) throws IOException {
  59. User user = (User) SecurityUtils.getSubject().getPrincipal();
  60. // TODO: 2021/7/26 暂时没有登录
  61. String uuid = UUID.randomUUID().toString();
  62. String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
  63. FileUtil.del(FileUtil.file(url));
  64. ExcelWriter writer = new ExcelWriter(url);
  65. // 样式
  66. Sheet sheet = writer.getSheet();
  67. // 内容
  68. writer.merge(0, 1, 0, 4, "为了保证成功导入,请勿修改模板格式", false);
  69. writer.passCurrentRow();
  70. writer.passCurrentRow();
  71. writer.merge(2, 2, 0, 4, "测试医院用户导入", false);
  72. writer.passCurrentRow();
  73. writer.writeRow(Arrays.asList("院区","姓名", "工号", "密码","手机号码"));
  74. // 写入响应
  75. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  76. response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
  77. ServletOutputStream out = null;
  78. out = response.getOutputStream();
  79. writer.flush(out, true);
  80. writer.close();
  81. IoUtil.close(out);
  82. }
  83. @PostMapping("/importUser")
  84. @ApiOperation("导入用户")
  85. public Result importUser(@RequestParam("file") MultipartFile file) {
  86. InputStream in;
  87. try {
  88. in = file.getInputStream();
  89. ExcelReader reader = ExcelUtil.getReader(in);
  90. List<List<Object>> read = reader.read();
  91. log.info("最开始:read={}",read);
  92. log.info("-------------------------------假装我是个分割线------------------------------------");
  93. User user = (User) SecurityUtils.getSubject().getPrincipal();
  94. return userService.importUser(read, user);
  95. } catch (IOException e) {
  96. e.printStackTrace();
  97. throw new CostException(500, "导入失败");
  98. }
  99. }
  100. /**
  101. * 科室模板导出功能设置
  102. */
  103. @ApiOperation("科室导出模板设置")
  104. @GetMapping("/getDepartmentTemplate")
  105. public void getImportDepartmentTemplate(HttpServletResponse response) throws IOException {
  106. User user = (User) SecurityUtils.getSubject().getPrincipal();
  107. String uuid = UUID.randomUUID().toString();
  108. String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
  109. FileUtil.del(FileUtil.file(url));
  110. ExcelWriter writer = new ExcelWriter(url);
  111. // 样式
  112. Sheet sheet = writer.getSheet();
  113. // 内容
  114. writer.merge(0, 1, 0, 2, "为了保证成功导入,请勿修改模板格式", false);
  115. writer.passCurrentRow();
  116. writer.passCurrentRow();
  117. writer.merge(2, 2, 0, 2, "医院科室批量导入", false);
  118. writer.passCurrentRow();
  119. writer.setColumnWidth(0,20);
  120. writer.setColumnWidth(1,15);
  121. writer.setColumnWidth(2,15);
  122. writer.writeRow(Arrays.asList("院区","科室名称", "科室代码"));
  123. // 写入响应
  124. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  125. response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
  126. ServletOutputStream out = null;
  127. out = response.getOutputStream();
  128. writer.flush(out, true);
  129. writer.close();
  130. IoUtil.close(out);
  131. }
  132. /**
  133. * 批量导入科室信息
  134. */
  135. @PostMapping("/importDepartment")
  136. @ApiOperation("导入科室信息")
  137. public Result importDepartment(@RequestParam("file") MultipartFile file){
  138. InputStream in;
  139. try {
  140. in = file.getInputStream();
  141. ExcelReader reader = ExcelUtil.getReader(in);
  142. List<List<Object>> read = reader.read();
  143. log.info("最开始:read={}",read);
  144. log.info("-------------------------------------------------------------------");
  145. User user = (User) SecurityUtils.getSubject().getPrincipal();
  146. Long hospId = user.getHospId();
  147. return departmentService.importDepartment(read, hospId);
  148. }catch (IOException e){
  149. e.printStackTrace();;
  150. throw new CostException(500, "导入失败");
  151. }
  152. }
  153. /**
  154. * 收入成本项目批量导入模板
  155. */
  156. @ApiOperation("收入成本导出模板设置")
  157. @GetMapping("/getImportProductTemplate")
  158. public void getImportProductTemplate(HttpServletResponse response) throws IOException {
  159. User user = (User) SecurityUtils.getSubject().getPrincipal();
  160. String uuid = UUID.randomUUID().toString();
  161. String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
  162. FileUtil.del(FileUtil.file(url));
  163. ExcelWriter writer = new ExcelWriter(url);
  164. // 样式
  165. Sheet sheet = writer.getSheet();
  166. // 内容
  167. writer.merge(0, 1, 0, 2, "为了保证成功导入,请勿修改模板格式", false);
  168. writer.passCurrentRow();
  169. writer.passCurrentRow();
  170. writer.merge(2, 2, 0, 2, "医院成本收入批量导入", false);
  171. writer.passCurrentRow();
  172. writer.setColumnWidth(0,20);
  173. writer.setColumnWidth(1,15);
  174. writer.setColumnWidth(2,15);
  175. writer.writeRow(Arrays.asList("院区","成本项目名", "成本项目编号"));
  176. // 写入响应
  177. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  178. response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
  179. ServletOutputStream out = null;
  180. out = response.getOutputStream();
  181. writer.flush(out, true);
  182. writer.close();
  183. IoUtil.close(out);
  184. }
  185. /**
  186. * 收入成本项目批量导入
  187. */
  188. @PostMapping("/importProduct")
  189. @ApiOperation("批量导入收入成本信息")
  190. public Result importProduct(@RequestParam("file") MultipartFile file){
  191. InputStream in;
  192. try {
  193. in = file.getInputStream();
  194. ExcelReader reader = ExcelUtil.getReader(in);
  195. List<List<Object>> read = reader.read();
  196. log.info("最开始:read={}",read);
  197. log.info("-------------------------------------------------------------------");
  198. User user = (User) SecurityUtils.getSubject().getPrincipal();
  199. Long hospId = user.getHospId();
  200. return productService.importProduct(read, hospId);
  201. }catch (IOException e){
  202. e.printStackTrace();;
  203. throw new CostException(500, "导入失败");
  204. }
  205. }
  206. /**
  207. * 收入成本数据导出模板
  208. * @param accountType 1收入数据 2 成本数据(支出数据)
  209. */
  210. @ApiOperation("收入数据导出模板设置")
  211. @GetMapping("/getImportProductAccountTemplate")
  212. public void getImportProductAccountTemplate(HttpServletResponse response,Integer accountType) throws IOException {
  213. Long hospId = UserContext.getHospId();
  214. String uuid = UUID.randomUUID().toString();
  215. String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
  216. FileUtil.del(FileUtil.file(url));
  217. ExcelWriter writer = new ExcelWriter(url);
  218. // 样式
  219. Sheet sheet = writer.getSheet();
  220. // 内容
  221. writer.merge(0, 1, 0, 6, "为了保证成功导入,请勿修改模板格式", false);
  222. writer.passCurrentRow();
  223. writer.passCurrentRow();
  224. String stateType= NumberConstant.ONE.equals(accountType)?"医院收入数据批量导入":"医院成本数据批量导入";
  225. writer.merge(2, 2, 0, 6, stateType, false);
  226. writer.passCurrentRow();
  227. // 冻结前四行
  228. writer.setFreezePane(4);
  229. writer.writeRow(Arrays.asList("","","开单科室","开单科室代码", "执行科室","执行科室代码","金额"));
  230. // 所有的成本项目
  231. List<Product> productList = productService.list(new QueryWrapper<Product>().lambda().eq(Product::getHospId, hospId));
  232. // 所有成本会计对照数据
  233. List<AccountingProduct> accountingProductList = accountingProductService.list(new QueryWrapper<AccountingProduct>().lambda().eq(AccountingProduct::getHospId, hospId));
  234. // 所有会计科目列表数据
  235. List<Accounting> accountingList = accountingService.list(new QueryWrapper<Accounting>().lambda().eq(Accounting::getHospId, hospId));
  236. List<Product> products = new ArrayList<>();
  237. Map<Long, List<AccountingProduct>> accountProductMap = accountingProductList.stream().collect(Collectors.groupingBy(AccountingProduct::getProductId));
  238. Map<Long, List<Accounting>> accountMap = accountingList.stream().collect(Collectors.groupingBy(Accounting::getId));
  239. productList.forEach(i->{
  240. Long productId = i.getId();
  241. List<AccountingProduct> accountingProducts = accountProductMap.get(productId);
  242. if (CollUtil.isNotEmpty(accountingProducts)){
  243. Long accountingId = accountingProducts.get(0).getAccountingId();
  244. List<Accounting> accountings = accountMap.get(accountingId);
  245. if (CollUtil.isNotEmpty(accountings) && accountType.equals(accountings.get(0).getAccountingType())){
  246. products.add(i);
  247. }
  248. }
  249. });
  250. // 写入响应第一列 第二列的数据
  251. for (int j = 0; j<products.size(); j++){
  252. writer.writeCellValue(0,j+4,productList.get(j).getProductCode());
  253. writer.writeCellValue(1,j+4,productList.get(j).getProductName());
  254. }
  255. writer.setColumnWidth(0,10);
  256. writer.setColumnWidth(1,10);
  257. writer.setColumnWidth(2,20);
  258. writer.setColumnWidth(3,20);
  259. writer.setColumnWidth(4,20);
  260. writer.setColumnWidth(5,20);
  261. writer.setColumnWidth(6,20);
  262. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  263. response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
  264. ServletOutputStream out = null;
  265. out = response.getOutputStream();
  266. writer.flush(out, true);
  267. writer.close();
  268. IoUtil.close(out);
  269. }
  270. /**
  271. * 收入成本项目批量导入
  272. */
  273. @PostMapping("/importProductAccount")
  274. @ApiOperation("批量导入收入数据信息")
  275. public Result importProductAccount(@RequestParam("file") MultipartFile file){
  276. InputStream in;
  277. try {
  278. in = file.getInputStream();
  279. ExcelReader reader = ExcelUtil.getReader(in);
  280. List<List<Object>> read = reader.read();
  281. log.info("最开始:read={}",read);
  282. log.info("-------------------------------------------------------------------");
  283. Long hospId = UserContext.getHospId();
  284. return productService.importProduct(read, hospId);
  285. }catch (IOException e){
  286. e.printStackTrace();;
  287. throw new CostException(500, "导入失败");
  288. }
  289. }
  290. }