package thyyxxk.webserver.dao.his.outpatient.interactive;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
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.inpatient.nursemodule.response.PatientBriefInfo;
import thyyxxk.webserver.entity.outpatient.interactive.clinicsatisfied.request.ClinicSatisfiedInquiry;
import thyyxxk.webserver.entity.outpatient.interactive.clinicsatisfied.request.ModifyStatus;
import thyyxxk.webserver.entity.outpatient.interactive.clinicsatisfied.response.BriefSuggestion;
import thyyxxk.webserver.entity.outpatient.interactive.clinicsatisfied.response.Patientinfo;
import thyyxxk.webserver.entity.outpatient.interactive.clinicsatisfied.response.QuestionnaireV2;
import thyyxxk.webserver.entity.outpatient.interactive.clinicsatisfied.response.SatisfiedBody;

import java.util.Date;
import java.util.List;

@Mapper
public interface ClinicSatisfiedDao {
    @Select("select * from " +
            "(SELECT ROW_NUMBER() OVER(ORDER BY id) AS idx,* " +
            "from ${table} a ${ew.customSqlSegment}) t " +
            "where t.idx>(#{page}-1)*#{size} and t.idx<=#{page}*#{size}")
    List<SatisfiedBody> selectSatisfiedBodies(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper,
                                              @Param("page") int page, @Param("size") int size,
                                              @Param("table") String table);

    @Select("select * from " +
            "(SELECT ROW_NUMBER() OVER(ORDER BY id) AS idx,* " +
            "from ${table} a ${ew.customSqlSegment}) t " +
            "where t.idx>(#{page}-1)*#{size} and t.idx<=#{page}*#{size}")
    List<QuestionnaireV2> selectQuestionnaireBodies(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper,
                                                @Param("page") int page, @Param("size") int size,
                                                @Param("table") String table);

    @Select("select count(1) from ${table} ${ew.customSqlSegment}")
    int selectValidCount(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper,
                         @Param("table") String table);

    @Update("update ${table} set deleted=#{deleted},delete_staff=#{staff},delete_time=#{date} where id=#{id}")
    int updateAssessmentStatus(ModifyStatus status);

    @Select("select * from ${table} ${ew.customSqlSegment} order by id")
    List<SatisfiedBody> selectAllSatisfiedBodies(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper,
                                                 @Param("table") String table);

    @Select("select *,deptName=(select rtrim(d.name) from zd_unit_code d where d.code=dept_code) " +
            "from ${table} ${ew.customSqlSegment} order by id")
    List<QuestionnaireV2> selectAllQuestionnaireBodies(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper,
                                                 @Param("table") String table);

    @Select("select *,deptName=(select rtrim(d.name) from zd_unit_code d where d.code=a.dept_code) " +
            "from t_clinic_satisfied_assessment a ${ew.customSqlSegment} order by id")
    List<SatisfiedBody> selectAllOutpatientSatisfiedBodies(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);

    @Select("select *,deptName=(select rtrim(d.name) from zd_unit_code d where d.code=" +
            "(select e.zk_ward from zy_inactpatient e where e.inpatient_no=a.inpatient_no and e.admiss_times=a.admiss_times)) " +
            "from t_inpatient_satisfied_assessment a ${ew.customSqlSegment} order by id")
    List<SatisfiedBody> selectAllInpatientSatisfiedBodies(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);

    @Select("select count(1) from ${table} ${ew.customSqlSegment}")
    int selectUnsatisfiedCount(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper,
                               @Param("table") String table);

    @Select("select id,inpatient_no,patient_id,create_datetime,suggestion, " +
            "patName=(select rtrim(d.name) from mz_patient_mi d " +
            "where d.patient_id=a.patient_id) " +
            "from t_clinic_satisfied_assessment a ${ew.customSqlSegment} " +
            "and suggestion is not null and LEN(suggestion)>5 order by id")
    List<BriefSuggestion> selectOutpatientSuggestions(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);

    @Select("select id,patient_id,create_datetime,dwydjyhyj as suggestion, " +
            "patName=(select rtrim(d.name) from mz_patient_mi d " +
            "where d.patient_id=a.patient_id) " +
            "from t_outpatient_questionnaire_v2 a ${ew.customSqlSegment} " +
            "and dwydjyhyj is not null and LEN(dwydjyhyj)>5 order by id")
    List<BriefSuggestion> selectOutpatientSuggestionsV2(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);

    @Select("select id,inpatient_no as patientId,create_datetime,suggestion, " +
            "deptName=(select rtrim(e.name) from zd_unit_code e where e.code=" +
            "(select top 1 rtrim(d.zk_ward) from zy_inactpatient d where " +
            "d.inpatient_no=a.inpatient_no order by a.admiss_times desc)), " +
            "patName=(select rtrim(d.name) from mz_patient_mi d " +
            "where d.patient_id=a.patient_id) " +
            "from t_inpatient_satisfied_assessment a ${ew.customSqlSegment} " +
            "and dwydjyhyj is not null and LEN(dwydjyhyj)>5 order by id")
    List<BriefSuggestion> selectInpatientSuggestions(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);

    @Select("select id,inpatient_no as patientId,create_datetime,dwydjyhyj as suggestion, " +
            "deptName=(select rtrim(e.name) from zd_unit_code e where e.code=" +
            "(select top 1 rtrim(d.zk_ward) from zy_inactpatient d where " +
            "d.inpatient_no=a.inpatient_no order by a.times desc)), " +
            "patName=(select rtrim(d.name) from mz_patient_mi d " +
            "where d.patient_id=a.patient_id) " +
            "from t_inpatient_questionnaire_v2 a ${ew.customSqlSegment} " +
            "and dwydjyhyj is not null and LEN(dwydjyhyj)>5 order by id")
    List<BriefSuggestion> selectInpatientSuggestionsV2(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);

    @Select("select rtrim(name) as patName,patNo=#{patNo}, " +
            "phoneNo=(select isnull(d.home_tel,d.employer_tel) from a_patient_mi d " +
            "where d.inpatient_no=a.inpatient_no), " +
            "wardName=(select rtrim(d.name) from zd_unit_code d where d.code=a.zk_ward) " +
            "from ${table} a where inpatient_no=#{patNo} and admiss_times=#{times}")
    PatientBriefInfo selectInpatientBriefInfo(String table, String patNo, Integer times);

    @Select("select rtrim(name) as patName,rtrim(phone_no) as phoneNo,patNo=#{patNo} " +
            "from mz_patient_mi where patient_id=#{patNo}")
    PatientBriefInfo selectOutpatientBriefInfo(String patNo);

    @Select("select patNo=rtrim(inpatient_no),times=admiss_times,patName=rtrim(a.name), " +
            "disdate=convert(varchar(10),dis_date,21),disdept=b.name, " +
            "questionnaireCount=(select count(1) from t_inpatient_questionnaire_v2 d " +
            "where d.inpatient_no=a.inpatient_no and d.times=a.admiss_times " +
            "and d.create_datetime>=#{start} and d.create_datetime<=#{end}) " +
            "from zy_inactpatient a,zd_unit_code b where dis_date>=#{start} " +
            "and dis_date<=#{end} ${statement} and b.code=a.dis_ward")
    List<Patientinfo> getPatientinfoList(ClinicSatisfiedInquiry inquiry);
}
