/**
 * Copyright (C), 2015-2019
 * Author:   HJL
 * Date:     2019/7/19 15:37
 * Description:病历文件信息管理
 */
package com.emr.service.ipml;

import com.emr.dao.Archive_DetailMapper;
import com.emr.dao.Archive_MasterMapper;
import com.emr.entity.*;
import com.emr.service.Archive_DetailService;
import com.emr.util.Jpg2PdfUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
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 javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
public class Archive_DetailServiceImpl implements Archive_DetailService {

    @Autowired
    private Archive_DetailMapper archiveDetailMapper;

    @Autowired
    private Archive_MasterMapper archiveMasterMapper;


    @Override
    public List<Archive_Detail_Vo> selectByClo(Archive_Detail_Vo record) {
        return archiveDetailMapper.selectByClo(record);
    }

    @Override
    public List<Archive_Detail_Vo> getCollectionDetail(Archive_Detail_Vo ArchiveDetailVo) {
        return archiveDetailMapper.getCollectionDetail(ArchiveDetailVo);
    }

    @Override
    public Integer selectChildByClo(Archive_Detail_Vo record) {
        return archiveDetailMapper.selectChildByClo(record);
    }

    @Override
    public OffsetLimitPage selectChild(Archive_Detail_Vo record, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Archive_Detail_Vo> list = archiveDetailMapper.selectChild(record);
        for(Archive_Detail_Vo returnList:list){
            //改变新生儿数据的title，新格式为title+inpNo
            returnList=changeTitle(returnList);
        }
        return new OffsetLimitPage((Page) list);
    }

    @Override
    public List<Archive_Detail> selectChildList(Archive_Detail record) {
        List<Archive_Detail> list = archiveDetailMapper.selectChildList(record);
        return list;
    }

    @Override
    public OffsetLimitPage detailByClo(Archive_Detail_Vo record, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        //如果是进入新打开的页面，就把新生儿数据加上
        if((record.getAssortId()==null||"".equals(record.getAssortId()))&&(!"".equals(record.getPatientId())&&record.getPatientId()!=null)){
            List<Archive_Detail_Vo> list1= archiveDetailMapper.detailWithChildByClo(record);
            for(Archive_Detail_Vo returnList:list1){
                if(!record.getPatientId().equals(returnList.getMasterid())){
                    //改变新生儿数据的title，新格式为title+inpNo
                    returnList=changeTitle(returnList);
                }
            }
            return new OffsetLimitPage((Page) list1);
        }else{
            List<Archive_Detail_Vo> list = archiveDetailMapper.detailByClo(record);
            return new OffsetLimitPage((Page) list);
    }
    }

    @Override
    public List<Archive_Detail_Vo> sourceByClo(Archive_Detail_Vo record) {
        return archiveDetailMapper.sourceByClo(record);
    }

    @Override
    public int updateCloById(Archive_Detail record) {
        return archiveDetailMapper.updateCloById(record);
    }

    @Override
    public int insertSel(Archive_Detail record) {
        return archiveDetailMapper.insertSel(record);
    }

    @Override
    public Archive_Detail selectByid(String id) {
        return archiveDetailMapper.selectByid(id);
    }

    @Override
    public int deleteByClo(Archive_Detail record) {
        return archiveDetailMapper.deleteByClo(record);
    }

    @Override
    public int delByClo(Archive_Detail record) {
        return archiveDetailMapper.delByClo(record);
    }

    @Override
    public int delById(String id) {
        return archiveDetailMapper.delById(id);
    }

    @Override
    public List<Archive_Detail> selectByColm(Archive_Detail record) {
        return archiveDetailMapper.selectByColm(record);
    }

    @Override
    public List<Archive_Detail> selectByCol(Archive_Detail record) {
        return archiveDetailMapper.selectByCol(record);
    }

    @Override
    public List<Archive_Detail> selectByIdStr(Archive_Detail record) {
        return archiveDetailMapper.selectByIdStr(record);
    }

    @Override
    public List<Archive_Detail> selectByCol2(Archive_Detail record) {
        return archiveDetailMapper.selectByCol2(record);
    }

    @Override
    public List<Archive_Detail> selectTypeTreeByPatientIdAndAssortIds(String patientId, String assortIds) {
        return archiveDetailMapper.selectTypeTreeByPatientIdAndAssortIds(patientId,assortIds);
    }

    @Override
    public void selectByIdStrFont(HttpServletResponse response, HttpSession session, Archive_Detail detail) {
        List<Archive_Detail> arList = archiveDetailMapper.selectByIdStr1(detail);
        List<String> pdfList = mulFile2OneById(response, arList);
        //存至session
        session.setAttribute("showRecord",pdfList);
    }

    /**
     * 封装合成pdf方法
     * @param response
     * @param arList
     */
    @Override
    public List<String> mulFile2OneById(HttpServletResponse response,List<Archive_Detail> arList) {
        response.setCharacterEncoding("utf-8");
        List<String> pdfList =  new ArrayList<>();
        if (arList != null && !arList.isEmpty()) {
            //TODO 抽取首次病程记录
            String strTemp = "首次病程";
            String assortId = "AFB9FBE656D7492C80AEDE6E685A851A";
            //定义护理记录里的首次护理记录排前头
            String nurseTemp = "首次护理";
            String nurseAssortId = "C70E8C427A3648B79BE80798C08F4D12";
            if(!CollectionUtils.isEmpty(arList)) {
                String strPath = "";
                String nursePath = "";
                for (int i = 0; i < arList.size(); i++) {
                    String assortid = arList.get(i).getAssortid();
                    if(StringUtils.isNotBlank(assortid) && assortid.equals(assortId) && arList.get(i).getTitle().contains(strTemp)){
                        strPath = arList.get(i).getPdfPath();
                    }
                    if(StringUtils.isNotBlank(assortid) && assortid.equals(nurseAssortId) && arList.get(i).getTitle().contains(nurseTemp)){
                        nursePath = arList.get(i).getPdfPath();
                    }
                }
                //定义是否添加首次病程
                boolean flag = false;
                boolean nurseFlag = false;
                for (Archive_Detail detail : arList) {
                    String str = detail.getPdfPath();
                    if (StringUtils.isNoneBlank(str)) {
                        //病程分类先添加首次病程
                        if (detail.getAssortid().equals(assortId) && !flag && StringUtils.isNotBlank(strPath)) {
                            pdfList.add(strPath);
                            flag = true;
                        }
                        //护理记录单先添加首次护理
                        if (detail.getAssortid().equals(nurseAssortId) && !nurseFlag && StringUtils.isNotBlank(nursePath)) {
                            pdfList.add(nursePath);
                            nurseFlag = true;
                        }
                        if(!detail.getPdfPath().equals(strPath) && !detail.getPdfPath().equals(nursePath)){
                            pdfList.add(str);
                        }
                    }
                }
            }
        }
        return pdfList;
    }

    @Override
    public void showPdf(HttpServletResponse response, HttpSession session, String pdfWater) {
        List<String> pdfList = (List<String>)session.getAttribute("showRecord");
        if(!CollectionUtils.isEmpty(pdfList)) {
            Jpg2PdfUtil jpg2PdfUtil = Jpg2PdfUtil.getInstance();
            jpg2PdfUtil.mulFile2One(response, pdfList, pdfWater);
        }
        //删除缓存`                                                                 `   `
        session.removeAttribute("showRecord");
    }

    public List<Archive_Detail_Vo>  getList(List<Archive_Detail_Vo> record){
        List<Archive_Detail_Vo> list =new ArrayList<Archive_Detail_Vo>();
        for (Archive_Detail_Vo newrecord:record){
            List<Archive_Detail_Vo> returnList = archiveDetailMapper.detailByClo(newrecord);
            for (Archive_Detail_Vo newReturnList:returnList){
                list.add(newReturnList);
            }
        }
        return list;
    }

    public Archive_Detail_Vo  changeTitle(Archive_Detail_Vo newList){
                //改变title，变成title+inpNo
                Archive_Master master= archiveMasterMapper.selectById(newList.getMasterid());
                if (master!= null) {
                    String inpNo = master.getInpNo();
                    if (inpNo != null && !"".equals(inpNo)) {
                        newList.setTitle(newList.getTitle() +"_"+ inpNo);
                    }
                }
        return newList;
    }
}
 

