package com.emr.service.ipml;

import com.emr.dao.ArchiveDetailCaCopyMapper;
import com.emr.dao.Archive_MasterMapper;
import com.emr.dto.Archive_MasterDto;
import com.emr.entity.Archive_Master;
import com.emr.service.ArchiveDetailCaCopyServer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ArchiveDetailCaCopyServerImpl
 * @Description
 * @Author linjj
 * @Date 2023/10/31 13:58
 * @Version 1.0
 */
@Slf4j
@Service
public class ArchiveDetailCaCopyServerImpl implements ArchiveDetailCaCopyServer {

    @Autowired
    private Archive_MasterMapper archiveMasterMapper;
    @Autowired
    private ArchiveDetailCaCopyMapper archiveDetailCaCopyMapper;


    @Override
    public List<String> getPdfUrlByMasterOrId(String[] assortIds, String masterId, String flag, String fileIds) {
        List<String> resultList = new ArrayList<>();
        //查询本病历
        List<String> pdfUrlByMasterOrId = archiveDetailCaCopyMapper.getPdfUrlByMasterOrId(assortIds, masterId, flag, fileIds);
        if(!CollectionUtils.isEmpty(pdfUrlByMasterOrId)){
            resultList.addAll(pdfUrlByMasterOrId);
        }
        //判断是否存在查询新生儿
        if(null != assortIds){
            boolean childFlag = false;
            for (String assortId:assortIds){
                if(StringUtils.isNotBlank(assortId) && assortId.equals("1000")){
                    childFlag = true;
                    break;
                }
            }
            if(childFlag){
                String childMidsByMasterId = getChildMidsByMasterId(masterId);
                List<String> childPdfUrlByMasterIds = archiveDetailCaCopyMapper.getChildPdfUrlByMasterIds(childMidsByMasterId);
                if(!CollectionUtils.isEmpty(childPdfUrlByMasterIds)){
                    resultList.addAll(childPdfUrlByMasterIds);
                }
            }
        }
        return resultList;
    }


    /**
     * 根据masterId查询新生儿masterId逗号隔开的字符串
     * @param masterId
     * @return
     */
    private String getChildMidsByMasterId(String masterId) {
        //查询该主键的病案号和住院时间
        Archive_Master archiveMaster = archiveMasterMapper.selectById(masterId);
        //查询该住院时间的数据集合
        List<Archive_MasterDto> list = archiveMasterMapper.selectChildMasterId(archiveMaster.getAdmissionDateTime());
        //定义是该患者的婴儿的masterId集合
        StringBuilder mIdStr = new StringBuilder();
        if(!CollectionUtils.isEmpty(list)){
            for (Archive_MasterDto dto : list){
                if(!dto.getId().equals(archiveMaster.getId()) && (dto.getInpNo().contains(archiveMaster.getInpNo() + "B") || dto.getInpNo().contains(archiveMaster.getInpNo() + "_B"))){
                    if(StringUtils.isNotBlank(mIdStr)){
                        mIdStr.append(",");
                    }
                    mIdStr.append("'").append(dto.getId()).append("'");
                }
            }
        }
        return mIdStr.toString();
    }
}
