DepartmentServiceImpl.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package com.imed.costaccount.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  7. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  8. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  9. import com.imed.costaccount.common.exception.CostException;
  10. import com.imed.costaccount.common.util.BeanUtil;
  11. import com.imed.costaccount.common.util.PageUtils;
  12. import com.imed.costaccount.common.util.Result;
  13. import com.imed.costaccount.constants.NumberConstant;
  14. import com.imed.costaccount.mapper.DepartmentMapper;
  15. import com.imed.costaccount.model.Department;
  16. import com.imed.costaccount.model.Hospital;
  17. import com.imed.costaccount.model.User;
  18. import com.imed.costaccount.model.dto.DepartmentRequest;
  19. import com.imed.costaccount.model.vo.DepartmentVO;
  20. import com.imed.costaccount.service.DepartmentService;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.apache.shiro.SecurityUtils;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.transaction.annotation.Propagation;
  26. import org.springframework.transaction.annotation.Transactional;
  27. import org.springframework.util.CollectionUtils;
  28. import org.springframework.util.StringUtils;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. import java.util.Map;
  32. import java.util.Objects;
  33. import java.util.stream.Collectors;
  34. @Slf4j
  35. @Service("departmentService")
  36. public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {
  37. @Autowired
  38. private HospitalServiceImpl hospitalService;
  39. /**
  40. * 分页查询科室信息
  41. *
  42. * @param current
  43. * @param pageSize
  44. * @param hospId
  45. * @param name
  46. * @return
  47. */
  48. @Override
  49. public PageUtils queryList(Integer current, Integer pageSize,Long hospId, String name) {
  50. Page<Department> departPage = new Page<>(current, pageSize);
  51. Page<Department> pages = this.page(departPage, new QueryWrapper<Department>().lambda()
  52. .eq(!StringUtils.isEmpty(hospId), Department::getHospId, hospId)
  53. .like(!StringUtils.isEmpty(name), Department::getDepartmentName, name).orderByDesc(Department::getCreateTime));
  54. List<Department> records = pages.getRecords();
  55. List<DepartmentVO> departmentVOList = BeanUtil.convertList(records, DepartmentVO.class);
  56. Hospital hospital = hospitalService.getOne(new QueryWrapper<Hospital>().lambda().eq(Hospital::getId, hospId));
  57. departmentVOList.forEach(i->{
  58. if (Objects.nonNull(hospital)){
  59. i.setHospName(hospital.getName());
  60. }
  61. i.setCreateDateTime(DateUtil.format(DateUtil.date(i.getCreateTime()),"yyyy-MM-dd HH:mm:ss"));
  62. });
  63. PageUtils pageUtils = new PageUtils(pages);
  64. pageUtils.setList(departmentVOList);
  65. return pageUtils;
  66. }
  67. /**
  68. * 根据指定条件查询科室的相关信息
  69. * @param id
  70. * @param departmentName
  71. * @param departmentCode
  72. * @param hospId
  73. * @return
  74. */
  75. @Override
  76. public List<Department> getByDepartment(Integer id,String departmentName,String departmentCode,Long hospId) {
  77. LambdaQueryWrapper<Department> wrapper = new QueryWrapper<Department>().lambda()
  78. .eq(!StringUtils.isEmpty(id), Department::getId, id)
  79. .like(!StringUtils.isEmpty(departmentName), Department::getDepartmentName, departmentName)
  80. .eq(!StringUtils.isEmpty(departmentCode), Department::getDepartmentCode, departmentCode)
  81. .eq(!StringUtils.isEmpty(hospId), Department::getHospId, hospId)
  82. .orderByDesc(Department::getCreateTime);
  83. List<Department> departmentList = baseMapper.selectList(wrapper);
  84. return departmentList;
  85. }
  86. /**
  87. * 保存科室信息
  88. * TODO 检验科室信息是否存在
  89. * @param departmentRequest
  90. */
  91. @Override
  92. @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
  93. public void addDepartment(DepartmentRequest departmentRequest) {
  94. User user = (User) SecurityUtils.getSubject().getPrincipal();
  95. Long hospId = user.getHospId();
  96. // 查询所有的科室信息
  97. List<Department> departmentList = baseMapper.selectList(new QueryWrapper<Department>().lambda().eq(Department::getHospId, hospId));
  98. Map<String, List<Department>> map =
  99. departmentList.stream().collect(Collectors.groupingBy(Department::getDepartmentCode));
  100. if (!CollectionUtils.isEmpty(map.get(departmentRequest.getDepartmentCode()))){
  101. throw new CostException(500,"科室代码"+departmentRequest.getDepartmentCode()+"已存在");
  102. }
  103. departmentRequest.setHospId(hospId);
  104. Department department = BeanUtil.convertObj(departmentRequest, Department.class);
  105. department.setId(null);
  106. department.setCreateTime(System.currentTimeMillis());
  107. baseMapper.insert(department);
  108. }
  109. /**
  110. * 修改科室信息
  111. *
  112. * @param departmentRequest
  113. */
  114. @Override
  115. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  116. public void updateByDepartment(DepartmentRequest departmentRequest) {
  117. User user = (User) SecurityUtils.getSubject().getPrincipal();
  118. Long hospId = user.getHospId();
  119. Long id = departmentRequest.getId();
  120. Department department = baseMapper.selectById(id);
  121. if (Objects.isNull(department)){
  122. throw new CostException("未找到相关科室信息");
  123. }
  124. baseMapper.deleteById(id);
  125. // 查询所有的科室信息
  126. List<Department> departmentList = baseMapper.selectList(new QueryWrapper<Department>().lambda().eq(Department::getHospId, hospId));
  127. Map<String, List<Department>> map =
  128. departmentList.stream().collect(Collectors.groupingBy(Department::getDepartmentCode));
  129. if (!CollectionUtils.isEmpty(map.get(departmentRequest.getDepartmentCode()))){
  130. throw new CostException(500,"科室代码"+departmentRequest.getDepartmentCode()+"已存在");
  131. }
  132. // 新增操作
  133. Department departmentResponse = BeanUtil.convertObj(departmentRequest, Department.class);
  134. departmentResponse.setId(null);
  135. departmentResponse.setCreateTime(System.currentTimeMillis());
  136. departmentResponse.setHospId(hospId);
  137. baseMapper.insert(departmentResponse);
  138. }
  139. /**
  140. * 导入科室信息
  141. *
  142. * @param list
  143. * @param hospId
  144. * @return
  145. */
  146. @Override
  147. public Result importDepartment(List<List<Object>> list, Long hospId) {
  148. for (int i = list.size() - 1; i >= 0; i--) {
  149. if (i == NumberConstant.ZERO || i == NumberConstant.ONE || i == NumberConstant.TWO || i == NumberConstant.THREE) {
  150. list.remove(list.get(i));
  151. }
  152. }
  153. log.info("读取的数据为:{}", list);
  154. List<Department> departmentList = new ArrayList<>();
  155. List<String> errRowNums = new ArrayList<>();
  156. for (int i = 0; i < list.size(); i++) {
  157. List<Object> data = list.get(i);
  158. log.info("得到用户输入的数据为:{}", data);
  159. Department department = new Department();
  160. department.setHospId(hospId);
  161. department.setCreateTime(System.currentTimeMillis());
  162. int size = data.size();
  163. if (size != 5) {
  164. // 补充读取的null问题
  165. if (size == 4) {
  166. data.add(StrUtil.EMPTY);
  167. }
  168. for (int j = 0; j < 3; j++) {
  169. if (Objects.isNull(data.get(j))) {
  170. data.set(j, StrUtil.EMPTY);
  171. }
  172. }
  173. }
  174. for (int j = 0; j < 3; j++) {
  175. String str = String.valueOf(data.get(j));
  176. log.info("得到的字符串{}", str);
  177. if (StrUtil.isBlank(str)) {
  178. errRowNums.add("" + (i + 3));
  179. }
  180. if (j == 0) {
  181. // Excel里面输入医院名称 根据医院名称解析
  182. Hospital hospital = hospitalService.getByName(str);
  183. department.setHospId(hospital.getId());
  184. } else if (j == 1) {
  185. department.setDepartmentName(str);
  186. } else {
  187. department.setDepartmentCode(str);
  188. }
  189. }
  190. departmentList.add(department);
  191. }
  192. if (CollUtil.isNotEmpty(errRowNums)) {
  193. String collect = errRowNums.stream().collect(Collectors.joining(StrUtil.COMMA));
  194. throw new CostException(500, "第" + collect + "行数据异常");
  195. }
  196. // 校验输入的Department是否存在 输入的科室里面筛选出已经存在的科室信息
  197. List<Department> realDepartments = new ArrayList<>();
  198. departmentList.forEach(i -> {
  199. Department one = getOne(
  200. new QueryWrapper<Department>().lambda()
  201. .eq(Department::getDepartmentCode, i.getDepartmentCode())
  202. .eq(Department::getHospId, i.getHospId())
  203. );
  204. if (Objects.nonNull(one)) {
  205. realDepartments.add(one);
  206. }
  207. });
  208. departmentList = departmentList.stream().filter(i -> !realDepartments.stream().map(Department::getDepartmentCode).collect(Collectors.toList()).contains(i.getDepartmentCode())).collect(Collectors.toList());
  209. this.saveBatch(departmentList);
  210. if (CollectionUtils.isEmpty(realDepartments)){
  211. return Result.build(200,"数据导入成功",null);
  212. }else {
  213. return Result.build(200, "有" + realDepartments.size() + "条数据已存在,未被导入", null);
  214. }
  215. }
  216. }