package thyyxxk.webserver.dao.his.inpatient.casefrontsheet;

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.casefrontsheet.CaseFrontsheetSurgery;
import thyyxxk.webserver.entity.casefrontsheet.request.DismissedSheetInquiry;
import thyyxxk.webserver.entity.casefrontsheet.request.SheetOverview;
import thyyxxk.webserver.entity.casefrontsheet.response.BaOpLog;

import java.util.List;

@Mapper
public interface BasSelectOverviewDao extends BaseMapper<CaseFrontsheetSurgery> {

    @Select("select " +
            "fileStatus=0, " +
            "bedNo=rtrim(a.bed_no), " +
            "bah=rtrim(a.inpatient_no), " +
            "times=rtrim(a.admiss_times), " +
            "name=rtrim(a.name), " +
            "a.sex,a.admiss_date, " +
            "deptName=(select rtrim(name) from zd_unit_code with(nolock) where code=a.zk_ward), " +
            "doctorName=(select rtrim(name) from a_employee_mi with(nolock) where code=b.zyys), " +
            "a.dis_date, " +
            "unarcheivTimes=(select count(1) from ba_op_log d where " +
            "d.inpatient_no=a.inpatient_no and d.admiss_times=a.admiss_times and d.op_type=3) " +
            "from zy_inactpatient a with(nolock), batj_ba2 b with(nolock) " +
            "where " +
            "a.inpatient_no not like 'S%' and " +
            "a.inpatient_no not like 'JT%' and " +
            "a.zk_ward!='1090000' and " +
            "a.inpatient_no=#{bah} and " +
            "a.inpatient_no=b.zyh and " +
            "a.admiss_times=b.zycs and " +
            "a.dis_date>=#{start} and " +
            "a.dis_date<=#{end} and " +
            "isnull(a.file_status, 0)!=1 order by a.zk_ward,b.zyys")
    List<SheetOverview> selectPatientsForBasByBah(DismissedSheetInquiry param);

    @Select("select sign_date,file_status,bah,name, " +
            "admiss_times as times,admiss_date,sex,zy_dismiss_way, " +
            "dismiss_dept as deptName,admiss_doctor_name as doctorName,late_flag, " +
            "lateDays=datediff(day, dismiss_date, sign_date), " +
            "unarcheivTimes=(select count(1) from ba_op_log d where " +
            "d.inpatient_no=a.bah and d.admiss_times=a.admiss_times and d.op_type=3), " +
            "dismiss_date as disDate from t_case_frontsheet_main a with(nolock) " +
            "where bah=#{bah} and file_status=#{fileStatus} and dismiss_date>=#{start} and " +
            "dismiss_date<=#{end} order by admiss_dept,admiss_doctor")
    List<SheetOverview> selectPatientsFromSignedBase(DismissedSheetInquiry param);

    @Select("select " +
            "fileStatus=0, " +
            "bedNo=rtrim(a.bed_no), " +
            "bah=rtrim(a.inpatient_no), " +
            "times=rtrim(a.admiss_times), " +
            "name=rtrim(a.name), " +
            "a.sex,a.admiss_date, " +
            "deptName=(select rtrim(name) from zd_unit_code with(nolock) where code=a.zk_ward), " +
            "doctorName=(select rtrim(name) from a_employee_mi with(nolock) where code=b.zyys), " +
            "lateCount=(select count(1) from t_case_frontsheet_main with(nolock) where late_flag=1 and admiss_doctor=b.zyys), " +
            "unarcheivTimes=(select count(1) from ba_op_log d " +
            "where d.inpatient_no=a.inpatient_no and d.admiss_times=a.admiss_times and d.op_type=3), " +
            "a.dis_date " +
            "from zy_inactpatient a with(nolock), batj_ba2 b with(nolock) " +
            "where " +
            "a.inpatient_no not like 'S%' and " +
            "a.inpatient_no not like 'JT%' and " +
            "a.inpatient_no=b.zyh and " +
            "a.admiss_times=b.zycs and " +
            "a.dis_date>=#{start} and " +
            "a.dis_date<=#{end} and " +
            "a.zk_ward=#{ward} and " +
            "isnull(a.file_status, 0)!=1 and " +
            "charindex('$',a.inpatient_no)=0 order by a.zk_ward,b.zyys")
    List<SheetOverview> selectPatientsForBasByWard(DismissedSheetInquiry param);

    @Select("select sign_date,file_status,bah,admiss_times as times,zy_dismiss_way, " +
            "name,sex,admiss_date,dismiss_dept as deptName,admiss_doctor_name as doctorName, " +
            "late_flag,lateDays=datediff(day,dismiss_date,sign_date), " +
            "unarcheivTimes=(select count(1) from ba_op_log d " +
            "where d.inpatient_no=a.bah and d.admiss_times=a.admiss_times and d.op_type=3), " +
            "dismiss_date as disDate from t_case_frontsheet_main a with(nolock) " +
            "where dismiss_date>=#{start} and dismiss_date<=#{end} and file_status=#{fileStatus} " +
            "and dismiss_dept_code=#{ward} order by dismiss_dept, admiss_doctor")
    List<SheetOverview> selectPatientsForBasByWard2(DismissedSheetInquiry param);

    @Select("select " +
            "fileStatus=0, " +
            "bedNo=rtrim(a.bed_no), " +
            "bah=rtrim(a.inpatient_no), " +
            "times=rtrim(a.admiss_times), " +
            "name=rtrim(a.name), " +
            "a.sex,a.admiss_date, " +
            "deptName=(select rtrim(name) from zd_unit_code with(nolock) where code=a.zk_ward), " +
            "doctorName=(select rtrim(name) from a_employee_mi with(nolock) where code=b.zyys), " +
            "a.dis_date, " +
            "unarcheivTimes=(select count(1) from ba_op_log d " +
            "where d.inpatient_no=a.inpatient_no and d.admiss_times=a.admiss_times and d.op_type=3) " +
            "from zy_inactpatient a with(nolock), batj_ba2 b with(nolock) " +
            "where " +
            "a.inpatient_no not like 'S%' and " +
            "a.inpatient_no not like 'JT%' and " +
            "a.zk_ward!='1090000' and " +
            "a.inpatient_no=b.zyh and " +
            "a.admiss_times=b.zycs and " +
            "a.dis_date>=#{start} and " +
            "a.dis_date<=#{end} and " +
            "isnull(a.file_status, 0)!=1 and " +
            "charindex('$',a.inpatient_no)=0 order by a.zk_ward,b.zyys")
    List<SheetOverview> selectPatientsForBasByFileStatus(DismissedSheetInquiry param);

    @Select("select sign_date,file_status,bah,admiss_times as times,zy_dismiss_way," +
            "name,sex,admiss_date,dismiss_dept as deptName,admiss_doctor_name as doctorName," +
            "late_flag,lateDays=datediff(day, dismiss_date, sign_date),dismiss_date as disDate, " +
            "unarcheivTimes=(select count(1) from ba_op_log d " +
            "where d.inpatient_no=a.bah and d.admiss_times=a.admiss_times and d.op_type=3) " +
            "from t_case_frontsheet_main a with(nolock) " +
            "where dismiss_date>=#{start} and dismiss_date<=#{end} and " +
            "file_status=#{fileStatus} order by trans_dept,admiss_doctor")
    List<SheetOverview> selectPatientsForBasByFileStatus2(DismissedSheetInquiry param);

    @Update("update zy_actpatient set file_status=#{status} where inpatient_no=#{bah} and admiss_times=#{times};" +
            "update zy_inactpatient set file_status=#{status} where inpatient_no=#{bah} and admiss_times=#{times};")
    void updateActFileStatus(@Param("bah") String bah, @Param("times") int times, @Param("status") int status);

    @Select("select op_type,op_id,op_time, " +
            "opTypeName=case when op_type=1 then '保存' when op_type=2 then '签收' " +
            "when op_type=3 then '解除签收' else '打印' end, " +
            "opName=(select rtrim(d.name) from a_employee_mi d where d.code=op_id) " +
            "from ba_op_log where inpatient_no=#{bah} and admiss_times=#{times} " +
            "order by op_time desc")
    List<BaOpLog> getBaOpLogs(String bah, Integer times);

    @Select("select major_error from t_frontsheet_quality_verification " +
            "where pat_no=#{bah} and times=#{times} and major_error is not null")
    List<String> getMajorErrors(String bah, Integer times);
}
