CostIncomeFileServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. package com.kcim.service.impl;
  2. import cn.hutool.core.date.DateUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  7. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  8. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  9. import com.kcim.common.exception.CostException;
  10. import com.kcim.common.util.*;
  11. import com.kcim.common.constants.NumberConstant;
  12. import com.kcim.common.enums.DateStyleEnum;
  13. import com.kcim.dao.mapper.CostCostingGroupMapper;
  14. import com.kcim.dao.mapper.CostIncomeFileMapper;
  15. import com.kcim.dao.mapper.CostIncomeGroupMapper;
  16. import com.kcim.dao.mapper.ShareParamValueMapper;
  17. import com.kcim.dao.model.*;
  18. import com.kcim.dao.repository.ImportEmpCostRepository;
  19. import com.kcim.dao.repository.ImportPatientInfoRepository;
  20. import com.kcim.dao.repository.ImportPatientItemRepository;
  21. import com.kcim.service.EmpCostImportService;
  22. import com.kcim.service.PatientInfoImportService;
  23. import com.kcim.service.PatientItemImportService;
  24. import com.kcim.vo.CostIncomeFileVO;
  25. import com.kcim.vo.IncomeErrorMessage;
  26. import com.kcim.vo.SessionUserVO;
  27. import com.kcim.service.CostIncomeFileService;
  28. import org.springframework.stereotype.Service;
  29. import org.springframework.transaction.annotation.Propagation;
  30. import org.springframework.transaction.annotation.Transactional;
  31. import org.springframework.util.CollectionUtils;
  32. import org.springframework.util.StringUtils;
  33. import org.springframework.web.multipart.MultipartFile;
  34. import java.util.Date;
  35. import java.util.List;
  36. import java.util.Objects;
  37. import java.util.stream.Collectors;
  38. @Service("costIncomeFileService")
  39. public class CostIncomeFileServiceImpl extends ServiceImpl<CostIncomeFileMapper, CostIncomeFile> implements CostIncomeFileService {
  40. private static final String INCOME_FILETYPE = "收入数据";
  41. private static final String COST_FILETYPE = "成本数据";
  42. private static final String SHAREPARAM_FILETYPE = "成本分摊参数数据";
  43. private static final String EMP_COST_TYPE = "人事成本数据";
  44. private static final String PATIENT_INFO = "患者信息";
  45. private static final String PATIENT_ITEM = "患者项目";
  46. private final CostIncomeGroupMapper costIncomeGroupMapper;
  47. private final CostCostingGroupMapper costCostingGroupMapper;
  48. private final ShareParamValueMapper paramValueMapper;
  49. private final ImportPatientInfoRepository importPatientInfoRepository;
  50. private final ImportPatientItemRepository importPatientItemRepository;
  51. private final ImportEmpCostRepository importEmpCostRepository;
  52. public CostIncomeFileServiceImpl(CostIncomeGroupMapper costIncomeGroupMapper, CostCostingGroupMapper costCostingGroupMapper,
  53. ShareParamValueMapper paramValueMapper, ImportPatientInfoRepository importPatientInfoRepository,
  54. ImportPatientItemRepository importPatientItemRepository, ImportEmpCostRepository importEmpCostRepository) {
  55. this.costIncomeGroupMapper = costIncomeGroupMapper;
  56. this.costCostingGroupMapper = costCostingGroupMapper;
  57. this.paramValueMapper = paramValueMapper;
  58. this.importPatientInfoRepository = importPatientInfoRepository;
  59. this.importPatientItemRepository = importPatientItemRepository;
  60. this.importEmpCostRepository = importEmpCostRepository;
  61. }
  62. /**
  63. * 保存文件上传记录
  64. *
  65. * @param list 文件数据
  66. * @param file 上传文件
  67. * @param hospId 医院Id
  68. * @param incomeErrorMessageList 错误信息
  69. * @param uploadFile 文件路径
  70. * @return
  71. */
  72. @Override
  73. public CostIncomeFile saveCostIncomeFile(List<List<Object>> list, MultipartFile file, Long hospId, List<IncomeErrorMessage> incomeErrorMessageList, String uploadFile, Integer fileType, Integer year, Integer month) {
  74. CostIncomeFile costIncomeFile = new CostIncomeFile();
  75. String substring = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf(".")) + System.currentTimeMillis() + ".xsl";
  76. costIncomeFile.setFileName(substring);
  77. costIncomeFile.setFileUrl(uploadFile);
  78. costIncomeFile.setTotalAmount(list.size());
  79. if (!CollectionUtils.isEmpty(incomeErrorMessageList)) {
  80. costIncomeFile.setSuccessAmount(NumberConstant.ZERO);
  81. costIncomeFile.setErrorList(JacksonUtil.obj2Str(incomeErrorMessageList));
  82. } else {
  83. costIncomeFile.setSuccessAmount(list.size());
  84. }
  85. if (NumberConstant.ONE.equals(fileType)) {
  86. costIncomeFile.setFileType(SHAREPARAM_FILETYPE);
  87. } else if (NumberConstant.TWO.equals(fileType)) {
  88. costIncomeFile.setFileType(INCOME_FILETYPE);
  89. } else if (NumberConstant.THREE.equals(fileType)) {
  90. costIncomeFile.setFileType(COST_FILETYPE);
  91. } else {
  92. costIncomeFile.setFileType(file.getContentType());
  93. }
  94. SessionUserVO user = UserContext.getCurrentUser();
  95. costIncomeFile.setHospId(hospId);
  96. costIncomeFile.setUserName(user.getName());
  97. costIncomeFile.setUserId(user.getId());
  98. costIncomeFile.setDateYear(year);
  99. costIncomeFile.setDateMonth(month);
  100. costIncomeFile.setCreateTime(System.currentTimeMillis());
  101. this.save(costIncomeFile);
  102. return costIncomeFile;
  103. }
  104. @Override
  105. public CostIncomeFile saveFile(Integer fileSize, MultipartFile file, Long hospId, List<IncomeErrorMessage> incomeErrorMessageList, String uploadFile, Integer fileType, Integer year, Integer month) {
  106. CostIncomeFile costIncomeFile = new CostIncomeFile();
  107. String substring = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf(".")) + System.currentTimeMillis() + ".xsl";
  108. costIncomeFile.setFileName(substring);
  109. costIncomeFile.setFileUrl(uploadFile);
  110. costIncomeFile.setTotalAmount(fileSize);
  111. if (!CollectionUtils.isEmpty(incomeErrorMessageList)) {
  112. costIncomeFile.setSuccessAmount(NumberConstant.ZERO);
  113. costIncomeFile.setErrorList(JacksonUtil.obj2Str(incomeErrorMessageList));
  114. } else {
  115. costIncomeFile.setSuccessAmount(fileSize);
  116. }
  117. if (NumberConstant.ONE.equals(fileType)) {
  118. costIncomeFile.setFileType(SHAREPARAM_FILETYPE);
  119. } else if (NumberConstant.TWO.equals(fileType)) {
  120. costIncomeFile.setFileType(INCOME_FILETYPE);
  121. } else if (NumberConstant.THREE.equals(fileType)) {
  122. costIncomeFile.setFileType(COST_FILETYPE);
  123. } else if (NumberConstant.FOUR.equals(fileType)){
  124. costIncomeFile.setFileType(EMP_COST_TYPE);
  125. }else if (NumberConstant.FIVE.equals(fileType)){
  126. costIncomeFile.setFileType(PATIENT_INFO);
  127. }else if (NumberConstant.SIX.equals(fileType)){
  128. costIncomeFile.setFileType(PATIENT_ITEM);
  129. }else {
  130. costIncomeFile.setFileType(file.getContentType());
  131. }
  132. SessionUserVO user = UserContext.getCurrentUser();
  133. costIncomeFile.setHospId(hospId);
  134. costIncomeFile.setUserName(user.getName());
  135. costIncomeFile.setUserId(user.getId());
  136. costIncomeFile.setDateYear(year);
  137. costIncomeFile.setDateMonth(month);
  138. costIncomeFile.setCreateTime(System.currentTimeMillis());
  139. this.save(costIncomeFile);
  140. return costIncomeFile;
  141. }
  142. /**
  143. * 分页查询查询记录数据
  144. *
  145. * @param current 当前页
  146. * @param pageSize 页容量
  147. * @param fileName 文件名
  148. * @param dateTime 日期
  149. * @param hospId 当前院区
  150. * @param filter
  151. * @return
  152. */
  153. @Override
  154. public PageUtils queryList(Integer current, Integer pageSize, String fileName, String dateTime, Long hospId, String filter) {
  155. int year = 0;
  156. int month = 0;
  157. if (StrUtil.isNotBlank(dateTime)) {
  158. Date date = DateUtils.StringToDate(dateTime, DateStyleEnum.YYYY_MM);
  159. year = DateUtil.year(date);
  160. month = DateUtil.month(date) + 1;
  161. }
  162. Page<CostIncomeFile> costIncomeFilePage = new Page<>(current, pageSize);
  163. Page<CostIncomeFile> page = this.page(costIncomeFilePage, new QueryWrapper<CostIncomeFile>().lambda()
  164. .eq(CostIncomeFile::getHospId, hospId)
  165. .like(StrUtil.isNotBlank(fileName), CostIncomeFile::getFileName, fileName)
  166. .eq(StrUtil.isNotBlank(dateTime),CostIncomeFile::getDateYear,year)
  167. .eq(StrUtil.isNotBlank(dateTime),CostIncomeFile::getDateMonth,month)
  168. .orderByDesc(CostIncomeFile::getCreateTime));
  169. List<CostIncomeFile> records = page.getRecords();
  170. List<CostIncomeFileVO> costIncomeFileVOList = BeanUtil.convertList(records, CostIncomeFileVO.class);
  171. costIncomeFileVOList.forEach(i -> {
  172. String errorList = i.getErrorList();
  173. if (StrUtil.isNotBlank(errorList)) {
  174. i.setErrStatus(NumberConstant.ONE);
  175. }
  176. i.setDateTime(DateUtil.format(DateUtil.date(i.getCreateTime()), "yyyy-MM-dd"));
  177. });
  178. PageUtils pageUtils = new PageUtils(page);
  179. pageUtils.setList(costIncomeFileVOList);
  180. return pageUtils;
  181. }
  182. /**
  183. * 撤销导入
  184. *
  185. * @param id
  186. * @param hospId
  187. * @param computeDate
  188. */
  189. @Override
  190. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  191. public void deleteImport(Long id, Long hospId, String computeDate) {
  192. CostIncomeFile costIncomeFile = this.getById(id);
  193. if (Objects.isNull(costIncomeFile)) {
  194. throw new CostException(500, "文件记录不存在");
  195. }
  196. String fileType = costIncomeFile.getFileType();
  197. if (INCOME_FILETYPE.equals(fileType)) {
  198. // 根据文件的Id 和 医院Id获取数据
  199. List<CostIncomeGroup> costIncomeGroupList = costIncomeGroupMapper.selectList(new QueryWrapper<CostIncomeGroup>().lambda()
  200. .eq(CostIncomeGroup::getHospId, hospId).eq(CostIncomeGroup::getFileId, id));
  201. if (CollectionUtils.isEmpty(costIncomeGroupList)) {
  202. throw new CostException(410, "数据已撤销");
  203. }
  204. List<Long> list = costIncomeGroupList.stream().map(CostIncomeGroup::getId).collect(Collectors.toList());
  205. costIncomeGroupMapper.deleteBatchIds(list);
  206. } else if (COST_FILETYPE.equals(fileType)) {
  207. // 根据文件的Id 和 医院Id获取数据
  208. List<CostCostingGroup> costIncomeGroupList = costCostingGroupMapper.selectList(new QueryWrapper<CostCostingGroup>().lambda()
  209. .eq(CostCostingGroup::getHospId, hospId).eq(CostCostingGroup::getFileId, id));
  210. if (CollectionUtils.isEmpty(costIncomeGroupList)) {
  211. throw new CostException(410, "数据已撤销");
  212. }
  213. List<Long> list = costIncomeGroupList.stream().map(CostCostingGroup::getId).collect(Collectors.toList());
  214. costCostingGroupMapper.deleteBatchIds(list);
  215. }else if (SHAREPARAM_FILETYPE.equals(fileType)) {
  216. // 根据文件的Id 和 医院Id获取数据
  217. List<ShareParamValue> shareParamValues = paramValueMapper.selectList(
  218. new LambdaQueryWrapper<ShareParamValue>()
  219. .eq(ShareParamValue::getHospId, hospId)
  220. .eq(ShareParamValue::getFileId, id));
  221. if (CollectionUtils.isEmpty(shareParamValues)) {
  222. throw new CostException(410, "数据已撤销");
  223. }
  224. List<Long> list = shareParamValues.stream().map(ShareParamValue::getId).collect(Collectors.toList());
  225. paramValueMapper.deleteBatchIds(list);
  226. } else if (EMP_COST_TYPE.equals(fileType)) {
  227. //人事成本数据
  228. importEmpCostRepository.removeEmpCostByFileId(computeDate,id);
  229. } else if (PATIENT_INFO.equals(fileType)) {
  230. //患者信息
  231. importPatientInfoRepository.removePatientInfoByFileId(computeDate,id);
  232. } else if (PATIENT_ITEM.equals(fileType)) {
  233. //患者项目
  234. importPatientItemRepository.removePatientItemByFileId(computeDate,id);
  235. }
  236. costIncomeFile.setRollbackStatus(NumberConstant.ONE);
  237. this.updateById(costIncomeFile);
  238. }
  239. /**
  240. * 错误详情
  241. *
  242. * @param id
  243. * @param hospId
  244. * @return
  245. */
  246. @Override
  247. public List<IncomeErrorMessage> getErrorList(Long id, Long hospId) {
  248. CostIncomeFile costIncomeFile = this.getById(id);
  249. if (Objects.isNull(costIncomeFile)) {
  250. throw new CostException(500, "文件记录不存在");
  251. }
  252. String errorList = costIncomeFile.getErrorList();
  253. if (StrUtil.isBlank(errorList)) {
  254. return null;
  255. }
  256. // List<IncomeErrorMessage> incomeErrorMessageList = JsonUtil.toList(errorList, IncomeErrorMessage.class);
  257. List<IncomeErrorMessage> incomeErrorMessageList = JacksonUtil.str2ObjList(errorList, List.class, IncomeErrorMessage.class);
  258. return incomeErrorMessageList;
  259. }
  260. /**
  261. * 文件记录删除
  262. *
  263. * @param idList 文件记录的Id集合
  264. */
  265. @Override
  266. @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
  267. public void deleteByIds(List<Long> idList) {
  268. this.removeByIds(idList);
  269. }
  270. @Override
  271. @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
  272. public void reducingImport(Long id, Long hospId, String computeDate) {
  273. CostIncomeFile costIncomeFile = this.getById(id);
  274. if (Objects.isNull(costIncomeFile)) {
  275. throw new CostException(500, "文件记录不存在");
  276. }
  277. String fileType = costIncomeFile.getFileType();
  278. if (INCOME_FILETYPE.equals(fileType)) {
  279. // 根据文件的Id 和 医院Id获取数据 已撤销数据
  280. List<CostIncomeGroup> historyList = costIncomeGroupMapper.selectHistoryList(hospId,id);
  281. if(!CollectionUtils.isEmpty(historyList)){
  282. List<Long> list = historyList.stream().map(CostIncomeGroup::getId).collect(Collectors.toList());
  283. costIncomeGroupMapper.reducingHistory(id,hospId,list);
  284. }
  285. } else if (COST_FILETYPE.equals(fileType)) {
  286. // 根据文件的Id 和 医院Id获取数据
  287. List<CostCostingGroup> historyList = costCostingGroupMapper.selectHistoryList(hospId,id);
  288. if(!CollectionUtils.isEmpty(historyList)){
  289. List<Long> list = historyList.stream().map(CostCostingGroup::getId).collect(Collectors.toList());
  290. costCostingGroupMapper.reducingHistory(id,hospId,list);
  291. }
  292. }else if (SHAREPARAM_FILETYPE.equals(fileType)) {
  293. // 根据文件的Id 和 医院Id获取数据
  294. List<ShareParamValue> historyList = paramValueMapper.selectHistoryList(hospId,id);
  295. if(!CollectionUtils.isEmpty(historyList)){
  296. List<Long> list = historyList.stream().map(ShareParamValue::getId).collect(Collectors.toList());
  297. paramValueMapper.reducingHistory(id,hospId,list);
  298. }
  299. } else if (EMP_COST_TYPE.equals(fileType)) {
  300. //人事成本数据
  301. importEmpCostRepository.reducingEmpCostByFileId(computeDate,id);
  302. } else if (PATIENT_INFO.equals(fileType)) {
  303. //患者信息
  304. importPatientInfoRepository.reducingPatientInfoByFileId(computeDate,id);
  305. } else if (PATIENT_ITEM.equals(fileType)) {
  306. //患者项目
  307. importPatientItemRepository.reducingPatientItemByFileId(computeDate,id);
  308. }
  309. costIncomeFile.setRollbackStatus(NumberConstant.ZERO);
  310. this.updateById(costIncomeFile);
  311. }
  312. @Override
  313. public void removeByComputeAndFileType(String computeDate, Integer type) {
  314. //
  315. /**
  316. * private static final String INCOME_FILETYPE = "收入数据";
  317. * private static final String COST_FILETYPE = "成本数据";
  318. * private static final String SHAREPARAM_FILETYPE = "成本分摊参数数据";
  319. *
  320. * private static final String EMP_COST_TYPE = "人事成本数据";
  321. *
  322. * private static final String PATIENT_INFO = "患者信息";
  323. *
  324. * private static final String PATIENT_ITEM = "患者项目";
  325. */
  326. String fileType = null;
  327. if(type.equals(NumberConstant.ONE)){
  328. fileType = INCOME_FILETYPE;
  329. } else if (type.equals(NumberConstant.TWO)) {
  330. fileType = COST_FILETYPE;
  331. } else if (type.equals(NumberConstant.THREE)) {
  332. fileType = SHAREPARAM_FILETYPE;
  333. } else if (type.equals(NumberConstant.FOUR)) {
  334. fileType = EMP_COST_TYPE;
  335. } else if (type.equals(NumberConstant.FIVE)) {
  336. fileType = PATIENT_INFO;
  337. } else if (type.equals(NumberConstant.SIX)) {
  338. fileType = PATIENT_ITEM;
  339. }
  340. if(StringUtils.isEmpty(fileType)){
  341. throw new CostException("未知的文件类型");
  342. }
  343. this.removeFilter(computeDate,fileType);
  344. }
  345. private void removeFilter(String computeDate,String fileType) {
  346. UpdateWrapper<CostIncomeFile> updateWrapper = new UpdateWrapper<>();
  347. updateWrapper.lambda().eq(CostIncomeFile::getDeleteTime,0)
  348. .eq(CostIncomeFile::getHospId,UserContext.getHospId())
  349. .eq(CostIncomeFile::getDateYear,ComputeDateUtils.getComputeYear(computeDate))
  350. .eq(CostIncomeFile::getDateMonth,ComputeDateUtils.getComputeMonth(computeDate))
  351. .eq(CostIncomeFile::getFileType,fileType)
  352. .set(CostIncomeFile::getRollbackStatus,NumberConstant.ONE);
  353. this.update(updateWrapper);
  354. }
  355. }