CostIncomeGroupServiceImpl.java 19 KB

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