package thyyxxk.webserver.dao.his.executeItem;

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.executeItem.ExecuteItemData;
import thyyxxk.webserver.entity.executeItem.ExecuteItemParam;

import java.util.List;

/**
 * @Description:
 * @Author:lihong
 * @Date: 2023/11/21
 */
@Mapper
public interface ExecuteItemDao {


    @Select("<script>" +
            " SELECT DISTINCT yz_act_occ.parent_no, " +
            "                yz_act_occ.inpatient_no, " +
            "                yz_act_occ.inpatient_name name, " +
            "                yz_act_occ.bed_no, " +
            "                yz_act_occ.specification, " +
            "                drug_quan   = b.drug_quan, " +
            "                supply_name = yz_supply_type.print_name, " +
            "                yz_act_occ.infant_flag, " +
            "                yz_act_occ.instruction, " +
            "                yz_act_occ.phy_name, " +
            "                yz_act_occ.occ_time, " +
            "                occ_oper= (select a_employee_mi.name from a_employee_mi where a_employee_mi.code = yz_act_occ.occ_oper),"+
            "                '                                ', " +
            "                yz_act_occ.parent_sort, " +
            "                yz_act_occ.act_order_no, " +
            "                dose        = rtrim( " +
            "                                      ltrim( " +
            "                                              isnull(yz_act_occ.dose, ''))) + rtrim( " +
            "                                      ltrim( " +
            "                                              isnull(yp_zd_unit.name, ''))), " +
            "                yz_act_occ.order_name,b.self_buy, " +
            "                yz_act_occ.frequ_code, " +
            "                yz_act_occ.print_flag, " +
            "                yz_act_occ.dept_code, " +
            "                yz_act_occ.ward_code, " +
            "                mini_unit   = (select name from yp_zd_unit a where a.code =b.mini_unit), " +
            "                zb          = (CASE b.self_buy WHEN '1' THEN '自备' ELSE '' END) " +
            " FROM yz_act_occ " +
            " INNER JOIN yz_act_order b ON yz_act_occ.inpatient_no = b.inpatient_no " +
            "   AND yz_act_occ.admiss_times = b.admiss_times " +
            "   AND yz_act_occ.act_order_no = b.act_order_no " +
            " INNER JOIN zy_actpatient c (nolock) ON yz_act_occ.inpatient_no = c.inpatient_no " +
            "   AND yz_act_occ.admiss_times = c.admiss_times " +
            " INNER JOIN yz_supply_type ON yz_act_occ.supply_code = yz_supply_type.supply_code " +
            " LEFT JOIN yp_zd_unit ON yz_act_occ.dose_unit = yp_zd_unit.code " +
            " WHERE yz_act_occ.occ_time &gt;= #{executeTime} " +
            "   AND yz_act_occ.occ_time &lt; #{endTime} " +
            "   AND c.dept = #{wardCode} " +
            "   AND c.ward = #{wardCode} " +
            "   AND yz_act_occ.dept_code = #{wardCode} " +
            "   AND yz_act_occ.ward_code = #{wardCode} " +
            "   AND isnull(yz_act_occ.status_flag, '') != 'd' " +
            " <if test=\" pageClass == '0'.toString and (classCode == '0'.toString or classCode == '3'.toString)   \">"+
            "   AND yz_supply_type.class in (${codeCondition}) " +
            "</if>"+
            " <if test=\" pageClass == '0'.toString and classCode == '1'.toString   \">"+
            "   AND yz_supply_type.supply_code in (${codeCondition}) " +
            "</if>"+
            " <if test=\" startBedNo != null and startBedNo != ''  \">"+
            "   AND yz_act_occ.bed_no &gt;= CAST(#{startBedNo} AS FLOAT) " +
            "</if>"+
            " <if test=\" endBedNo != null and endBedNo != ''  \">"+
            "   AND yz_act_occ.bed_no &lt;= CAST(#{endBedNo} AS FLOAT) " +
            "</if>"+
            " <if test=\" frequCodeFlag =='1'.toString  \">"+
            "   AND yz_act_occ.always_flag ='t' " +
            "</if>"+
            " <if test=\" frequCodeFlag =='2'.toString  \">"+
            "   AND yz_act_occ.always_flag ='a' " +
            "</if>"+
            " <if test=\" printFlag =='1'.toString  \">"+
            "   AND isnull(yz_act_occ.print_flag, '') = '' " +
            "</if>"+
            " <if test=\" printFlag =='2'.toString  \">"+
            "   AND isnull(yz_act_occ.print_flag, '') != '' " +
            "</if>"+
            " UNION " +
            " SELECT DISTINCT yz_act_occ.parent_no, " +
            "                yz_act_occ.inpatient_no, " +
            "                yz_act_occ.inpatient_name name, " +
            "                yz_act_occ.bed_no, " +
            "                yz_act_occ.specification, " +
            "                NULL, " +
            "                '      ', " +
            "                yz_act_occ.infant_flag, " +
            "                yz_act_occ.instruction, " +
            "                yz_act_occ.phy_name, " +
            "                yz_act_occ.occ_time, " +
            "                occ_oper= (select a_employee_mi.name from a_employee_mi where a_employee_mi.code = yz_act_occ.occ_oper),"+
            "                '                                ', " +
            "                yz_act_occ.parent_sort, " +
            "                yz_act_occ.act_order_no, " +
            "                dose      = rtrim( " +
            "                                    ltrim( " +
            "                                            isnull(yz_act_occ.dose, ''))) + rtrim( " +
            "                                    ltrim( " +
            "                                            isnull(yz_act_occ.dose_unit, ''))), " +
            "                yz_act_occ.order_name,b.self_buy, " +
            "                yz_act_occ.frequ_code, " +
            "                yz_act_occ.print_flag, " +
            "                yz_act_occ.dept_code, " +
            "                yz_act_occ.ward_code, " +
            "                mini_unit = '', " +
            "                zb        = (CASE b.self_buy WHEN '1' THEN '自备' ELSE '' END) " +
            " FROM yz_act_occ " +
            " INNER JOIN yz_act_order b ON yz_act_occ.inpatient_no = b.inpatient_no " +
            "   AND yz_act_occ.admiss_times = b.admiss_times " +
            "   AND yz_act_occ.act_order_no = b.act_order_no " +
            " INNER JOIN zy_actpatient c (nolock) ON yz_act_occ.inpatient_no = c.inpatient_no " +
            "   AND yz_act_occ.admiss_times = c.admiss_times " +
            " WHERE isnull(yz_act_occ.supply_code, '') = '' " +
            "   AND yz_act_occ.occ_time &gt;= #{executeTime} " +
            "   AND yz_act_occ.occ_time &lt; #{endTime} " +
            "   AND c.dept = #{wardCode} " +
            "   AND c.ward = #{wardCode} " +
            "   AND yz_act_occ.dept_code = #{wardCode} " +
            "   AND yz_act_occ.ward_code = #{wardCode} " +
            "   AND isnull(yz_act_occ.drug_quan, 1) &gt; 0 " +
            "   AND isnull(yz_act_occ.status_flag, '') != 'd' " +
            "   AND isnull(yz_act_occ.parent_no, 0) = 0 " +
            " <if test=\" pageClass == '0'.toString and (classCode == '0'.toString or classCode == '1'.toString or classCode == '3'.toString)   \">"+
            "   AND isnull(yz_act_occ.supply_code, 'ZL') in (${codeCondition}) " +
            "</if>"+
            " <if test=\" startBedNo != null and startBedNo != ''  \">"+
            "   AND yz_act_occ.bed_no &gt;= CAST(#{startBedNo} AS FLOAT) " +
            "</if>"+
            " <if test=\" endBedNo != null and endBedNo != ''  \">"+
            "   AND yz_act_occ.bed_no &lt;= CAST(#{endBedNo} AS FLOAT) " +
            "</if>"+
            " <if test=\" frequCodeFlag =='1'.toString  \">"+
            "   AND yz_act_occ.always_flag ='t' " +
            "</if>"+
            " <if test=\" frequCodeFlag =='2'.toString  \">"+
            "   AND yz_act_occ.always_flag ='a' " +
            "</if>"+
            " <if test=\" printFlag =='1'.toString  \">"+
            "   AND isnull(yz_act_occ.print_flag, '') = '' " +
            "</if>"+
            " <if test=\" printFlag =='2'.toString  \">"+
            "   AND isnull(yz_act_occ.print_flag, '') != '' " +
            "</if>"+
            " </script>")
    List<ExecuteItemData> selectExecuteItem(ExecuteItemParam param);
    @Select("<script>" +
            " SELECT DISTINCT " +
            " yz_act_occ.parent_no, " +
            " yz_act_occ.inpatient_no, " +
            " yz_act_occ.inpatient_name name , " +
            " yz_act_occ.bed_no, " +
            " yz_act_occ.specification, " +
            " yz_act_occ.drug_quan, " +
            " supply_name = '', " +
            " yz_act_occ.infant_flag, " +
            " yz_act_occ.instruction, " +
            " yz_act_occ.phy_name, " +
            " yz_act_occ.occ_time, " +
            " '                                ', " +
            " yz_act_occ.parent_sort, " +
            " yz_act_occ.act_order_no, " +
            " yz_act_occ.dose, " +
            " yz_act_occ.order_name, " +
            " yz_act_occ.frequ_code, " +
            " yz_act_occ.print_flag, " +
            " yz_act_occ.dept_code, " +
            " yz_act_occ.ward_code, " +
            " mini_unit   = (select name from yp_zd_unit a where a.code =b.mini_unit),b.self_buy, " +
            " zb = ''  " +
            " FROM " +
            " yz_act_occ, " +
            " yz_act_order b, " +
            " zy_actpatient c ( nolock )  " +
            " WHERE " +
            " yz_act_occ.occ_time &gt;= #{executeTime}  " +
            " AND yz_act_occ.occ_time &lt; #{endTime}  " +
            " AND c.dept = #{wardCode}  " +
            " AND c.ward =  #{wardCode}   " +
            " AND yz_act_occ.dept_code =  #{wardCode}   " +
            " AND yz_act_occ.ward_code =  #{wardCode}   " +
            " AND yz_act_occ.inpatient_no= b.inpatient_no  " +
            " AND yz_act_occ.admiss_times= b.admiss_times  " +
            " AND yz_act_occ.act_order_no= b.act_order_no  " +
            " AND b.group_no = '00'  " +
            " AND b.order_code IN ( ${codeCondition} )  " +
            " AND isnull( yz_act_occ.drug_quan, 1 ) &gt; 0  " +
            " AND isnull( yz_act_occ.status_flag, '' ) != 'd'  " +
            " <if test=\" startBedNo != null and  startBedNo != ''  \">"+
            "          AND yz_act_occ.bed_no &gt;= CAST(#{startBedNo} AS FLOAT) " +
            "</if>"+
            " <if test=\" endBedNo != null and  endBedNo != ''  \">"+
            "          AND yz_act_occ.bed_no &lt;= CAST(#{endBedNo} AS FLOAT) " +
            "</if>"+
            " <if test=\" frequCodeFlag =='1'.toString  \">"+
            "          AND yz_act_occ.always_flag ='t' " +
            "</if>"+
            " <if test=\" frequCodeFlag =='2'.toString  \">"+
            "          AND yz_act_occ.always_flag ='a' " +
            "</if>"+
            " <if test=\" printFlag =='1'.toString  \">"+
            "          AND isnull(yz_act_occ.print_flag, '') = '' " +
            "</if>"+
            " <if test=\" printFlag =='2'.toString  \">"+
            "          AND isnull(yz_act_occ.print_flag, '') != '' " +
            "</if>"+
            " AND yz_act_occ.inpatient_no = c.inpatient_no  " +
            " AND yz_act_occ.admiss_times = c.admiss_times  " +
            " </script> ")
    List<ExecuteItemData> selectExecuteItemData(ExecuteItemParam param);
    @Update("  update yz_act_occ set print_flag ='1' where inpatient_no =#{inpatientNo} and act_order_no in (${actOrderNoCondition}) ")
    int updaYzActOccPrintFlag(@Param("inpatientNo") String inpatientNo, @Param("actOrderNoCondition") String actOrderNoCondition);


    @Select("<script>" +
            " SELECT DISTINCT yz_act_occ.parent_no," +
            "                yz_act_occ.inpatient_no," +
            "                yz_act_occ.inpatient_name name," +
            "                yz_act_occ.bed_no," +
            "                yz_act_occ.specification," +
            "                yz_act_occ.drug_quan," +
            "                supply_name=yz_supply_type.print_name," +
            "                yz_act_occ.infant_flag," +
            "                yz_act_occ.instruction," +
            "                yz_act_occ.phy_name," +
            "                yz_act_occ.occ_time," +
            "                '                                '," +
            "                yz_act_occ.parent_sort," +
            "                yz_act_occ.act_order_no," +
            "                dose=rtrim(ltrim(isnull(yz_act_occ.dose, '')))," +
            "               dose_unit=(select t1.name from yp_zd_unit t1 where yz_act_occ.dose_unit =t1.code)," +
            "                order_name= case" +
            "                                when yz_act_occ.parent_no is not null" +
            "                                    then ' --' + yz_act_occ.order_name + '(' + yz_act_occ.specification + ')'" +
            "                                else yz_act_occ.order_name + '(' + yz_act_occ.specification + ')' end," +
            "                yz_act_occ.frequ_code,b.self_buy," +
            "                yz_act_occ.print_flag," +
            "                yz_act_occ.dept_code," +
            "                yz_act_occ.ward_code," +
            "                unit=cast(yz_act_occ.drug_quan2 as varchar(6)) + min_unit.name," +
            "                sort=case when yz_act_occ.parent_no is null then yz_act_occ.act_order_no else yz_act_occ.parent_no end" +
            " FROM yz_act_occ" +
            " INNER JOIN yz_act_order b ON yz_act_occ.inpatient_no = b.inpatient_no" +
            "   AND yz_act_occ.admiss_times = b.admiss_times" +
            "   AND yz_act_occ.act_order_no = b.act_order_no" +
            " INNER JOIN zy_actpatient c (nolock) ON yz_act_occ.inpatient_no = c.inpatient_no" +
            "   AND yz_act_occ.admiss_times = c.admiss_times" +
            " INNER JOIN yz_supply_type ON yz_act_occ.supply_code = yz_supply_type.supply_code" +
            " LEFT JOIN yp_zd_unit yp_unit ON yz_act_occ.dose_unit = yp_unit.code" +
            " LEFT JOIN yp_zd_unit min_unit ON b.mini_unit = min_unit.code" +
            " WHERE yz_act_occ.occ_time &gt;= #{executeTime}" +
            "   AND yz_act_occ.occ_time &lt; #{endTime}" +
            "   AND c.dept = #{wardCode}" +
            "   AND c.ward = #{wardCode}" +
            "   AND yz_act_occ.dept_code = #{wardCode}" +
            "   AND yz_act_occ.ward_code = #{wardCode}" +
            "   AND ((case #{caseValue}" +
            "            when 'c' then yz_supply_type.class" +
            "            when 'a' then 'a'" +
            "            when 'x' then yz_supply_type.supply_code" +
            "            when 'yp' then b.order_code end) in (${codeCondition}))" +
            "   AND isnull(yz_act_occ.drug_quan, 1) > 0" +
            "   AND isnull(yz_act_occ.status_flag, '') != 'd'" +
            " <if test=\" startBedNo != null and startBedNo != '' \">" +
            "   AND yz_act_occ.bed_no &gt;= CAST(#{startBedNo} AS FLOAT)" +
            " </if>" +
            " <if test=\" endBedNo != null and endBedNo != '' \">" +
            "   AND yz_act_occ.bed_no &lt;= CAST(#{endBedNo} AS FLOAT)" +
            " </if>" +
            " <if test=\" frequCodeFlag =='1'.toString \">" +
            "   AND yz_act_occ.always_flag ='t'" +
            " </if>" +
            " <if test=\" frequCodeFlag =='2'.toString \">" +
            "   AND yz_act_occ.always_flag ='a'" +
            " </if>" +
            " <if test=\" printFlag =='1'.toString \">" +
            "   AND isnull(yz_act_occ.print_flag, '') = ''" +
            " </if>" +
            " <if test=\" printFlag =='2'.toString \">" +
            "   AND isnull(yz_act_occ.print_flag, '') != ''" +
            " </if>" +
            " UNION" +
            " SELECT DISTINCT yz_act_occ.parent_no," +
            "                yz_act_occ.inpatient_no," +
            "                yz_act_occ.inpatient_name name," +
            "                yz_act_occ.bed_no," +
            "                yz_act_occ.specification," +
            "                yz_act_occ.drug_quan," +
            "                '      '," +
            "                yz_act_occ.infant_flag," +
            "                yz_act_occ.instruction," +
            "                yz_act_occ.phy_name," +
            "                yz_act_occ.occ_time," +
            "                '                                '," +
            "                yz_act_occ.parent_sort," +
            "                yz_act_occ.act_order_no," +
            "                dose=rtrim(ltrim(isnull(yz_act_occ.dose, '')))," +
            "               dose_unit=(select t1.name from yp_zd_unit t1 where yz_act_occ.dose_unit =t1.code)," +
            "                order_name= case" +
            "                                when yz_act_occ.parent_no is not null" +
            "                                    then ' --' + yz_act_occ.order_name + '(' + yz_act_occ.specification + ')'" +
            "                                else yz_act_occ.order_name + '(' + yz_act_occ.specification + ')' end," +
            "                yz_act_occ.frequ_code,b.self_buy," +
            "                yz_act_occ.print_flag," +
            "                yz_act_occ.dept_code," +
            "                yz_act_occ.ward_code," +
            "                unit=cast(yz_act_occ.drug_quan2 as varchar(6)) + min_unit.name," +
            "                sort=''" +
            " FROM yz_act_occ" +
            " INNER JOIN yz_act_order b ON yz_act_occ.inpatient_no = b.inpatient_no" +
            "   AND yz_act_occ.admiss_times = b.admiss_times" +
            "   AND yz_act_occ.act_order_no = b.act_order_no" +
            " INNER JOIN zy_actpatient c (nolock) ON yz_act_occ.inpatient_no = c.inpatient_no" +
            "   AND yz_act_occ.admiss_times = c.admiss_times" +
            " LEFT JOIN yp_zd_unit yp_unit ON yz_act_occ.dose_unit = yp_unit.code" +
            " LEFT JOIN yp_zd_unit min_unit ON b.mini_unit = min_unit.code" +
            " WHERE yz_act_occ.occ_time &gt;= #{executeTime}" +
            "   AND yz_act_occ.occ_time &lt; #{endTime}" +
            "   AND c.dept = #{wardCode}" +
            "   AND c.ward = #{wardCode}" +
            "   AND yz_act_occ.dept_code = #{wardCode}" +
            "   AND yz_act_occ.ward_code = #{wardCode}" +
            "   AND ((case #{caseValue}" +
            "            when 'c' then isnull(yz_act_occ.supply_code, 'ZL')" +
            "            when 'a' then 'ZL'" +
            "            when 'xm' then b.order_code end) in (${codeCondition}))" +
            "   AND isnull(yz_act_occ.drug_quan, 1) &gt; 0" +
            "   AND isnull(yz_act_occ.status_flag, '') != 'd'" +
            "   AND isnull(yz_act_occ.parent_no, 0) = 0" +
            " <if test=\" startBedNo != null and startBedNo != '' \">" +
            "   AND yz_act_occ.bed_no &gt;= CAST(#{startBedNo} AS FLOAT)" +
            " </if>" +
            " <if test=\" endBedNo != null and endBedNo != '' \">" +
            "   AND yz_act_occ.bed_no &lt;= CAST(#{endBedNo} AS FLOAT)" +
            " </if>" +
            " <if test=\" frequCodeFlag =='1'.toString \">" +
            "   AND yz_act_occ.always_flag ='t'" +
            " </if>" +
            " <if test=\" frequCodeFlag =='2'.toString \">" +
            "   AND yz_act_occ.always_flag ='a'" +
            " </if>" +
            " <if test=\" printFlag =='1'.toString \">" +
            "   AND isnull(yz_act_occ.print_flag, '') = ''" +
            " </if>" +
            " <if test=\" printFlag =='2'.toString \">" +
            "   AND isnull(yz_act_occ.print_flag, '') != ''" +
            " </if>" +
            " ORDER BY yz_act_occ.bed_no, sort, yz_act_occ.act_order_no, yz_act_occ.occ_time" +
            " </script>")
    List<ExecuteItemData> selectExecuteItemDetail(ExecuteItemParam param);
    @Select(" select order_name,act_order_no  from yz_act_order where act_order_no in (${actOrderNo}) ")
    List<ExecuteItemData> selectOrderName(String actOrderNo);
}
