package com.emr.service.ipml;

import com.emr.dao.ArchivePrintinfoMapper;
import com.emr.dao.Archive_MasterMapper;
import com.emr.entity.ArchivePrintinfo;
import com.emr.entity.Archive_Master;
import com.emr.entity.Archive_Master_Vo;
import com.emr.entity.OffsetLimitPage;
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 org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ProjectName:
 * @Description:
 * @Param 传输参数
 * @Return
 * @Author: 曾文和
 * @CreateDate: 2021/11/15 14:19
 * @UpdateUser: 曾文和
 * @UpdateDate: 2021/11/15 14:19
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Service
@Transactional
public class ObservationRecordService {
    @Autowired
    private Archive_MasterMapper archiveMasterMapper;
    @Autowired
    private ArchivePrintinfoMapper archivePrintinfoMapper;
    public OffsetLimitPage getObservationRecordList(Archive_Master_Vo master, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Archive_Master> list = getList(master);
        //查询打印次数
        selectPrintCountByMasterId(list);
        return new OffsetLimitPage((Page) list);
    }

    /**
     * 查询打印次数
     * @param list
     */
    private void selectPrintCountByMasterId(List<Archive_Master> list) {
        if(!CollectionUtils.isEmpty(list)){
            StringBuilder masterIds = new StringBuilder();
            for(Archive_Master master : list){
                if(StringUtils.isNotBlank(masterIds)){
                    masterIds.append(",");
                }
                masterIds.append(master.getId());
            }
            List<ArchivePrintinfo> archivePrintinfos = archivePrintinfoMapper.selectPrintCountByMasterId(masterIds.toString());
            if(!CollectionUtils.isEmpty(archivePrintinfos)){
                for(Archive_Master master : list){
                    for(ArchivePrintinfo archivePrintinfo :archivePrintinfos){
                        if(master.getId().equals(archivePrintinfo.getMasterid())){
                            master.setPrintNum(archivePrintinfo.getAdmissTimes().toString());
                            break;
                        }
                    }
                }
            }
        }
    }

    public List<Archive_Master> getList(Archive_Master_Vo master){
        return archiveMasterMapper.getObservationRecordList(master);
    }
}
