CostIncomeGroupServiceImpl.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. package com.imed.costaccount.service.impl;
  2. import cn.hutool.core.date.DateUtil;
  3. import cn.hutool.core.util.StrUtil;
  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.common.enums.DateStyleEnum;
  11. import com.imed.costaccount.common.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. private final CostIncomeGroupSetService costIncomeGroupSetService;
  46. public CostIncomeGroupServiceImpl(DepartmentService departmentService, ResponsibilityService responsibilityService, ProductService productService, AccountingService accountingService, ResponsibilityDepartmentService responsibilityDepartmentService, AccountingProductService accountingProductService, CostIncomeFileService costIncomeFileService, CostIncomeGroupSetService costIncomeGroupSetService) {
  47. this.departmentService = departmentService;
  48. this.responsibilityService = responsibilityService;
  49. this.productService = productService;
  50. this.accountingService = accountingService;
  51. this.responsibilityDepartmentService = responsibilityDepartmentService;
  52. this.accountingProductService = accountingProductService;
  53. this.costIncomeFileService = costIncomeFileService;
  54. this.costIncomeGroupSetService = costIncomeGroupSetService;
  55. }
  56. /**
  57. * 分页查询收入归集前的数据
  58. *
  59. * @param current 当前页
  60. * @param pageSize 当前页大小
  61. * @param dateTime 年月
  62. * @param responsibilityCode 责任中心代码
  63. * @param productCode 会计科目的Code
  64. * @param hospId 医院Id
  65. * @return
  66. */
  67. @Override
  68. public PageUtils queryList(Integer current, Integer pageSize, String dateTime, String responsibilityCode, String productCode, Long hospId) {
  69. // 先检验当前年月是否存在数据
  70. int year = 0;
  71. int month = 0;
  72. Date date = DateUtils.StringToDate(dateTime, DateStyleEnum.YYYY_MM);
  73. if (StrUtil.isNotBlank(dateTime)) {
  74. year = DateUtil.year(date);
  75. month = DateUtil.month(date) + 1;
  76. }
  77. Page<CostIncomeGroup> costIncomeGroupPage = new Page<>(current, pageSize);
  78. Page<CostIncomeGroup> pages = this.page(costIncomeGroupPage, new QueryWrapper<CostIncomeGroup>().lambda()
  79. .eq(Objects.nonNull(hospId), CostIncomeGroup::getHospId, hospId)
  80. .eq(!NumberConstant.ZERO.equals(year), CostIncomeGroup::getDateYear, year)
  81. .eq(!NumberConstant.ZERO.equals(month), CostIncomeGroup::getDateMonth, month)
  82. .and(StrUtil.isNotBlank(responsibilityCode), i -> i.like(CostIncomeGroup::getOpenResponsibilityCode, responsibilityCode)
  83. .or().like(CostIncomeGroup::getStartResponsibilityCode, responsibilityCode))
  84. .like(StrUtil.isNotBlank(productCode), CostIncomeGroup::getProductCode, productCode));
  85. List<CostIncomeGroup> records = pages.getRecords();
  86. List<CostIncomeGroupBeforeVO> costIncomeGroupBeforeVOList = BeanUtil.convertList(records, CostIncomeGroupBeforeVO.class);
  87. if (CollectionUtils.isEmpty(costIncomeGroupBeforeVOList)) {
  88. PageUtils pageUtils = new PageUtils(pages);
  89. pageUtils.setList(null);
  90. pageUtils.setTotalAmount(new BigDecimal("0.0000"));
  91. return pageUtils;
  92. }
  93. List<CostIncomeGroup> list = this.list(new QueryWrapper<CostIncomeGroup>().lambda()
  94. .eq(Objects.nonNull(hospId), CostIncomeGroup::getHospId, hospId)
  95. .eq(!NumberConstant.ZERO.equals(year), CostIncomeGroup::getDateYear, year)
  96. .eq(!NumberConstant.ZERO.equals(month), CostIncomeGroup::getDateMonth, month)
  97. .and(StrUtil.isNotBlank(responsibilityCode), i -> i.like(CostIncomeGroup::getOpenResponsibilityCode, responsibilityCode)
  98. .or().like(CostIncomeGroup::getStartResponsibilityCode, responsibilityCode))
  99. .like(StrUtil.isNotBlank(productCode), CostIncomeGroup::getProductCode, productCode));
  100. // 进行金额合并
  101. List<CostIncomeGroupBeforeVO> costIncomeGroupBeforeVOList1 = BeanUtil.convertList(list, CostIncomeGroupBeforeVO.class);
  102. List<Long> idList = costIncomeGroupBeforeVOList1.stream().map(CostIncomeGroupBeforeVO::getId).collect(Collectors.toList());
  103. List<CostIncomeGroupAllAmountVO> costIncomeGroupAllAmountVoS = baseMapper.countMoney(idList);
  104. // 查询所有的责任中心 科室 会计科目 成本项目的数据 处理名字
  105. setCodeName(costIncomeGroupAllAmountVoS);
  106. // 对,的金额进行合并
  107. AtomicReference<BigDecimal> totalAmount = new AtomicReference<>(new BigDecimal("0.0000"));
  108. costIncomeGroupAllAmountVoS.forEach(i -> {
  109. String allMoney = i.getAllMoney();
  110. if (StrUtil.isNotBlank(allMoney) && allMoney.contains(StrUtil.COMMA)) {
  111. // 存在,在进行求和
  112. BigDecimal sum;
  113. List<BigDecimal> bigDecimals = Arrays.stream(allMoney.split(StrUtil.COMMA)).map(BigDecimal::new).collect(Collectors.toList());
  114. sum = bigDecimals.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
  115. i.setAmount(sum);
  116. }
  117. totalAmount.updateAndGet(v -> v.add(i.getAmount()));
  118. });
  119. PageUtils pageUtils = new PageUtils(pages);
  120. pageUtils.setList(costIncomeGroupAllAmountVoS);
  121. pageUtils.setTotalAmount(totalAmount.get());
  122. return pageUtils;
  123. }
  124. /**
  125. * 批量导入收入数据
  126. *
  127. * @param list 输入的文件
  128. * @param user 用户
  129. * @param file
  130. * @return
  131. */
  132. @Override
  133. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  134. public Result importIncomeGroup(List<List<Object>> list, User user, MultipartFile file, String dateTime, Integer fileType) {
  135. // 先检验当前年月是否存在数据
  136. int year = 0;
  137. int month = 0;
  138. Date date = DateUtils.StringToDate(dateTime, DateStyleEnum.YYYY_MM);
  139. if (StrUtil.isNotBlank(dateTime)) {
  140. year = DateUtil.year(date);
  141. month = DateUtil.month(date) + 1;
  142. }
  143. Long hospId = user.getHospId();
  144. List<CostIncomeGroup> groups = this.list(new QueryWrapper<CostIncomeGroup>().lambda().eq(CostIncomeGroup::getHospId, hospId)
  145. .eq(CostIncomeGroup::getDateYear, year).eq(CostIncomeGroup::getDateMonth, month));
  146. if (!CollectionUtils.isEmpty(groups)) {
  147. throw new CostException(500, year + "年" + month + "月数据已存在");
  148. }
  149. // 移除前几行的抬头内容 list的大小对应的就是行数的大小
  150. removeTitle(list);
  151. log.info("读取的数据为:{}", list);
  152. List<IncomeErrorMessage> incomeErrorMessageList = new ArrayList<>();
  153. //获取所有的科室 成本项目 责任中心 会计科目
  154. //
  155. Map<String, Department> departmentMap = getDepartmentByCodeNameMap(hospId);
  156. Map<String, Product> productMap = getProductByCodeNameMap(hospId);
  157. Map<Long, Responsibility> responsibilityMap = getResponsibilityIdResponsibilityMap(hospId);
  158. Map<Long, Accounting> accountingMap = getAccountIdAccountingMap(hospId);
  159. List<ResponsibilityDepartment> responsibilityDepartmentList = getResponsibilityDepartments(hospId);
  160. if (CollectionUtils.isEmpty(responsibilityDepartmentList)) {
  161. throw new CostException(500, "没有科室责任中心对照数据");
  162. }
  163. List<AccountingProduct> accountingProductList = accountingProductService.list(new QueryWrapper<AccountingProduct>().lambda().eq(AccountingProduct::getHospId, hospId));
  164. if (CollectionUtils.isEmpty(accountingProductList)) {
  165. throw new CostException(500, "没有成本会计对照数据");
  166. }
  167. Map<Long, Long> responsibilityDepMap = getDepartmentIdResponsibilityIdMap(responsibilityDepartmentList);
  168. Map<Long, Long> accountProMap = getProductIdAccountIdMap(accountingProductList);
  169. List<CostIncomeGroup> costIncomeGroupArrayList = new ArrayList<>();
  170. // 检验数据
  171. checkImportData(list, incomeErrorMessageList, costIncomeGroupArrayList, departmentMap, productMap, responsibilityMap, accountingMap, responsibilityDepMap, accountProMap, hospId, year, month);
  172. // 文件上传
  173. String uploadFile = uploadFile(file, UserContext.getCurrentUser());
  174. // 上传记录保存
  175. if (StrUtil.isBlank(uploadFile)) {
  176. throw new CostException(500, "文件上传异常");
  177. }
  178. // 记录文件上传记录
  179. CostIncomeFile costIncomeFile = costIncomeFileService.saveCostIncomeFile(list, user, file, hospId, incomeErrorMessageList, uploadFile, fileType, year, month);
  180. Long id = costIncomeFile.getId();
  181. costIncomeGroupArrayList.forEach(i -> {
  182. // 设置文件Id
  183. i.setFileId(id);
  184. });
  185. if (CollectionUtils.isEmpty(incomeErrorMessageList)) {
  186. this.saveBatch(costIncomeGroupArrayList);
  187. return Result.build(200, "数据导入成功", null);
  188. } else {
  189. return Result.build(200, "数据未成功导入", null);
  190. }
  191. }
  192. private void removeTitle(List<List<Object>> list) {
  193. for (int i = list.size() - 1; i >= 0; i--) {
  194. if (i == NumberConstant.ZERO || i == NumberConstant.ONE || i == NumberConstant.TWO || i == NumberConstant.THREE) {
  195. list.remove(list.get(i));
  196. }
  197. }
  198. }
  199. /**
  200. * 返回成本会计对照数据 Key 成本项目Id Value 会计科目Id
  201. *
  202. * @param accountingProductList
  203. * @return
  204. */
  205. public Map<Long, Long> getProductIdAccountIdMap(List<AccountingProduct> accountingProductList) {
  206. return accountingProductList.stream().collect(Collectors.toMap(AccountingProduct::getProductId, AccountingProduct::getAccountingId));
  207. }
  208. /**
  209. * Map类型
  210. * Key DepartmentId
  211. * Value ResponsibilityId
  212. *
  213. * @param responsibilityDepartmentList
  214. * @return
  215. */
  216. public Map<Long, Long> getDepartmentIdResponsibilityIdMap(List<ResponsibilityDepartment> responsibilityDepartmentList) {
  217. return responsibilityDepartmentList.stream().collect(Collectors.toMap(ResponsibilityDepartment::getDepartmentId, ResponsibilityDepartment::getResponsibilityId));
  218. }
  219. public List<ResponsibilityDepartment> getResponsibilityDepartments(Long hospId) {
  220. return responsibilityDepartmentService.list(new QueryWrapper<ResponsibilityDepartment>().lambda()
  221. .eq(ResponsibilityDepartment::getHospId, hospId));
  222. }
  223. /**
  224. * Key AccountId
  225. * Value Accounting对象
  226. *
  227. * @param hospId
  228. * @return
  229. */
  230. public Map<Long, Accounting> getAccountIdAccountingMap(Long hospId) {
  231. return accountingService.list(new QueryWrapper<Accounting>().lambda().eq(Accounting::getHospId, hospId)).stream().collect(Collectors.toMap(Accounting::getId, synOe -> synOe));
  232. }
  233. /**
  234. * Key responsibilityId 责任中心Id
  235. * Value Responsibility对象
  236. *
  237. * @param hospId
  238. * @return
  239. */
  240. public Map<Long, Responsibility> getResponsibilityIdResponsibilityMap(Long hospId) {
  241. return responsibilityService.list(new QueryWrapper<Responsibility>().lambda().eq(Responsibility::getHospId, hospId)).stream().collect(Collectors.toMap(Responsibility::getId, synOe -> synOe));
  242. }
  243. /**
  244. * Key 成本项目Code+Name
  245. * Value 成本项目对象
  246. *
  247. * @param hospId
  248. * @return
  249. */
  250. public Map<String, Product> getProductByCodeNameMap(Long hospId) {
  251. return productService.list(new QueryWrapper<Product>().lambda().eq(Product::getHospId, hospId)).stream().collect(Collectors.toMap(k -> k.getProductCode() + k.getProductName(), synOe -> synOe));
  252. }
  253. /**
  254. * Key 科室项目Code+Name
  255. * Value 科室对象
  256. *
  257. * @param hospId
  258. * @return
  259. */
  260. public Map<String, Department> getDepartmentByCodeNameMap(Long hospId) {
  261. return departmentService.list(new QueryWrapper<Department>().lambda().eq(Department::getHospId, hospId)).stream().collect(Collectors.toMap(k -> k.getDepartmentCode() + k.getDepartmentName(), synOe -> synOe));
  262. }
  263. /**
  264. * 导入收入数据
  265. *
  266. * @param read 读取到的初始数据
  267. * @param user 当前登录用户
  268. * @param file 导入的文件
  269. * @param year 年
  270. * @param month 月
  271. * @return {@link Result}
  272. */
  273. @Override
  274. public Result importDataByIncomeData(List<List<Object>> read, User user, MultipartFile file, Integer year, Integer month) {
  275. // 去头
  276. this.removeTitle(read);
  277. return null;
  278. }
  279. /**
  280. * 删除收入归集的数据
  281. *
  282. * @param asList
  283. */
  284. @Override
  285. @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
  286. public void deleteByIds(List<Long> asList) {
  287. this.removeByIds(asList);
  288. }
  289. /**
  290. * 检验数据
  291. *
  292. * @param list 表单数据
  293. * @param incomeErrorMessageList 存储错误信息的集合
  294. * @param costIncomeGroupArrayList
  295. * @param departmentMap 科室Map
  296. * @param productMap 成本项目map
  297. * @param responsibilityMap 责任中心Map
  298. * @param accountingMap 会计科目Map
  299. * @param responsibilityDepMap 责任中心科室对照Map
  300. * @param accountProMap 责任中心Map
  301. * @param hospId
  302. */
  303. 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) {
  304. for (int i = 0; i < list.size(); i++) {
  305. int row = i + 5;
  306. CostIncomeGroup costIncomeGroup = new CostIncomeGroup();
  307. AfterIncomeGroup afterIncomegroup = new AfterIncomeGroup();
  308. // 用来检验数据合理性的循环
  309. List<Object> data = list.get(i);
  310. log.info("用户输入的数据是{}", data);
  311. // 成本项目的代码和名称
  312. if (Objects.isNull(data.get(0)) || Objects.isNull(data.get(1))){
  313. continue;
  314. }
  315. String productCode = data.get(0).toString().trim();
  316. String productName = data.get(1).toString().trim();
  317. // 开单科室 执行科室的代码和名称
  318. String openDepartmentName = data.get(2).toString();
  319. String openDepartmentCode = data.get(3).toString();
  320. String startDepartmentName = data.get(4).toString();
  321. String startDepartmentCode = data.get(5).toString();
  322. if (StrUtil.isBlank(openDepartmentCode) && StrUtil.isBlank(openDepartmentName) && StrUtil.isBlank(startDepartmentCode) && StrUtil.isBlank(startDepartmentName)) {
  323. continue;
  324. }
  325. BigDecimal beforeMoney = BigDecimal.valueOf(Double.parseDouble(("0".equals(data.get(15).toString()) || StrUtil.isBlank(data.get(15).toString())) ? "0.00" : data.get(15).toString()));
  326. boolean checkNull = StrUtil.isNotBlank(data.get(15).toString());
  327. boolean checkOne = "0".equals(data.get(15).toString());
  328. // TODO 为0的时候这一行是否可以输入空的
  329. if (checkNull || checkOne) {
  330. // 不为空要求这一行全部输入 前6列数据不能为空
  331. for (int j = 0; j < NumberConstant.SIX; j++) {
  332. if (StrUtil.isBlank(data.get(j).toString())) {
  333. IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
  334. incomeErrorMessage.setTotal(row);
  335. int column = j + 1;
  336. incomeErrorMessage.setErrMessage("第{" + row + "}行第{" + column + "}列数据为空");
  337. incomeErrorMessageList.add(incomeErrorMessage);
  338. }
  339. }
  340. }
  341. // 检验收入成本项目数据是否存在
  342. Product product = productMap.get(productCode.trim() + productName.trim());
  343. Department department = departmentMap.get(openDepartmentCode.trim() + openDepartmentName.trim());
  344. Department department1 = departmentMap.get(startDepartmentCode.trim() + startDepartmentName.trim());
  345. if (StrUtil.isNotBlank(productCode) && StrUtil.isNotBlank(productName)) {
  346. if (Objects.isNull(product)) {
  347. IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
  348. incomeErrorMessage.setTotal(row);
  349. incomeErrorMessage.setErrMessage("成本项目代码:" + productCode + " 名称:" + productName + "不存在");
  350. incomeErrorMessageList.add(incomeErrorMessage);
  351. } else {
  352. // 检验对应的会计科目是否存在
  353. Long id = product.getId();
  354. Long accountId = accountProMap.get(id);
  355. if (Objects.isNull(accountingMap.get(accountId))) {
  356. IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
  357. incomeErrorMessage.setTotal(row);
  358. incomeErrorMessage.setErrMessage("成本项目代码:" + productCode + " 名称:" + productName + "对应的会计科目不存在");
  359. incomeErrorMessageList.add(incomeErrorMessage);
  360. } else {
  361. costIncomeGroup.setProductCode(productCode);
  362. costIncomeGroup.setProductName(productName);
  363. costIncomeGroup.setAccountCode(accountingMap.get(accountId).getAccountingCode());
  364. costIncomeGroup.setAccountName(accountingMap.get(accountId).getAccountingName());
  365. }
  366. }
  367. } else {
  368. costIncomeGroup.setProductCode(null);
  369. costIncomeGroup.setProductName(null);
  370. costIncomeGroup.setAccountCode(null);
  371. costIncomeGroup.setAccountName(null);
  372. }
  373. // 检验开单科室
  374. if (StrUtil.isNotBlank(openDepartmentCode) && StrUtil.isNotBlank(openDepartmentName)) {
  375. // 开单科室
  376. if (Objects.isNull(department)) {
  377. IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
  378. incomeErrorMessage.setTotal(row);
  379. incomeErrorMessage.setErrMessage("开单科室代码:" + openDepartmentCode + " 科室名称:" + openDepartmentName + "不存在");
  380. incomeErrorMessageList.add(incomeErrorMessage);
  381. } else {
  382. Long id = department.getId();
  383. Long responsibilityId = responsibilityDepMap.get(id);
  384. if (Objects.isNull(responsibilityMap.get(responsibilityId))) {
  385. IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
  386. incomeErrorMessage.setTotal(row);
  387. incomeErrorMessage.setErrMessage("开单科室代码代码:" + openDepartmentCode + " 科室名称:" + openDepartmentName + "对应的责任中心不存在");
  388. incomeErrorMessageList.add(incomeErrorMessage);
  389. } else {
  390. Responsibility responsibility = responsibilityMap.get(responsibilityId);
  391. costIncomeGroup.setOpenDepartmentCode(openDepartmentCode);
  392. costIncomeGroup.setOpenDepartmentName(openDepartmentName);
  393. costIncomeGroup.setOpenResponsibilityCode(responsibility.getResponsibilityCode());
  394. costIncomeGroup.setOpenResponsibilityName(responsibility.getResponsibilityName());
  395. afterIncomegroup.setOpenDepartmentCode(openDepartmentCode);
  396. afterIncomegroup.setOpenDepartmentName(openDepartmentName);
  397. afterIncomegroup.setOpenDepartmentStatus(responsibility.getResponsibilityType());
  398. afterIncomegroup.setOpenResponsibilityCode(responsibility.getResponsibilityCode());
  399. afterIncomegroup.setOpenResponsibilityName(responsibility.getResponsibilityName());
  400. }
  401. }
  402. } else {
  403. costIncomeGroup.setOpenDepartmentCode(null);
  404. costIncomeGroup.setOpenDepartmentName(null);
  405. costIncomeGroup.setOpenResponsibilityCode(null);
  406. costIncomeGroup.setOpenResponsibilityName(null);
  407. }
  408. // 检验执行科室
  409. if (StrUtil.isNotBlank(startDepartmentCode) && StrUtil.isNotBlank(startDepartmentName)) {
  410. //执行科室
  411. if (Objects.isNull(department1)) {
  412. IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
  413. incomeErrorMessage.setTotal(row);
  414. incomeErrorMessage.setErrMessage("执行科室代码:" + startDepartmentCode + " 科室名称:" + startDepartmentName + "科室不存在");
  415. incomeErrorMessageList.add(incomeErrorMessage);
  416. } else {
  417. Long id = department1.getId();
  418. Long responsibilityId = responsibilityDepMap.get(id);
  419. if (Objects.isNull(responsibilityMap.get(responsibilityId))) {
  420. IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
  421. incomeErrorMessage.setTotal(row);
  422. incomeErrorMessage.setErrMessage("执行科室代码:" + startDepartmentCode + " 名称:" + startDepartmentCode + "对应的责任中心不存在");
  423. incomeErrorMessageList.add(incomeErrorMessage);
  424. } else {
  425. Responsibility responsibility = responsibilityMap.get(responsibilityId);
  426. costIncomeGroup.setStartDepartmentCode(startDepartmentCode);
  427. costIncomeGroup.setStartDepartmentName(startDepartmentName);
  428. costIncomeGroup.setStartResponsibilityCode(responsibility.getResponsibilityCode());
  429. costIncomeGroup.setStartResponsibilityName(responsibility.getResponsibilityName());
  430. afterIncomegroup.setStartDepartmentCode(startDepartmentCode);
  431. afterIncomegroup.setStartDepartmentName(startDepartmentName);
  432. afterIncomegroup.setStartDepartmentStatus(responsibility.getResponsibilityType());
  433. afterIncomegroup.setStartResponsibilityCode(responsibility.getResponsibilityCode());
  434. afterIncomegroup.setStartResponsibilityName(responsibility.getResponsibilityName());
  435. }
  436. }
  437. } else {
  438. costIncomeGroup.setStartDepartmentCode(null);
  439. costIncomeGroup.setStartDepartmentName(null);
  440. costIncomeGroup.setStartResponsibilityCode(null);
  441. costIncomeGroup.setStartResponsibilityName(null);
  442. }
  443. costIncomeGroup.setDoctorNumber(StrUtil.isBlank(data.get(6).toString()) ? null : Long.parseLong(data.get(6).toString())).setDoctorName(data.get(7).toString())
  444. .setPatientId(StrUtil.isBlank(data.get(8).toString()) ? null : Long.parseLong(data.get(8).toString())).setOutpatientId(StrUtil.isBlank(data.get(9).toString()) ? null : Long.parseLong(data.get(9).toString()))
  445. .setPatientName(data.get(10).toString()).setPatientFee(data.get(11).toString()).setReceiptFee(data.get(12).toString())
  446. .setTotalNumber(StrUtil.isBlank(data.get(13).toString()) ? null : Integer.parseInt(data.get(13).toString())).setUnit(data.get(14).toString())
  447. .setFeeDatetime(StrUtil.isBlank(data.get(16).toString()) ? null : DateUtils.StringToDate(data.get(16).toString(), DateStyleEnum.YYYY_MM_DD_HH_MM_SS));
  448. costIncomeGroup.setHospId(hospId);
  449. costIncomeGroup.setCreateTime(System.currentTimeMillis());
  450. costIncomeGroup.setDateYear(year);
  451. costIncomeGroup.setDateMonth(month);
  452. costIncomeGroup.setAmount(beforeMoney);
  453. // 检验数据
  454. getAfterData(hospId, costIncomeGroup, afterIncomegroup, beforeMoney);
  455. costIncomeGroupArrayList.add(costIncomeGroup);
  456. }
  457. }
  458. /**
  459. * 封装需要的数据
  460. *
  461. * @param hospId
  462. * @param costIncomeGroup
  463. * @param afterIncomegroup
  464. * @param beforeMoney
  465. */
  466. private void getAfterData(Long hospId, CostIncomeGroup costIncomeGroup, AfterIncomeGroup afterIncomegroup, BigDecimal beforeMoney) {
  467. Integer openDepartmentStatus = afterIncomegroup.getOpenDepartmentStatus();
  468. Integer startDepartmentStatus = afterIncomegroup.getStartDepartmentStatus();
  469. if (Objects.nonNull(openDepartmentStatus) && Objects.nonNull(startDepartmentStatus)) {
  470. Map<String, CostIncomeGroupSet> incomeGroupSetMap = costIncomeGroupSetService.list(new QueryWrapper<CostIncomeGroupSet>().lambda().eq(CostIncomeGroupSet::getHospId, hospId)).stream().collect(Collectors.toMap(k -> k.getOpenDepartmentStatus().toString() + k.getStartDepartmentStatus().toString(), synOe -> synOe));
  471. CostIncomeGroupSet costIncomeGroupSet = incomeGroupSetMap.get(openDepartmentStatus.toString() + startDepartmentStatus.toString());
  472. if (Objects.nonNull(costIncomeGroupSet)) {
  473. Map<String, Responsibility> map = responsibilityService.list(new QueryWrapper<Responsibility>().lambda().eq(Responsibility::getHospId, hospId)).stream().collect(Collectors.toMap(Responsibility::getResponsibilityCode, synOe -> synOe));
  474. // 开单科室比例
  475. BigDecimal openDepartmentProportion = new BigDecimal(costIncomeGroupSet.getOpenDepartmentProportion().toString());
  476. // 执行科室比例
  477. BigDecimal startDepartmentProportion = new BigDecimal(costIncomeGroupSet.getStartDepartmentProportion().toString());
  478. // beforeMoney
  479. afterIncomegroup.setOpenDepartmentDecimal(beforeMoney.multiply(openDepartmentProportion).divide(new BigDecimal(100), 4, BigDecimal.ROUND_HALF_UP));
  480. afterIncomegroup.setStartDepartmentDecimal(beforeMoney.multiply(startDepartmentProportion).divide(new BigDecimal(100), 4, BigDecimal.ROUND_HALF_UP));
  481. String responsibilityCodes = costIncomeGroupSet.getResponsibilityCodes();
  482. if (NumberConstant.TWO.equals(costIncomeGroupSet.getOpenDepartmentStatus()) && NumberConstant.TWO.equals(costIncomeGroupSet.getStartDepartmentStatus()) && StrUtil.isNotBlank(responsibilityCodes)) {
  483. // /拼接的
  484. afterIncomegroup.setDirectStatus(NumberConstant.TWO);
  485. String responsibilityCode = responsibilityCodes.split(StrUtil.SLASH)[responsibilityCodes.split(StrUtil.SLASH).length - 1];
  486. Responsibility responsibility = map.get(responsibilityCode);
  487. if (Objects.nonNull(responsibility)) {
  488. afterIncomegroup.setResponsibilityCode(responsibility.getResponsibilityCode());
  489. afterIncomegroup.setResponsibilityName(responsibility.getResponsibilityName());
  490. afterIncomegroup.setOtherResponsibilityDecimal(beforeMoney);
  491. }
  492. }
  493. costIncomeGroup.setAfterIncomeGroup(JacksonUtil.obj2Str(afterIncomegroup));
  494. }
  495. }
  496. }
  497. /**
  498. * 设置相关名称
  499. *
  500. * @param costIncomeGroupAllAmountVOList
  501. */
  502. private void setCodeName(List<CostIncomeGroupAllAmountVO> costIncomeGroupAllAmountVOList) {
  503. costIncomeGroupAllAmountVOList.forEach(i -> {
  504. // 以为这里的数据导入的 在导入的时候进行数据校验
  505. // 设置开单科室名称 执行科室名称 开单责任中心名称 执行责任中心名称 成本项目的名称 会计科目名称
  506. i.setOpenDepartmentCodeName("[" + i.getOpenDepartmentCode() + "]" + i.getOpenDepartmentName());
  507. i.setOpenResponsibilityCodeName("[" + i.getOpenResponsibilityCode() + "]" + i.getOpenResponsibilityName());
  508. i.setStartDepartmentCodeName("[" + i.getStartDepartmentCode() + "]" + i.getStartDepartmentName());
  509. i.setStartResponsibilityCodeName("[" + i.getStartResponsibilityCode() + "]" + i.getStartResponsibilityName());
  510. i.setProductCodeName("[" + i.getProductCode() + "]" + i.getProductName());
  511. i.setAccountCodeName("[" + i.getAccountCode() + "]" + i.getAccountName());
  512. });
  513. }
  514. /**
  515. * 文件上传
  516. */
  517. public String uploadFile(MultipartFile file, User user) {
  518. Long hospId = user.getHospId();
  519. String fileName = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf(".")) + System.currentTimeMillis() + ".xsl";
  520. String localFilePath = fileTempPath + StrUtil.SLASH + hospId + StrUtil.SLASH;
  521. File tempFile = new File(localFilePath);
  522. if (!tempFile.exists()) {
  523. tempFile.mkdirs();
  524. }
  525. localFilePath = localFilePath + fileName;
  526. try {
  527. file.transferTo(new File(localFilePath));
  528. } catch (IOException e) {
  529. log.error("【文件上传至本地】失败,绝对路径:{}", e.getMessage());
  530. throw new CostException(ErrorCodeEnum.FILE_UPLOAD_ERROR);
  531. } finally {
  532. }
  533. return FILE_PATH + hospId + StrUtil.SLASH + fileName;
  534. }
  535. }