package com.fs.company.mapper;

import com.fs.company.domain.CompanyRechargeOrder;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 公司充值订单表 Mapper
 */
@Mapper
public interface CompanyRechargeOrderMapper {

    /**
     * 根据ID查询订单
     */
    @Select("SELECT * FROM company_recharge_order WHERE id = #{id}")
    CompanyRechargeOrder selectById(Long id);

    /**
     * 根据订单号查询订单
     */
    @Select("SELECT * FROM company_recharge_order WHERE order_no = #{orderNo}")
    CompanyRechargeOrder selectByOrderNo(String orderNo);

    /**
     * 根据交易号查询订单
     */
    @Select("SELECT * FROM company_recharge_order WHERE transaction_id = #{transactionId}")
    CompanyRechargeOrder selectByTransactionId(String transactionId);

    /**
     * 查询所有订单
     */
    @Select("SELECT * FROM company_recharge_order")
    List<CompanyRechargeOrder> selectAll();

    /**
     * 新增订单
     */
    @Insert("INSERT INTO company_recharge_order (order_no, transaction_id, pay_type, pay_amount, pay_status, payment_time, callback_content,company_id,user_id) " +
            "VALUES (#{orderNo}, #{transactionId}, #{payType}, #{payAmount}, #{payStatus}, #{paymentTime}, #{callbackContent},#{companyId},#{userId})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(CompanyRechargeOrder order);

    /**
     * 更新订单
     */
    @Update("UPDATE company_recharge_order SET " +
            "transaction_id = #{transactionId}, " +
            "pay_type = #{payType}, " +
            "pay_amount = #{payAmount}, " +
            "pay_status = #{payStatus}, " +
            "payment_time = #{paymentTime}, " +
            "callback_content = #{callbackContent} " +
            "WHERE order_no = #{orderNo}")
    int updateByOrderNo(CompanyRechargeOrder order);

    /**
     * 更新订单支付状态
     */
    @Update("UPDATE company_recharge_order SET " +
            "pay_status = #{payStatus}, " +
            "payment_time = #{paymentTime}, " +
            "transaction_id = #{transactionId}, " +
            "callback_content = #{callbackContent} " +
            "WHERE order_no = #{orderNo}")
    int updatePayStatus(CompanyRechargeOrder order);

    @Select("SELECT * FROM company_recharge_order " +
            "WHERE pay_status = 1 " +
            "AND create_time <= DATE_SUB(NOW(), INTERVAL 2 HOUR)")
    List<CompanyRechargeOrder> queryOverdueOrder();
}
