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.dictionary.CodeName;
import thyyxxk.webserver.entity.surgeryarrangement.BriefOpRecord;
import thyyxxk.webserver.entity.zhuyuanyisheng.shoushu.OpRecord;

import java.util.List;

@Mapper
public interface SurgeryRecordDao {
    @Select("select rtrim(ward_code) as code, rtrim(ward_name) as name from zy_adtward")
    List<CodeName> getAllSurgicalWard();

    @Select("select code,name,status_flag from op_zd_room")
    List<CodeName> selectSurgeryRooms();

    @Select("select rtrim(code) as code, rtrim(name) as name from zd_anaesthesia")
    List<CodeName> selectAnstWays();

    @Select("select code,name from op_zd_part_new")
    List<CodeName> selectSurgeryParts();

    @Select("select rtrim(code) as code,rtrim(name) as name from a_employee_mi " +
            "where dept_code='1300000' and isnull(del_flag,0)!=1")
    List<CodeName> selectSurgeryRoomStaffs();

    @Select("select record_id,inpatient_no as patNo,admiss_times as times,ward_code,op_name, " +
            "patient_name as patName from op_record where ${ew.sqlSegment} order by room_code")
    List<BriefOpRecord> selectBriefOpRecordList(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);

    @Select("select a.record_id,opStartDate=isnull(op_start_date,op_datetime), " +
            "op_end_date,anst_start_date,anst_end_date,a.sstc,a.req_date, " +
            "a.zy_serial_no,a.op_times,a.patient_name,a.dept_code,a.ward_code,a.apply_date,a.apply_user,a.apply_doc, " +
            "a.schedule_date,a.schedule_user,a.status,a.patient_type,a.act_order_no,a.op_code,a.op_name,a.op_datetime, " +
            "a.room_code,room_name=(select rtrim(d.name) from op_zd_room d where d.code=a.room_code),a.op_group_code, " +
            "a.hocus_code,hocus_name=(select rtrim(d.name) from zd_anaesthesia d where d.code=a.hocus_code), " +
            "a.diag_before_op,a.diag_after_op,a.infection_flag,a.urgent_clinic_flag,a.avocation_flag,a.blood_amount, " +
            "a.blood_type,a.op_stastics_id,a.isolation_indicator,a.op_scale,a.op_endtime,a.diag_before_code, " +
            "a.diag_after_code,a.hbsag,a.body_code,a.part_code,a.specialreq_code,a.instrument_code,a.aspesis_scale, " +
            "a.remark,a.op_dept,a.input_id,a.input_date,a.req_id,a.req_date,a.last_modify_id,a.arrange_id, " +
            "a.arrange_date,a.inpatient_no,a.admiss_times,a.cancel_id,a.cancel_time,b.doctor_zd,b.doctor_mz, " +
            "b.doctor_1,b.doctor_2,b.doctor_3,b.nurse_xh,b.nurse_qx,a.bed_no, " +
            "chargeSum=(select sum(d.charge_amount) from zy_detail_charge d where " +
            "d.inpatient_no=a.inpatient_no and d.ss_code=a.record_id), " +
            "sex=case when c.sex='1' then '男' else '女' end, " +
            "age=case when c.birth_date is null then '' " +
            "when datediff(year, c.birth_date, a.op_datetime) > 14 then " +
            "convert(varchar(2), datediff(year, c.birth_date, a.op_datetime)) + '岁' " +
            "when datediff(dd, c.birth_date, a.op_datetime) >= 365 then " +
            "convert(varchar(2), datediff(dd, c.birth_date, a.op_datetime) / 365) + '岁' + " +
            "convert(varchar(2), ((datediff(dd, c.birth_date, a.op_datetime) / 30) % 12)) + '月' " +
            "when datediff(dd, c.birth_date, a.op_datetime) < 30 then " +
            "convert(varchar(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(varchar(2), datediff(dd, c.birth_date, a.op_datetime) / 30) + '月' end, " +
            "a.tw_flag,b.doctor_mz_ys,b.doctor_mz_hs,rtrim(a.ssbc) as ssbc,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 a.record_id=#{recordId}")
    OpRecord selectOpRecord(Integer recordId);

    @Update("update op_record set urgent_clinic_flag=#{urgentClinicFlag},room_code=#{roomCode},apply_doc=#{applyDoc}, " +
            "op_code=#{opCode},op_name=#{opName},hocus_code=#{hocusCode},diag_before_code=#{diagBeforeCode}, " +
            "diag_before_op=#{diagBeforeOp},diag_after_code=#{diagAfterCode},diag_after_op=#{diagAfterOp}, " +
            "op_datetime=#{opStartDate},op_start_date=#{opStartDate},op_end_date=#{opEndDate},anst_start_date=#{anstStartDate}, " +
            "anst_end_date=#{anstEndDate},op_scale=#{opScale},part_code=#{partCode},body_code=#{bodyCode},status=#{status}, " +
            "blood_amount=#{bloodAmount},blood_type=#{bloodType},hbsag=#{hbsag},infection_flag=#{infectionFlag}, " +
            "isolation_indicator=#{isolationIndicator},remark=#{remark} where record_id=#{recordId} ; " +
            "update op_record_join set doctor_zd=#{doctorZd},doctor_1=#{doctor1},doctor_2=#{doctor2},doctor_3=#{doctor3}, " +
            "doctor_mz_ys=#{doctorMzYs},nurse_xh=#{nurseXh},nurse_qx=#{nurseQx} where record_id=#{recordId}")
    void updateRecord(OpRecord record);
}
