package com.emr.service.ipml;

import com.emr.entity.Archive_Master_Vo;
import com.emr.entity.OffsetLimitPage;
import com.emr.entity.Zd_Assort;
import com.emr.service.ScanInfoService;
import com.emr.util.JdbcUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ScanInfoServiceImpl implements ScanInfoService {

    @Override
    public OffsetLimitPage scanCountPage(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        //开始和结束行数
        Integer stratRow=(offset/10)*limit;
        Integer endRow=(offset/10)*limit+limit;
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        int rowTotal=0;
        List<Archive_Master_Vo> list=new ArrayList<>();
        int a=0,b=0,c=0,d=0;
        try {
            con = JdbcUtil.getConnection();
            String sql = "";//"select * from [zd_Assort] where print_flag=?";
            /*select c.patient_id,c.numb,a.assort_name,a.assort_date,content from(
                    select patient_id,assort_id,COUNT(*) numb from t_scan_assort where patient_id is not null and patient_id!=''  group by patient_id,assort_id
            )c
            LEFT JOIN(
                    select patient_id,assort_name,convert(varchar(10), assort_date,23) assort_date from t_gdh_index where patient_id is not null and patient_id!=''
            )a
            on c.patient_id=a.patient_id
            LEFT JOIN(
                    select patient_id,COUNT(*) content from t_scan_assort where patient_id is not null and patient_id!=''   group by patient_id
            )b
            on a.patient_id=b.patient_id*/
            //查询总记录数
            sql = "select count(*) from( " +
                    " select c.patient_id,c.numb,a.assort_name creater,a.create_time,content from( " +
                    "  select patient_id,assort_id,COUNT(*) numb from t_scan_assort where patient_id is not null and patient_id!=''  group by patient_id,assort_id " +
                    " )c " +
                    " LEFT JOIN( " +
                    " select patient_id,assort_name,convert(varchar(10), assort_date,23) create_time from t_gdh_index where patient_id is not null and patient_id!=''  " +
                    " )a " +
                    " on c.patient_id=a.patient_id " +
                    " LEFT JOIN( " +
                    "  select patient_id,COUNT(*) content from t_scan_assort where patient_id is not null and patient_id!='' group by patient_id " +
                    " )b " +
                    " on a.patient_id=b.patient_id " +
                    "  )m group by creater,create_time  having 1=1 ";
            if(StringUtils.isNotBlank(archiveMasterVo.getInpNo())){
                sql+=" and inp_no like '%'+?+'%'" ;
                a=1;
            }
            if(StringUtils.isNotBlank(archiveMasterVo.getInpNo())){
                sql+=" and inp_no like '%'+?+'%'" ;
                a=1;
            }
            if(StringUtils.isNotBlank(archiveMasterVo.getCreater())){
                sql+=" and creater like '%'+?+'%'" ;
                b=1;
            }
            if(StringUtils.isNotBlank(archiveMasterVo.getStartDate()) && StringUtils.isNotBlank(archiveMasterVo.getEndDate())){
                sql+=" and CONVERT (VARCHAR(10), create_time, 23) between ? and  ? " ;
                c=1;d=1;
            }else if(StringUtils.isNotBlank(archiveMasterVo.getStartDate())){
                c=1;
                sql+=" and CONVERT (VARCHAR(10), create_time, 23)>=?" ;
            }else  if(StringUtils.isNotBlank(archiveMasterVo.getEndDate())){
                d=1;
                sql+=" and CONVERT (VARCHAR(10), create_time, 23)>=?" ;
            }
            stmt = con.prepareStatement(sql);
            if(a==1){
                stmt.setString(a, archiveMasterVo.getInpNo());
            }
            if(b==1) {
                stmt.setString(a+b, archiveMasterVo.getCreater());
            }
            if(c==1) {
                stmt.setString(a+b+c, archiveMasterVo.getStartDate());
            }
            if(d==1) {
                stmt.setString(a+b+c+d, archiveMasterVo.getEndDate());
            }
            rs = stmt.executeQuery();
            rs.next();
            rowTotal=rs.getInt(1);
           sql="select * from(" +
                   "select ROW_NUMBER() over(order by creater desc) RowNum,creater,create_time,sum(numb) numb ,sum(content) content from( " +
                   " select c.patient_id,c.numb,a.assort_name creater,a.create_time,content from( " +
                   "  select patient_id,assort_id,COUNT(*) numb from t_scan_assort where patient_id is not null and patient_id!=''  group by patient_id,assort_id " +
                   " )c " +
                   " LEFT JOIN( " +
                   " select patient_id,assort_name,convert(varchar(10), assort_date,23) create_time from t_gdh_index where patient_id is not null and patient_id!=''  " +
                   " )a " +
                   " on c.patient_id=a.patient_id " +
                   " LEFT JOIN( " +
                   "  select patient_id,COUNT(*) content from t_scan_assort where patient_id is not null and patient_id!='' group by patient_id " +
                   " )b " +
                   " on a.patient_id=b.patient_id " +
                   "  )m group by creater,create_time  having 1=1 ";
            if(StringUtils.isNotBlank(archiveMasterVo.getInpNo())){
                sql+=" and inp_no like '%'+?+'%'" ;
                a=1;
            }
            if(StringUtils.isNotBlank(archiveMasterVo.getCreater())){
                sql+=" and creater like '%'+?+'%'" ;
                b=1;
            }
            if(StringUtils.isNotBlank(archiveMasterVo.getStartDate()) && StringUtils.isNotBlank(archiveMasterVo.getEndDate())){
                sql+=" and CONVERT (VARCHAR(10), create_time, 23) between ? and  ? " ;
                c=1;d=1;
            }else if(StringUtils.isNotBlank(archiveMasterVo.getStartDate())){
                c=1;
                sql+=" and CONVERT (VARCHAR(10), create_time, 23)>=?" ;
            }else  if(StringUtils.isNotBlank(archiveMasterVo.getEndDate())){
                d=1;
                sql+=" and CONVERT (VARCHAR(10), create_time, 23)>=?" ;
            }
            sql+=      " ) as t1 " +
                    " where 1=1 and RowNum>"+stratRow+" and RowNum<="+endRow   ;
            stmt = con.prepareStatement(sql);
            if(a==1){
                stmt.setString(a, archiveMasterVo.getInpNo());
            }
            if(b==1) {
                stmt.setString(a+b, archiveMasterVo.getCreater());
            }
            if(c==1) {
                stmt.setString(a+b+c, archiveMasterVo.getStartDate());
            }
            if(d==1) {
                stmt.setString(a+b+c+d, archiveMasterVo.getEndDate());
            }
            rs = stmt.executeQuery();

            Archive_Master_Vo archiveMasterVo1=new Archive_Master_Vo();
            while(rs.next())
            {
                archiveMasterVo1=new Archive_Master_Vo();
                String creater = rs.getString("creater");//扫描人
                String createTime = rs.getString("create_time");//扫描时间
                int numb=Integer.parseInt(rs.getString("numb"));//扫描次数
                String content=rs.getString("content");//扫描份数
                archiveMasterVo1.setCreater(creater);
                archiveMasterVo1.setCreateTime(createTime);
                archiveMasterVo1.setNumb(numb);
                archiveMasterVo1.setContent(content);
                list.add(archiveMasterVo1);
            }
        } catch (Exception w) {
            w.printStackTrace();
        }finally{
            JdbcUtil.close(rs, stmt, con);
        }
        OffsetLimitPage offsetLimitPage=new OffsetLimitPage();
        offsetLimitPage.setTotal((long) rowTotal);
        offsetLimitPage.setRows(list);
        return offsetLimitPage;
    }

    @Override
    public List<Archive_Master_Vo> scanCountList(Archive_Master_Vo archiveMasterVo) {
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        List<Archive_Master_Vo> list=new ArrayList<>();
        int a=0,b=0,c=0,d=0;
        try {
            con = JdbcUtil.getConnection();
            String sql = "";
            sql = "select creater,create_time,sum(numb) numb ,sum(content) content from( " +
                    " select c.patient_id,c.numb,a.assort_name creater,a.create_time,content from( " +
                    "  select patient_id,assort_id,COUNT(*) numb from t_scan_assort where patient_id is not null and patient_id!=''  group by patient_id,assort_id " +
                    " )c " +
                    " LEFT JOIN( " +
                    " select patient_id,assort_name,convert(varchar(10), assort_date,23) create_time from t_gdh_index where patient_id is not null and patient_id!=''  " +
                    " )a " +
                    " on c.patient_id=a.patient_id " +
                    " LEFT JOIN( " +
                    "  select patient_id,COUNT(*) content from t_scan_assort where patient_id is not null and patient_id!='' group by patient_id " +
                    " )b " +
                    " on a.patient_id=b.patient_id " +
                    "  )m group by creater,create_time  having 1=1 ";
            if(StringUtils.isNotBlank(archiveMasterVo.getInpNo())){
                sql+=" and inp_no like '%'+?+'%'" ;
                a=1;
            }
            if(StringUtils.isNotBlank(archiveMasterVo.getCreater())){
                sql+=" and creater like '%'+?+'%'" ;
                b=1;
            }
            if(StringUtils.isNotBlank(archiveMasterVo.getStartDate()) && StringUtils.isNotBlank(archiveMasterVo.getEndDate())){
                sql+=" and CONVERT (VARCHAR(10), create_time, 23) between ? and  ? " ;
                c=1;d=1;
            }else if(StringUtils.isNotBlank(archiveMasterVo.getStartDate())){
                c=1;
                sql+=" and CONVERT (VARCHAR(10), create_time, 23)>=?" ;
            }else  if(StringUtils.isNotBlank(archiveMasterVo.getEndDate())){
                d=1;
                sql+=" and CONVERT (VARCHAR(10), create_time, 23)>=?" ;
            }
            stmt = con.prepareStatement(sql);
            if(a==1){
                stmt.setString(a, archiveMasterVo.getInpNo());
            }
            if(b==1) {
                stmt.setString(a+b, archiveMasterVo.getCreater());
            }
            if(c==1) {
                stmt.setString(a+b+c, archiveMasterVo.getStartDate());
            }
            if(d==1) {
                stmt.setString(a+b+c+d, archiveMasterVo.getEndDate());
            }
            rs = stmt.executeQuery();
            Archive_Master_Vo archiveMasterVo1=new Archive_Master_Vo();
            while(rs.next())
            {
                archiveMasterVo1=new Archive_Master_Vo();
                String creater = rs.getString("creater");//扫描人
                String createTime = rs.getString("create_time");//扫描时间
                int numb=Integer.parseInt(rs.getString("numb"));//扫描次数
                String content=rs.getString("content");//扫描份数
                archiveMasterVo1.setCreater(creater);
                archiveMasterVo1.setCreateTime(createTime);
                archiveMasterVo1.setNumb(numb);
                archiveMasterVo1.setContent(content);
                list.add(archiveMasterVo1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            JdbcUtil.close(rs, stmt, con);
        }
        return list;
    }

    @Override
    public OffsetLimitPage scanDetailPage(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        //开始和结束行数
        Integer stratRow=(offset/10)*limit;
        Integer endRow=(offset/10)*limit+limit;
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        int rowTotal=0;
        int a=0,b=0,c=0,d=0;
        List<Archive_Master_Vo> list=new ArrayList<>();;
        try {
            con = JdbcUtil.getConnection();
            String sql = "";//"select * from [zd_Assort] where print_flag=?";
            //查询总记录数
            sql = "select count(*) from(" +
                    " select patient_id patient_id2,assort_id,scan_page from t_scan_assort  where patient_id is not null and patient_id!=''" +
                    " )a" +
                    " left join (" +
                    "   select patient_id,inpatient_no inp_no,admiss_times visit_id,name,convert(varchar(19), dis_date,120) discharge_date_time,assort_name creater,convert(varchar(10), assort_date,23) create_time " +
                    " from t_gdh_index where patient_id is not null and patient_id!=''" +
                    " )b" +
                    " on a.patient_id2=b.patient_id" +
                    " where 1=1";
            if(StringUtils.isNotBlank(archiveMasterVo.getInpNo())){
                sql+=" and inp_no like '%'+?+'%'" ;
                a=1;
            }
            if(StringUtils.isNotBlank(archiveMasterVo.getCreater())){
                sql+=" and creater like '%'+?+'%'" ;
                b=1;
            }
            if(StringUtils.isNotBlank(archiveMasterVo.getStartDate()) && StringUtils.isNotBlank(archiveMasterVo.getEndDate())){
                sql+=" and CONVERT (VARCHAR(10), b.create_time, 23) between ? and  ? " ;
                c=1;d=1;
            }else if(StringUtils.isNotBlank(archiveMasterVo.getStartDate())){
                c=1;
                sql+=" and CONVERT (VARCHAR(10), b.create_time, 23)>=?" ;
            }else  if(StringUtils.isNotBlank(archiveMasterVo.getEndDate())){
                d=1;
                sql+=" and CONVERT (VARCHAR(10), b.create_time, 23)>=?" ;
            }
            stmt = con.prepareStatement(sql);
            if(a==1){
                stmt.setString(a, archiveMasterVo.getInpNo());
            }
            if(b==1) {
                stmt.setString(a+b, archiveMasterVo.getCreater());
            }
            if(c==1) {
                stmt.setString(a+b+c, archiveMasterVo.getStartDate());
            }
            if(d==1) {
                stmt.setString(a+b+c+d, archiveMasterVo.getEndDate());
            }
            rs = stmt.executeQuery();
            rs.next();
            rowTotal=rs.getInt(1);

            sql="select *  from (" +
                    "select  ROW_NUMBER() over(order by assort_id desc) RowNum ,b.*,a.scan_page content from(" +
                    " select patient_id patient_id2,assort_id,scan_page from t_scan_assort  where patient_id is not null and patient_id!=''" +
                    " )a" +
                    " left join (" +
                    "   select patient_id,inpatient_no inp_no,admiss_times visit_id,name,convert(varchar(19), dis_date,120) discharge_date_time,assort_name creater,convert(varchar(10), assort_date,23) create_time " +
                    " from t_gdh_index where patient_id is not null and patient_id!=''" +
                    " )b" +
                    " on a.patient_id2=b.patient_id" +
                    " where 1=1 ";
                    if(StringUtils.isNotBlank(archiveMasterVo.getInpNo())){
                        sql+=" and inp_no like '%'+?+'%'" ;
                        a=1;
                    }
                    if(StringUtils.isNotBlank(archiveMasterVo.getCreater())){
                        sql+=" and creater like '%'+?+'%'" ;
                        b=1;
                    }
                    if(StringUtils.isNotBlank(archiveMasterVo.getStartDate()) && StringUtils.isNotBlank(archiveMasterVo.getEndDate())){
                        sql+=" and CONVERT (VARCHAR(10), b.create_time, 23) between ? and  ? " ;
                        c=1;d=1;
                    }else if(StringUtils.isNotBlank(archiveMasterVo.getStartDate())){
                        c=1;
                        sql+=" and CONVERT (VARCHAR(10), b.create_time, 23)>=?" ;
                    }else  if(StringUtils.isNotBlank(archiveMasterVo.getEndDate())){
                        d=1;
                        sql+=" and CONVERT (VARCHAR(10), b.create_time, 23)>=?" ;
                    }
            sql+=" ) as t1 "
                    + " where 1=1 and RowNum>"+stratRow+" and RowNum<="+endRow   ;
            stmt = con.prepareStatement(sql);
            if(a==1){
                stmt.setString(a, archiveMasterVo.getInpNo());
            }
            if(b==1) {
                stmt.setString(a+b, archiveMasterVo.getCreater());
            }
            if(c==1) {
                stmt.setString(a+b+c, archiveMasterVo.getStartDate());
            }
            if(d==1) {
                stmt.setString(a+b+c+d, archiveMasterVo.getEndDate());
            }
            rs = stmt.executeQuery();

            Archive_Master_Vo archiveMasterVo1=new Archive_Master_Vo();
            while(rs.next())
            {
                archiveMasterVo1=new Archive_Master_Vo();
                String patientId = rs.getString("patient_id");
                String inpNo = rs.getString("inp_no");
                String visitId = rs.getString("visit_id");
                String name = rs.getString("name");
               // String sex = rs.getString("sex");
                //String admissionDateTime = rs.getString("admission_date_time");
                String dischargeDateTime = rs.getString("discharge_date_time");
               // String deptName = rs.getString("dept_name");
              //  String doctorInCharge = rs.getString("doctor_in_charge");
                String creater = rs.getString("creater");//扫描人
                String content = rs.getString("content");//文件名
                String createTime = rs.getString("create_time");//扫描时间

                archiveMasterVo1.setPatientId(patientId);
                archiveMasterVo1.setInpNo(inpNo);
                archiveMasterVo1.setVisitId(visitId);
                archiveMasterVo1.setName(name);
                //archiveMasterVo1.setSex(sex);
                //archiveMasterVo1.setAdmissionDateTime(admissionDateTime);
                archiveMasterVo1.setDischargeDateTime(dischargeDateTime);
               // archiveMasterVo1.setDeptName(deptName);
               // archiveMasterVo1.setDoctorInCharge(doctorInCharge);
                archiveMasterVo1.setCreater(creater);
                archiveMasterVo1.setContent(content);
                archiveMasterVo1.setCreateTime(createTime);
                list.add(archiveMasterVo1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            JdbcUtil.close(rs, stmt, con);
        }
        OffsetLimitPage offsetLimitPage=new OffsetLimitPage();
        offsetLimitPage.setTotal((long) rowTotal);
        offsetLimitPage.setRows(list);
        return offsetLimitPage;
    }

    @Override
    public List<Archive_Master_Vo> scanDetailList(Archive_Master_Vo archiveMasterVo) {
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        List<Archive_Master_Vo> list=new ArrayList<>();;
        try {
            con = JdbcUtil.getConnection();
            int a=0,b=0,c=0,d=0;
            String sql = "select b.*,a.scan_page content from(" +
                    " select patient_id patient_id2,assort_id,scan_page from t_scan_assort  where patient_id is not null and patient_id!=''" +
                    " )a" +
                    " left join (" +
                    "   select patient_id,inpatient_no inp_no,admiss_times visit_id,name,convert(varchar(19), dis_date,120) discharge_date_time,assort_name creater,convert(varchar(10), assort_date,23) create_time " +
                    " from t_gdh_index where patient_id is not null and patient_id!=''" +
                    " )b" +
                    " on a.patient_id2=b.patient_id" +
                    " where 1=1";
            if(StringUtils.isNotBlank(archiveMasterVo.getInpNo())){
                sql+=" and inp_no like '%'+?+'%'" ;
                a=1;
            }
            if(StringUtils.isNotBlank(archiveMasterVo.getCreater())){
                sql+=" and creater like '%'+?+'%'" ;
                b=1;
            }
            if(StringUtils.isNotBlank(archiveMasterVo.getStartDate()) && StringUtils.isNotBlank(archiveMasterVo.getEndDate())){
                sql+=" and CONVERT (VARCHAR(10), b.create_time, 23) between ? and  ? " ;
                c=1;d=1;
            }else if(StringUtils.isNotBlank(archiveMasterVo.getStartDate())){
                c=1;
                sql+=" and CONVERT (VARCHAR(10), b.create_time, 23)>=?" ;
            }else  if(StringUtils.isNotBlank(archiveMasterVo.getEndDate())){
                d=1;
                sql+=" and CONVERT (VARCHAR(10), b.create_time, 23)>=?" ;
            }
            stmt = con.prepareStatement(sql);
            if(a==1){
                stmt.setString(a, archiveMasterVo.getInpNo());
            }
            if(b==1) {
                stmt.setString(a+b, archiveMasterVo.getCreater());
            }
            if(c==1) {
                stmt.setString(a+b+c, archiveMasterVo.getStartDate());
            }
            if(d==1) {
                stmt.setString(a+b+c+d, archiveMasterVo.getEndDate());
            }
            rs = stmt.executeQuery();
            Archive_Master_Vo archiveMasterVo1=new Archive_Master_Vo();
            while(rs.next())
            {
                archiveMasterVo1=new Archive_Master_Vo();
                String patientId = rs.getString("patient_id");
                String inpNo = rs.getString("inp_no");
                String visitId = rs.getString("visit_id");
                String name = rs.getString("name");
               // String sex = rs.getString("sex");
               // String admissionDateTime = rs.getString("admission_date_time");
                String dischargeDateTime = rs.getString("discharge_date_time");
               // String deptName = rs.getString("dept_name");
               // String doctorInCharge = rs.getString("doctor_in_charge");
                String creater = rs.getString("creater");//扫描人
                String content = rs.getString("content");//文件名
                String createTime = rs.getString("create_time");//扫描时间

                archiveMasterVo1.setPatientId(patientId);
                archiveMasterVo1.setInpNo(inpNo);
                archiveMasterVo1.setVisitId(visitId);
                archiveMasterVo1.setName(name);
                //archiveMasterVo1.setSex(sex);
                //archiveMasterVo1.setAdmissionDateTime(admissionDateTime);
                archiveMasterVo1.setDischargeDateTime(dischargeDateTime);
                //archiveMasterVo1.setDeptName(deptName);
               // archiveMasterVo1.setDoctorInCharge(doctorInCharge);
                archiveMasterVo1.setCreater(creater);
                archiveMasterVo1.setContent(content);
                archiveMasterVo1.setCreateTime(createTime);
                list.add(archiveMasterVo1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(rs, stmt, con);
        }
        return list;
    }


}
