package thyyxxk.webserver.dao.his.dictionary.clinicalPathway;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
import thyyxxk.webserver.entity.dictionary.pathwayTemplates.dto.ClinicalPathwayDay;
import thyyxxk.webserver.entity.dictionary.pathwayTemplates.dto.ClinicalPathwayOrder;
import thyyxxk.webserver.entity.dictionary.pathwayTemplates.dto.ClinicalPathwayTemplates;
import thyyxxk.webserver.entity.dictionary.pathwayTemplates.dto.ClinicalPathwayWork;
import thyyxxk.webserver.entity.dictionary.pathwayTemplates.vo.ClinicalPathwayDiagnoseVo;
import thyyxxk.webserver.entity.dictionary.pathwayTemplates.vo.JyJcItemVo;
import thyyxxk.webserver.entity.dictionary.pathwayTemplates.vo.PathwayTemplatesVo;

import java.util.List;

@Mapper
public interface ClinicalPathwayDao extends BaseMapper<ClinicalPathwayTemplates> {

    @Select("select code, name, yb_code, yb_name\n" +
            "from zd_icd_code_new\n" +
            "where isnull(del_flag, '0') <> '1'\n" +
            "  and (name like '%${code}%' or code like '%${code}%' or py_code like '%${code}%')")
    List<GetDropdownBox> getIcdCode(String code);

    @Select("select id,\n" +
            "       name,\n" +
            "       dept_code,\n" +
            "       dept_name  = (select rtrim(name) from zd_unit_code where code = dept_code),\n" +
            "       create_time,\n" +
            "       create_code,\n" +
            "       create_name= (select rtrim(name) from a_employee_mi a where a.code = create_code),\n" +
            "       update_time,\n" +
            "       update_code,\n" +
            "       update_name= (select rtrim(name) from a_employee_mi a where a.code = update_code),\n" +
            "       del_flag,is_dir,parent_code,publish,describe \n" +
            "from clinical_pathway_templates where  1=1 ${where}")
    List<PathwayTemplatesVo> getPathwayTemplates(String where);


    @Select("select zy_order_code as orderCode,\n" +
            "       name as orderName ,\n" +
            "       exec_unit as execDept,\n" +
            "       execDeptName = (select rtrim(name) from zd_unit_code where code = exec_unit),\n" +
            "       class_name     = (select name from jy_zd_class where class = jy_zd_class.code)," +
            "       pathwayType  = ${type} \n" +
            "from ${table}\n" +
            "where del_flag <> '1'\n" +
            "  and (\n" +
            "    name like '${key}'\n" +
            "        or zy_order_code like '${key}'\n" +
            "        or py_code like '${key}'\n" +
            "    )")
    List<JyJcItemVo> getJcJyItem(String table, Integer type, String key);


    @Update("\n" +
            "UPDATE\n" +
            "    clinical_pathway_templates \n" +
            "SET\n" +
            "    update_time=getdate(),\n" +
            "    update_code='${userCode}'," +
            "    del_flag = 1 \n" +
            "WHERE\n" +
            "    id='${id}';")
    void deleteTemplate(String id, String userCode);


    @Select("select code, name\n" +
            "from jy_zd_sample\n" +
            "where isnull(del_flag, '0') <> '1'")
    List<GetDropdownBox> getJyZdSample();


    @Select("select id,\n" +
            "       diagnose_code,\n" +
            "       diagnose_code_name = (select name from zd_icd_code_new where code = diagnose_code),\n" +
            "       template_id\n" +
            "from clinical_pathway_diagnose\n" +
            "where template_id = #{templateId}")
    List<ClinicalPathwayDiagnoseVo> getDiagnoseByTemplateId(String templateId);


    @Select("select * from clinical_pathway_day where template_id = '${templateId}'")
    List<ClinicalPathwayDay> getAllDayByTemplateId(String templateId);


    @Select("select a.*\n" +
            "from clinical_pathway_work a\n" +
            "         left join clinical_pathway_day b on (a.day_id = b.id)\n" +
            "where b.template_id = '${templateId}'")
    List<ClinicalPathwayWork> getWorkByTemplateId(String templateId);


    @Select("select o.*\n" +
            "from clinical_pathway_order o\n" +
            "         inner join clinical_pathway_work a on (o.work_id = a.id)\n" +
            "where a.id = '${templateId}'")
    List<ClinicalPathwayOrder> getOrdersByTemplateId(String templateId);
}
