package thyyxxk.webserver.dao.his.examinations;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.examinations.bookablemanage.ExamItem;
import thyyxxk.webserver.entity.examinations.inspections.request.ReportIndexInquiry;
import thyyxxk.webserver.entity.examinations.inspections.response.TestReport;

import java.util.List;

/**
 * @author dj
 */
@Mapper
public interface InspectionsDao {

    @Select("select rtrim(max(tj_no)) from mz_patient_mi with(nolock) where social_no=#{socialNo}")
    String selectTjNo(@Param("socialNo") String socialNo);

    @Select("select rtrim(code) as code, rtrim(name) as name, rtrim(exec_unit) as execUnit, " +
            "execUnitName=(select rtrim(name) from zd_unit_code with(nolock) where code=exec_unit), " +
            "rtrim(py_code) as pyCode, rtrim(d_code) as wbCode, wx_bookable_flag, book_tip " +
            "from ${tableName} with(nolock) where isnull(del_flag,0)!=1")
    IPage<ExamItem> searchItems(IPage<ExamItem> iPage,
                                @Param("tableName") String tableName);

    @Select("select rtrim(code) as code, rtrim(name) as name, rtrim(exec_unit) as execUnit, " +
            "execUnitName=(select rtrim(name) from zd_unit_code with(nolock) where code=exec_unit), " +
            "rtrim(py_code) as pyCode, rtrim(d_code) as wbCode, wx_bookable_flag, book_tip " +
            "from ${tableName} with(nolock) where isnull(del_flag,0)!=1 and ${method} like #{content}")
    IPage<ExamItem> searchItemsByCondition(IPage<ExamItem> iPage,
                                           @Param("tableName") String tableName,
                                           @Param("method") String method,
                                           @Param("content") String content);

    @Update("update ${tableName} set wx_bookable_flag=#{wxBookableFlag},book_tip=#{bookTip} where code=#{code}")
    void updateWxBookableFlag(ExamItem item);

    @Select("select patient_uid,patient_name,examin_eparts,check_time from t_check_data " +
            "where pat_no=#{patNo} and check_time>=#{reqStartTime} and check_time<=#{reqEndTime} ")
    List<TestReport> selectTestReportIndex(ReportIndexInquiry inquiry);

    @Select("select *, " +
            "doctorName=(select top 1 rtrim(d.name) from a_employee_mi d where d.code_rs=doctor_code) " +
            "from t_check_data where patient_uid=#{id}")
    TestReport selectTestReport(String id);

    @Select("select rtrim(social_no) from a_patient_mi where inpatient_no=#{patNo}")
    String selectInpatientSocialNo(String patNo);

    @Select("select rtrim(social_no) from mz_patient_mi where patient_id=#{patNo}")
    String selectOutpatientSocialNo(String patNo);
}
