package thyyxxk.webserver.dao.his.datamodify;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.MzChargeDetail;

import java.math.BigDecimal;

/**
 * <p>
 * 描述： 二次退费
 * </p>
 *
 * @author xc
 * @date 2021-05-18 11:22
 */
@Mapper
public interface SecondFefundDao {

    @Select("select confirm_flag,dec_amount,tc_name = (select distinct name from yp_zd_dict where code = charge_item_code), " +
            "rtrim(name)name,rtrim(charge_item_code)charge_item_code,rtrim(patient_id)patient_id,times,receipt_no,item_no" +
            "(quantity * drug_win) quantity from mz_charge_detail where " +
            "patient_id = #{patientId} and times = #{times} and charge_item_code = #{chargeItemCode}")
    MzChargeDetail getOrderInformation(@Param("patientId") String patientId,
                                       @Param("times") Integer times,
                                       @Param("chargeItemCode") String chargeItemCode);

    @Select("<script>" +
            "select confirm_flag,dec_amount,tc_name = (select distinct name from yp_zd_dict where code = charge_item_code), " +
            "rtrim(name)name,rtrim(charge_item_code)charge_item_code,rtrim(patient_id)patient_id,times,receipt_no,item_no," +
            "(quantity * drug_win) quantity from mz_charge_detail where " +
            "patient_id = #{patientId} and times = #{times} " +
            "<if test=\"chargeItemCode != null and chargeItemCode != '' \">" +
            "and charge_item_code = #{chargeItemCode}" +
            "</if>" +
            "and receipt_no not like '-%' " +
            "</script>")
    Page<MzChargeDetail> getMzTuiFei(Page<MzChargeDetail> page,
                                     @Param("patientId") String patientId,
                                     @Param("times") Integer times,
                                     @Param("chargeItemCode") String chargeItemCode);

    @Update("update mz_charge_detail set dec_amount=#{quantity},confirm_flag = 3  " +
            "where patient_id = #{patientId} and times = #{times} and charge_item_code = #{chargeItemCode} and item_no = #{itemNo}")
    void xiuGaiTuiFei(@Param("patientId") String patientId,
                      @Param("times") Integer times,
                      @Param("chargeItemCode") String chargeItemCode,
                      @Param("quantity") BigDecimal quantity,
                      @Param("itemNo") Integer itemNo);
}
