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

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import thyyxxk.webserver.entity.CodeName;
import thyyxxk.webserver.entity.dictionary.diagnoseReqTemplate.data.DiagnoseTemplate;
import thyyxxk.webserver.entity.dictionary.diagnoseReqTemplate.data.DiagnoseToReq;
import thyyxxk.webserver.entity.dictionary.diagnoseReqTemplate.vo.DiagnoseReqTemplateVo;
import thyyxxk.webserver.entity.dictionary.diagnoseReqTemplate.vo.DiagnoseToReqVo;

import java.util.List;

@Mapper
public interface DiagnoseToReqDao extends BaseMapper<DiagnoseToReq> {


    @Select("select *,create_name = (select rtrim(name) from a_employee_mi where code = diagnose_to_req.create_code)," +
            "  update_name = (select rtrim(name) from a_employee_mi where code = diagnose_to_req.update_code) " +
            "from diagnose_to_req ${ew.customSqlSegment} ")
    Page<DiagnoseToReqVo> selectTemplateList(Page<DiagnoseToReq> page, @Param("ew") Wrapper<DiagnoseToReq> queryWrapper);


    @Select("select *, diagnose_name = (select name from zd_icd_code_new where code = diagnose_code)\n" +
            "from diagnose_template\n" +
            "where template_id = #{id} ")
    List<DiagnoseTemplate> selectByTemplateId(String id);

    @Select("select a.*,\n" +
            "       b.name                        as                               order_name," +
            "       b.zy_order_code,\n" +
            "       class                         as                               classes,\n" +
            "       (select name from zd_unit_code zd where zd.code = a.exec_dept) exec_name,\n" +
            "       (select name\n" +
            "        from jy_zd_sample\n" +
            "        where code = part_or_sample) as                               part_or_sample_name\n" +
            "from diagnose_req_template a\n" +
            "         inner join\n" +
            "     jy_zd_item b\n" +
            "     on (\n" +
            "            a.order_code = b.code\n" +
            "            )\n" +
            "         and isnull(zy_flag, '0') <> '1'\n" +
            "         and isnull(del_flag, '0') <> '1'\n" +
            "where template_id = #{id}\n" +
            "  and req_type = '2'\n" +
            "union\n" +
            "    all\n" +
            "select a.*,\n" +
            "       b.name                        as                               order_name,\n" +
            "       b.zy_order_code,\n" +
            "       class                         as                               classes,\n" +
            "       (select name from zd_unit_code zd where zd.code = a.exec_dept) exec_name,\n" +
            "       (select name\n" +
            "        from ysh_zd_part_code\n" +
            "        where code = part_or_sample) as                               part_or_sample_name\n" +
            "from diagnose_req_template a\n" +
            "         inner join\n" +
            "     jc_zd_item b\n" +
            "     on (\n" +
            "         a.order_code = b.code\n" +
            "         )\n" +
            "where template_id = #{id}\n" +
            "  and req_type = '3'\n" +
            "  and isnull(zy_flag, '0') <> '1'\n" +
            "  and isnull(del_flag, '0') <> '1';")
    List<DiagnoseReqTemplateVo> selectReqByTemplateId(String id);


    @Delete("delete from diagnose_template where template_id = #{id};" +
            "delete from diagnose_req_template where template_id = #{id};")
    void delDiagnoseByTemplateId(String id);

    @Select("select template_id as code, name\n" +
            "from diagnose_template a\n" +
            "         inner join diagnose_to_req b on (a.template_id = b.id)\n" +
            " ${ew.customSqlSegment} " +
            "group by template_id, name")
    List<CodeName> selectDiagnoseTemplateByDiagnoseCode(
            @Param("ew") QueryWrapper<?> queryWrapper
    );

    @Select("select count(1)\n" +
            "from zy_actpatient\n" +
            "where inpatient_no = #{patNo}\n" +
            "  and admiss_times = #{times}")
    int inTheCourtyard(String patNo, Integer times);

}
