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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.casefrontsheet.JieShouHuiZhenPojo;
import thyyxxk.webserver.entity.executeItem.YzZyPatientFee;

import java.math.BigDecimal;
import java.util.List;

/**
 * <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 = a.dept_code), " +
            "req_dept1_name =   (select rtrim(name) from zd_unit_code with (nolock) where code = req_dept1)," +
            " admiss_date " +
            "from ysh_hz_record a with(nolock) " +
            " inner join view_zy_patient_all b with(nolock) on " +
            "    (b.inpatient_no = a.inpatient_no and a.admiss_times = b.admiss_times),view_yz_all_order c " +
            " ${ew.customSqlSegment} and a.act_order_no = c.act_order_no " +
            "  and c.status_flag not in ('1', '2')")
    List<JieShouHuiZhenPojo> getHuiZhenData(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);


    /**
     * 根据职称获取医嘱的编码
     *
     * @param code 职称
     * @return 医嘱码
     */
    @Select("SELECT order_code FROM zd_emp_title where code = #{code} ")
    String getOrderCodeByEmpTit(String code);

    @Select("select  rtrim(a.inpatient_no) inpatient_no ,a.admiss_times, " +
            "       b.admiss_date, " +
            "       rtrim(name)                                                                                          name, " +
            "       bed_no, " +
            "       a.admiss_times, " +
            "       sex, " +
            "       req_date," +
            "       req_dept1,input_id, " +
            "       (select rtrim(name) name from zd_unit_code with (nolock) where code = req_dept1)                     req_dept1_name, " +
            "       (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," +
            "       hz_doctor2,status_flag, " +
            "       (select rtrim(name) name " +
            "        from a_employee_mi with (nolock) " +
            "        where code = hz_doctor2)                                                                            hz_doctor2_name, " +
            "       hz_doctor2, " +
            "       hz_date,dept_code," +
            "       hz_level," +
            "       hz_level_name = (select rtrim(name) from ysh_zd_hz_level where code = hz_level)," +
            "       cast(act_order_no AS decimal)  as act_order_no " +
            "from ysh_hz_record a with(nolock) inner join view_zy_patient_all b with(nolock) on   " +
            "(b.inpatient_no = a.inpatient_no  and b.admiss_times = a.admiss_times)  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},hz_type = #{hzType}," +
            "<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);

    @Update("update yz_act_order set order_code = #{code} where act_order_no = #{orderNo} ")
    void updateOrderConsultationCode(String code, BigDecimal orderNo);

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

    @Select("select top 1 occ_code\n" +
            "from yz_order_occurence\n" +
            "where order_code = '${orderCode}';")
    String getOccCode(String orderCode);

    @Insert("insert into yz_zy_patient_fee (inpatient_no, admiss_times, ledger_sn, detail_sn, charge_date, op_id_code, charge_code,\n" +
            "                               infant_flag, charge_status, charge_fee, occ_time, act_order_no, dept_code, ward_code,\n" +
            "                               order_count, exec_unit)\n" +
            "values (#{inpatientNo}, #{admissTimes}, NULL, 0, #{chargeDate}, #{opIdCode}, #{chargeCode},\n" +
            "        #{infantFlag}, '1', (SELECT charge_amount FROM zd_charge_item where code = '${chargeCode}'), getdate(), #{actOrderNo},\n" +
            "        #{deptCode}, #{deptCode},\n" +
            "        1, #{execUnit})")
    void insertYzZyPatientFee(YzZyPatientFee fee);

    @Select("select count(1)\n" +
            "from zy_detail_charge\n" +
            "where inpatient_no = #{patNo}\n" +
            "  and admiss_times = #{times}\n" +
            "  and order_no = #{order} ")
    int orderFeeCount(String patNo, Integer times, BigDecimal order);

    @Select("select count(1)\n" +
            "from yz_zy_patient_fee\n" +
            "where inpatient_no = #{patNo}\n" +
            "  and admiss_times = #{times}\n" +
            "  and act_order_no = #{order} ")
    int feeCount(String patNo, Integer times, BigDecimal order);


}
