package com.kcim.dao.repository; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.kcim.common.constants.NumberConstant; import com.kcim.common.util.UserContext; import com.kcim.dao.mapper.DrugMapper; import com.kcim.dao.model.Drug; import com.kcim.vo.SessionUserVO; import org.springframework.stereotype.Repository; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; /** * @program: CostAccount * @description: 药品成本数据库交互 * @author: Wang.YS * @create: 2023-10-25 16:13 **/ @Repository public class DrugRepository extends ServiceImpl { public List getList() { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Drug::getHospId, UserContext.getHospId()); return this.list(queryWrapper); } public Map getCostMap() { List list = getList(); if(!CollectionUtils.isEmpty(list)){ return list.stream().collect(Collectors.toMap(Drug::getCode, Drug::getCost, (a, b) -> b)); } return new HashMap<>(); } public Page getPage(Integer current, Integer pageSize, String name, Integer stop) { Page page = new Page<>(current,pageSize); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Drug::getHospId, UserContext.getHospId()); if(!stop.equals(NumberConstant.ONE)){ queryWrapper.eq(Drug::getStatus,stop); } if(!StringUtils.isEmpty(name)){ queryWrapper.like(Drug::getName,name); } return this.page(page,queryWrapper); } public void deleteDrug(Integer id) { Drug drug = this.getById(id); if(Objects.nonNull(drug)){ drug.setDeleteTime(new Date()); drug.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId())); this.updateById(drug); this.removeById(id); } } public void removeList(List list) { for (Drug drug : list) { drug.setDeleteTime(new Date()); drug.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId())); } this.updateBatchById(list); List collect = list.stream().map(Drug::getId).collect(Collectors.toList()); this.removeBatchByIds(collect); } public List getByCode(String code) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Drug::getHospId, UserContext.getHospId()); queryWrapper.eq(Drug::getCode,code); return this.list(queryWrapper); } public Drug getOneByCode(String code) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Drug::getHospId, UserContext.getHospId()); queryWrapper.eq(Drug::getCode,code); return this.getOne(queryWrapper); } public List getList(String name) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Drug::getHospId, UserContext.getHospId()); if(!StringUtils.isEmpty(name)){ queryWrapper.like(Drug::getName, name); } return this.list(queryWrapper); } public Map getCostMap(SessionUserVO currentUser) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Drug::getHospId, currentUser.getHospId()); List list = this.list(queryWrapper); if(!CollectionUtils.isEmpty(list)){ return list.stream().collect(Collectors.toMap(Drug::getCode, Drug::getCost, (a, b) -> b)); } return new HashMap<>(); } }