package thyyxxk.webserver.dao.his.inpatient;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import thyyxxk.webserver.entity.datamodify.YzActOrder;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 描述： 医嘱退费
 * </p>
 *
 * @author xc
 * @date 2021-09-17 10:25
 */
@Mapper
public interface ZhuYuanYiZhuDao {

    /**
     * 分页查询 可以退费的医嘱信息
     *
     * @param page        页数
     * @param inpatientNo 住院号
     * @param admissTimes 住院次数
     * @param orderCode   医嘱编码
     * @param actOrderNo  医嘱号
     * @param startTime   开始时间
     * @param endTime     结束时间
     * @param infantFlag  婴儿标志
     * @return 返回分页数据
     */
    @Select("<script>" +
            "select drug_flag, " +
            "       order_name, " +
            "       dose, " +
            "       dose_unit = b.name, " +
            "       drug_occ," +
            "       drug_quan, " +
            "       rtrim(frequ_code) frequ_code, " +
            "       supply_code_name = c.supply_name, " +
            "       start_time, " +
            "       end_time, " +
            "       physician_name = d.name, " +
            "       order_code, " +
            "       drug_specification, " +
            "       exec_unit_name = e.name, " +
            "       group_no = (select rtrim(group_name) from yp_zd_group_name where a.group_no = yp_zd_group_name.group_no), " +
            "       serial, " +
            "       discription, " +
            "       act_order_no " +
            "from yz_act_order a ,yp_zd_unit b ,yz_supply_type c, a_employee_mi d , zd_unit_code e " +
            "where group_no != 00 and frequ_code = 'ONCE' and a.dose_unit = b.code and a.supply_code = c.supply_code " +
            "and a.physician = d.code and a.exec_unit = e.code  and infant_flag = #{infantFlag} " +
            "and inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} " +
            "<if test=\"orderCode != null and orderCode != '' \">" +
            "and order_code = #{orderCode} " +
            "</if>" +
            "<if test=\"actOrderNo != null\">" +
            "and act_order_no = #{actOrderNo} " +
            "</if>" +
            "<if test=\"startTime != null and startTime != '' \">" +
            "and order_time &gt;= #{startTime} and order_time &lt;= #{endTime} " +
            "</if>" +
            "</script>")
    IPage<YzActOrder> getYzXinXi(IPage<YzActOrder> page,
                                 @Param("inpatientNo") String inpatientNo,
                                 @Param("admissTimes") Integer admissTimes,
                                 @Param("orderCode") String orderCode,
                                 @Param("actOrderNo") BigDecimal actOrderNo,
                                 @Param("startTime") String startTime,
                                 @Param("endTime") String endTime,
                                 @Param("infantFlag") int infantFlag);

    /**
     * 查询原来的医嘱信息
     *
     * @param inpatientNo      住院号
     * @param admissTimes      住院次数
     * @param xuTuiFeiYiZhuHao 里面带上一些医嘱号
     * @return 返回被退费的医嘱信息
     */
    @Select("<script>" +
            "select order_code, order_name, frequ_code, discription, infant_flag,   " +
            "       order_time, start_time, confirm_time,status_time,enter_time, end_time,performance_time,print_order_change,refer_physician,consult_physician," +
            "       drug_specification, drug_quan,drug_flag,act_order_no,   " +
            "       mini_unit, drug_weight_unit,self_buy, dose, dose_unit, drug_occ, drug_volume,   " +
            "       drug_vol_unit, pay_self, serial, group_no, doctor_flag,  " +
            "       inpatient_no, admiss_times, exec_unit, dept_code, ward_code, reg_flag, yb_self_flag   " +
            "        from yz_act_order where inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} and act_order_no in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            "   #{item.actOrderNo} " +
            "</foreach>" +
            "</script>")
    List<YzActOrder> yuanYiZhu(@Param("inpatientNo") String inpatientNo,
                               @Param("admissTimes") Integer admissTimes,
                               @Param("list") List<YzActOrder> xuTuiFeiYiZhuHao);

    /**
     * 生成 退费数据 和
     *
     * @param list 生成新的医嘱数据
     * @param code 操作人的编码
     */
    @Insert("<script>" +
            "INSERT INTO  " +
            "    yz_act_order  " +
            "( order_code, order_name, frequ_code, discription, infant_flag,  " +
            "  order_time, start_time, confirm_time,status_time,enter_time,  " +
            "  drug_specification, drug_quan,drug_flag,  " +
            "  act_order_no,  " +
            "  mini_unit, drug_weight_unit,self_buy, dose, dose_unit, drug_occ, drug_volume,  " +
            "  drug_vol_unit, pay_self, serial, group_no, doctor_flag,  " +
            "  inpatient_no, admiss_times, exec_unit, dept_code, ward_code, reg_flag, yb_self_flag,  " +
            "  supply_code,  " +
            "  status_flag,  " +
            "  enter_oper,physician,signer,end_time,performance_time,print_order_change,refer_physician,consult_physician) values " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.orderCode},#{item.orderName},#{item.frequCode},#{item.discription},#{item.infantFlag},   " +
            "getdate(),getdate(),getdate(),#{item.statusTime},#{item.enterTime},   " +
            "#{item.drugSpecification}, cast(#{item.drugQuan,jdbcType=DECIMAL} as decimal(14,5)),#{item.drugFlag},   " +
            "#{item.newActOrderNo},#{item.miniUnit},#{item.drugWeightUnit},#{item.selfBuy},cast(#{item.dose,jdbcType=DECIMAL} as decimal(14,5)),#{item.doseUnit},   " +
            "cast(#{item.drugOcc,jdbcType=DECIMAL} as decimal(14,5)),cast(#{item.drugVolume,jdbcType=DECIMAL} as decimal(14,5)) ,#{item.drugVolUnit},#{item.paySelf},#{item.serial},#{item.groupNo},#{item.doctorFlag},   " +
            "#{item.inpatientNo},#{item.admissTimes},#{item.execUnit},#{item.deptCode},#{item.wardCode},#{item.regFlag},#{item.ybSelfFlag},   " +
            "'068','2',#{code},#{code},#{code},null,null,#{item.printOrderChange},#{item.referPhysician},#{item.consultPhysician})" +
            "</foreach>" +
            "</script>")
    void yiZhuTuiFei(@Param("list") List<YzActOrder> list,
                     @Param("code") String code);


    /**
     * 获取是否已经有存在了的退费医嘱
     *
     * @param yiZhuHao 要查询的医嘱号
     * @return 返回已经推过的医嘱
     */
    @Select("<script>" +
            "select act_order_no from yz_act_order_tf where act_order_no in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item.actOrderNo}" +
            "</foreach>" +
            "</script>")
    List<YzActOrder> getTuiFei(@Param("list") List<YzActOrder> yiZhuHao);

    /**
     * 插入已经退费的 被退的医嘱号 和 生成的退费数据的医嘱号
     *
     * @param yzActOrderList 被退的医嘱号 和 生成的退费数据的医嘱号
     */
    @Insert("<script>" +
            "insert into yz_act_order_tf (act_order_no, act_order_no_tf) values " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.actOrderNo},#{item.newActOrderNo})" +
            "</foreach>" +
            "</script>")
    void insertYzActOrderTf(@Param("list") List<YzActOrder> yzActOrderList);


}
