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.zhuyuanyisheng.shoushu.OpRecord;

import java.util.Date;
import java.util.List;

@Mapper
public interface SurgeryArrangementDao {
    @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 code='',name='' union " +
            "select rtrim(code) as code,rtrim(name) as name from a_employee_mi " +
            "where dept_code=#{deptCode} and isnull(del_flag,0)!=1")
    List<CodeName> selectStaffsByDeptCode(String deptCode);

    @Select("select a.record_id,op_start_date,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.nurse_xh,b.nurse_qx,a.bed_no,a.arrangement_executed, " +
            "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, " +
            "a.preoperative_visit,a.preoperative_preparation " +
            "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, a.room_code")
    List<OpRecord> selectSurgeryArrangements(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);

    @Update("update op_record set status=#{status} where record_id=#{recordId}")
    void updateSurgeryStatus(Integer recordId, String status);

    @Update("update ${table} set ${column}=#{value} where record_id=#{id}")
    void updateArrangement(String table, String column, String value, Integer id);

    @Select("select count(1) from dj_user_role where user_code=#{code} and role_id=51")
    Integer selectArrangementPermission(String code);

    @Select("select rtrim(code_rs) from a_employee_mi where code in (#{code1},#{code2},#{code3},#{code4},#{code5},#{code6},#{code7})")
    List<String> selectCodeRsList(String code1, String code2, String code3, String code4, String code5, String code6,String code7);

    @Select("select name from op_zd_room where code=#{code}")
    String selectRoomName(String code);

    @Select("select a.record_id,a.patient_name,a.dept_code,a.op_scale,a.urgent_clinic_flag, " +
            "deptName=(select rtrim(d.name) from zd_unit_code d where d.code=a.dept_code), " +
            "a.apply_date,a.inpatient_no,a.admiss_times,b.doctor_zd,b.doctor_mz,b.nurse_qx,b.nurse_xh, " +
            "doctorZdName=(select rtrim(d.name) from a_employee_mi d where d.code=b.doctor_zd), " +
            "doctor1Name=(select rtrim(d.name) from a_employee_mi d where d.code=b.doctor_1), " +
            "doctor2Name=(select rtrim(d.name) from a_employee_mi d where d.code=b.doctor_2), " +
            "doctorMzName=(select rtrim(d.name) from a_employee_mi d where d.code=b.doctor_mz), " +
            "nurseXhName=(select rtrim(d.name) from a_employee_mi d where d.code=b.nurse_xh), " +
            "nurseQxName=(select rtrim(d.name) from a_employee_mi d where d.code=b.nurse_qx), " +
            "applyDocName=(select rtrim(d.name) from a_employee_mi d where d.code=a.apply_doc), " +
            "a.status,a.patient_type,a.op_code,a.op_name,a.op_datetime,a.hocus_code, " +
            "hocusName=(select rtrim(d.name) from zd_anaesthesia d where d.code=a.hocus_code) " +
            "from op_record a,op_record_join b " +
            "where a.op_datetime>=#{start} and a.op_datetime<=#{end} and a.record_id=b.record_id " +
            "and cast(a.room_code as int)<15 and a.status!='d' " +
            "order by a.op_datetime")
    List<OpRecord> selectOpRecordStatistics(Date start, Date end);
}
