package cn.besteam.modules.medical.service.impl; import cn.besteam.common.constant.CommonConstant; import cn.besteam.common.util.UserContextUtil; import cn.besteam.modules.medical.dto.MwMaterialCategoryQueryDTO; import cn.besteam.modules.medical.entity.MwMaterialCategory; import cn.besteam.modules.medical.mapper.MwMaterialCategoryMapper; import cn.besteam.modules.medical.service.IMwMaterialCategoryService; import cn.besteam.modules.shiro.authc.util.EnvUtils; import cn.besteam.modules.system.entity.SysDepartment; import cn.besteam.modules.system.service.ISysDepartmentService; import cn.besteam.modules.system.util.MybatisParameterUtils; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * @Description: 资料分类服务实现 * @author: besteam * @date: 2025-08-17 */ @Service public class MwMaterialCategoryServiceImpl extends ServiceImpl implements IMwMaterialCategoryService { @Autowired private ISysDepartmentService sysDepartmentService; @Override public IPage selectPageList(Page page, MwMaterialCategoryQueryDTO queryDTO) { List hospitalIdList = sysDepartmentService.getSearchHospitalIdList(queryDTO.getHospitalId()); if (CollectionUtils.isEmpty(hospitalIdList)) { return page; } LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); try { MybatisParameterUtils.cutInParameter(queryWrapper, MwMaterialCategory::getHospitalId, hospitalIdList); } catch (Exception e) { throw new RuntimeException(e); } queryWrapper.eq(MwMaterialCategory::getDelFlag, CommonConstant.DEL_FLAG_0); if (queryDTO.getCategoryName() != null && !queryDTO.getCategoryName().isEmpty()) { queryWrapper.like(MwMaterialCategory::getCategoryName, queryDTO.getCategoryName()); } if (queryDTO.getCategoryCode() != null && !queryDTO.getCategoryCode().isEmpty()) { queryWrapper.like(MwMaterialCategory::getCategoryCode, queryDTO.getCategoryCode()); } if (queryDTO.getStatus() != null) { queryWrapper.eq(MwMaterialCategory::getStatus, queryDTO.getStatus()); } queryWrapper.orderByDesc(MwMaterialCategory::getCreateTime); IPage pageList = this.page(page, queryWrapper); // 填充医院名称 for (MwMaterialCategory category : pageList.getRecords()) { fillHospitalName(category); } return pageList; } @Override public MwMaterialCategory selectById(Integer id) { MwMaterialCategory category = this.getById(id); if (category != null && category.getDelFlag() == CommonConstant.DEL_FLAG_0) { fillHospitalName(category); return category; } return null; } @Override public boolean saveMaterialCategory(MwMaterialCategory materialCategory) { // 设置医院名称 if (materialCategory.getHospitalId() != null) { SysDepartment hospital = sysDepartmentService.getById(materialCategory.getHospitalId()); if (hospital != null) { materialCategory.setHospitalName(hospital.getDepartName()); } } // 设置默认状态为启用 if (materialCategory.getStatus() == null) { materialCategory.setStatus(1); } String name = EnvUtils.getUser().getName(); materialCategory.setCreateBy(name); return this.save(materialCategory); } @Override public boolean updateMaterialCategory(MwMaterialCategory materialCategory) { MwMaterialCategory existingCategory = this.getById(materialCategory.getId()); if (existingCategory == null) { return false; } // 如果医院ID发生变化,更新医院名称 if (materialCategory.getHospitalId() != null && !materialCategory.getHospitalId().equals(existingCategory.getHospitalId())) { SysDepartment hospital = sysDepartmentService.getById(materialCategory.getHospitalId()); if (hospital != null) { materialCategory.setHospitalName(hospital.getDepartName()); } } String name = EnvUtils.getUser().getName(); materialCategory.setUpdateBy(name); return this.updateById(materialCategory); } @Override public boolean deleteMaterialCategory(Integer id) { MwMaterialCategory category = this.getById(id); if (category != null) { category.setDelFlag(CommonConstant.DEL_FLAG_1); return this.updateById(category); } return false; } /** * 填充医院名称 * @param category 资料分类信息 */ private void fillHospitalName(MwMaterialCategory category) { if (category.getHospitalId() != null && category.getHospitalName() == null) { SysDepartment hospital = sysDepartmentService.getById(category.getHospitalId()); if (hospital != null) { category.setHospitalName(hospital.getDepartName()); } } } }