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.YzChangeData;
import thyyxxk.webserver.entity.executeItem.YzChangeParam;

import java.util.List;

/**
 * @Description:
 * @Author:lihong
 * @Date: 2023/11/6
 */
@Mapper
public interface YzChangeDao {

    /**
     * @description:查询医嘱变更单信息
     * @author: lihong
     * @date: 2023/11/6 9:43
     * @param: param
     * @return: java.util.List<thyyxxk.webserver.entity.executeItem.YzChangeData>
     **/
    @Select("<script>" +
            " SELECT zy_actpatient.bed_no," +
            "       zy_actpatient.name," +
            "<if test= \" changeClass=='0'.toString or changeClass=='3'.toString or changeClass=='4'.toString or changeClass=='7'.toString  \">" +
            "       case" +
            "           when yz_act_order.parent_no is null then rtrim(" +
            "                       ltrim(isnull(yz_act_order.order_name,'')) + '(' + ltrim(isnull(yz_act_order.drug_specification,'')) + ')')" +
            "           else ' ▲' + rtrim(ltrim(isnull(yz_act_order.order_name,'')) + '(' + ltrim(isnull(yz_act_order.drug_specification,'')) + ')') end act_order_name," +
            " </if>"+
            "<if test= \" changeClass=='1'.toString or changeClass=='2'.toString or changeClass=='5'.toString or changeClass=='6'.toString or changeClass=='8'.toString  \">" +
            "  rtrim(ltrim(yz_act_order.order_name)+'('+ltrim(isnull(yz_act_order.drug_specification,''))+')') act_order_name,"+
            " </if>"+
            "<if test= \" changeClass=='0'.toString \">" +
            "       case isnull(rtrim(convert(char, yz_act_order.dose)), '')" +
            "           when '' then isnull(rtrim(convert(char, yz_act_order.drug_quan)), 1) + ' ' + '' +" +
            "                        ltrim(isnull(yz_act_order.instruction, ''))" +
            "           else rtrim(isnull(rtrim(convert(char, yz_act_order.dose))," +
            "                             isnull(rtrim(convert(char, yz_act_order.drug_quan)), 1)) + isnull(ltrim(d.name),'') + '/' +" +
            "                      isnull(rtrim(convert(char, yz_act_order.drug_quan)), 1) + ' ' + '' +" +
            "                      ltrim(isnull(yz_act_order.instruction, ''))) end dose," +
            " </if>"+
            "<if test= \" changeClass !='0'.toString and changeClass !='8'.toString  \">" +
            "  rtrim(isnull(rtrim(convert(char,yz_act_order.dose)),isnull(rtrim(convert(char,yz_act_order.drug_quan)),1))+ltrim(isnull(d.name,''))+' '+''+ltrim(isnull(yz_act_order.instruction,''))) dose, "+
            " </if>"+
            "<if test= \" changeClass =='8'.toString  \">" +
            "  rtrim(isnull(rtrim(convert(char,yz_act_order.dose)),isnull(rtrim(convert(char,yz_act_order.drug_quan)),1))+' '+ltrim(isnull(yz_act_order.instruction,''))) dose,"+
            " </if>"+
            "       yz_supply_type.supply_name," +
            "       yz_act_order.drug_quan," +
            "       yz_act_order.frequ_code," +
            "       yz_act_order.start_time," +
            "       yz_act_order.end_time," +
            "       a_employee_mi.name  physician_name," +
            "       yz_act_order.print_order_change," +
            "       yz_act_order.print_order_change_end," +
            "       yz_act_order.act_order_no," +
            "       yz_act_order.inpatient_no," +
            "       yz_act_order.executer1," +
            "       yz_act_order.executer2," +
            "       yz_act_order.self_buy," +
            "       yz_act_order.order_time," +
            "       yz_act_order.exec_time," +
            "       exec_id=(select name from a_employee_mi a where a.code= yz_act_order.exec_id)," +
            "       order_flag='0'" +
            " FROM yz_act_order" +
            " LEFT JOIN a_employee_mi ON yz_act_order.physician = a_employee_mi.code" +
            " INNER JOIN zy_actpatient ON yz_act_order.inpatient_no = zy_actpatient.inpatient_no AND yz_act_order.admiss_times = zy_actpatient.admiss_times" +
            "<if test=\"typeClass != null and typeClass != '' \">" +
            "  INNER JOIN yz_supply_type ON yz_act_order.supply_code = yz_supply_type.supply_code" +
            " </if>"+
            "<if test=\"typeClass == null or typeClass == '' \">" +
            "  LEFT JOIN yz_supply_type ON yz_act_order.supply_code = yz_supply_type.supply_code" +
            " </if>"+
            "<if test=\" changeClass !='8'.toString  \">" +
            "  LEFT JOIN yp_zd_unit d ON yz_act_order.dose_unit = d.code OR yz_act_order.dose_unit = d.name" +
            " </if>"+
            "<if test=\" changeClass =='8'.toString  \">" +
            "  LEFT JOIN yz_order_item d ON yz_act_order.order_code = d.order_code" +
            " </if>"+
            " WHERE yz_act_order.doctor_flag = '1'" +
            "  AND (yz_act_order.status_flag &gt; '1')" +
            "  AND (yz_act_order.ward_code = #{wardCode})" +
            "<if test=\"printFlag == '1'.toString and  changeClass=='0'.toString \">" +
            "  AND (((yz_act_order.start_time &gt;= #{startDate}) AND (yz_act_order.start_time &lt; #{endDate}) AND" +
            "        ((isnull(yz_act_order.print_order_change, '') like ''))) OR" +
            "       (isnull(yz_act_order.executer2, '') != '' AND isnull(print_order_change_end, '') = '') OR" +
            "       ((yz_act_order.end_time &gt;= #{startDate}) AND (yz_act_order.end_time &lt; #{endDate}) AND" +
            "        (isnull(yz_act_order.print_order_change, '') != '') AND" +
            "        ((isnull(yz_act_order.print_order_change_end, '') = ''))))" +
            "</if>"+
            "<if test=\"printFlag == '2'.toString and  changeClass=='0'.toString \">" +
            "  AND (((yz_act_order.start_time &gt;= #{startDate}) AND (yz_act_order.start_time &lt; #{endDate}) AND\n" +
            "        ((isnull(yz_act_order.print_order_change, '') like 'p'))) OR\n" +
            "       (isnull(yz_act_order.executer2, '') != '' AND isnull(print_order_change_end, '') = '') OR\n" +
            "       ((yz_act_order.end_time &gt;= #{startDate}) AND (yz_act_order.end_time &lt; #{endDate}) AND\n" +
            "        (isnull(yz_act_order.print_order_change, '') != '') AND\n" +
            "        ((isnull(yz_act_order.print_order_change_end, '') = 'p'))))"+
            "</if>"+
            "<if test=\"printFlag == '0'.toString and  changeClass=='0'.toString \">" +
            "  AND (((yz_act_order.start_time &gt;= #{startDate}) AND (yz_act_order.start_time &lt; #{endDate}) AND\n" +
            "        ((isnull(yz_act_order.print_order_change, '') like '%'))) OR\n" +
            "       (isnull(yz_act_order.executer2, '') != '' AND isnull(print_order_change_end, '') = '') OR\n" +
            "       ((yz_act_order.end_time &gt;= #{startDate}) AND (yz_act_order.end_time &lt; #{endDate}) AND\n" +
            "        (isnull(yz_act_order.print_order_change, '') != '') AND\n" +
            "        ((isnull(yz_act_order.print_order_change_end, '') like '%'))))"+
            "</if>"+
            "<if test=\"  changeClass !='0'.toString \">" +
            " AND (((yz_act_order.start_time &gt;= #{startDate}) AND (yz_act_order.start_time &lt; #{endDate})) OR\n" +
            "       ((yz_act_order.end_time &gt;= #{startDate}) AND (yz_act_order.end_time &lt; #{endDate})))"+
            "</if>"+
            "<if test=\"  changeClass !='0'.toString and printFlag == '1'.toString \">" +
            "  AND  (isnull(yz_act_order.print_order_change, '') = '')" +
            "</if>"+
            "<if test=\"  changeClass !='0'.toString and printFlag == '2'.toString \">" +
            "  AND  (isnull(yz_act_order.print_order_change, '0') = 'p')" +
            "</if>"+
            "<if test=\"startBedNo != null and startBedNo != '' \">" +
            "  AND (zy_actpatient.bed_no &gt;= cast(#{startBedNo} as float))" +
            "</if>"+
            "<if test=\"endBedNo != null and endBedNo != '' \">" +
            "  AND (zy_actpatient.bed_no &lt;= cast(endBedNo as float))" +
            "</if>"+
            "<if test=\"changeClass =='4'.toString \">" +
            " AND ((yz_act_order.supply_code != '001' ) OR (yz_act_order.supply_code is null))" +
            "</if>"+
            "<if test=\"frequCode != null and frequCode != '' \">" +
            "  AND yz_act_order.frequ_code  ${frequCode}" +
            "</if>"+
            "<if test=\"typeClass != null and typeClass != '' \">" +
            "  AND yz_supply_type.class = #{typeClass}  " +
            "</if>"+
            "<if test=\"changeClass == '8'.toString \">" +
            "  AND yz_act_order.order_code = d.order_code\n" +
            "  AND isnull(yz_act_order.group_no, '00') = '00'\n" +
            "  AND isnull(d.order_class, '') != 'D'"+
            "</if>"+
            " </script>"
    )
    List<YzChangeData> listYzActOrder(YzChangeParam param);
    /**
     * @description:查询撤销医嘱
     * @author: lihong
     * @date: 2023/11/6 9:56
     * @param: param
     * @return: java.util.List<thyyxxk.webserver.entity.executeItem.YzChangeData>
     **/
    @Select("<script>" +
            " SELECT zy_actpatient.bed_no," +
            "       zy_actpatient.name," +
            "       case" +
            "           when parent_no is null then rtrim(" +
            "                       ltrim(isnull(yz_erase_order.order_name,'')) + '(' + ltrim(isnull(yz_erase_order.drug_specification,'')) + ')')" +
            "           else ' ▲' +" +
            "                rtrim(ltrim(yz_erase_order.order_name) + '(' + ltrim(isnull(yz_erase_order.drug_specification,'')) + ')') end act_order_name," +
            "       '(撤销)' + case when status_flag &gt; '2' then '(已执行)' else ' ' end +" +
            "       rtrim(isnull(rtrim(convert(char, yz_erase_order.dose)) ," +
            "                    isnull(rtrim(convert(char, yz_erase_order.drug_quan)), '1')) + isnull(ltrim(yz_erase_order.dose_unit),'') +" +
            "             + ltrim(isnull(yz_erase_order.instruction, ''))) dose," +
            "       yz_supply_type.supply_name," +
            "       yz_erase_order.drug_quan," +
            "       yz_erase_order.frequ_code," +
            "       yz_erase_order.start_time," +
            "       yz_erase_order.end_time," +
            "       a_employee_mi.name physician_name," +
            "       yz_erase_order.print_order_change," +
            "       yz_erase_order.print_order_change_end," +
            "       yz_erase_order.act_order_no," +
            "       yz_erase_order.inpatient_no," +
            "       yz_erase_order.executer1," +
            "       yz_erase_order.executer2," +
            "       yz_erase_order.self_buy," +
            "       yz_erase_order.order_time," +
            "       yz_erase_order.exec_time," +
            "       exec_id=(select name from a_employee_mi a where a.code= yz_erase_order.exec_id)," +
            "       order_flag='1'" +
            " FROM yz_erase_order yz_erase_order" +
            " LEFT JOIN a_employee_mi ON yz_erase_order.physician = a_employee_mi.code" +
            " INNER JOIN zy_actpatient ON yz_erase_order.inpatient_no = zy_actpatient.inpatient_no AND yz_erase_order.admiss_times = zy_actpatient.admiss_times" +
            "<if test=\"typeClass != null and typeClass != '' \">" +
            "  INNER JOIN yz_supply_type ON yz_erase_order.supply_code = yz_supply_type.supply_code" +
            " </if>" +
            "<if test=\"typeClass == null or typeClass == '' \">" +
            "  LEFT JOIN yz_supply_type ON yz_erase_order.supply_code = yz_supply_type.supply_code" +
            " </if>" +
            "<if test=\" changeClass =='8'.toString  \">" +
            "  LEFT JOIN yz_order_item d ON yz_erase_order.order_code = d.order_code" +
            " </if>" +
            " WHERE yz_erase_order.doctor_flag = '1'" +
            "  AND (yz_erase_order.ward_code like #{wardCode})" +
            "  AND (yz_erase_order.status_flag &gt; '1')" +
            "  AND (yz_erase_order.status_time &gt;= #{startDate} AND yz_erase_order.status_time &lt; #{endDate})" +
            " <if test=\"startBedNo != null and startBedNo != '' \">" +
            "  AND (zy_actpatient.bed_no &gt;= cast(#{startBedNo} as float))" +
            " </if>" +
            " <if test=\"endBedNo != null and endBedNo != '' \">" +
            "  AND (zy_actpatient.bed_no &lt;= cast(#{endBedNo} as float))" +
            " </if>" +
            " <if test=\"printFlag == '1'.toString  \">" +
            "  AND isnull(yz_erase_order.print_order_change, '') = '' " +
            " </if>" +
            " <if test=\"printFlag == '2'.toString  \">" +
            "  AND isnull(yz_erase_order.print_order_change, '') = 'p'  " +
            " </if>" +
            " <if test=\"printFlag == '0'.toString  \">" +
            "  AND isnull(yz_erase_order.print_order_change, '') != 'd'  " +
            " </if>" +
            "<if test=\"frequCode != null and frequCode != '' \">" +
            "  AND yz_erase_order.frequ_code  ${frequCode}" +
            "</if>" +
            "<if test=\"typeClass != null and typeClass != '' \">" +
            "  AND yz_supply_type.class = #{typeClass}  " +
            "</if>" +
            "<if test=\"changeClass =='4'.toString \">" +
            " AND ((yz_erase_order.supply_code != '001' ) OR (yz_erase_order.supply_code is null))" +
            "</if>" +
            "<if test=\"changeClass == '8'.toString \">" +
            "  AND yz_erase_order.order_code = d.order_code\n" +
            "  AND isnull(yz_erase_order.group_no, '00') = '00'\n" +
            "  AND isnull(d.order_class, '') != 'D'" +
            "</if>" +
            " </script>")
    List<YzChangeData> listYzEraseOrder(YzChangeParam param);
    /**
     * @description:修改变更
     * @author: lihong
     * @date: 2023/11/6 16:31
     * @param: tableName
     * @param: userId
     * @param: actOrderNoList
     **/
    @Update("<script>" +
            " UPDATE ${tableName} SET" +
            "<if test=\" tableName =='yz_erase_order' \">" +
            " print_order_change = 'p', print_order_change_end = 'p', executer2 = #{userId}"+
            "</if>"+
            "<if test=\" tableName !='yz_erase_order' \">" +
             " print_order_change_end = 'p', print_order_change = 'p',executer1=#{userId}, executer2 = #{userId}"+
            "</if>"+
            " where act_order_no in (${actOrderNoList})"+
            " </script>")
    void updatePrintChange(@Param("tableName") String tableName,@Param("userId") String userId, @Param("actOrderNoList") String actOrderNoList);
}
