package thyyxxk.webserver.dao.his.zhuyuanyisheng;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.casefrontsheet.YshHzRecord;
import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
import thyyxxk.webserver.entity.datamodify.SelectValueObject;
import thyyxxk.webserver.entity.zhuyuanyisheng.jianyanjiancha.*;
import thyyxxk.webserver.entity.zhuyuanyisheng.yizhuluru.PatientTemp;

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

/**
 * <p>
 * 描述： 检查申请
 * </p>
 *
 * @author xc
 * @date 2022-02-10 15:42
 */
@Mapper
public interface JianYanJianChaDao {


    @Select("select order_code code, " +
            "       rtrim(order_name) value " +
            "from ysh_yj_req with (NOLOCK) " +
            "where inpatient_no = #{patNo} " +
            "  and admiss_times = #{times} " +
            "  and bed_no = #{bedNo} " +
            "  and order_name like #{name} " +
            "  and req_type = #{reqType}")
    List<Map<String, String>> huoQuJianJianChaMing(@Param("patNo") String patNo,
                                                   @Param("times") Integer times,
                                                   @Param("bedNo") String bedNo,
                                                   @Param("name") String name,
                                                   @Param("reqType") String reqType);


    @Select("<script>" +
            "select order_name,req_no,act_order_no, start_time,inpatient_no,admiss_times,receive_flag " +
            "from ysh_yj_req with (NOLOCK) " +
            "where inpatient_no = #{patNo}" +
            "  and admiss_times = #{times} " +
            "  and req_type = '3' " +
            "<if test=\" orderName != null and orderName != '' \">" +
            " and order_name like #{orderName} " +
            "</if>" +
            "<if test=\"startTime != null and startTime != '' \">" +
            " and start_time &gt;= #{startTime} and start_time &lt;= #{endTime}" +
            "</if>" +
            "</script>")
    IPage<YshYjReq> huoQuJianChaShenQing(IPage<YshYjReq> page,
                                         @Param("patNo") String patNo,
                                         @Param("times") Integer times,
                                         @Param("orderName") String orderName,
                                         @Param("startTime") String startTime,
                                         @Param("endTime") String endTime);


    @Select("select *, " +
            "       req_dept_name   = (select rtrim(name) from zd_unit_code where code = req_dept), " +
            "       req_doctor_name = (select rtrim(name) from a_employee_mi where code = req_doctor) " +
            "from ysh_yj_req with (NOLOCK) " +
            "where req_no = #{reqNo}")
    YshYjReq huoQuShengQingXiangQing(@Param("reqNo") Integer reqNo);

    @Select("select sex, " +
            "       home_street," +
            "       home_tel " +
            "from a_patient_mi with (NOLOCK) " +
            "where inpatient_no = #{patNo}")
    PatientTemp huoQuHuanZheXinXi(@Param("patNo") String patNo);

    @Select("select responce_type_name = (select rtrim(name) from zy_zd_responce_type where code = responce_type) " +
            "from zy_actpatient with (NOLOCK) " +
            "where inpatient_no = #{patNo}")
    String huoQuHuanZheShenFen(@Param("patNo") String patNo);

    @Select("select rtrim(comm) comm, " +
            "       comment1, " +
            "       comment2, " +
            "       comment3, " +
            "       comment4, " +
            "       comment5, " +
            "       comment6, " +
            "       comment7, " +
            "       comment8 " +
            "from jc_zd_class with (NOLOCK) " +
            "where code = #{code}")
    JcZdClass daYingXiangQing(@Param("code") String code);


    @Select("select top 10 * " +
            "from ( " +
            "         SELECT       ROW_NUMBER() over (order by a.code ) rowIndex, " +
            "             code   = rtrim(a.code), " +
            "             name   = isnull(d.name + '(' + rtrim(a.name) + ')', rtrim(a.name)), " +
            "             bw_code= isnull(d.code, 0) " +
            "         FROM jc_zd_class a with (NOLOCK), " +
            "              yz_order_item b with (NOLOCK), " +
            "              jc_zd_item c with (NOLOCK), " +
            "              ysh_zd_part_code d with (NOLOCK) " +
            "         where a.code = b.order_type " +
            "                   and isnull(b.del_flag, '') <> '1' " +
            "                   and c.bw_code *= d.code " +
            "                   and a.code = c.class " +
            "         group by a.name, d.code, d.name, a.code " +
            "     ) temp " +
            "where rowIndex > (${currentPage} - 1) * 10")
    List<JcZdClass> jianChaMing(long currentPage);


    @Select("select count(*) " +
            "from ( " +
            "         SELECT a.name, d.code, d.name d_name, a.code a_code " +
            "         FROM jc_zd_class a with (NOLOCK), " +
            "              yz_order_item b with (NOLOCK), " +
            "              jc_zd_item c with (NOLOCK), " +
            "              ysh_zd_part_code d with (NOLOCK) " +
            "         where a.code = b.order_type " +
            "                   and isnull(b.del_flag, '') <> '1' " +
            "                   and c.bw_code *= d.code " +
            "                   and a.code = c.class " +
            "         group by a.name, d.code, d.name, a.code " +
            "     ) temp")
    long jianChaMingTiaoShu();

    @Select("<script>" +
            "SELECT tc_name name, " +
            "       tc_no   code " +
            "FROM zy_tc_yj with (NOLOCK) " +
            "where isnull(del_flag, '0') &lt;&gt; '1' " +
            "  and req_type = #{reqType} " +
            "<if test=\"leiXing == 2\">" +
            "  AND op_id = #{userCode}  " +
            "</if>" +
            "<if test=\"leiXing == 3\">" +
            "  AND tc_exec = (select dept_code from a_employee_mi where code = #{userCode}) and order_type = '2' " +
            "</if>" +
            "order by isnull(sort_no, 0), tc_no" +
            "</script>")
    Page<JcZdClass> jianChaJianYanMuBan(IPage<JcZdClass> page,
                                        @Param("leiXing") Integer leiXing,
                                        @Param("userCode") String userCode,
                                        @Param("reqType") String reqType);

    @Select("SELECT rtrim(a.order_code) orderCode, " +
            "       rtrim(a.order_name) orderName, " +
            "       rtrim(a.yb_comment) ybComment, " +
            "       checkFlag='0', " +
            "       a.order_type AS orderType," +
            "       a.exec_dept as execDept," +
            "       (select rtrim(name) from zd_unit_code where code = a.exec_dept) execDeptName," +
            "       getdate() as startTime " +
            "FROM yz_order_item a with (NOLOCK), " +
            "     jc_zd_item b with (NOLOCK), " +
            "     ysh_zd_part_code c with (NOLOCK) " +
            "where a.order_type = #{orderType} " +
            "          and isnull(b.bw_code, 0) = #{bwCode} and " +
            "      isnull(a.del_flag, '') <> '1' and " +
            "      a.order_code = b.zy_order_code and b.bw_code *= c.code " +
            "order by a.order_name")
    List<Map<String, String>> chaKanFenLeiXiangQing(@Param("orderType") String orderType,
                                                    @Param("bwCode") String bwCode);


    @Select("SELECT         rtrim(a.order_code)                                             orderCode, " +
            "               rtrim(a.order_name)                                             orderName, " +
            "               rtrim(a.yb_comment)                                             ybComment, " +
            "    checkFlag='0', " +
            "               a.exec_dept   as                                                execDept, " +
            "               (select rtrim(name) from zd_unit_code with (NOLOCK) where code = a.exec_dept) execDeptName, " +
            "               getdate()     as                                                startTime, " +
            "               b.sample_code as                                                inspectStuff," +
            "    (select name from jy_zd_sample with (NOLOCK) where jy_zd_sample.code = b.sample_code) as inspectStuffName " +
            "FROM yz_order_item a with (NOLOCK), " +
            "     jy_zd_item b with (NOLOCK) " +
            "where jy_type = #{code} " +
            "  and isnull(a.del_flag, '') <> '1' " +
            "  and a.order_code = b.zy_order_code " +
            "order by order_name")
    List<Map<String, String>> chaKanJianYanXiangQing(@Param("code") String code);

    @Select("select op_id,tc_exec " +
            "from zy_tc_yj with (NOLOCK) " +
            "where tc_no = #{code}")
    ZyTcYj jianChaLuRuRen(String code);

    @Delete("delete zy_tc_yj " +
            "where tc_no = #{code}; " +
            "delete zy_tc_detail_yj " +
            "where tc_no = #{code};")
    void shanChuJianChaMuBan(String code);


    @Select("SELECT rtrim(a.order_code) as                                          orderCode, " +
            "       rtrim(a.order_name) as                                          orderName, " +
            "       rtrim(a.yb_comment)                                             ybComment, " +
            "       '0'                 as                                          checkFlag, " +
            "       a.exec_dept         as                                          execDept, " +
            "       a.order_type        as                                          orderType, " +
            "       (select rtrim(name) from zd_unit_code with (NOLOCK) where code = a.exec_dept) execDeptName, " +
            "       getdate()           as                                          startTime," +
            "       inspect_stuff as inspectStuff , " +
            "       inspectStuffName = (select rtrim(name) from jy_zd_sample with (NOLOCK) where code = inspect_stuff) " +
            "FROM yz_order_item a with (NOLOCK), " +
            "     zy_tc_detail_yj b with (NOLOCK), " +
            "     zy_tc_yj c with (NOLOCK) " +
            "where b.tc_no = c.tc_no " +
            "  and b.tc_no = #{code} " +
            "  and c.req_type = #{reqType} " +
            "  and a.order_code = b.order_code " +
            "  and isnull(a.del_flag, '') <> '1' ")
    List<Map<String, String>> chaKanMuBanNeiRong(@Param("code") String code,
                                                 @Param("reqType") String reqType);


    @Select("<script>" +
            "SELECT jc_text,name,code,wh_flag,op_id " +
            "FROM ysh_zd_yj_template with (NOLOCK) " +
            "WHERE (py_code like #{name} or d_code like #{name} or name like #{name}) " +
            "<if test=\"leiXing == 1\">" +
            "  and op_id = #{userCode} " +
            "</if>" +
            "<if test=\"leiXing == 2\">" +
            "  and dept_code = (select rtrim(dept_code) from a_employee_mi where code = #{userCode}) and wh_flag = '2' " +
            "</if>" +
            "<if test=\"leiXing == 3\">" +
            "  and wh_flag = '3' " +
            "</if>" +
            "</script>")
    Page<YshZdYjTemplate> huoQuShuoMingMuBan(IPage<YshZdYjTemplate> page,
                                             @Param("name") String name,
                                             @Param("leiXing") Integer leiXing,
                                             @Param("userCode") String userCode);

    @Select("select * from ysh_zd_yj_template with (NOLOCK) where code = #{code}")
    YshZdYjTemplate muBanShuJu(String code);

    @Update("update ysh_zd_yj_template " +
            "set jc_text  = #{jcText}, " +
            "    name     = #{name}, " +
            "    py_code  = #{pyCode}, " +
            "    d_code   = #{dCode}, " +
            "    sys_date = getdate() " +
            "where code = #{code} ")
    void genXingShouMingMuBan(@Param("jcText") String jcText,
                              @Param("name") String name,
                              @Param("pyCode") String pyCode,
                              @Param("dCode") String dCode,
                              @Param("code") String code);

    @Delete("delete from ysh_zd_yj_template where code = #{code} ")
    void shanChuShouMingMuBan(String code);

    @Insert("insert into ysh_zd_yj_template (code, name, py_code, d_code, jc_text, jc_type, dept_code, op_id, sys_date, wh_flag) " +
            "values (#{code}, #{name}, #{pyCode}, #{dCode}, #{jcText}, 3, " +
            "(select rtrim(a_employee_mi.dept_code) from a_employee_mi where a_employee_mi.code = #{opId}), #{opId}, getdate(), #{whFlag})")
    void xinZengShouMingMuBan(YshZdYjTemplate param);


    @Select("select rtrim(yb_code) as code, rtrim(yb_name) as name " +
            "from zd_icd_code_new with (NOLOCK) " +
            "where (yb_code like #{name} or yb_name like #{name} or yb_py_code like #{name})")
    List<GetDropdownBox> yiBaoZhenDuan(String name);

    @Select("SELECT rtrim(a.code) as code, " +
            "       rtrim(a.name) as name " +
            "FROM zd_icd_code a with (NOLOCK) " +
            "where (a.py_code like #{name} " +
            "    or a.code like #{name} " +
            "    or a.name like #{name}) " +
            "  and isnull(a.del_flag, '') <> '1'")
    List<GetDropdownBox> benYuanLinChuangZhenDuan(String name);

    @Select("select tc_no " +
            "from zy_tc_yj with (NOLOCK) " +
            "where op_id = #{code} " +
            "  and tc_name = #{name}" +
            "  and req_type = #{reqType} ")
    List<String> huoQuJianChaJianYanChongFuMuBanMing(@Param("name") String name,
                                                     @Param("code") String code,
                                                     @Param("reqType") String reqType);

    /**
     * 插入检查或检验模板
     */
    @Insert("INSERT INTO zy_tc_yj (tc_no, tc_name, tc_exec, del_flag, py_code, d_code, n_code, order_type/*1个人 2-科室*/,op_id, " +
            "                                    req_type, tc_flag/*录入方式 1-手工选择 0-自动选择*/, create_date, sort_no) " +
            "VALUES (#{tcNo}, #{tcName}, #{tcExec}, N'0',#{pyCode}, #{dCode}, null, #{orderType}, #{opId}, #{reqType}, #{tcFlag}, getdate(), #{sortNo});")
    void chaRuJCJYMuBan(ZyTcYj param);

    @Insert("<script>" +
            "insert into zy_tc_detail_yj (tc_no, order_code,  inspect_part, inspect_stuff, item_no) " +
            "values " +
            "<foreach collection='list' item='item' index='index' separator=','>" +
            "(#{tcNo}, #{item.orderCode},  " +
            "(select bw_code " +
            "from jc_zd_item " +
            "where zy_order_code = #{item.orderCode}) , #{item.inspectStuff}" +
            ", ${index} + 1)" +
            "</foreach>" +
            "</script>")
    void chaRuJCJYMuBanNeiRong(List<YshHzRecord> list,
                               @Param("tcNo") String tcNo,
                               @Param("reqType") String reqType);

    @Insert("<script>" +
            "insert into ysh_yj_req (req_no, inpatient_no, p_name, p_age, admiss_times, bed_no, group_name/* order_name一样*/, " +
            "                        act_order_no, order_code, order_name, page_no/* req_no */, req_comment, req_doctor, " +
            "                        req_dept, req_ward, exec_dept, req_date,/*getdate*/ " +
            "                        receive_flag/*1*/, req_type/*2检验3检查*/, sys_date/*getdate*/, start_time/*用户选择的*/, diag_code, " +
            "                        diag_text, " +
            "                        order_type, jz_flag/*1 和 null*/, req_tz_comment/*体征信息*/, req_other_result/*相关辅检结果*/," +
            "inspect_stuff) " +
            "values " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.reqNo}, #{inpatientNo}, #{name}, #{age}, #{admissTimes}, #{bedNo}, #{item.orderName}, " +
            "        #{item.actOrderNo}, #{item.orderCode}, #{item.orderName}, #{item.reqNo}, #{item.reqComment}, #{userCode}, " +
            "        #{deptCode}, #{wardCode}, #{item.execDept}, getdate(), '1', #{reqType}, getdate(), #{item.startTime},#{item.diagCode},#{item.diagText}, " +
            "        (select order_type from yz_order_item where order_code = #{item.orderCode}),#{item.jzFlag},#{item.reqTzComment},#{item.reqOtherResult}," +
            "       #{item.inspectStuff})" +
            "</foreach>" +
            "</script>")
    void chaRuShenQing(@Param("list") List<YshYjReq> list,
                       @Param("inpatientNo") String inpatientNo,
                       @Param("name") String name,
                       @Param("age") String age,
                       @Param("admissTimes") Integer admissTimes,
                       @Param("bedNo") String bedNo,
                       @Param("userCode") String userCode,
                       @Param("deptCode") String deptCode,
                       @Param("wardCode") String wardCode,
                       @Param("reqType") String reqType);


    @Insert("<script>" +
            "INSERT INTO yz_act_order (order_code, order_name, frequ_code, infant_flag/*婴儿标志*/, order_time, physician, start_time, " +
            "                          act_order_no, inpatient_no, admiss_times, drug_flag/*o*/, enter_oper/*录入人*/, enter_time/*录时间*/, " +
            "                          status_flag/*2*/, confirm_time, signer,/*在我这里保存了医嘱就直接确定了*/ " +
            "                          self_buy/*0*/, pay_self/*0*/, serial/*00*/, group_no/*00*/, doctor_flag, exec_unit, dept_code, " +
            "                          ward_code, reg_flag/*1*/,emergency_flag,yb_self_flag) " +
            "VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.orderCode}, #{item.orderName}, 'ONCE', #{infantFlag}, getdate(), #{userCode}, getdate(), " +
            "        #{item.actOrderNo}, #{inpatientNo}, " +
            "        #{admissTimes}, 'o', #{userCode}, getdate(), '2', getdate(), #{userCode}, '0', '0', '00', '00', '1', " +
            "        #{item.execDept}, " +
            "        #{deptCode}, #{wardCode}, '1',#{item.jzFlag},#{item.ybSelfFlag})" +
            "</foreach>" +
            "</script>")
    void chaRuShenQingYiZhu(@Param("list") List<YshYjReq> list,
                            @Param("inpatientNo") String inpatientNo,
                            @Param("admissTimes") Integer admissTimes,
                            @Param("userCode") String userCode,
                            @Param("deptCode") String deptCode,
                            @Param("wardCode") String wardCode,
                            @Param("infantFlag") Integer infantFlag);


    @Select("<script>" +
            "select req_no," +
            "       inpatient_no," +
            "       admiss_times, " +
            "       group_name," +
            "       order_name, " +
            "       act_order_no, " +
            "       order_code, " +
            "       req_comment, " +
            "       req_doctor, " +
            "       req_doctor_name= (select rtrim(name) from a_employee_mi with (NOLOCK) where code = req_doctor), " +
            "       req_dept, " +
            "       req_dept_name = (select rtrim(name) from zd_unit_code with (NOLOCK) where code = req_dept), " +
            "       exec_dept, " +
            "       exec_dept_name = (select rtrim(name) from zd_unit_code with (NOLOCK) where code = exec_dept), " +
            "       req_date, " +
            "       inspect_stuff, " +
            "       inspect_stuff_name = (select rtrim(name) from jy_zd_sample with (NOLOCK) where code = inspect_stuff), " +
            "       jz_flag," +
            "       receive_flag " +
            "from ysh_yj_req with (NOLOCK) " +
            "where inpatient_no = #{patNo} " +
            "  and admiss_times = #{times} " +
            "  and req_type = '2'" +
            "<if test=\" startTime != null and startTime != '' \"> " +
            " and start_time &gt;=#{startTime} and start_time &lt;= #{startTime} " +
            "</if>" +
            "<if test=\"orderName != null and orderName != '' \">" +
            " and order_name = #{orderName}" +
            "</if>" +
            "</script>")
    Page<YshYjReq> huoQuJianYan(IPage<YshYjReq> page,
                                @Param("patNo") String patNo,
                                @Param("times") Integer times,
                                @Param("startTime") String startTime,
                                @Param("endTime") String endTime,
                                @Param("orderName") String orderName);


    @Select("SELECT rtrim(a.code) as code, " +
            "       rtrim(a.name) as name " +
            "FROM jy_zd_class a with (NOLOCK), " +
            "     yz_order_item b with (NOLOCK) " +
            "where a.code = b.jy_type " +
            "  and isnull(b.del_flag, '') <> '1' " +
            "group by a.code, a.name ")
    IPage<JcZdClass> jianYanFenLei(IPage<JcZdClass> page);


    @Select("select rtrim(code) as code,name from jy_zd_sample with (NOLOCK) " +
            "where (name like #{name} " +
            "    or py_code like #{name} " +
            "    or code like #{name} or d_code like #{name} )")
    List<GetDropdownBox> biaoBenLeiXing(String name);


    @Select("select * " +
            "from ysh_yj_req with (NOLOCK) where req_no = #{reqNo} and inpatient_no = #{patNo} and admiss_times = #{times}")
    YshYjReq huoQuJianYanJianCha(@Param("reqNo") Integer reqNo,
                                 @Param("patNo") String patNo,
                                 @Param("times") Integer times);

    @Delete("delete from ysh_yj_req where req_no = #{reqNo} and inpatient_no = #{patNo} and admiss_times = #{times}")
    void shanChuJianYanJianCha(@Param("reqNo") Integer reqNo,
                               @Param("patNo") String patNo,
                               @Param("times") Integer times);

    @Delete("delete from yz_act_order where act_order_no = #{orderNo}")
    void shanChuYiZhu(@Param("orderNo") BigDecimal orderNo);


    @Select("select a.order_code,\n" +
            "       a.occ_code,\n" +
            "       b.name,\n" +
            "       a.amount,\n" +
            "       cast(a.amount * b.charge_amount as varchar) as charge_amount,\n" +
            "       cast(b.charge_amount as varchar)            as unit_price\n" +
            "from yz_order_occurence a,\n" +
            "     zd_charge_item b\n" +
            "where a.occ_code = b.code\n" +
            "  and a.order_code = #{orderCode}")
    List<JianYanFeiYongXiangQing> viewInspectionItemDetails(String orderCode);
}
