zqy
2024-06-23 a69aafe60ce001834b981778f12fd74d4af77e23
zhang-content/src/main/java/com/ruoyi/service/impl/ZfDoctorServiceImpl.java
@@ -5,20 +5,33 @@
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.EsModel;
import com.ruoyi.common.utils.MapUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.ZfDoctor;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.*;
import com.ruoyi.mapper.ZfDoctorMapper;
import com.ruoyi.service.*;
import com.ruoyi.service.ZfDoctorService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import static com.ruoyi.constant.MenuAuthority.*;
/**
 * <p>
 *  服务实现类
 * 服务实现类
 * </p>
 *
 * @author ojq
@@ -28,35 +41,418 @@
@Slf4j
public class ZfDoctorServiceImpl extends ServiceImpl<ZfDoctorMapper, ZfDoctor> implements ZfDoctorService {
    @Resource
    ZInfoUserService zInfoUserService;
    @Resource
    ZfDoctorService zfDoctorService;
    @Resource
    ZAuthorityService zAuthorityService;
    @Resource
    ZfDoctorMapper zfDoctorMapper;
    @Resource
    EsService esService;
    @Resource
    private RestHighLevelClient restHighLevelClient;
    @Resource
    ZfDoctorShareService zfDoctorShareService;
    @Override
    public AjaxResult selectDoctorList(ZfDoctor zfDoctor, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<ZfDoctor> lqw = buildCondition(zfDoctor);
//        LambdaQueryWrapper<ZfDoctor> lqw = buildCondition(zfDoctor);
//        lqw.orderByDesc(ZfDoctor::getCreateTime);
//
//        Page<ZfDoctor> ZfDoctorPage = new Page<>(pageNum, pageSize);
//        Page<ZfDoctor> pageResult = page(ZfDoctorPage, lqw);
//
//        HashMap<String, Object> data = MapUtils.getResult(pageResult);
//        return AjaxResult.success(data);
        //要查自己家庭的
        ZInfoUser myself = zInfoUserService.getMyself();
        if (myself == null) {
            //   System.out.println("ssssss");
            return AjaxResult.success("您没加入到对应的家庭,请联系管理员");
        }
        Long familyId = myself.getFamilyId();
        //也要查别人授权的
        List<ZAuthority> authority = zAuthorityService.getAuthority();
        List<Long> idList = authority.stream().filter(auth -> auth.getAuthority().toString().equals(DOCTOR_LIST)).map(ZAuthority::getFid).collect(Collectors.toList());
        //加上自己家庭的id
        idList.add(familyId);
//        String familyIds = listFamilyIds();
//        String secondFamilyAuthority = listSecondFamilyIds()
        LambdaQueryWrapper<ZfDoctor> lqw = buildCondition(zfDoctor, idList);
        Page<ZfDoctor> ZfDoctorPage = new Page<>(pageNum,pageSize);
        Page<ZfDoctor> pageResult = page(ZfDoctorPage, lqw);
        Page<ZfDoctor> zfDoctorPage = new Page<>(pageNum, pageSize);
        Page<ZfDoctor> pageResult = page(zfDoctorPage, lqw);
        HashMap<String, Object> data = MapUtils.getResult(pageResult);
        List<ZfDoctor> beanRecords = pageResult.getRecords();//得到查询出来的数据
        List<ZfDoctor> dtoResult = markOwnData(familyId, beanRecords);
        HashMap<String, Object> data = MapUtils.getResult(pageResult, dtoResult);
        return AjaxResult.success(data);
    }
    @Override
    public List<ZfDoctor> selectByCondition(ZfDoctor zfDoctor) {
        LambdaQueryWrapper<ZfDoctor> lambdaQueryWrapper = buildCondition(zfDoctor);
        List<ZfDoctor> list = list(lambdaQueryWrapper);
        log.info("返回的数据为:{}",list);
//        LambdaQueryWrapper<ZfDoctor> lambdaQueryWrapper = buildCondition(zfDoctor);
//        lambdaQueryWrapper.orderByDesc(ZfDoctor::getCreateTime);
//        List<ZfDoctor> list = list(lambdaQueryWrapper);
//        log.info("返回的数据为:{}", list);
//        return list;
        //要查自己家庭的
        ZInfoUser myself = zInfoUserService.getMyself();
        Long familyId = myself.getFamilyId();
        //也要查别人授权的
        List<ZAuthority> authority = zAuthorityService.getAuthority();
        List<Long> idList = authority.stream().filter(auth -> auth.getAuthority().toString().equals(DOCTOR_LIST)).map(ZAuthority::getFid).collect(Collectors.toList());
        //加上自己家庭的id
        idList.add(familyId);
        LambdaQueryWrapper<ZfDoctor> lambdaQueryWrapper = buildCondition(zfDoctor, idList);
        List<ZfDoctor> beanRecords = list(lambdaQueryWrapper);
        log.info("从数据库中查到的为:{}", beanRecords);
        return markOwnData(familyId, beanRecords);
    }
    public List<ZfDoctor> markOwnData(Long familyId, List<ZfDoctor> beanRecords) {
        return beanRecords.stream().peek(
                bean -> {
                    if (bean.getFamilyId() == familyId) {
                        bean.setOwnData(1);
                    } else {
                        bean.setOwnData(0);
                    }
                }
        ).sorted((a, b) -> b.getOwnData() - a.getOwnData()).collect(Collectors.toList());
    }
    @Override
    public List<ZfDoctor> selectByIds(Long[] ids) {
        List<ZfDoctor> list = new ArrayList<>();
        if (ids.length != 0)
            list = listByIds(Arrays.asList(ids));
        else
            list = list();
        return list;
    }
    private LambdaQueryWrapper<ZfDoctor> buildCondition(ZfDoctor zfDoctor) {
    @Override
    public AjaxResult importExcel(MultipartFile file) {
        ExcelUtil<ZfDoctor> util = new ExcelUtil<>(ZfDoctor.class);
        List<ZfDoctor> dataList = null;
        try {
            dataList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        log.info("数据列表为:{}", dataList);
        for (ZfDoctor zfDoctor : dataList) {
            if (zfDoctor.getSuitable().length() != 0 || zfDoctor.getType().length() != 0 || zfDoctor.getEffect().length() != 0 ) {
                if(zfDoctor.getSymptom().length() == 0 || zfDoctor.getSymptom() == null){
                    throw new RuntimeException("症状为空,导入数据失败");
                }
                else {
                    zfDoctorService.addData(zfDoctor);
                }
            }
        }
        return AjaxResult.success("导入数据成功");
    }
    @Override
    public AjaxResult addData(ZfDoctor zfDoctor) {
        ZInfoUser myself = zInfoUserService.getMyself();
        Long familyId = myself.getFamilyId();
        if (familyId == null) {
            throw new RuntimeException("您还未加入任何家庭");
        }
        List<ZAuthority> authority = zAuthorityService.getAuthority();
        List<Long> familyIdList = authority.stream().filter(auth -> auth.getAuthority().toString().equals(DOCTOR_LIST_ADD)).map(ZAuthority::getFid).collect(Collectors.toList());
        familyIdList.add(familyId);
//        List<ZfDoctorShare> authority1 = zfDoctorShareService.getAuthority();
//        List<Long> idList1 = authority1.stream().filter(auth -> auth.getShareContent().toString().equals(DOCTOR_LIST)).map(ZfDoctorShare::getShareId).collect(Collectors.toList());
//        zfDoctorService.addData(zfDoctor);
        if (zfDoctor.getFamilyId() != null && !familyIdList.contains(zfDoctor.getFamilyId())) {
            throw new RuntimeException("你没有权限操作此家庭的数据");
        }
        if (zfDoctor.getFamilyId() == null) {
            //默认添加自己家庭的数据
            zfDoctor.setFamilyId(familyId);
        }
        //判断是否有重复数据
        LambdaQueryWrapper<ZfDoctor> lqw = uniqueCondition(zfDoctor);
        List<ZfDoctor> list = list(lqw);
        if (list.size() > 0) {
            throw new RuntimeException("请勿新增重复数据");
        }
        if (save(zfDoctor)) {
            EsModel esModel = new EsModel();
            Integer inte = zfDoctor.getId().intValue();
            String uuid = UUID.randomUUID().toString().replace("-", "");
            esModel.setId(uuid);
            esModel.setCtId(Long.valueOf(inte));
            esModel.setCtTableName("家庭小医生");
            esModel.setBy1(zfDoctor.getType());
            esModel.setBy2(zfDoctor.getSymptom());
            esModel.setBy3(zfDoctor.getEffect());
            esModel.setBy4(zfDoctor.getSuitable());
            esModel.setBy5("/family/zfDoctor");
            esModel.setBy6(zfDoctor.getCmedical());
            esModel.setBy7(zfDoctor.getWmedical());
//            esModel.setBy7();
            esModel.setRemark(zfDoctor.getRemark());
            esModel.setFid(familyId);
            //这里存储查询详情的路径
            esService.insertTable(esModel);
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }
    @Resource
    ZfLogService zfLogService;
    @Override
    public AjaxResult updateData(ZfDoctor zfDoctor) {
        ZInfoUser myself = zInfoUserService.getMyself();
        Long familyId = myself.getFamilyId();
        //先根据id查询出数据的familyId,看看有没有权限操作
        Long dataFamilyId = getById(zfDoctor.getId()).getFamilyId();
        List<ZAuthority> authority = zAuthorityService.getAuthority();
        List<Long> familyIdList = authority.stream().filter(auth -> auth.getAuthority().toString().equals(DOCTOR_LIST_UPDATE)).map(ZAuthority::getFid).collect(Collectors.toList());
        familyIdList.add(familyId);
        if (dataFamilyId != null && !familyIdList.contains(dataFamilyId)) {
            throw new RuntimeException("你没有权限操作此家庭的数据");
        }
        //操作后加入日志
        ZfLog zfLog = new ZfLog();
        zfLog.setUpdateTime(LocalDateTime.now());
        zfLog.setModule("家庭小医生");
        zfLog.setUpdater(zInfoUserService.getMyself().getNickName());
        zfLogService.save(zfLog);
        if (updateById(zfDoctor)) {
            //到数据库中查询对应的数据
            ZfDoctor dataById = getById(zfDoctor.getId());
            //先到es中查询到对应那条数据在es的id
            EsModel esResult = esService.findByCtId(dataById.getId().intValue(), "家庭小医生");
            if (esResult == null) {
                return AjaxResult.success();
            }
            //操作es修改数据
            EsModel newModel = new EsModel();
            if (zfDoctor.getType() != null) {
                newModel.setBy1(zfDoctor.getType());
            } else {
                newModel.setBy1(dataById.getType());
            }
            if (zfDoctor.getSymptom() != null) {
                newModel.setBy2(zfDoctor.getSymptom());
            } else {
                newModel.setBy2(dataById.getSymptom());
            }
            if (zfDoctor.getEffect() != null) {
                newModel.setBy3(zfDoctor.getEffect());
            } else {
                newModel.setBy3(dataById.getEffect());
            }
            if (zfDoctor.getSuitable() != null) {
                newModel.setBy4(zfDoctor.getSuitable());
            } else {
                newModel.setBy4(dataById.getSuitable());
            }
            if (zfDoctor.getCmedical() != null) {
                newModel.setBy6(zfDoctor.getCmedical());
            } else {
                newModel.setBy6(dataById.getCmedical());
            }
            if (zfDoctor.getWmedical() != null) {
                newModel.setBy7(zfDoctor.getWmedical());
            } else {
                newModel.setBy7(dataById.getWmedical());
            }
            if (zfDoctor.getRemark() != null) {
                newModel.setRemark(zfDoctor.getRemark());
            } else {
                newModel.setRemark(dataById.getRemark());
            }
            UpdateRequest updateRequest = new UpdateRequest("allsearchdata", esResult.getId());
            updateRequest.doc(
                    "by1", newModel.getBy1(),
                    "by2", newModel.getBy2(),
                    "by3", newModel.getBy3(),
                    "by4", newModel.getBy4(),
                    "by6", newModel.getBy6(),
                    "by7", newModel.getBy7(),
                    "remark", newModel.getRemark()
            );
            try {
                restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }
    @Override
    public AjaxResult deleteData(Long[] ids) {
        List<ZfDoctor> dataList = zfDoctorService.listByIds(Arrays.asList(ids));
        ZInfoUser myself = zInfoUserService.getMyself();
        Long familyId = myself.getFamilyId();
        List<ZAuthority> authority = zAuthorityService.getAuthority();
        List<Long> familyIdList = authority.stream().filter(auth -> auth.getAuthority().toString().equals(DOCTOR_LIST_REMOVE)).map(ZAuthority::getFid).collect(Collectors.toList());
        familyIdList.add(familyId);
        for (ZfDoctor data : dataList) {
            if (!familyIdList.contains(data.getFamilyId())) {
                throw new RuntimeException("你没有权限操作此家庭的数据");
            }
        }
        List<ZfDoctor> zfDoctors = listByIds(Arrays.asList(ids));
        if (zfDoctorService.removeByIds(Arrays.asList(ids))) {
            //删除es中的数据
            zfDoctors.stream().forEach(zfDoctor -> {
                EsModel esModel = esService.findByCtId(zfDoctor.getId().intValue(), "家庭小医生");
                if (esModel != null) {
                    DeleteRequest deleteRequest = new DeleteRequest("allsearchdata", esModel.getId());
                    try {
                        restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }
    @Override
    public AjaxResult listType() {
        List<String> result = zfDoctorMapper.listType();
        return AjaxResult.success(result);
    }
//    @Override
//    public AjaxResult mySave(ZfDoctor zfDoctor) {
//        //判断是否有重复数据
//        LambdaQueryWrapper<ZfDoctor> lqw = uniqueCondition(zfDoctor);
//        List<ZfDoctor> list = list(lqw);
//        if (list.size() > 0) {
//            throw new RuntimeException("请勿新增重复数据");
//        }
//
//        if (save(zfDoctor)) {
//            return AjaxResult.success();
//        } else {
//            return AjaxResult.error();
//        }
//
//
//    }
    private LambdaQueryWrapper<ZfDoctor> uniqueCondition(ZfDoctor zfDoctor) {
        LambdaQueryWrapper<ZfDoctor> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(zfDoctor.getType()),ZfDoctor::getType,zfDoctor.getType());
        lqw.like(StringUtils.isNotEmpty(zfDoctor.getSymptom()),ZfDoctor::getSymptom,zfDoctor.getSymptom());
        lqw.like(StringUtils.isNotEmpty(zfDoctor.getDuration()),ZfDoctor::getDuration,zfDoctor.getDuration());
        lqw.like(StringUtils.isNotEmpty(zfDoctor.getCmedical()),ZfDoctor::getCmedical,zfDoctor.getCmedical());
        lqw.like(StringUtils.isNotEmpty(zfDoctor.getWmedical()),ZfDoctor::getWmedical,zfDoctor.getWmedical());
        lqw.like(StringUtils.isNotEmpty(zfDoctor.getEffect()),ZfDoctor::getEffect,zfDoctor.getEffect());
        lqw.like(StringUtils.isNotEmpty(zfDoctor.getSuitable()),ZfDoctor::getSuitable,zfDoctor.getSuitable());
        lqw.like(StringUtils.isNotEmpty(zfDoctor.getRemark()),ZfDoctor::getRemark,zfDoctor.getRemark());
        lqw.eq(StringUtils.isNotEmpty(zfDoctor.getType()), ZfDoctor::getType, zfDoctor.getType());
        lqw.eq(StringUtils.isNotEmpty(zfDoctor.getSymptom()), ZfDoctor::getSymptom, zfDoctor.getSymptom());
        lqw.eq(StringUtils.isNotEmpty(zfDoctor.getDuration()), ZfDoctor::getDuration, zfDoctor.getDuration());
        lqw.eq(StringUtils.isNotEmpty(zfDoctor.getCmedical()), ZfDoctor::getCmedical, zfDoctor.getCmedical());
        lqw.eq(StringUtils.isNotEmpty(zfDoctor.getWmedical()), ZfDoctor::getWmedical, zfDoctor.getWmedical());
        lqw.eq(StringUtils.isNotEmpty(zfDoctor.getEffect()), ZfDoctor::getEffect, zfDoctor.getEffect());
        lqw.eq(StringUtils.isNotEmpty(zfDoctor.getSuitable()), ZfDoctor::getSuitable, zfDoctor.getSuitable());
        lqw.eq(StringUtils.isNotEmpty(zfDoctor.getRemark()), ZfDoctor::getRemark, zfDoctor.getRemark());
        lqw.eq(zfDoctor.getFamilyId() != null, ZfDoctor::getFamilyId, zfDoctor.getFamilyId());
        return lqw;
    }
}
    private LambdaQueryWrapper<ZfDoctor> buildCondition(ZfDoctor zfDoctor, List<Long> familyIdList) {
        LambdaQueryWrapper<ZfDoctor> lqw = new LambdaQueryWrapper<>();
        lqw.in(ZfDoctor::getFamilyId, familyIdList);
        lqw.orderByDesc(ZfDoctor::getCreateTime);
        lqw.like(StringUtils.isNotEmpty(zfDoctor.getType()), ZfDoctor::getType, zfDoctor.getType())
                .like(StringUtils.isNotEmpty(zfDoctor.getSymptom()), ZfDoctor::getSymptom, zfDoctor.getSymptom())
                .like(StringUtils.isNotEmpty(zfDoctor.getDuration()), ZfDoctor::getDuration, zfDoctor.getDuration())
                .like(StringUtils.isNotEmpty(zfDoctor.getEffect()), ZfDoctor::getEffect, zfDoctor.getEffect())
                .like(StringUtils.isNotEmpty(zfDoctor.getSuitable()), ZfDoctor::getSuitable, zfDoctor.getSuitable())
                .like(StringUtils.isNotEmpty(zfDoctor.getWmedical()), ZfDoctor::getWmedical, zfDoctor.getWmedical())
                .like(StringUtils.isNotEmpty(zfDoctor.getCmedical()), ZfDoctor::getCmedical, zfDoctor.getCmedical())
                .like(StringUtils.isNotEmpty(zfDoctor.getRemark()), ZfDoctor::getRemark, zfDoctor.getRemark());
        if (StringUtils.isNotEmpty(zfDoctor.getPrescription())) {
            lqw.and(wrapper -> {
                wrapper.like(StringUtils.isNotEmpty(zfDoctor.getPrescription()), ZfDoctor::getWmedical, zfDoctor.getPrescription())
                        .or()
                        .like(StringUtils.isNotEmpty(zfDoctor.getPrescription()), ZfDoctor::getCmedical, zfDoctor.getPrescription());
            });
        }
        return lqw;
    }
//        private List<Long> buildCondition1(){
//        List<ZfDoctorShare> authority1 = zfDoctorShareService.getAuthority();
//        List<Long> idList1 = authority1.stream().filter(auth -> auth.getShareContent().toString().equals(DOCTOR_LIST)).map(ZfDoctorShare::getShareId).collect(Collectors.toList());
//        return idList1;
//    }
//    private LambdaQueryWrapper<ZfDoctor> buildCondition2(ZfDoctor zfDoctor, List<Long> IdList) {
//        LambdaQueryWrapper<ZfDoctorShare> lqw = new LambdaQueryWrapper<>();
//        lqw.in(ZfDoctorShare::getShareId, IdList);
//        List<ZfDoctorShare> beanRecords = list(lqw);
//        for (ZfDoctorShare shareContent : beanRecords){
//            zfDoctorService.getById(shareContent);
//        }
//
//    }
}