package cn.iocoder.yudao.module.pay.service.refundrecord;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.pay.controller.admin.refundrecord.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.refundrecord.RefundRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.pay.dal.mysql.refundrecord.RefundRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*;

/**
 * 退款记录 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class RefundRecordServiceImpl implements RefundRecordService {

    @Resource
    private RefundRecordMapper refundRecordMapper;

    @Override
    public Long createRefundRecord(RefundRecordSaveReqVO createReqVO) {
        // 插入
        RefundRecordDO refundRecord = BeanUtils.toBean(createReqVO, RefundRecordDO.class);
        refundRecordMapper.insert(refundRecord);
        // 返回
        return refundRecord.getId();
    }

    @Override
    public void updateRefundRecord(RefundRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateRefundRecordExists(updateReqVO.getId());
        // 更新
        RefundRecordDO updateObj = BeanUtils.toBean(updateReqVO, RefundRecordDO.class);
        refundRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteRefundRecord(Long id) {
        // 校验存在
        validateRefundRecordExists(id);
        // 删除
        refundRecordMapper.deleteById(id);
    }

    private void validateRefundRecordExists(Long id) {
        if (refundRecordMapper.selectById(id) == null) {
            throw exception(REFUND_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public RefundRecordDO getRefundRecord(Long id) {
        return refundRecordMapper.selectById(id);
    }

    @Override
    public PageResult<RefundRecordDO> getRefundRecordPage(RefundRecordPageReqVO pageReqVO) {
        return refundRecordMapper.selectPage(pageReqVO);
    }

}