package thyyxxk.webserver.dao.his.zhuyuanyisheng.patientClinicalPathway;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import thyyxxk.webserver.entity.dictionary.pathwayTemplates.dto.ClinicalPathwayTemplates;
import thyyxxk.webserver.entity.zhuyuanyisheng.patientClinicalPathway.dto.ClinicalPathwayPatient;
import thyyxxk.webserver.entity.zhuyuanyisheng.patientClinicalPathway.dto.ClinicalPathwayPatientDayLog;
import thyyxxk.webserver.entity.zhuyuanyisheng.patientClinicalPathway.vo.ClinicalPathwayPatientWorkLogVo;
import thyyxxk.webserver.entity.zhuyuanyisheng.patientClinicalPathway.vo.PatientDayWorkVo;
import thyyxxk.webserver.entity.zhuyuanyisheng.patientClinicalPathway.vo.PatientOrderDayVo;

import java.util.List;

@Mapper
public interface ClinicalPathwayPatientDao extends BaseMapper<ClinicalPathwayPatient> {
    @Select("select b.*\n" +
            "from clinical_pathway_diagnose a\n" +
            "         inner join clinical_pathway_templates b on (a.template_id = b.id)\n" +
            " where diagnose_code = #{code} and publish = 1\n" +
            "  and del_flag <> 1 ")
    List<ClinicalPathwayTemplates> getTemplateByDiagnose(String code);


    @Select("select top 1 *\n" +
            "from clinical_pathway_patient\n" +
            "where pat_no = #{patNo}\n" +
            "  and times = #{times}\n" +
            "order by create_time desc")
    ClinicalPathwayPatient selectPatientClinicalPathway(String patNo, Integer times);


    @Select("select *\n" +
            "from clinical_pathway_patient_day_log\n" +
            "where pat_no = #{patNo}\n" +
            "  and times = #{times}")
    List<PatientDayWorkVo> getDayLogByPatientNo(String patNo, Integer times);


    @Select("select *\n" +
            "from clinical_pathway_patient_day_log\n" +
            "where pat_no = #{patNo}\n" +
            "  and times = #{times}\n" +
            "  and template_id = #{id} and exec_date >= #{createDate}")
    List<ClinicalPathwayPatientDayLog> selectExecDay(String patNo, Integer times, String id,String createDate);


    @Select("select sum(execution_day)\n" +
            "from clinical_pathway_day\n" +
            "where template_id = '${templateId}'")
    int execDayByTemplateId(String templateId);


    @Select("select b.*, c.work_content, c.type\n" +
            "from clinical_pathway_patient_work_log b\n" +
            "         inner join clinical_pathway_work c on (b.content_id = c.id) " +
            "where b.pat_no = #{patNo}\n" +
            "  and b.times = #{times}")
    List<ClinicalPathwayPatientWorkLogVo> getPatientWorkLogByPatientNo(String patNo, Integer times);


    @Select("select                 b.work_content,\n" +
            "                       b.exec_type as work_type,\n" +
            "                       c.*,\n" +
            "    supply_code_name = case\n" +
            "                           when c.type = 1 then\n" +
            "                                   (select rtrim(name) from jy_zd_sample where code = supply_code)\n" +
            "                           else (select rtrim(supply_name)\n" +
            "                                 from yz_supply_type\n" +
            "                                 where yz_supply_type.supply_code = c.supply_code) end,\n" +
            "    mini_unit_name   = (select rtrim(name)\n" +
            "                        from yp_zd_unit\n" +
            "                        where code = mini_unit),\n" +
            "    dose_unit_name   = (select rtrim(name)\n" +
            "                        from yp_zd_unit\n" +
            "                        where code = dose_unit),\n" +
            "    exec_unit_name   = (select rtrim(name)\n" +
            "                        from zd_unit_code\n" +
            "                        where code = exec_unit)\n" +
            "from clinical_pathway_patient_work_log a\n" +
            "         inner join clinical_pathway_work b on (b.id = content_id)\n" +
            "         inner join clinical_pathway_order c on (c.work_id = b.id)\n" +
            "where pat_no = #{patNo}\n" +
            "  and times = #{times}\n" +
            "  AND a.day_id = #{dayId}\n" +
            "  AND b.type IN (1, 2)")
    List<PatientOrderDayVo> getPatientOrderWork(String patNo, Integer times, String dayId);


    @Select("select *\n" +
            "from clinical_pathway_patient\n" +
            "where pat_no = #{patNo}\n" +
            "  and times = #{times} order by create_time")
    List<ClinicalPathwayPatient> selectPatientCount(String patNo, Integer times);
}
