DepartmentServiceImpl.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package com.imed.costaccount.service.impl;
  2. import cn.hutool.core.date.DateUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  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.BeanUtil;
  9. import com.imed.costaccount.common.util.PageUtils;
  10. import com.imed.costaccount.mapper.DepartmentMapper;
  11. import com.imed.costaccount.model.Department;
  12. import com.imed.costaccount.model.Hospital;
  13. import com.imed.costaccount.model.User;
  14. import com.imed.costaccount.model.dto.DepartmentRequest;
  15. import com.imed.costaccount.model.vo.DepartmentVO;
  16. import com.imed.costaccount.service.DepartmentService;
  17. import org.apache.shiro.SecurityUtils;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.transaction.annotation.Propagation;
  21. import org.springframework.transaction.annotation.Transactional;
  22. import org.springframework.util.CollectionUtils;
  23. import org.springframework.util.StringUtils;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Objects;
  27. import java.util.stream.Collectors;
  28. @Service("departmentService")
  29. public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {
  30. @Autowired
  31. private HospitalServiceImpl hospitalService;
  32. /**
  33. * 分页查询科室信息
  34. *
  35. * @param page
  36. * @param pageSize
  37. * @param hospId
  38. * @param name
  39. * @return
  40. */
  41. @Override
  42. public PageUtils queryList(Integer page, Integer pageSize,Integer hospId, String name) {
  43. Page<Department> departPage = new Page<>(page, pageSize);
  44. Page<Department> pages = this.page(departPage, new QueryWrapper<Department>().lambda()
  45. .eq(!StringUtils.isEmpty(hospId), Department::getHospId, hospId)
  46. .like(!StringUtils.isEmpty(name), Department::getDepartmentName, name));
  47. List<Department> records = pages.getRecords();
  48. List<DepartmentVO> departmentVOList = BeanUtil.convertList(records, DepartmentVO.class);
  49. Hospital hospital = hospitalService.getOne(new QueryWrapper<Hospital>().lambda().eq(Hospital::getId, hospId));
  50. departmentVOList.forEach(i->{
  51. if (Objects.nonNull(hospital)){
  52. i.setHospName(hospital.getName());
  53. }
  54. i.setCreateDateTime(DateUtil.format(DateUtil.date(i.getCreateTime()),"yyyy-MM-dd HH:mm:ss"));
  55. });
  56. PageUtils pageUtils = new PageUtils(pages);
  57. pageUtils.setList(departmentVOList);
  58. return pageUtils;
  59. }
  60. /**
  61. * 根据指定条件查询科室的相关信息
  62. * @param id
  63. * @param departmentName
  64. * @param departmentCode
  65. * @param hospId
  66. * @return
  67. */
  68. @Override
  69. public List<Department> getByDepartment(Integer id,String departmentName,String departmentCode,Integer hospId) {
  70. LambdaQueryWrapper<Department> wrapper = new QueryWrapper<Department>().lambda()
  71. .eq(!StringUtils.isEmpty(id), Department::getId, id)
  72. .like(!StringUtils.isEmpty(departmentName), Department::getDepartmentName, departmentName)
  73. .eq(!StringUtils.isEmpty(departmentCode), Department::getDepartmentCode, departmentCode)
  74. .eq(!StringUtils.isEmpty(hospId), Department::getHospId, hospId);
  75. List<Department> departmentList = baseMapper.selectList(wrapper);
  76. return departmentList;
  77. }
  78. /**
  79. * 保存科室信息
  80. * TODO 检验科室信息是否存在
  81. * @param departmentRequest
  82. */
  83. @Override
  84. @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
  85. public void addDepartment(DepartmentRequest departmentRequest) {
  86. User user = (User) SecurityUtils.getSubject().getPrincipal();
  87. Integer hospId = user.getHospId();
  88. // 查询所有的科室信息
  89. List<Department> departmentList = baseMapper.selectList(new QueryWrapper<Department>().lambda().eq(Department::getHospId, hospId));
  90. Map<String, List<Department>> map =
  91. departmentList.stream().collect(Collectors.groupingBy(Department::getDepartmentCode));
  92. if (!CollectionUtils.isEmpty(map.get(departmentRequest.getDepartmentCode()))){
  93. throw new CostException(500,"代码"+departmentRequest.getDepartmentCode()+"已存在");
  94. }
  95. departmentRequest.setHospId(hospId);
  96. Department department = BeanUtil.convertObj(departmentRequest, Department.class);
  97. department.setId(null);
  98. department.setCreateTime(System.currentTimeMillis());
  99. baseMapper.insert(department);
  100. }
  101. /**
  102. * 修改科室信息
  103. *
  104. * @param departmentRequest
  105. */
  106. @Override
  107. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  108. public void updateByDepartment(DepartmentRequest departmentRequest) {
  109. User user = (User) SecurityUtils.getSubject().getPrincipal();
  110. Integer hospId = user.getHospId();
  111. Integer id = departmentRequest.getId();
  112. Department department = baseMapper.selectById(id);
  113. if (Objects.isNull(department)){
  114. throw new CostException("未找到相关科室信息");
  115. }
  116. baseMapper.deleteById(id);
  117. // 新增操作
  118. Department departmentResponse = BeanUtil.convertObj(departmentRequest, Department.class);
  119. departmentResponse.setId(null);
  120. departmentResponse.setCreateTime(System.currentTimeMillis());
  121. departmentResponse.setHospId(hospId);
  122. baseMapper.insert(departmentResponse);
  123. }
  124. }