package cn.hnthyy.thmz.mapper.his.mz;

import cn.hnthyy.thmz.entity.his.mz.MzYpFydl;
import org.apache.ibatis.annotations.*;

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

public interface MzYpFydlMapper {
    /**
     * 新增发药队列记录
     *
     * @param mzYpFydl
     * @return
     */
    @Insert("INSERT INTO mz_yp_fydl(patient_id,times,receipt_no,order_no,group_no,windows_no_yf,confirm_time,seq_no,name,print_date,serial_no," +
            "order_time,status_flag,status_led,yf_no,slow_flag,call_flag,call_time) VALUES(#{patientId,jdbcType=CHAR},#{times,jdbcType=INTEGER}," +
            "#{receiptNo,jdbcType=INTEGER},#{orderNo,jdbcType=INTEGER},#{groupNo,jdbcType=CHAR},#{windowsNoYf,jdbcType=CHAR},#{confirmTime,jdbcType=TIMESTAMP}" +
            ",#{seqNo,jdbcType=INTEGER},#{name,jdbcType=CHAR},#{printDate,jdbcType=TIMESTAMP},#{serialNo,jdbcType=INTEGER},#{orderTime,jdbcType=TIMESTAMP}," +
            "#{statusFlag,jdbcType=CHAR},#{statusLed,jdbcType=INTEGER},#{yfNo,jdbcType=INTEGER},#{slowFlag,jdbcType=CHAR},#{callFlag,jdbcType=CHAR},#{callTime,jdbcType=TIMESTAMP}) ")
    int insertMzYpFydl(MzYpFydl mzYpFydl);

    /**
     * 按照病人当前就诊次数查询发药队列
     *
     * @param patientId
     * @param times
     * @return
     */
    @Select("select * from mz_yp_fydl where patient_id = #{patientId} and times = #{times} ")
    List<MzYpFydl> selectMzYpFydl(@Param(value = "patientId") String patientId, @Param("times") Integer times);

    /**
     * 删除发药队列
     *
     * @param patientId
     * @param times
     * @return
     */
    @Delete({"<script>",
            "delete from mz_yp_fydl  where patient_id = #{patientId} and times = #{times}",
            "<when test='receiptNo!=null'>",
            " and receipt_no =#{receiptNo}",
            "</when>",
            "</script>"})
    int deleteMzYpFydl(@Param(value = "patientId") String patientId, @Param("times") Integer times, @Param("receiptNo") Integer receiptNo);

    /**
     * 修改本院记账的发药队列的流水号为新收款的流水号
     *
     * @param mzYpFydl
     * @return
     */
    @Update("update mz_yp_fydl set serial_no=#{serialNo},receipt_no = #{receiptNo} where patient_id = #{patientId} and times = #{times} ")
    int updateSeralNoForByjz(MzYpFydl mzYpFydl);

    /**
     * 查询未发药药品
     * @param groupNo 药房分组 71 西药房 72 住院药房 81 中药房 82 颗粒剂药房
     * @param beginDate 开始时间
     * @param endDate 结束时间
     * @return
     */
    @Select("  SELECT DISTINCT mz_charge_detail.patient_id," +
            "         mz_charge_detail.times," +
            "         mz_charge_detail.receipt_no," +
            "         mz_charge_detail.order_no," +
            "         mz_charge_detail.group_no," +
            "         mz_charge_detail.real_no," +
            "         mz_charge_detail.name," +
            "         mz_charge_detail.price_time,"+
            "         doctor_flag," +
            "         warn_dept," +
            "         windows_no_yf" +
            "    FROM mz_charge_detail,yp_zd_dict" +
            "   WHERE        " +
            "         ( mz_charge_detail.charge_date >= #{beginDate} ) AND  " +
            "         ( mz_charge_detail.charge_date <= #{endDate} ) AND  " +
            " ( mz_charge_detail.group_no = #{groupNo} ) AND" +
            "( mz_charge_detail.charge_item_code = yp_zd_dict.code ) AND" +
            "( mz_charge_detail.serial = yp_zd_dict.serial ) AND" +
            "( isnull(yp_zd_dict.class_code,'') <> '210100') AND" +
            "         (mz_charge_detail.windows_no_yf  like (case '71' when '81' then '%' else '04' end) )  AND" +
            "         ( mz_charge_detail.pay_mark='0' and mz_charge_detail.serial_no > 0) and" +
            "         (isnull(mz_charge_detail.confirm_flag,'0') in('0','2') OR mz_charge_detail.print_flag_yj='1')  and" +
            "         (isnull(mz_charge_detail.confirm_zs,'') <> '1' and " +
            " (isnull(mz_charge_detail.confirm_zs,'') <> '4') ) and" +
            " not EXISTS(select * from mz_receipt_serial i where mz_charge_detail.patient_id = i.patient_id and " +
            "                                                   mz_charge_detail.times = i.times and" +
            "                                                   mz_charge_detail.real_no = -i.receipt_sn  )")
    List<Map<String, Object>> selectToMedicine(@Param(value = "groupNo") String groupNo,@Param("beginDate") Date beginDate, @Param("endDate") Date endDate);


}
