CostIncomeFileServiceImpl.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.LambdaQueryWrapper;
  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.BeanUtil;
  10. import com.imed.costaccount.common.util.DateUtils;
  11. import com.imed.costaccount.common.util.JacksonUtil;
  12. import com.imed.costaccount.common.util.PageUtils;
  13. import com.imed.costaccount.constants.NumberConstant;
  14. import com.imed.costaccount.common.enums.DateStyleEnum;
  15. import com.imed.costaccount.mapper.CostCostingGroupMapper;
  16. import com.imed.costaccount.mapper.CostIncomeFileMapper;
  17. import com.imed.costaccount.mapper.CostIncomeGroupMapper;
  18. import com.imed.costaccount.mapper.ShareParamValueMapper;
  19. import com.imed.costaccount.model.*;
  20. import com.imed.costaccount.model.vo.CostIncomeFileVO;
  21. import com.imed.costaccount.model.vo.IncomeErrorMessage;
  22. import com.imed.costaccount.service.CostIncomeFileService;
  23. import org.springframework.stereotype.Service;
  24. import org.springframework.transaction.annotation.Propagation;
  25. import org.springframework.transaction.annotation.Transactional;
  26. import org.springframework.util.CollectionUtils;
  27. import org.springframework.web.multipart.MultipartFile;
  28. import java.util.Date;
  29. import java.util.List;
  30. import java.util.Objects;
  31. import java.util.stream.Collectors;
  32. @Service("costIncomeFileService")
  33. public class CostIncomeFileServiceImpl extends ServiceImpl<CostIncomeFileMapper, CostIncomeFile> implements CostIncomeFileService {
  34. private static final String INCOME_FILETYPE = "收入数据";
  35. private static final String COST_FILETYPE = "成本数据";
  36. private static final String SHAREPARAM_FILETYPE = "成本分摊参数数据";
  37. private final CostIncomeGroupMapper costIncomeGroupMapper;
  38. private final CostCostingGroupMapper costCostingGroupMapper;
  39. private final ShareParamValueMapper paramValueMapper;
  40. public CostIncomeFileServiceImpl(CostIncomeGroupMapper costIncomeGroupMapper, CostCostingGroupMapper costCostingGroupMapper, ShareParamValueMapper paramValueMapper) {
  41. this.costIncomeGroupMapper = costIncomeGroupMapper;
  42. this.costCostingGroupMapper = costCostingGroupMapper;
  43. this.paramValueMapper = paramValueMapper;
  44. }
  45. /**
  46. * 保存文件上传记录
  47. *
  48. * @param list 文件数据
  49. * @param user 当前用户
  50. * @param file 上传文件
  51. * @param hospId 医院Id
  52. * @param incomeErrorMessageList 错误信息
  53. * @param uploadFile 文件路径
  54. * @return
  55. */
  56. @Override
  57. public CostIncomeFile saveCostIncomeFile(List<List<Object>> list, User user, MultipartFile file, Long hospId, List<IncomeErrorMessage> incomeErrorMessageList, String uploadFile, Integer fileType, Integer year, Integer month) {
  58. CostIncomeFile costIncomeFile = new CostIncomeFile();
  59. String substring = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf(".")) + System.currentTimeMillis() + ".xsl";
  60. costIncomeFile.setFileName(substring);
  61. costIncomeFile.setFileUrl(uploadFile);
  62. costIncomeFile.setTotalAmount(list.size());
  63. if (!CollectionUtils.isEmpty(incomeErrorMessageList)) {
  64. costIncomeFile.setSuccessAmount(NumberConstant.ZERO);
  65. costIncomeFile.setErrorList(JacksonUtil.obj2Str(incomeErrorMessageList));
  66. } else {
  67. costIncomeFile.setSuccessAmount(list.size());
  68. }
  69. if (NumberConstant.ONE.equals(fileType)) {
  70. costIncomeFile.setFileType(SHAREPARAM_FILETYPE);
  71. } else if (NumberConstant.TWO.equals(fileType)) {
  72. costIncomeFile.setFileType(INCOME_FILETYPE);
  73. } else if (NumberConstant.THREE.equals(fileType)) {
  74. costIncomeFile.setFileType(COST_FILETYPE);
  75. } else {
  76. costIncomeFile.setFileType(file.getContentType());
  77. }
  78. costIncomeFile.setHospId(hospId);
  79. costIncomeFile.setUserName(user.getName());
  80. costIncomeFile.setUserId(user.getId());
  81. costIncomeFile.setDateYear(year);
  82. costIncomeFile.setDateMonth(month);
  83. costIncomeFile.setCreateTime(System.currentTimeMillis());
  84. this.save(costIncomeFile);
  85. return costIncomeFile;
  86. }
  87. /**
  88. * 分页查询查询记录数据
  89. *
  90. * @param current
  91. * @param pageSize
  92. * @param fileName
  93. * @param dateTime
  94. * @param hospId
  95. * @return
  96. */
  97. @Override
  98. public PageUtils queryList(Integer current, Integer pageSize, String fileName, String dateTime, Long hospId) {
  99. int year = 0;
  100. int month = 0;
  101. if (StrUtil.isNotBlank(dateTime)) {
  102. Date date = DateUtils.StringToDate(dateTime, DateStyleEnum.YYYY_MM);
  103. year = DateUtil.year(date);
  104. month = DateUtil.month(date) + 1;
  105. }
  106. Page<CostIncomeFile> costIncomeFilePage = new Page<>(current, pageSize);
  107. Page<CostIncomeFile> page = this.page(costIncomeFilePage, new QueryWrapper<CostIncomeFile>().lambda()
  108. .eq(CostIncomeFile::getHospId, hospId)
  109. .like(StrUtil.isNotBlank(fileName), CostIncomeFile::getFileName, fileName)
  110. .eq(StrUtil.isNotBlank(dateTime),CostIncomeFile::getDateYear,year)
  111. .eq(StrUtil.isNotBlank(dateTime),CostIncomeFile::getDateMonth,month)
  112. .orderByDesc(CostIncomeFile::getCreateTime));
  113. List<CostIncomeFile> records = page.getRecords();
  114. List<CostIncomeFileVO> costIncomeFileVOList = BeanUtil.convertList(records, CostIncomeFileVO.class);
  115. costIncomeFileVOList.forEach(i -> {
  116. String errorList = i.getErrorList();
  117. if (StrUtil.isNotBlank(errorList)) {
  118. i.setErrStatus(NumberConstant.ONE);
  119. }
  120. i.setDateTime(DateUtil.format(DateUtil.date(i.getCreateTime()), "yyyy-MM-dd"));
  121. });
  122. PageUtils pageUtils = new PageUtils(page);
  123. pageUtils.setList(costIncomeFileVOList);
  124. return pageUtils;
  125. }
  126. /**
  127. * 撤销导入
  128. *
  129. * @param id
  130. * @param hospId
  131. */
  132. @Override
  133. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  134. public void deleteImport(Long id, Long hospId) {
  135. CostIncomeFile costIncomeFile = this.getById(id);
  136. if (Objects.isNull(costIncomeFile)) {
  137. throw new CostException(500, "文件记录不存在");
  138. }
  139. String fileType = costIncomeFile.getFileType();
  140. if (INCOME_FILETYPE.equals(fileType)) {
  141. // 根据文件的Id 和 医院Id获取数据
  142. List<CostIncomeGroup> costIncomeGroupList = costIncomeGroupMapper.selectList(new QueryWrapper<CostIncomeGroup>().lambda()
  143. .eq(CostIncomeGroup::getHospId, hospId).eq(CostIncomeGroup::getFileId, id));
  144. if (CollectionUtils.isEmpty(costIncomeGroupList)) {
  145. throw new CostException(410, "数据已撤销");
  146. }
  147. List<Long> list = costIncomeGroupList.stream().map(CostIncomeGroup::getId).collect(Collectors.toList());
  148. costIncomeGroupMapper.deleteBatchIds(list);
  149. } else if (COST_FILETYPE.equals(fileType)) {
  150. // 根据文件的Id 和 医院Id获取数据
  151. List<CostCostingGroup> costIncomeGroupList = costCostingGroupMapper.selectList(new QueryWrapper<CostCostingGroup>().lambda()
  152. .eq(CostCostingGroup::getHospId, hospId).eq(CostCostingGroup::getFileId, id));
  153. if (CollectionUtils.isEmpty(costIncomeGroupList)) {
  154. throw new CostException(410, "数据已撤销");
  155. }
  156. List<Long> list = costIncomeGroupList.stream().map(CostCostingGroup::getId).collect(Collectors.toList());
  157. costCostingGroupMapper.deleteBatchIds(list);
  158. }
  159. if (SHAREPARAM_FILETYPE.equals(fileType)) {
  160. // 根据文件的Id 和 医院Id获取数据
  161. List<ShareParamValue> shareParamValues = paramValueMapper.selectList(
  162. new LambdaQueryWrapper<ShareParamValue>()
  163. .eq(ShareParamValue::getHospId, hospId)
  164. .eq(ShareParamValue::getFileId, id));
  165. if (CollectionUtils.isEmpty(shareParamValues)) {
  166. throw new CostException(410, "数据已撤销");
  167. }
  168. List<Long> list = shareParamValues.stream().map(ShareParamValue::getId).collect(Collectors.toList());
  169. paramValueMapper.deleteBatchIds(list);
  170. }
  171. costIncomeFile.setRollbackStatus(NumberConstant.ONE);
  172. this.updateById(costIncomeFile);
  173. }
  174. /**
  175. * 错误详情
  176. *
  177. * @param id
  178. * @param hospId
  179. * @return
  180. */
  181. @Override
  182. public List<IncomeErrorMessage> getErrorList(Long id, Long hospId) {
  183. CostIncomeFile costIncomeFile = this.getById(id);
  184. if (Objects.isNull(costIncomeFile)) {
  185. throw new CostException(500, "文件记录不存在");
  186. }
  187. String errorList = costIncomeFile.getErrorList();
  188. if (StrUtil.isBlank(errorList)) {
  189. return null;
  190. }
  191. // List<IncomeErrorMessage> incomeErrorMessageList = JsonUtil.toList(errorList, IncomeErrorMessage.class);
  192. List<IncomeErrorMessage> incomeErrorMessageList = JacksonUtil.str2ObjList(errorList, List.class, IncomeErrorMessage.class);
  193. return incomeErrorMessageList;
  194. }
  195. /**
  196. * 文件记录删除
  197. *
  198. * @param idList 文件记录的Id集合
  199. */
  200. @Override
  201. @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
  202. public void deleteByIds(List<Long> idList) {
  203. this.removeByIds(idList);
  204. }
  205. }