CostIncomeGroupServiceImpl.java 32 KB

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