package com.imed.costaccount.service.impl; import cn.hutool.core.date.DateUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.imed.costaccount.common.exception.CostException; import com.imed.costaccount.common.util.BeanUtil; import com.imed.costaccount.common.util.PageUtils; import com.imed.costaccount.mapper.DepartmentMapper; import com.imed.costaccount.model.Department; import com.imed.costaccount.model.Hospital; import com.imed.costaccount.model.User; import com.imed.costaccount.model.dto.DepartmentRequest; import com.imed.costaccount.model.vo.DepartmentVO; import com.imed.costaccount.service.DepartmentService; import org.apache.shiro.SecurityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; @Service("departmentService") public class DepartmentServiceImpl extends ServiceImpl implements DepartmentService { @Autowired private HospitalServiceImpl hospitalService; /** * 分页查询科室信息 * * @param current * @param pageSize * @param hospId * @param name * @return */ @Override public PageUtils queryList(Integer current, Integer pageSize,Integer hospId, String name) { Page departPage = new Page<>(current, pageSize); Page pages = this.page(departPage, new QueryWrapper().lambda() .eq(!StringUtils.isEmpty(hospId), Department::getHospId, hospId) .like(!StringUtils.isEmpty(name), Department::getDepartmentName, name).orderByDesc(Department::getCreateTime)); List records = pages.getRecords(); List departmentVOList = BeanUtil.convertList(records, DepartmentVO.class); Hospital hospital = hospitalService.getOne(new QueryWrapper().lambda().eq(Hospital::getId, hospId)); departmentVOList.forEach(i->{ if (Objects.nonNull(hospital)){ i.setHospName(hospital.getName()); } i.setCreateDateTime(DateUtil.format(DateUtil.date(i.getCreateTime()),"yyyy-MM-dd HH:mm:ss")); }); PageUtils pageUtils = new PageUtils(pages); pageUtils.setList(departmentVOList); return pageUtils; } /** * 根据指定条件查询科室的相关信息 * @param id * @param departmentName * @param departmentCode * @param hospId * @return */ @Override public List getByDepartment(Integer id,String departmentName,String departmentCode,Integer hospId) { LambdaQueryWrapper wrapper = new QueryWrapper().lambda() .eq(!StringUtils.isEmpty(id), Department::getId, id) .like(!StringUtils.isEmpty(departmentName), Department::getDepartmentName, departmentName) .eq(!StringUtils.isEmpty(departmentCode), Department::getDepartmentCode, departmentCode) .eq(!StringUtils.isEmpty(hospId), Department::getHospId, hospId) .orderByDesc(Department::getCreateTime); List departmentList = baseMapper.selectList(wrapper); return departmentList; } /** * 保存科室信息 * TODO 检验科室信息是否存在 * @param departmentRequest */ @Override @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class) public void addDepartment(DepartmentRequest departmentRequest) { User user = (User) SecurityUtils.getSubject().getPrincipal(); Integer hospId = user.getHospId(); // 查询所有的科室信息 List departmentList = baseMapper.selectList(new QueryWrapper().lambda().eq(Department::getHospId, hospId)); Map> map = departmentList.stream().collect(Collectors.groupingBy(Department::getDepartmentCode)); if (!CollectionUtils.isEmpty(map.get(departmentRequest.getDepartmentCode()))){ throw new CostException(500,"科室代码"+departmentRequest.getDepartmentCode()+"已存在"); } departmentRequest.setHospId(hospId); Department department = BeanUtil.convertObj(departmentRequest, Department.class); department.setId(null); department.setCreateTime(System.currentTimeMillis()); baseMapper.insert(department); } /** * 修改科室信息 * * @param departmentRequest */ @Override @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public void updateByDepartment(DepartmentRequest departmentRequest) { User user = (User) SecurityUtils.getSubject().getPrincipal(); Integer hospId = user.getHospId(); Integer id = departmentRequest.getId(); Department department = baseMapper.selectById(id); if (Objects.isNull(department)){ throw new CostException("未找到相关科室信息"); } baseMapper.deleteById(id); // 查询所有的科室信息 List departmentList = baseMapper.selectList(new QueryWrapper().lambda().eq(Department::getHospId, hospId)); Map> map = departmentList.stream().collect(Collectors.groupingBy(Department::getDepartmentCode)); if (!CollectionUtils.isEmpty(map.get(departmentRequest.getDepartmentCode()))){ throw new CostException(500,"科室代码"+departmentRequest.getDepartmentCode()+"已存在"); } // 新增操作 Department departmentResponse = BeanUtil.convertObj(departmentRequest, Department.class); departmentResponse.setId(null); departmentResponse.setCreateTime(System.currentTimeMillis()); departmentResponse.setHospId(hospId); baseMapper.insert(departmentResponse); } }