package thyyxxk.webserver.dao.his.executeItem;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.executeItem.YpZyPatient;
import thyyxxk.webserver.entity.executeItem.YzYpZyOrder;

import java.util.List;

/**
 * (YzYpZyOrder)表数据库访问层
 *
 * @author lihong
 * @since 2024-01-08 16:18:32
 */
@Mapper
public interface YzYpZyOrderDao extends BaseMapper<YzYpZyOrder> {
    /**
     * @description: 生成药品或生成诊疗后最后执行的更新状态
     * @author: lihong
     * @date: 2024/1/8 17:21
     * @param: wardCode
     * @return: int
     **/
  @Update(" update yz_yp_zy_order" +
          " set status_flag ='2'" +
          " from yz_act_order" +
          " where yz_yp_zy_order.dept_code = #{wardCode}" +
          "  and yz_yp_zy_order.ward_code = #{wardCode}" +
          "  and yz_yp_zy_order.occ_time > yz_act_order.end_time" +
          "  and yz_yp_zy_order.status_flag <> '0'" +
          "  and yz_yp_zy_order.status_flag <> '4'" +
          "  and yz_act_order.frequ_code <> 'ONCE'" +
          "  and yz_yp_zy_order.act_order_no = yz_act_order.act_order_no" +
          "  and (yz_yp_zy_order.amount > 0 or isnull(yz_act_order.drug_quan, 1) < 0) ")
  int updateStatusFlag(String wardCode);
  /**
   * @description: 金额改为负 状态改为 初始 生成药品或生成诊疗后最后执行的更新状态
   * @author: lihong
   * @date: 2024/1/8 17:32
   * @param: wardCode
   * @return: int
   **/
  @Update(" update yz_yp_zy_order" +
          " set amount      =0 - amount," +
          "    status_flag ='1'" +
          " from yz_act_order" +
          " where yz_yp_zy_order.dept_code = #{wardCode}" +
          "  and yz_yp_zy_order.ward_code = #{wardCode}" +
          "  and yz_yp_zy_order.occ_time > yz_act_order.end_time" +
          "  and (yz_yp_zy_order.status_flag = '0' or yz_yp_zy_order.pay_self >= '2')" +
          "  and yz_yp_zy_order.amount > 0" +
          "  and yz_act_order.frequ_code <> 'ONCE'" +
          "  and yz_yp_zy_order.act_order_no = yz_act_order.act_order_no")
  int updateAmountAndStatusFlag(String wardCode);
  @Insert("<script>" +
          " insert into yz_yp_zy_order (inpatient_no, admiss_times, name, bed_no, dept_code, ward_code, charge_code, amount, retprice, charge_date, infant_flag, drawer, always_flag, status_flag, drug_class, serail, occ_time, act_order_no, page_no, pay_self, self_flag, separate_flag, suprice_flag, drug_flag, serial, group_no, doctor_name, zy_serial_no, page_class, yb_self_flag, ledger_sn, detail_sn) values " +
          "<foreach collection='list' item='item'  separator=','>"+
          " ( #{item.inpatientNo},#{item.admissTimes} ,#{item.name}, #{item.bedNo}, #{item.deptCode}, #{item.wardCode}, #{item.chargeCode},  #{item.amount,jdbcType=DECIMAL}, #{item.retprice,jdbcType=DECIMAL}, #{item.chargeDate,jdbcType=TIMESTAMP}, #{item.infantFlag}, #{item.drawer},#{item.alwaysFlag}, #{item.statusFlag},  #{item.drugClass},  #{item.serail}, #{item.occTime,jdbcType=TIMESTAMP}, ${item.actOrderNoStr} ,#{item.pageNo,jdbcType=DECIMAL} , #{item.paySelf}, #{item.selfFlag}, #{item.separateFlag}, #{item.supriceFlag}, #{item.drugFlag},  #{item.serial}, #{item.groupNo}, #{item.doctorName}, #{item.zySerialNo}, #{item.pageClass}, #{item.ybSelfFlag}, #{item.ledgerSn}, #{item.detailSn})"+
          "</foreach>" +
          "</script>")
  int batchInsertData(List<YzYpZyOrder> list);

  @Insert("<script> insert into yp_zy_patient (order_date,out_seri,dept_code,ward_code,inpatient_no,name,bed_no,charge_code,serial,amount,drawer,retprice, infant_flag,always_flag,drug_class,acct_sign,total_flag, admiss_times,supply_code,act_order_no,page_no,pay_self,self_flag,separate_flag,suprice_flag,drug_flag,group_no) values  " +
          " <foreach collection='list' item='item'  separator=','>  " +
          " (#{item.orderDate,jdbcType=TIMESTAMP},#{item.outSeri},#{item.deptCode},#{item.wardCode},#{item.inpatientNo},#{item.name},#{item.bedNo},#{item.chargeCode},#{item.serial},#{item.amount,jdbcType=DECIMAL},#{item.drawer},#{item.retprice,jdbcType=DECIMAL},#{item.infantFlag},#{item.alwaysFlag},#{item.drugClass},#{item.acctSign},#{item.totalFlag},#{item.admissTimes},#{item.supplyCode},${item.actOrderNoStr},#{item.pageNo},#{item.paySelf},#{item.selfFlag},#{item.separateFlag},#{item.supriceFlag},#{item.drugFlag},#{item.groupNo}) " +
          " </foreach>  " +
          " </script> ")
  int batchInsertYpZyPatient(List<YpZyPatient> list);
  @Insert(" insert into yz_yp_zy_order (inpatient_no, admiss_times, name, bed_no, dept_code, ward_code, charge_code, amount, retprice, charge_date, infant_flag, drawer, always_flag, status_flag, drug_class, serail, occ_time, act_order_no, page_no, pay_self, self_flag, separate_flag, suprice_flag, drug_flag, serial, group_no, doctor_name, zy_serial_no, page_class, yb_self_flag, ledger_sn, detail_sn) values" +
          " ( #{item.inpatientNo},#{item.admissTimes} ,#{item.name}, #{item.bedNo}, #{item.deptCode}, #{item.wardCode}, #{item.chargeCode},  #{item.amount,jdbcType=DECIMAL}, #{item.retprice,jdbcType=DECIMAL}, #{item.chargeDate,jdbcType=TIMESTAMP}, #{item.infantFlag}, #{item.drawer},#{item.alwaysFlag}, #{item.statusFlag},  #{item.drugClass},  #{item.serail}, #{item.occTime,jdbcType=TIMESTAMP}, ${item.actOrderNoStr} ,#{item.pageNo,jdbcType=DECIMAL} , #{item.paySelf}, #{item.selfFlag}, #{item.separateFlag}, #{item.supriceFlag}, #{item.drugFlag},  #{item.serial}, #{item.groupNo}, #{item.doctorName}, #{item.zySerialNo}, #{item.pageClass}, #{item.ybSelfFlag}, #{item.ledgerSn}, #{item.detailSn}) ")
  int insertOne(@Param("item") YzYpZyOrder yzYpZyOrder);

  @Select(" select order_date, out_seri, dept_code, ward_code, section_code, inpatient_no, name, bed_no, charge_code, retprice, amount, infant_flag, drawer, keeper, date1, date2, always_flag, drug_class, acct_sign, total_flag, admiss_times, supply_code, act_order_no, page_no, self_flag, separate_flag, suprice_flag, drug_flag, pay_self, group_no, confirm_time, serial, doctor_name, country_flag, zy_serial_no, page_type, jy_flag, manu_no from yp_zy_patient where act_order_no = ${actOrderNoStr} and order_date > #{endTime} and amount > 0  ")
  List<YpZyPatient> selectYpZyPatientByOccTime(@Param("actOrderNoStr") String actOrderNoStr, @Param("endTime") String endTime);
  @Update(" update yz_yp_zy_order set amount=-amount  where  act_order_no=${actOrderNoStr} and occ_time > #{endTime} ")
  int updateTfAmount(@Param("actOrderNoStr") String actOrderNoStr, @Param("endTime") String endTime);

  /**
   * 查询需要执行时间超过结束时间的医嘱
   * @author lihong
   * @since 2025/8/7 15:07
   * @param
   * @param wardCode
   * @return List<YzYpZyOrder>
   **/
  @Select(" SELECT yz_yp_zy_order.act_order_no, " +
          "       yz_yp_zy_order.charge_code, " +
          "       yz_yp_zy_order.occ_time, " +
          "       yz_yp_zy_order.status_flag, " +
          "       yz_yp_zy_order.amount, " +
          "       yz_yp_zy_order.pay_self, " +
          "       yz_yp_zy_order.inpatient_no, " +
          "       yz_yp_zy_order.admiss_times, " +
          "       yz_yp_zy_order.name, " +
          "       yz_yp_zy_order.bed_no, " +
          "       yz_yp_zy_order.dept_code, " +
          "       yz_yp_zy_order.ward_code, " +
          "       yz_yp_zy_order.retprice, " +
          "       yz_yp_zy_order.charge_date, " +
          "       yz_yp_zy_order.infant_flag, " +
          "       yz_yp_zy_order.drawer, " +
          "       yz_yp_zy_order.always_flag, " +
          "       yz_yp_zy_order.drug_class, " +
          "       yz_yp_zy_order.serail, " +
          "       yz_yp_zy_order.page_no, " +
          "       yz_yp_zy_order.self_flag, " +
          "       yz_yp_zy_order.separate_flag, " +
          "       yz_yp_zy_order.suprice_flag, " +
          "       yz_yp_zy_order.drug_flag, " +
          "       yz_yp_zy_order.serial, " +
          "       yz_yp_zy_order.group_no, " +
          "       yz_yp_zy_order.doctor_name " +
          "FROM yz_yp_zy_order  ,yz_act_order b " +
          "WHERE yz_yp_zy_order.dept_code = #{wardCode} and " +
          "    yz_yp_zy_order.ward_code = #{wardCode} and " +
          "    yz_yp_zy_order.status_flag in ('3','4') and " +
          "    yz_yp_zy_order.amount > 0 and " +
          "    b.frequ_code !='ONCE' and " +
          "    yz_yp_zy_order.occ_time >  b.end_time and " +
          "    yz_yp_zy_order.inpatient_no=b.inpatient_no and " +
          "    yz_yp_zy_order.admiss_times=b.admiss_times and " +
          "    yz_yp_zy_order.act_order_no=b.act_order_no and " +
          "    yz_yp_zy_order.drug_class <> 'd'")
  List<YzYpZyOrder> selectYzYpZyOrderByEnd(@Param("wardCode") String wardCode);

  @Delete(" delete  a from yz_yp_zy_order as a join yz_act_order b on a.inpatient_no=b.inpatient_no " +
          "   and a.admiss_times=b.admiss_times " +
          "   and a.act_order_no = b.act_order_no " +
          "   where a.drug_class <>'d' " +
          "   and  a.occ_time > b.end_time " +
          "   and a.amount > 0" +
          "   and b.frequ_code != 'ONCE'" +
          "   and a.status_flag in ('3','4') " +
          "   and a.dept_code = #{wardCode} " +
          "   and a.ward_code = #{wardCode} ")
  int deleteYzYpZyOrderEnd(@Param("wardCode") String wardCode);

}

