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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
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.outpatient.thmz.MzEmrPatientData;
import thyyxxk.webserver.entity.outpatient.thmz.MzEmrPatientVo;
import thyyxxk.webserver.entity.zhuyuanyisheng.emr.EmrFolder;

import java.util.List;

/**
 * @Description:
 * @Author:lihong
 * @Date: 2023/10/18
 */
@Mapper
public interface MzEmrDao extends BaseMapper<MzEmrPatientData> {
    @Select(" select top 1 rtrim(name) name , case when sex = 1 then '男' when sex = 2 then '女' else '未知' end sex   ,age,birth_day,social_no from mz_patient_mi   where patient_id =#{patientId}")
    MzEmrPatientVo selectMzEmrPatientVo(MzEmrPatientVo param);

    @Select(" select top 1 rtrim(inpatient_no) inpatient_no from a_patient_mi where mz_no=  #{patientId} ")
    String selectPatNo(String patientId);

    @Select("select id as emr_document_id, " +
            "       name as emr_name, name," +
            "       'This is Dir' as emr_category_code," +
            "       parent, sort " +
            "from emr_folder where folder_name = 'OUT_HOSPITAL' ")
    List<MzEmrPatientData> getMzEmrFolder();

    @Select("select *, (select rtrim(name) from a_employee_mi where code = create_id) create_name" +
            " from mz_emr_patient_data ${ew.customSqlSegment}")
    List<MzEmrPatientData> selectMyList(@Param("ew") LambdaQueryWrapper<MzEmrPatientData> ew);


    @Update("update mz_emr_patient_data set del_flag = 0 where emr_document_id = #{id}")
    int restoreMedicalRecords(String id);


    @Update("update mz_visit_list\n" +
            "set visit_status = 1 , visit_end_time = getdate()\n" +
            "where patient_id = #{patientNo}\n" +
            "  and times = #{times}\n" +
            "  and visit_doctor = #{visitDoctor} and visit_status = 0")
    int endOfVisit(String patientNo, Integer times, String visitDoctor);
}
