CostIncomeGroupServiceImpl.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. package com.imed.costaccount.service.impl;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.alibaba.fastjson.JSON;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import com.imed.costaccount.common.exception.CostException;
  8. import com.imed.costaccount.common.util.*;
  9. import com.imed.costaccount.constants.NumberConstant;
  10. import com.imed.costaccount.enums.DateStyleEnum;
  11. import com.imed.costaccount.enums.ErrorCodeEnum;
  12. import com.imed.costaccount.mapper.CostIncomeGroupMapper;
  13. import com.imed.costaccount.model.*;
  14. import com.imed.costaccount.model.vo.CostIncomeGroupAllAmountVO;
  15. import com.imed.costaccount.model.vo.CostIncomeGroupBeforeVO;
  16. import com.imed.costaccount.model.vo.IncomeErrorMessage;
  17. import com.imed.costaccount.service.*;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.springframework.beans.factory.annotation.Value;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.transaction.annotation.Propagation;
  22. import org.springframework.transaction.annotation.Transactional;
  23. import org.springframework.util.CollectionUtils;
  24. import org.springframework.web.multipart.MultipartFile;
  25. import java.io.File;
  26. import java.io.IOException;
  27. import java.math.BigDecimal;
  28. import java.util.*;
  29. import java.util.concurrent.atomic.AtomicReference;
  30. import java.util.stream.Collectors;
  31. @Slf4j
  32. @Service("costIncomeGroupService")
  33. public class CostIncomeGroupServiceImpl extends ServiceImpl<CostIncomeGroupMapper, CostIncomeGroup> implements CostIncomeGroupService {
  34. @Value("${file.serverPath}")
  35. private String fileTempPath;
  36. @Value("${file.serverUrl}")
  37. private String FILE_PATH;
  38. private final DepartmentService departmentService;
  39. private final ResponsibilityService responsibilityService;
  40. private final ProductService productService;
  41. private final AccountingService accountingService;
  42. private final ResponsibilityDepartmentService responsibilityDepartmentService;
  43. private final AccountingProductService accountingProductService;
  44. private final CostIncomeFileService costIncomeFileService;
  45. public CostIncomeGroupServiceImpl(DepartmentService departmentService, ResponsibilityService responsibilityService, ProductService productService, AccountingService accountingService, ResponsibilityDepartmentService responsibilityDepartmentService, AccountingProductService accountingProductService, CostIncomeFileService costIncomeFileService) {
  46. this.departmentService = departmentService;
  47. this.responsibilityService = responsibilityService;
  48. this.productService = productService;
  49. this.accountingService = accountingService;
  50. this.responsibilityDepartmentService = responsibilityDepartmentService;
  51. this.accountingProductService = accountingProductService;
  52. this.costIncomeFileService = costIncomeFileService;
  53. }
  54. /**
  55. * 分页查询收入归集前的数据
  56. *
  57. * @param current 当前页
  58. * @param pageSize 当前页大小
  59. * @param dateTime 年月
  60. * @param responsibilityCode 责任中心代码
  61. * @param accountCode 会计科目的Code
  62. * @param hospId 医院Id
  63. * @return
  64. */
  65. @Override
  66. public PageUtils queryList(Integer current, Integer pageSize, String dateTime, String responsibilityCode, String accountCode, Long hospId) {
  67. int year = 0;
  68. int month = 0;
  69. if (StrUtil.isNotBlank(dateTime)) {
  70. year = DateUtils.getYear(dateTime);
  71. month = DateUtils.getMonth(dateTime);
  72. }
  73. Page<CostIncomeGroup> costIncomeGroupPage = new Page<>(current, pageSize);
  74. Page<CostIncomeGroup> pages = this.page(costIncomeGroupPage, new QueryWrapper<CostIncomeGroup>().lambda()
  75. .eq(Objects.nonNull(hospId), CostIncomeGroup::getHospId, hospId)
  76. .eq(!NumberConstant.ZERO.equals(year), CostIncomeGroup::getDateYear, year)
  77. .eq(!NumberConstant.ONE.equals(month), CostIncomeGroup::getDateMonth, month)
  78. .and(StrUtil.isNotBlank(responsibilityCode), i -> i.like(CostIncomeGroup::getOpenResponsibilityCode, responsibilityCode)
  79. .or().like(CostIncomeGroup::getStartResponsibilityCode, responsibilityCode))
  80. .like(StrUtil.isNotBlank(accountCode), CostIncomeGroup::getAccountCode, accountCode));
  81. List<CostIncomeGroup> records = pages.getRecords();
  82. List<CostIncomeGroupBeforeVO> costIncomeGroupBeforeVOList = BeanUtil.convertList(records, CostIncomeGroupBeforeVO.class);
  83. // 查询所有的责任中心 科室 会计科目 成本项目的数据 处理名字
  84. // TODO 修改直接获取名字
  85. setCodeName(hospId, costIncomeGroupBeforeVOList);
  86. // 进行金额合并
  87. List<CostIncomeGroupAllAmountVO> costIncomeGroupAllAmountVoS = baseMapper.countMoney(costIncomeGroupBeforeVOList);
  88. // 对,的金额进行合并
  89. AtomicReference<BigDecimal> totalAmount= null;
  90. costIncomeGroupAllAmountVoS.forEach(i -> {
  91. String allMoney = i.getAllMoney();
  92. if (allMoney.contains(StrUtil.COMMA)) {
  93. // 存在,在进行求和
  94. long sum;
  95. List<Long> list = Arrays.stream(allMoney.split(StrUtil.COMMA)).map(Long::valueOf).collect(Collectors.toList());
  96. sum = list.stream().mapToLong(m -> m).sum();
  97. i.setAmount(BigDecimal.valueOf(sum));
  98. }
  99. //TODO 统计总金额
  100. // totalAmount.updateAndGet(v -> v + i.getAmount());
  101. });
  102. PageUtils pageUtils = new PageUtils(pages);
  103. pageUtils.setList(costIncomeGroupAllAmountVoS);
  104. return pageUtils;
  105. }
  106. /**
  107. * 批量导入收入数据
  108. *
  109. * @param list 输入的文件
  110. * @param user 用户
  111. * @param file
  112. * @return
  113. */
  114. @Override
  115. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  116. public Result importIncomeGroup(List<List<Object>> list, User user, MultipartFile file,Integer year,Integer month) {
  117. Long hospId = user.getHospId();
  118. // 移除前几行的抬头内容 list的大小对应的就是行数的大小
  119. for (int i = list.size() - 1; i >= 0; i--) {
  120. if (i == NumberConstant.ZERO || i == NumberConstant.ONE || i == NumberConstant.TWO || i == NumberConstant.THREE) {
  121. list.remove(list.get(i));
  122. }
  123. }
  124. log.info("读取的数据为:{}", list);
  125. List<IncomeErrorMessage> incomeErrorMessageList = new ArrayList<>();
  126. //获取所有的科室 成本项目 责任中心 会计科目
  127. Map<String, Department> departmentMap = departmentService.list(new QueryWrapper<Department>().lambda().eq(Department::getHospId, hospId)).stream().collect(Collectors.toMap(k -> k.getDepartmentCode() + k.getDepartmentName(), synOe -> synOe));
  128. Map<String, Product> productMap = productService.list(new QueryWrapper<Product>().lambda().eq(Product::getHospId, hospId)).stream().collect(Collectors.toMap(k -> k.getProductCode() + k.getProductName(), synOe -> synOe));
  129. Map<Long, Responsibility> responsibilityMap = responsibilityService.list(new QueryWrapper<Responsibility>().lambda().eq(Responsibility::getHospId, hospId)).stream().collect(Collectors.toMap(Responsibility::getId, synOe -> synOe));
  130. Map<Long, Accounting> accountingMap = accountingService.list(new QueryWrapper<Accounting>().lambda().eq(Accounting::getHospId, hospId)).stream().collect(Collectors.toMap(Accounting::getId, synOe -> synOe));
  131. List<ResponsibilityDepartment> responsibilityDepartmentList = responsibilityDepartmentService.list(new QueryWrapper<ResponsibilityDepartment>().lambda()
  132. .eq(ResponsibilityDepartment::getHospId, hospId));
  133. if (CollectionUtils.isEmpty(responsibilityDepartmentList)) {
  134. throw new CostException(500, "没有科室责任中心对照数据");
  135. }
  136. List<AccountingProduct> accountingProductList = accountingProductService.list(new QueryWrapper<AccountingProduct>().lambda().eq(AccountingProduct::getHospId, hospId));
  137. if (CollectionUtils.isEmpty(accountingProductList)) {
  138. throw new CostException(500, "没有成本会计对照数据");
  139. }
  140. Map<Long, Long> responsibilityDepMap = responsibilityDepartmentList.stream().collect(Collectors.toMap(ResponsibilityDepartment::getDepartmentId, ResponsibilityDepartment::getResponsibilityId));
  141. Map<Long, Long> accountProMap = accountingProductList.stream().collect(Collectors.toMap(AccountingProduct::getProductId, AccountingProduct::getAccountingId));
  142. List<CostIncomeGroup> costIncomeGroupArrayList = new ArrayList<>();
  143. // 检验数据 TODO 缺少文件Id
  144. checkImportData(list, incomeErrorMessageList, costIncomeGroupArrayList, departmentMap, productMap, responsibilityMap, accountingMap, responsibilityDepMap, accountProMap,hospId,year,month);
  145. // 文件上传
  146. String uploadFile = uploadFile(file, UserContext.getCurrentUser());
  147. // 上传记录保存
  148. if (StrUtil.isBlank(uploadFile)){
  149. throw new CostException(500,"文件上传异常");
  150. }
  151. // 记录文件上传记录
  152. CostIncomeFile costIncomeFile = new CostIncomeFile();
  153. costIncomeFile.setFileType(file.getContentType());
  154. String substring = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."))+System.currentTimeMillis()+".xsl";
  155. costIncomeFile.setFileName(substring);
  156. costIncomeFile.setFileUrl(uploadFile);
  157. costIncomeFile.setTotalAmount(list.size());
  158. if (!CollectionUtils.isEmpty(incomeErrorMessageList)) {
  159. costIncomeFile.setSuccessAmount(NumberConstant.ZERO);
  160. costIncomeFile.setErrorList(JSON.toJSONString(incomeErrorMessageList));
  161. }else {
  162. costIncomeFile.setSuccessAmount(list.size());
  163. }
  164. costIncomeFile.setHospId(hospId);
  165. costIncomeFile.setUserName(user.getName());
  166. costIncomeFile.setUserId(user.getId());
  167. costIncomeFile.setCreateTime(System.currentTimeMillis());
  168. costIncomeFileService.save(costIncomeFile);
  169. Long id = costIncomeFile.getId();
  170. costIncomeGroupArrayList.forEach(i->{
  171. // 设置文件Id
  172. i.setFileId(id);
  173. });
  174. if (CollectionUtils.isEmpty(incomeErrorMessageList)){
  175. this.saveBatch(costIncomeGroupArrayList);
  176. }
  177. return Result.ok();
  178. }
  179. /**
  180. * 检验数据
  181. * @param list 表单数据
  182. * @param incomeErrorMessageList 存储错误信息的集合
  183. * @param costIncomeGroupArrayList
  184. * @param departmentMap 科室Map
  185. * @param productMap 成本项目map
  186. * @param responsibilityMap 责任中心Map
  187. * @param accountingMap 会计科目Map
  188. * @param responsibilityDepMap 责任中心科室对照Map
  189. * @param accountProMap 责任中心Map
  190. * @param hospId
  191. */
  192. private void checkImportData(List<List<Object>> list, List<IncomeErrorMessage> incomeErrorMessageList, List<CostIncomeGroup> costIncomeGroupArrayList, Map<String, Department> departmentMap, Map<String, Product> productMap, Map<Long, Responsibility> responsibilityMap, Map<Long, Accounting> accountingMap, Map<Long, Long> responsibilityDepMap, Map<Long, Long> accountProMap, Long hospId,Integer year,Integer month) {
  193. for (int i = 0; i < list.size(); i++) {
  194. CostIncomeGroup costIncomeGroup = new CostIncomeGroup();
  195. // 用来检验数据合理性的循环
  196. IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
  197. List<Object> data = list.get(i);
  198. log.info("用户输入的数据是{}", data);
  199. // TODO 暂时不走循环 直接获取数据的处理
  200. // 成本项目的代码和名称
  201. String productCode = data.get(0).toString();
  202. String productName = data.get(1).toString();
  203. // 开单科室 执行科室的代码和名称
  204. String openDepartmentName = data.get(2).toString();
  205. String openDepartmentCode = data.get(3).toString();
  206. String startDepartmentName = data.get(4).toString();
  207. String startDepartmentCode = data.get(5).toString();
  208. BigDecimal beforeMoney = BigDecimal.valueOf(Double.parseDouble(data.get(15).toString()));
  209. boolean checkNull = StrUtil.isBlank(data.get(15).toString());
  210. boolean checkOne = NumberConstant.ZERO.equals(data.get(15).toString());
  211. if (checkNull || checkOne) {
  212. // 要求这一行的数据必须全部填写
  213. for (int j = 0; j < NumberConstant.FIVE; j++) {
  214. if (Objects.isNull(data.get(j))) {
  215. incomeErrorMessage.setTotal(i);
  216. int row = i + 1;
  217. int column = j + 1;
  218. incomeErrorMessage.setErrMessage("第{" + row + "}行第{" + column + "}列数据为空");
  219. incomeErrorMessageList.add(incomeErrorMessage);
  220. }
  221. }
  222. }
  223. // 检验收入成本项目数据是否存在
  224. Product product = productMap.get(productCode + productName);
  225. Department department = departmentMap.get(openDepartmentCode + openDepartmentName);
  226. Department department1 = departmentMap.get(startDepartmentCode + startDepartmentName);
  227. if (!checkNull && !checkOne ){
  228. if (StrUtil.isNotBlank(productCode) && StrUtil.isNotBlank(productName)){
  229. if (Objects.isNull(product)) {
  230. incomeErrorMessage.setTotal(i);
  231. incomeErrorMessage.setErrMessage("代码:" + productCode + " 名称:" + productName + "成本项目不存在");
  232. incomeErrorMessageList.add(incomeErrorMessage);
  233. } else {
  234. // 检验对应的会计科目是否存在
  235. Long id = product.getId();
  236. Long accountId = accountProMap.get(id);
  237. if (Objects.isNull(accountingMap.get(accountId))) {
  238. incomeErrorMessage.setTotal(i);
  239. incomeErrorMessage.setErrMessage("代码:" + productCode + " 名称:" + productName + "成本项目对应的会计科目不存在");
  240. incomeErrorMessageList.add(incomeErrorMessage);
  241. } else {
  242. costIncomeGroup.setProductCode(productCode);
  243. costIncomeGroup.setProductName(productName);
  244. costIncomeGroup.setAccountCode(accountingMap.get(accountId).getAccountingCode());
  245. costIncomeGroup.setAccountName(accountingMap.get(accountId).getAccountingName());
  246. }
  247. }
  248. }else{
  249. costIncomeGroup.setProductCode(null);
  250. costIncomeGroup.setProductName(null);
  251. costIncomeGroup.setAccountCode( null);
  252. costIncomeGroup.setAccountName(null);
  253. }
  254. }
  255. // 检验开单科室
  256. if (!checkNull && !checkOne ){
  257. if (StrUtil.isNotBlank(openDepartmentCode) && StrUtil.isNotBlank(openDepartmentName)){
  258. // 开单科室
  259. if (Objects.isNull(department)) {
  260. incomeErrorMessage.setTotal(i);
  261. incomeErrorMessage.setErrMessage("代码:" + productCode + " 名称:" + productName + "开单科室不存在");
  262. incomeErrorMessageList.add(incomeErrorMessage);
  263. } else {
  264. Long id = department.getId();
  265. Long responsibilityId = responsibilityDepMap.get(id);
  266. if (Objects.isNull(responsibilityMap.get(responsibilityId))) {
  267. incomeErrorMessage.setTotal(i);
  268. incomeErrorMessage.setErrMessage("代码:" + openDepartmentCode + " 名称:" + openDepartmentName + "科室对应的责任中心不存在");
  269. incomeErrorMessageList.add(incomeErrorMessage);
  270. } else {
  271. costIncomeGroup.setOpenDepartmentCode(openDepartmentCode);
  272. costIncomeGroup.setOpenDepartmentName(openDepartmentName);
  273. costIncomeGroup.setOpenResponsibilityCode(responsibilityMap.get(responsibilityId).getResponsibilityCode());
  274. costIncomeGroup.setOpenResponsibilityName(responsibilityMap.get(responsibilityId).getResponsibilityName());
  275. }
  276. }
  277. }else {
  278. costIncomeGroup.setOpenDepartmentCode(null);
  279. costIncomeGroup.setOpenDepartmentName(null);
  280. costIncomeGroup.setOpenResponsibilityCode(null);
  281. costIncomeGroup.setOpenResponsibilityName(null);
  282. }
  283. }
  284. // 检验执行科室
  285. if ( !checkNull && !checkOne ){
  286. if (StrUtil.isNotBlank(startDepartmentCode) && StrUtil.isNotBlank(startDepartmentName)){
  287. //执行科室
  288. if (Objects.isNull(department1)) {
  289. incomeErrorMessage.setTotal(i);
  290. incomeErrorMessage.setErrMessage("代码:" + productCode + " 名称:" + productName + "执行科室不存在");
  291. incomeErrorMessageList.add(incomeErrorMessage);
  292. } else {
  293. Long id = department1.getId();
  294. Long responsibilityId = responsibilityDepMap.get(id);
  295. if (Objects.isNull(responsibilityMap.get(responsibilityId))) {
  296. incomeErrorMessage.setTotal(i);
  297. incomeErrorMessage.setErrMessage("代码:" + openDepartmentCode + " 名称:" + openDepartmentName + "科室对应的责任中心不存在");
  298. incomeErrorMessageList.add(incomeErrorMessage);
  299. } else {
  300. costIncomeGroup.setStartDepartmentCode(startDepartmentCode);
  301. costIncomeGroup.setStartDepartmentName(startDepartmentName);
  302. costIncomeGroup.setStartResponsibilityCode(responsibilityMap.get(responsibilityId).getResponsibilityCode());
  303. costIncomeGroup.setStartResponsibilityName(responsibilityMap.get(responsibilityId).getResponsibilityName());
  304. }
  305. }
  306. }else {
  307. costIncomeGroup.setStartDepartmentCode(null);
  308. costIncomeGroup.setStartDepartmentName(null);
  309. costIncomeGroup.setStartResponsibilityCode(null);
  310. costIncomeGroup.setStartResponsibilityName(null);
  311. }
  312. }
  313. costIncomeGroup.setDoctorNumber(Long.parseLong(data.get(6).toString())).setDoctorName(data.get(7).toString())
  314. .setPatientId(Long.parseLong(data.get(8).toString())).setOutpatientId(Long.parseLong(data.get(9).toString()))
  315. .setPatientName(data.get(10).toString()).setPatientFee(data.get(11).toString()).setReceiptFee(data.get(12).toString())
  316. .setTotalNumber(Integer.parseInt(data.get(13).toString())).setUnit(data.get(14).toString())
  317. .setFeeDatetime(DateUtils.StringToDate(data.get(16).toString(), DateStyleEnum.YYYY_MM_DD_HH_MM_SS_EN));
  318. //
  319. costIncomeGroup.setHospId(hospId);
  320. costIncomeGroup.setCreateTime(System.currentTimeMillis());
  321. costIncomeGroup.setDateYear(year);
  322. costIncomeGroup.setDateMonth(month);
  323. costIncomeGroup.setAmount(beforeMoney);
  324. costIncomeGroupArrayList.add(costIncomeGroup);
  325. }
  326. }
  327. /**
  328. * 设置相关名称
  329. *
  330. * @param hospId
  331. * @param costIncomeGroupBeforeVOList
  332. */
  333. private void setCodeName(Long hospId, List<CostIncomeGroupBeforeVO> costIncomeGroupBeforeVOList) {
  334. // List<Responsibility> responsibilityList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda().eq(Responsibility::getHospId, hospId));
  335. // Map<String, String> responsibilityMap = responsibilityList.stream().collect(Collectors.toMap(Responsibility::getResponsibilityCode, Responsibility::getResponsibilityName));
  336. // List<Department> departmentList = departmentService.list(new QueryWrapper<Department>().lambda().eq(Department::getHospId, hospId));
  337. // Map<String, String> departmentMap = departmentList.stream().collect(Collectors.toMap(Department::getDepartmentCode, Department::getDepartmentName));
  338. // List<Product> productList = productService.list(new QueryWrapper<Product>().lambda().eq(Product::getHospId, hospId));
  339. // Map<String, String> productMap = productList.stream().collect(Collectors.toMap(Product::getProductCode, Product::getProductName));
  340. // List<Accounting> accountingList = accountingService.list(new QueryWrapper<Accounting>().lambda().eq(Accounting::getHospId, hospId));
  341. // Map<String, String> accountMap = accountingList.stream().collect(Collectors.toMap(Accounting::getAccountingCode, Accounting::getAccountingName));
  342. costIncomeGroupBeforeVOList.forEach(i -> {
  343. // 以为这里的数据导入的 在导入的时候进行数据校验
  344. // 设置开单科室名称 执行科室名称 开单责任中心名称 执行责任中心名称 成本项目的名称 会计科目名称
  345. i.setOpenDepartmentCodeName("[" + i.getOpenDepartmentCode() + "]" + i.getOpenDepartmentName());
  346. i.setOpenResponsibilityCodeName("[" + i.getOpenResponsibilityCode() + "]" + i.getOpenResponsibilityName());
  347. i.setStartDepartmentCodeName("[" + i.getStartDepartmentCode() + "]" + i.getStartDepartmentName());
  348. i.setStartResponsibilityCodeName("[" + i.getStartResponsibilityCode() + "]" + i.getStartResponsibilityCode());
  349. i.setProductCodeName("[" + i.getProductCode() + "]" + i.getProductName());
  350. i.setAccountCodeName("[" + i.getAccountCode() + "]" + i.getAccountName());
  351. });
  352. }
  353. /**
  354. * 文件上传
  355. */
  356. public String uploadFile(MultipartFile file, User user) {
  357. Long hospId = user.getHospId();
  358. String fileName = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."))+System.currentTimeMillis()+".xsl";
  359. String localFilePath = fileTempPath + StrUtil.SLASH + hospId + StrUtil.SLASH;
  360. File tempFile = new File(localFilePath);
  361. if (!tempFile.exists()) {
  362. tempFile.mkdirs();
  363. }
  364. localFilePath = localFilePath + fileName;
  365. try {
  366. file.transferTo(new File(localFilePath));
  367. } catch (IOException e) {
  368. log.error("【文件上传至本地】失败,绝对路径:{}", e.getMessage());
  369. throw new CostException(ErrorCodeEnum.FILE_UPLOAD_ERROR);
  370. } finally {
  371. }
  372. return FILE_PATH + hospId + StrUtil.SLASH + fileName;
  373. }
  374. }