package cn.hnthyy.thmz.mapper.his;

import cn.hnthyy.thmz.entity.his.YpMzFytj;
import cn.hnthyy.thmz.vo.ChargeFeeParamsVo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Service;

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

public interface YpMzFytjMapper {

    /**
     * 查询当前病人本次已经确认发药了的发药统计记录
     *
     * @return
     */
    @Select("select rtrim(patient_id) patient_id,times,receipt_no,order_no,item_no,rtrim(charge_item_code) charge_item_code,rtrim(bill_item_code) bill_item_code," +
            "quantity,unit_price,rtrim(pay_mark) pay_mark,rtrim(serial) serial,rtrim(charge_item_type) charge_item_type,rtrim(country_flag) country_flag,rtrim(drug_flag) drug_flag," +
            "serial_no,rtrim(name) name,charge_date,rtrim(group_no) group_no,confirm_time,rtrim(input_id) input_id,input_time,drugname,specification,confirm_name,confirm_id," +
            "rtrim(doctor_id) doctor_id,doctor_name,confirm_flag,cy_fy,rtrim(warn_dept) warn_dept,rtrim(windows_no) windows_no,rtrim(win_no) win_no,input_date," +
            "rtrim(group_no_out) group_no_out,rtrim(response_type) response_type,rtrim(self_flag) self_flag,real_no from yp_mz_fytj where patient_id = #{patientId} and times = #{times} and serial_no > 0 and  confirm_flag = '1' ")
    List<YpMzFytj> selectConfirdYpMzFytj(@Param(value = "patientId") String patientId, @Param("times") Integer times);


    /**
     * 根据缴费流水号和药房号查询所有的药品记录
     *
     * @return
     */
    @Select("select rtrim(patient_id) patient_id,times,receipt_no,order_no,item_no,rtrim(charge_item_code) charge_item_code,rtrim(bill_item_code) bill_item_code," +
            "quantity,unit_price,rtrim(pay_mark) pay_mark,rtrim(serial) serial,rtrim(charge_item_type) charge_item_type,rtrim(country_flag) country_flag,rtrim(drug_flag) drug_flag," +
            "serial_no,rtrim(name) name,charge_date,rtrim(group_no) group_no,confirm_time,rtrim(input_id) input_id,input_time,drugname,specification,confirm_name,confirm_id," +
            "rtrim(doctor_id) doctor_id,doctor_name,confirm_flag,cy_fy,rtrim(warn_dept) warn_dept,rtrim(windows_no) windows_no,rtrim(win_no) win_no,input_date," +
            "rtrim(group_no_out) group_no_out,rtrim(response_type) response_type,rtrim(self_flag) self_flag,real_no from yp_mz_fytj where serial_no = #{serialNo} and group_no = #{groupNoOut} and order_no = #{orderNo}")
    List<YpMzFytj> selectConfirdYpMzFytjBySerialNoAndGroupNo(@Param("serialNo") String serialNo, @Param("groupNoOut") String groupNoOut,@Param("orderNo") String orderNo);



    /**
     * 查询当前病人本次已经退药的西药发药统计记录
     *
     * @return
     */
    @Select("select rtrim(patient_id) patient_id,times,receipt_no,order_no,item_no,rtrim(charge_item_code) charge_item_code,rtrim(bill_item_code) bill_item_code," +
            "quantity,unit_price,rtrim(pay_mark) pay_mark,rtrim(serial) serial,rtrim(charge_item_type) charge_item_type,rtrim(country_flag) country_flag,rtrim(drug_flag) drug_flag," +
            "serial_no,rtrim(name) name,charge_date,rtrim(group_no) group_no,confirm_time,rtrim(input_id) input_id,input_time,drugname,specification,confirm_name,confirm_id," +
            "rtrim(doctor_id) doctor_id,doctor_name,confirm_flag,cy_fy,rtrim(warn_dept) warn_dept,rtrim(windows_no) windows_no,rtrim(win_no) win_no,input_date," +
            "rtrim(group_no_out) group_no_out,rtrim(response_type) response_type,rtrim(self_flag) self_flag,real_no from yp_mz_fytj " +
            " where patient_id = #{patientId} and times = #{times} and confirm_flag =3 and bill_item_code in('010','011') ")
    List<YpMzFytj> selectXyTyYpMzFytj(@Param(value = "patientId") String patientId, @Param("times") Integer times);






    /**
     * 新增发药统计
     *
     * @param ypMzFytj
     * @return
     */
    @Insert("INSERT INTO yp_mz_fytj(patient_id,times,receipt_no,order_no,item_no,charge_item_code,bill_item_code,quantity,unit_price,pay_mark,serial,charge_item_type,country_flag," +
            "drug_flag,serial_no,name,charge_date,group_no,confirm_time,input_id,input_time,drugname,specification,confirm_name,confirm_id,doctor_id,doctor_name,confirm_flag,cy_fy," +
            "warn_dept,windows_no,win_no,input_date,group_no_out,response_type,self_flag,real_no) " +
            "VALUES(#{patientId,jdbcType=CHAR},#{times,jdbcType=INTEGER},#{receiptNo,jdbcType=SMALLINT},#{orderNo,jdbcType=TINYINT},#{itemNo,jdbcType=TINYINT}," +
            "#{chargeItemCode,jdbcType=CHAR},#{billItemCode,jdbcType=CHAR},#{quantity,jdbcType=DOUBLE},#{unitPrice,jdbcType=DECIMAL},#{payMark,jdbcType=CHAR}," +
            "#{serial,jdbcType=CHAR},#{chargeItemType,jdbcType=CHAR},#{countryFlag,jdbcType=CHAR},#{drugFlag,jdbcType=CHAR},#{serialNo,jdbcType=INTEGER},#{name,jdbcType=CHAR}," +
            "#{chargeDate,jdbcType=TIMESTAMP},#{groupNo,jdbcType=CHAR},#{confirmTime,jdbcType=TIMESTAMP},#{inputId,jdbcType=CHAR},#{inputTime,jdbcType=TIMESTAMP}," +
            "#{drugname,jdbcType=VARCHAR},#{specification,jdbcType=VARCHAR},#{confirmName,jdbcType=VARCHAR},#{confirmId,jdbcType=VARCHAR},#{doctorId,jdbcType=CHAR}," +
            "#{doctorName,jdbcType=VARCHAR},#{confirmFlag,jdbcType=INTEGER},#{cyFy,jdbcType=INTEGER},#{warnDept,jdbcType=CHAR},#{windowsNo,jdbcType=CHAR},#{winNo,jdbcType=CHAR}," +
            "#{inputDate,jdbcType=TIMESTAMP},#{groupNoOut,jdbcType=CHAR},#{responseType,jdbcType=CHAR},#{selfFlag,jdbcType=CHAR},#{realNo,jdbcType=INTEGER})")
    int insertYpMzFytj(YpMzFytj ypMzFytj);


    /**
     * 将已经发药的记录改为负数记录
     *
     * @param ypMzFytj
     * @return
     */
    @Update("update yp_mz_fytj set real_no=#{realNo},serial_no=#{serialNo} where patient_id = #{patientId} and times = #{times} and charge_item_code=#{chargeItemCode}")
    int updateYpMzFytj(YpMzFytj ypMzFytj);


    /**
     * 通用查询发药统计
     *
     * @return
     */
    @Select({"<script>",
            "select DISTINCT rtrim(patient_id) patient_id,times,receipt_no,order_no,rtrim(name) name,charge_date,rtrim(group_no) group_no,confirm_time,rtrim(doctor_id) doctor_id,rtrim(doctor_name) doctor_name,real_no from yp_mz_fytj where serial_no <![CDATA[>]]> 0 ",
            "<when test='name!=null'>",
            " and name like #{name}",
            "</when>",
            "<when test='patientId!=null'>",
            " and patient_id = #{patientId}",
            "</when>",
            "<when test='serialNo!=null'>",
            " and serial_no = #{serialNo}",
            "</when>",
            "<when test='groupNoOut!=null'>",
            " and group_no_out = #{groupNoOut}",
            "</when>",
            "<when test='confirmFlag!=null'>",
            " and confirm_flag = #{confirmFlag}",
            "</when>",
            "<when test='beginDate!=null'>",
            " and charge_date <![CDATA[>=]]> #{beginDate}",
            "</when>",
            "<when test='endDate!=null'>",
            " and charge_date <![CDATA[<=]]> #{endDate}",
            "</when>",
            "</script>"})
    List<YpMzFytj> selectYpMzFytj(ChargeFeeParamsVo chargeFeeParamsVo);


    /**
     * 查询已经确认的药品记录明细
     * @param chargeFeeParamsVo
     * @return
     */
    @Select({"<script>" +
            "SELECT DISTINCT yp_mz_fytj.serial_no,   " +
            "         yp_mz_fytj.order_no,   " +
            "         yp_mz_fytj.patient_id,   " +
            "         yp_mz_fytj.times,   " +
            "         yp_mz_fytj.receipt_no,   " +
            "         drugname=c.name+'('+isnull(d.abbr_name,isnull(d.name,''))+')',   " +
            "         specification=c.specification,   " +
            "         quantity= case yp_mz_fytj.group_no when 81 then yp_mz_fytj.quantity * cy_fy else yp_mz_fytj.quantity  end,   " +
            "         yp_mz_fytj.unit_price,   " +
            "         yp_mz_fytj.item_no,   " +
            "         yp_mz_fytj.drug_flag,   " +
            "         yp_mz_fytj.charge_item_code,   " +
            "         yp_mz_fytj.serial,   " +
            "         yp_mz_fytj.confirm_time,   " +
            "         yp_mz_fytj.group_no,   " +
            "         yp_mz_fytj.bill_item_code,   " +
            "         yp_mz_fytj.pay_mark,   " +
            "         yp_mz_fytj.charge_item_type,   " +
            "         yp_mz_fytj.name,   " +
            "         yp_mz_fytj.charge_date,   " +
            "         '',   " +
            "         yp_mz_fytj.cy_fy,   " +
            "         yp_mz_fytj.specification,   " +
            "         yp_mz_fytj.drugname,   " +
            "         yp_mz_fytj.doctor_name,   " +
            "         yp_mz_fytj.doctor_id,   " +
            "         dec_amount=case yp_mz_fytj.group_no when 81 then yp_mz_fytj.quantity * cy_fy else yp_mz_fytj.quantity  end,       " +
            "         warn_dept=yp_mz_fytj.warn_dept,   " +
            "         yp_mz_fytj.country_flag,   " +
            "         yp_mz_fytj.confirm_flag,   " +
            "         yp_mz_fytj.self_flag,   " +
            "         yp_mz_fytj.response_type,   " +
            "         yp_mz_fytj.warn_dept  ," +
            "         real_no=yp_mz_fytj.real_no," +
            "         win_no = yp_mz_fytj.win_no " +
            "    FROM yp_mz_fytj  , " +
            "         yp_zd_dict c, " +
            "         yp_zd_manufactory d " +
            "   WHERE  yp_mz_fytj.patient_id = #{patientId}  AND  " +
            "          yp_mz_fytj.times = #{times}  AND  " +
            "          yp_mz_fytj.receipt_no = #{receiptNo}  AND  " +
            "          yp_mz_fytj.order_no = #{orderNo}  AND  " +
            "         yp_mz_fytj.group_no_out = #{groupNoOut} AND  " +
            "          yp_mz_fytj.charge_item_code = c.code  and  " +
            "          yp_mz_fytj.serial = c.serial  and  " +
            "         yp_mz_fytj.confirm_flag = '1'   and " +
            "         yp_mz_fytj.serial_no > 0 and " +
            "         c.manu_code *= d.code " +
            "ORDER BY yp_mz_fytj.item_no ASC ",
            "</script>"})
    List<Map<String,Object>> selectYpMx(ChargeFeeParamsVo chargeFeeParamsVo);
    /**
     * 修改本院记账的发药统计的流水号为新收款的流水号
     * @param ypMzFytj
     * @return
     */
    @Update("update yp_mz_fytj set serial_no=#{serialNo},real_no=#{realNo},receipt_no=#{receiptNo} where patient_id = #{patientId} and times = #{times} ")
    int updateSeralNoForByjz(YpMzFytj ypMzFytj);

    /**
     * 查询最后一条发药记录
     * @param groupNo 药房分组 71 西药房 72 住院药房 81 中药房 82 颗粒剂药房
     * @param beginDate 开始时间
     * @param endDate 结束时间
     * @return
     */
    @Select("  SELECT DISTINCT " +
            "          top 1 patient_id = a.patient_id,   " +
            "         times = a.times,   " +
            "         receipt_no = a.receipt_no,   " +
            "         order_no = a.order_no,   " +
            "         group_no = a.group_no,   " +
            "         real_no = a.real_no,   " +
            "         name = a.name     ," +
            "         doctor_flag = ''               ," +
            "         warn_dept =warn_dept ," +
            "         windows_no_yf = a.win_no," +
            "         confirm_flag = confirm_flag," +
            " charge_date=a.charge_date" +
            "    FROM yp_mz_fytj a" +
            "   WHERE        " +
            "         ( a.group_no_out = #{groupNo} ) AND  " +
            "         ( a.charge_date >= #{beginDate} ) AND  " +
            "         ( a.charge_date <= #{endDate} ) AND  " +
            "         ( a.pay_mark='0' ) and" +
            "         ( a.confirm_flag>'0') " +
            " order by charge_date desc")
    YpMzFytj selectlastItem(@Param(value = "groupNo") String groupNo, @Param("beginDate") Date beginDate, @Param("endDate") Date endDate);

}
