package thyyxxk.webserver.dao.his.outpatient;

import com.baomidou.mybatisplus.core.metadata.IPage;
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.outpatient.wxapi.RefundParam;
import thyyxxk.webserver.entity.outpatient.wxapi.WxPayOrder;

import java.util.List;

@Mapper
public interface WxApiDao {

    @Select("<script>" +
            "select *,refundOpName=(select rtrim(name) from a_employee_mi where code=refund_op_code) " +
            "from t_wechat_pay_order where create_datetime&gt;=#{start} and " +
            "create_datetime&lt;=#{end} and patient_id like #{patientId} and patient_name " +
            "like #{patientName} and order_type like #{orderType} and trade_state in " +
            "<foreach collection='statuses' item='status' separator=',' open='(' close=')'>" +
            "#{status}" +
            "</foreach>" +
            "</script>")
    IPage<WxPayOrder> selectOrdersByConditions(IPage<WxPayOrder> iPage,
                                               @Param("patientId") String patientId,
                                               @Param("patientName") String patientName,
                                               @Param("orderType") String orderType,
                                               @Param("statuses") String[] statuses,
                                               @Param("start") String start,
                                               @Param("end") String end);

    @Select("select *,refundOpName=(select rtrim(name) from a_employee_mi where code=refund_op_code) " +
            "from t_wechat_pay_order where create_datetime>=#{start} and " +
            "create_datetime<=#{end} and patient_id like #{patientId} and patient_name " +
            "like #{patientName} and order_type like #{orderType}")
    IPage<WxPayOrder> selectOrdersWithoutStatus(IPage<WxPayOrder> iPage,
                                               @Param("patientId") String patientId,
                                               @Param("patientName") String patientName,
                                               @Param("orderType") String orderType,
                                               @Param("start") String start,
                                               @Param("end") String end);

    @Select("select * from t_wechat_pay_order where trade_no=#{tradeNo}")
    List<WxPayOrder> selectOrdersByTradeNo(@Param("tradeNo") String tradeNo);

    @Update("update t_wechat_pay_order set refund_id=#{refundId}, refund_op_code=#{refundOpCode}, his_status=0, " +
            "refund_op_datetime=#{refundOpDatetime}, refund_reason=#{refundReason}, trade_state='REFUND' where trade_no=#{tradeNo}")
    void updateRefundId(RefundParam param);

    @Update("update t_wechat_pay_order set trade_state='REFUND', refund_reason=#{msg}, his_status=0 where trade_no=#{tradeNo}")
    void alreadyRefund(@Param("tradeNo") String tradeNo, @Param("msg") String msg);
}
