package thyyxxk.webserver.dao.his.surgicalmanagement;

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.datamodify.GetDropdownBox;
import thyyxxk.webserver.entity.zhuyuanyisheng.shoushu.OpRecord;

import java.util.List;

@Mapper
public interface SurgeryArrangementDao {

    @Select("SELECT rtrim(ward_code) as code, rtrim(ward_name) as name, dept_code, dept_name " +
            "FROM zy_adtward " +
            "order by dept_code, ward_code")
    List<GetDropdownBox> getASurgicalWard();

    @Select("SELECT record_id=a.record_id," +
            "       op_start_date, " +
            "       op_end_date, " +
            "       anst_start_date," +
            "       anst_end_date, " +
            "       req_date = a.req_date, " +
            "       zy_serial_no=a.zy_serial_no, " +
            "       op_times=a.op_times, " +
            "       patient_name=a.patient_name, " +
            "       dept_code=a.dept_code, " +
            "       ward_code=a.ward_code, " +
            "       apply_date=a.apply_date, " +
            "       apply_user=a.apply_user, " +
            "       apply_doc=a.apply_doc, " +
            "       schedule_date=a.schedule_date, " +
            "       schedule_user=a.schedule_user, " +
            "       status=a.status, " +
            "       patient_type=a.patient_type, " +
            "       act_order_no=a.act_order_no, " +
            "       op_code=a.op_code, " +
            "       op_name=a.op_name, " +
            "       op_datetime=a.op_datetime, " +
            "       room_code=a.room_code, " +
            "       room_name=(select rtrim(name) from op_zd_room where op_zd_room.code = a.room_code), " +
            "       op_group_code=a.op_group_code, " +
            "       hocus_code=a.hocus_code, " +
            "       hocus_name = (select rtrim(name) from zd_anaesthesia where zd_anaesthesia.code = a.hocus_code), " +
            "       diag_before_op=a.diag_before_op, " +
            "       diag_after_op=a.diag_after_op, " +
            "       infection_flag=a.infection_flag, " +
            "       urgent_clinic_flag=a.urgent_clinic_flag, " +
            "       avocation_flag=a.avocation_flag, " +
            "       blood_amount=a.blood_amount, " +
            "       blood_type=a.blood_type, " +
            "       op_stastics_id=a.op_stastics_id, " +
            "       isolation_indicator=a.isolation_indicator, " +
            "       op_scale=a.op_scale, " +
            "       op_endtime=a.op_endtime, " +
            "       diag_before_code=a.diag_before_code, " +
            "       diag_after_code=a.diag_after_code, " +
            "       hbsag=a.hbsag, " +
            "       body_code=a.body_code, " +
            "       part_code=a.part_code, " +
            "       specialreq_code=a.specialreq_code, " +
            "       instrument_code=a.instrument_code, " +
            "       aspesis_scale=a.aspesis_scale, " +
            "       remark=a.remark, " +
            "       op_dept=a.op_dept, " +
            "       input_id=a.input_id, " +
            "       input_date=a.input_date, " +
            "       req_id=a.req_id, " +
            "       req_date=a.req_date, " +
            "       last_modify_id=a.last_modify_id, " +
            "       arrange_id=a.arrange_id, " +
            "       arrange_date=a.arrange_date, " +
            "       inpatient_no=a.inpatient_no, " +
            "       admiss_times=a.admiss_times, " +
            "       cancel_id=a.cancel_id, " +
            "       cancel_time=a.cancel_time, " +
            "       doctor_zd=b.doctor_zd, " +
            "       doctor_zd_name = (select rtrim(name) from a_employee_mi where a_employee_mi.code = doctor_zd), " +
            "       doctor_mz=b.doctor_mz, " +
            "       doctor_1 =b.doctor_1, " +
            "       doctor_2 =b.doctor_2, " +
            "       nurse_xh =b.nurse_xh, " +
            "       nurse_xh_name = (select rtrim(name) from a_employee_mi where a_employee_mi.code = nurse_xh), " +
            "       nurse_qx =b.nurse_qx, " +
            "       nurse_qx_name = (select rtrim(name) from a_employee_mi where a_employee_mi.code = nurse_qx), " +
            "       bed_no=a.bed_no, " +
            "       sex = case when isnull(c.sex, '') = '' then '' when c.sex = '1' then '男' else '女' end, " +
            "       sstc=a.sstc, " +
            "       age=case " +
            "               when c.birth_date is null then ' ' " +
            "               when datediff(year, c.birth_date, a.op_datetime) > 14 then " +
            "                       convert(char(2), datediff(year, c.birth_date, a.op_datetime)) + '岁' " +
            "               when datediff(dd, c.birth_date, a.op_datetime) >= 365 then " +
            "                       convert(char(2), datediff(dd, c.birth_date, a.op_datetime) / 365) + '岁' + " +
            "                       convert(char(2), ((datediff(dd, c.birth_date, a.op_datetime) / 30) % 12)) + '月' " +
            "               when datediff(dd, c.birth_date, a.op_datetime) < 30 then " +
            "                       convert(char(2), case datediff(dd, c.birth_date, a.op_datetime) " +
            "                                            when 0 then 1 " +
            "                                            else datediff(dd, c.birth_date, a.op_datetime) end) + '天' " +
            "               else convert(char(2), datediff(dd, c.birth_date, a.op_datetime) / 30) + '月' end, " +
            "       tw_flag=a.tw_flag, " +
            "       doctor_mz_ys=b.doctor_mz_ys, " +
            "       doctor_mz_ys_name=(select rtrim(name) from a_employee_mi where a_employee_mi.code = b.doctor_mz_ys), " +
            "       doctor_mz_hs=b.doctor_mz_hs, " +
            "       doctor_mz_hs_name=(select rtrim(name) from a_employee_mi where a_employee_mi.code = b.doctor_mz_hs), " +
            "       ssbc=a.ssbc, " +
            "       pd=(select max(e.admiss_times) " +
            "           from zy_detail_charge e " +
            "           where e.inpatient_no = a.inpatient_no " +
            "             and e.admiss_times = a.admiss_times " +
            "             and exec_unit = '1300000' " +
            "             and e.charge_date >= a.op_datetime " +
            "           group by e.inpatient_no, e.admiss_times), " +
            "       doctor_zd_sj=b.doctor_zd_sj " +
            "FROM op_record a, " +
            "     op_record_join b, " +
            "     a_patient_mi c " +
            "where a.record_id = b.record_id " +
            "  and a.inpatient_no = c.inpatient_no and " +
            " ${ew.sqlSegment} " +
            " order by a.urgent_clinic_flag desc ")
    List<OpRecord> huoQuShouShuAnPaiXinXi(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);


    @Update("update op_record " +
            "set op_start_date   = #{opStartDate}, " +
            "    op_end_date     = #{opEndDate}, " +
            "    anst_start_date = #{anstStartDate}, " +
            "    anst_end_date   = #{anstEndDate} " +
            "where record_id = #{recordId} " +
            "  and inpatient_no = #{inpatientNo}")
    void modifySurgeryTime(OpRecord pojo);
}
