CostIncomeGroupServiceImpl.java 21 KB

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