package cn.sikey.hmd.dal.mysql;


import cn.sikey.hmd.api.hmd.vo.payments.PaymentsReqVO;
import cn.sikey.hmd.dal.dataobject.PaymentsDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;

import java.time.Instant;
import java.util.Objects;

/**
 * 支付记录
 *
 * @author qin
 */
@Mapper
public interface PaymentsMapper extends BaseMapper<PaymentsDO> {
    default void updatePayments(PaymentsReqVO paymentsReqVO) {
        UpdateWrapper<PaymentsDO> queryWrapper = new UpdateWrapper<>();
        queryWrapper.eq("order_id", paymentsReqVO.getOrderId());
        if (paymentsReqVO.getStatus() != null) {
            queryWrapper.set("status", paymentsReqVO.getStatus());
        }
        if (StringUtils.isNotBlank(paymentsReqVO.getTransactionId())) {
            queryWrapper.set("transaction_id", paymentsReqVO.getTransactionId());
        }
        if (Objects.nonNull(paymentsReqVO.getPaymentTime())) {
            queryWrapper.set("payment_time", paymentsReqVO.getPaymentTime());
        }
        queryWrapper.set("update_time", Instant.now());
        update(queryWrapper);
    }

    default PaymentsDO queryPayments(Long orderId) {
        QueryWrapper<PaymentsDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        return selectOne(queryWrapper);
    }
}
