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

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.wxrefund.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.wxrefund.WxRefundDO;
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.wxrefund.WxRefundMapper;

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 WxRefundServiceImpl implements WxRefundService {

    @Resource
    private WxRefundMapper wxRefundMapper;

    @Override
    public Long createWxRefund(WxRefundSaveReqVO createReqVO) {
        // 插入
        WxRefundDO wxRefund = BeanUtils.toBean(createReqVO, WxRefundDO.class);
        wxRefundMapper.insert(wxRefund);
        // 返回
        return wxRefund.getId();
    }

    @Override
    public void updateWxRefund(WxRefundSaveReqVO updateReqVO) {
        // 校验存在
        validateWxRefundExists(updateReqVO.getId());
        // 更新
        WxRefundDO updateObj = BeanUtils.toBean(updateReqVO, WxRefundDO.class);
        wxRefundMapper.updateById(updateObj);
    }

    @Override
    public void deleteWxRefund(Long id) {
        // 校验存在
        validateWxRefundExists(id);
        // 删除
        wxRefundMapper.deleteById(id);
    }

    private void validateWxRefundExists(Long id) {
        if (wxRefundMapper.selectById(id) == null) {
            throw exception(WX_REFUND_NOT_EXISTS);
        }
    }

    @Override
    public WxRefundDO getWxRefund(Long id) {
        return wxRefundMapper.selectById(id);
    }

    @Override
    public PageResult<WxRefundDO> getWxRefundPage(WxRefundPageReqVO pageReqVO) {
        return wxRefundMapper.selectPage(pageReqVO);
    }

}