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.datamodify.YzActOrder;
import thyyxxk.webserver.entity.executeItem.YzOrderFrequency;
import thyyxxk.webserver.entity.executeItem.YzSupplyCharge;
import thyyxxk.webserver.entity.executeItem.YzSupplyType;
import thyyxxk.webserver.entity.executeItem.ZdChargeItem;
import thyyxxk.webserver.entity.executeItem.ZyLedgerFile;
import thyyxxk.webserver.entity.inpatient.ZyActpatient;
import thyyxxk.webserver.entity.medicine.YpZdDictVo;

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

/**
 * @Description: 医嘱生成
 * @Author:lihong
 * @Date: 2024/1/8
 */
@Mapper
public interface GenerateYzDao {

    @Select(" SELECT  inpatient_no,  " +
            "        admiss_times,  " +
            "        name,  " +
            "        bed_no,  " +
            "        total_charge,  " +
            "        balance,  " +
            "       admiss_date," +
            "       yb_flag," +
            "       owe_flag," +
            "       zk_ward," +
            "       zy_serial_no," +
            "       dept ," +
            "       ward ," +
            "       dept dept_code," +
            "       ward ward_code," +
            "       own_flag  " +
            " from zy_actpatient  " +
            " where dept = #{wardCode}  " +
            "  and ward = #{wardCode}  " +
            " order by cast(bed_no as float)")
    List<ZyActpatient> selectGenerateYzList(String wardCode);

   @Select(" SELECT code, " +
           "       week_day, " +
           "week_7          week7        , " +
           "week_1          week1        , " +
           "week_2          week2        , " +
           "week_3          week3         , " +
           "week_4          week4       , " +
           "week_5          week5       , " +
           "week_6          week6       , " +
           "day_2400      day2400       , " +
           "day_030      day030       , " +
           "day_100      day100       , " +
           "day_130      day130       , " +
           "day_200      day200       , " +
           "day_230      day230       , " +
           "day_300      day300       , " +
           "day_330      day330       , " +
           "day_400      day400       , " +
           "day_430      day430       , " +
           "day_500      day500       , " +
           "day_530      day530       , " +
           "day_600      day600       , " +
           "day_630      day630       , " +
           "day_700      day700       , " +
           "day_730      day730       , " +
           "day_800      day800       , " +
           "day_830      day830       , " +
           "day_900      day900       , " +
           "day_930      day930       , " +
           "day_1000      day1000       , " +
           "day_1030      day1030       , " +
           "day_1100      day1100       , " +
           "day_1130      day1130       , " +
           "day_1200      day1200       , " +
           "day_1230      day1230       , " +
           "day_1300      day1300       , " +
           "day_1330      day1330       , " +
           "day_1400      day1400       , " +
           "day_1430      day1430       , " +
           "day_1500      day1500       , " +
           "day_1530      day1530       , " +
           "day_1600      day1600       , " +
           "day_1630      day1630       , " +
           "day_1700      day1700       , " +
           "day_1730      day1730       , " +
           "day_1800      day1800       , " +
           "day_1830      day1830       , " +
           "day_1900      day1900       , " +
           "day_1930      day1930       , " +
           "day_2000      day2000       , " +
           "day_2030      day2030       , " +
           "day_2100      day2100       , " +
           "day_2130      day2130       , " +
           "day_2200      day2200       , " +
           "day_2230      day2230       , " +
           "day_2300      day2300       , " +
           "day_2330  day2330   "+
           " from yz_order_frequency ")
    List<YzOrderFrequency> selectYzOrderFrequency();
   /**
    * @description: 查询病房所有的药品医嘱
    * @author: lihong
    * @date: 2024/1/8 16:52
    * @param: wardCode
    * @return: java.util.List<thyyxxk.webserver.entity.datamodify.YzActOrder>
    **/
   @Select(" SELECT yz_act_order.act_order_no,\n" +
           "       yz_act_order.inpatient_no,\n" +
           "       yz_act_order.admiss_times,\n" +
           "       yz_act_order.order_code,\n" +
           "       yz_act_order.drug_flag,\n" +
           "       yz_act_order.drug_specification,\n" +
           "       yz_act_order.drug_quan,\n" +
           "       yz_act_order.start_time,\n" +
           "       yz_act_order.end_time,\n" +
           "       yz_act_order.infant_flag,\n" +
           "       yz_act_order.frequ_code,\n" +
           "       yz_act_order.supply_code,\n" +
           "       yz_act_order.physician,\n" +
           "       yz_act_order.instruction,\n" +
           "       yz_act_order.parent_no,\n" +
           "       yz_act_order.self_buy,\n" +
           "       yz_act_order.status_flag,\n" +
           "       yz_act_order.status_time,\n" +
           "       zy_actpatient.name,\n" +
           "       zy_actpatient.bed_no,\n" +
           "       zy_actpatient.dept as dept_code,\n" +
           "       zy_actpatient.ward as ward_code,\n" +
           "       yz_act_order.drug_occ,\n" +
           "       yz_act_order.order_name,\n" +
           "       yz_act_order.exclu_back_time,\n" +
           "       yz_act_order.exclu_act_order_no,\n" +
           "       yz_act_order.performance_time,\n" +
           "       yz_act_order.dose,\n" +
           "       yz_act_order.dose_unit,\n" +
           "       yz_act_order.pay_self,\n" +
           "       yz_act_order.serial,\n" +
           "       yz_act_order.group_no,\n" +
           "       yz_act_order.exec_unit,\n" +
           "       zy_actpatient.responce_type,\n" +
           "       pass_flag = yz_act_order.pass_flag\n" +
           " FROM yz_act_order\n" +
           " INNER JOIN zy_actpatient ON yz_act_order.inpatient_no = zy_actpatient.inpatient_no \n" +
           "   AND yz_act_order.admiss_times = zy_actpatient.admiss_times\n" +
           " LEFT JOIN zy_ledger_file ON zy_actpatient.inpatient_no = zy_ledger_file.inpatient_no \n" +
           "   AND zy_actpatient.admiss_times = zy_ledger_file.admiss_times\n" +
           "   AND zy_actpatient.times_billed = zy_ledger_file.ledger_sn\n" +
           " INNER JOIN zy_adtward ON zy_actpatient.ward = zy_adtward.ward_code\n" +
           " WHERE zy_actpatient.dept = #{wardCode}\n" +
           "   AND zy_actpatient.ward = #{wardCode}\n" +
           "   AND yz_act_order.status_flag > '1'\n" +
           "   AND yz_act_order.status_flag < '5'\n" +
           "   AND isnull(yz_act_order.group_no, '00') <> '00'\n" +
           "   AND ((isnull(zy_actpatient.balance, 0) >= -zy_adtward.owe_limit and zy_actpatient.inpatient_no not like '%$%') \n" +
           "      OR zy_actpatient.inpatient_no like '%$%')\n" +
           " UNION\n" +
           " SELECT yz_act_order.act_order_no,\n" +
           "       yz_act_order.inpatient_no,\n" +
           "       yz_act_order.admiss_times,\n" +
           "       yz_act_order.order_code,\n" +
           "       yz_act_order.drug_flag,\n" +
           "       yz_act_order.drug_specification,\n" +
           "       yz_act_order.drug_quan,\n" +
           "       yz_act_order.start_time,\n" +
           "       yz_act_order.end_time,\n" +
           "       yz_act_order.infant_flag,\n" +
           "       yz_act_order.frequ_code,\n" +
           "       yz_act_order.supply_code,\n" +
           "       yz_act_order.physician,\n" +
           "       yz_act_order.instruction,\n" +
           "       yz_act_order.parent_no,\n" +
           "       yz_act_order.self_buy,\n" +
           "       yz_act_order.status_flag,\n" +
           "       yz_act_order.status_time,\n" +
           "       zy_actpatient.name,\n" +
           "       zy_actpatient.bed_no,\n" +
           "       zy_actpatient.dept as dept_code ,\n" +
           "       zy_actpatient.ward  as ward_code,\n" +
           "       yz_act_order.drug_occ,\n" +
           "       yz_act_order.order_name,\n" +
           "       yz_act_order.exclu_back_time,\n" +
           "       yz_act_order.exclu_act_order_no,\n" +
           "       yz_act_order.performance_time,\n" +
           "       yz_act_order.dose,\n" +
           "       yz_act_order.dose_unit,\n" +
           "       yz_act_order.pay_self,\n" +
           "       yz_act_order.serial,\n" +
           "       yz_act_order.group_no,\n" +
           "       yz_act_order.exec_unit,\n" +
           "       zy_actpatient.responce_type,\n" +
           "       pass_flag = yz_act_order.pass_flag\n" +
           " FROM yz_act_order\n" +
           " INNER JOIN zy_actpatient ON yz_act_order.inpatient_no = zy_actpatient.inpatient_no \n" +
           "   AND yz_act_order.admiss_times = zy_actpatient.admiss_times\n" +
           " LEFT JOIN zy_ledger_file ON zy_actpatient.inpatient_no = zy_ledger_file.inpatient_no \n" +
           "   AND zy_actpatient.admiss_times = zy_ledger_file.admiss_times\n" +
           "   AND zy_actpatient.times_billed = zy_ledger_file.ledger_sn\n" +
           " INNER JOIN zy_adtward ON zy_actpatient.ward = zy_adtward.ward_code\n" +
           " WHERE zy_actpatient.dept = #{wardCode}\n" +
           "   AND zy_actpatient.ward = #{wardCode}\n" +
           "   AND yz_act_order.status_flag > '1'\n" +
           "   AND yz_act_order.status_flag < '5'\n" +
           "   AND isnull(yz_act_order.group_no, '00') <> '00'\n" +
           "   AND isnull(zy_actpatient.balance, 0) <= -zy_adtward.owe_limit\n" +
           "   AND yz_act_order.act_order_no not in (\n" +
           "       select act_order_no\n" +
           "       from yz_act_order\n" +
           "       where act_order_no in (select act_order_no\n" +
           "                              from yz_act_order\n" +
           "                              where order_code not in (select code from yp_zd_dict where qj_flag = 1))\n" +
           "          or isnull(parent_no, '') in (select act_order_no\n" +
           "                                       from yz_act_order\n" +
           "                                       where order_code not in (select code from yp_zd_dict where qj_flag = 1))\n" +
           "          or act_order_no in (select parent_no\n" +
           "                              from yz_act_order\n" +
           "                              where order_code not in (select code from yp_zd_dict where qj_flag = 1)\n" +
           "                                and isnull(parent_no, '') <> '')\n" +
           "          or isnull(parent_no, '') in (select parent_no\n" +
           "                                       from yz_act_order\n" +
           "                                       where order_code not in (select code from yp_zd_dict where qj_flag = 1)\n" +
           "                                         and isnull(parent_no, '') <> ''))")
    List<YzActOrder> selectYpYz(String wardCode);
    /**
     * @description: 查询诊疗医嘱
     * @author: lihong
     * @date: 2024/1/8 16:59
     * @param: wardCode
     * @return: java.util.List<thyyxxk.webserver.entity.datamodify.YzActOrder>
     **/
    @Select(" SELECT yz_act_order.act_order_no,\n" +
            "       yz_act_order.inpatient_no,\n" +
            "       yz_act_order.admiss_times,\n" +
            "       yz_act_order.order_code,\n" +
            "       yz_act_order.drug_flag,\n" +
            "       yz_act_order.drug_specification,\n" +
            "       yz_act_order.drug_quan,\n" +
            "       yz_act_order.start_time,\n" +
            "       yz_act_order.end_time,\n" +
            "       yz_act_order.infant_flag,\n" +
            "       yz_act_order.frequ_code,\n" +
            "       yz_act_order.supply_code,\n" +
            "       yz_act_order.physician,\n" +
            "       yz_act_order.instruction,\n" +
            "       yz_act_order.parent_no,\n" +
            "       yz_act_order.self_buy,\n" +
            "       yz_act_order.status_flag,\n" +
            "       yz_act_order.status_time,\n" +
            "       zy_actpatient.name,\n" +
            "       zy_actpatient.bed_no,\n" +
            "       zy_actpatient.dept,\n" +
            "       zy_actpatient.ward,\n" +
            "       yz_act_order.drug_occ,\n" +
            "       yz_act_order.order_name,\n" +
            "       yz_act_order.exclu_back_time,\n" +
            "       yz_act_order.exclu_act_order_no,\n" +
            "       yz_act_order.performance_time,\n" +
            "       yz_act_order.dose,\n" +
            "       yz_act_order.dose_unit,\n" +
            "       yz_act_order.pay_self,\n" +
            "       yz_act_order.serial,\n" +
            "       yz_act_order.group_no,\n" +
            "       yz_act_order.exec_unit,\n" +
            "       zy_actpatient.responce_type,\n" +
            "       pass_flag=yz_act_order.pass_flag\n" +
            " from yz_act_order,\n" +
            "     zy_actpatient\n" +
            " where (yz_act_order.inpatient_no = zy_actpatient.inpatient_no)\n" +
            "  AND (yz_act_order.admiss_times = zy_actpatient.admiss_times)\n" +
            "  AND (zy_actpatient.dept = #{wardCode})\n" +
            "  AND (zy_actpatient.ward = #{wardCode})\n" +
            "  AND (yz_act_order.status_flag > '1')\n" +
            "  AND (yz_act_order.status_flag < '5')\n" +
            "  and isnull(yz_act_order.group_no, '00') = '00'")
    List<YzActOrder> selectZlYz(String wardCode);
    /**
     * @description: 查询药品的收费
     * @author: lihong
     * @date: 2024/1/9 17:38
     * @param: codes
     * @return: java.util.List<thyyxxk.webserver.entity.executeItem.ZdChargeItem>
     **/
    @Select(" select code,serial,group_no,name, pack_retprice charge_amount, self_flag, separate_flag, cast(suprice_flag  as int) suppress_flag, drug_flag, visible_flag_zy " +
            " from view_yp_zd_dict_base_yf " +
            " where code in (${codes}) ")
    List<ZdChargeItem> selectYpBaseDict(String codes);
    /**
     * @description: 查询检验检查收费
     * @author: lihong
     * @date: 2024/1/9 17:38
     * @param: codes
     * @return: java.util.List<thyyxxk.webserver.entity.executeItem.ZdChargeItem>
     **/
    @Select(" select charge_amount, self_flag, separate_flag, suppress_flag, name, isnull(co, '0') co , effective_amount " +
            " from zd_charge_item" +
            " where code in (${codes})")
    List<ZdChargeItem> selectZdChargeItem(String codes);

    @Select(" select isnull ( charge_yb , 0 ) charge_yb , isnull ( balance , 0 ) balance from zy_ledger_file where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} and ledger_sn =( select max ( ledger_sn ) from zy_ledger_file where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} group by inpatient_no , admiss_times )  ")
    ZyLedgerFile selectBalancefromZyLedgerFile(@Param("inpatientNo") String inpatientNo, @Param("admissTimes")Integer admissTimes);
    @Update(" <script>" +
            "update zy_actpatient set own_flag=#{ownFlag}," +
            "<if test = \"ownFlag =='2'.toString\">"+
            "  own_time = getdate() " +
            "</if>"+
            "<if test = \"ownFlag =='1'.toString\">"+
            "  own_time = null " +
            "</if>"+
            "  where inpatient_no=#{inpatientNo} and admiss_times=#{admissTimes} " +
            " </script>")
    int updateOwnFlagfromZyActpatient(@Param("inpatientNo") String inpatientNo, @Param("admissTimes")Integer admissTimes, @Param("ownFlag")String ownFlag);
    @Select("  select supply_code,supply_name,class supply_class,supply_child,self_buy from   yz_supply_type where supply_code in (${codes}) ")
    List<YzSupplyType> selectYzSupplyType(String codes);
    @Select(" select Isnull(start_day_time_kf,'') start_day_time_kf  from zy_adtward  where ward_code =#{wardCode} and dept_code =#{wardCode}")
    String selectStartDayTimeKfDept(String wardCode);
    @Select(" SELECT zd_charge_item.code , " +
            "       zd_charge_item.name, " +
            "       zd_charge_item.charge_amount, " +
            "       zd_charge_item.self_flag, " +
            "       zd_charge_item.separate_flag, " +
            "       zd_charge_item.suppress_flag, " +
            "       zd_charge_item.exec_unit, " +
            "       yz_order_occurence.amount, " +
            "       zd_charge_item.effective_amount " +
            "FROM yz_order_occurence, " +
            "     zd_charge_item " +
            "WHERE ( yz_order_occurence.occ_code = zd_charge_item.code ) and " +
            "    ( ( yz_order_occurence.order_code = #{orderCode} ) ) ")
    List<ZdChargeItem> selectOccZdChargeItemByOrderCode(String orderCode);

    @Select(" select code,charge_amount,self_flag,separate_flag, " +
            "       suppress_flag,name,isnull(co,'0'), " +
            "       effective_amount " +
            "    from zd_charge_item  where code =#{code} ")
    ZdChargeItem selectZdChargeItemByCode(String code);

    @Select(" select code  from zd_unit_code " +
            " where isnull(yj_flag,'0')='1' ")
    List<String> selectYjDept();

    @Select(" select code, name,serial,pack_retprice,self_flag,separate_flag,suprice_flag,drug_flag ,visible_flag_zy visible_flag from view_yp_zd_dict_base_yf where code =#{orderCode} and serial = #{serial} and group_no= #{groupNo} ")
    YpZdDictVo selectYpZdDictBase(@Param("orderCode") String orderCode, @Param("serial")String serial,  @Param("groupNo")String groupNo);

   @Select(" select code, name,serial,pack_retprice,self_flag,separate_flag,suprice_flag,drug_flag ,del_flag visible_flag from yp_zd_dict where code =#{orderCode} and serial = #{serial} ")
   YpZdDictVo selectYpZdDict(@Param("orderCode") String orderCode, @Param("serial")String serial);

    @Select(" SELECT yz_supply_charge.supply_code, " +
            "       yz_supply_charge.charge_code, " +
            "       yz_supply_charge.charge_amount, " +
            "       yz_supply_charge.serial " +
            " from yz_supply_charge ")
    List<YzSupplyCharge> selectYzSupplyChargeAll();
    @Update(" <script>" +
            " update yz_act_order " +
            "<trim prefix='set' prefixOverrides=','>" +
            "<if test=' statusTime !=null '>"+
            " status_time = #{statusTime,jdbcType=TIMESTAMP}"+
            "</if>"+
            "<if test=' endTime !=null '>"+
            ", end_time = #{endTime,jdbcType=TIMESTAMP}"+
            "</if>"+
            "<if test=' statusFlag !=null '>"+
            ", status_flag = #{statusFlag}"+
            "</if>"+
            "</trim>" +
            " where act_order_no = ${actOrderNoStr} " +
            "</script> ")
    int updateYzActOrderStatusFlag(YzActOrder yzActOrder);
    @Update(" update yz_act_order set performance_time =#{performanceTime,jdbcType=TIMESTAMP} where act_order_no in (${yzActNos})")
    int updateYzActOrderPerformanceTime(@Param("yzActNos") String yzActNos, @Param("performanceTime") Date performanceTime);
}
