package thyyxxk.webserver.dao.his.casefrontsheet;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.casefrontsheet.JieShouHuiZhenPojo;

/**
 * <p>
 * 描述： 接收会诊申请
 * </p>
 *
 * @author xc
 * @date 2021-07-27 10:29
 */
@Mapper
public interface JieShouHuiZhenDao {

    @Select("select bed_no,name,a.admiss_times,a.inpatient_no,req_times,hz_type," +
            "dept_code_name =   (select rtrim(name) from zd_unit_code with (nolock) where code = dept_code), " +
            "req_dept1_name =   (select rtrim(name) from zd_unit_code with (nolock) where code = req_dept1) " +
            "from ysh_hz_record a with(nolock) " +
            " inner join zy_actpatient b with(nolock) on " +
            "    (b.inpatient_no = a.inpatient_no)" +
            " ${ew.customSqlSegment} ")
    Page<JieShouHuiZhenPojo> getHuiZhenData(Page<JieShouHuiZhenPojo> page,
                                            @Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);

    @Select("select a.inpatient_no, " +
            "       b.admiss_date, " +
            "       rtrim(name)                                                                                          name, " +
            "       bed_no, " +
            "       a.admiss_times, " +
            "       sex, " +
            "       req_date, " +
            "       (select rtrim(name) name from zd_unit_code with (nolock) where code = req_dept1)                     req_dept1, " +
            "       (select birth_date from a_patient_mi with (nolock) where a.inpatient_no = a_patient_mi.inpatient_no) birth_date, " +
            "       (select rtrim(name) name from zd_unit_code with (nolock) where code = req_dept2)                     req_dept2, " +
            "       req_times, " +
            "       (select rtrim(name) name from a_employee_mi with (nolock) where input_id = code)                     req_doctor, " +
            "       hz_type, " +
            "       (select rtrim(name) name " +
            "        from zd_unit_code with (nolock) " +
            "        where code = dept_code)                                                                             dept_code_name, " +
            "       req_comment, " +
            "       hz_zd, " +
            "       hz_md, " +
            "       hz_comment, " +
            "       (select rtrim(name) name " +
            "        from a_employee_mi with (nolock) " +
            "        where code = hz_doctor2)                                                                            hz_doctor2_name, " +
            "       hz_doctor2, " +
            "       hz_date  " +
            "from ysh_hz_record a with(nolock) inner join zy_actpatient b with(nolock) on   " +
            "(b.inpatient_no = a.inpatient_no) where a.admiss_times = #{admissTimes} and a.inpatient_no = #{inpatientNo} and a.req_times = #{reqTimes}")
    JieShouHuiZhenPojo getHuanZheXinXi(@Param("admissTimes") Integer admissTimes,
                                       @Param("reqTimes") Integer reqTimes,
                                       @Param("inpatientNo") String inpatientNo);

    @Update("<script>" +
            "update ysh_hz_record " +
            "<trim prefix = 'set' suffixOverrides=','>" +
            " status_flag = #{statusFlag} , hz_comment = #{hzComment}," +
            "<if test=\"statusFlag == 2 \">" +
            "hz_date = getdate() , hz_id = hz_doctor1 , hz_doctor2 = #{hzDoctor2} " +
            "</if> " +
            "</trim>" +
            "where admiss_times = #{admissTimes} and inpatient_no = #{inpatientNo} and req_times = #{reqTimes}" +
            "</script>")
    void wanChenHuiZhen(JieShouHuiZhenPojo param);

    @Select("select name from zd_unit_code with(nolock) where code = #{deptCode}")
    String getDeptName(String deptCode);
}
