package cn.iocoder.yudao.module.member.service.storerefund;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.storerefund.vo.StoreRefundPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.storerefund.vo.StoreRefundSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.storerefund.StoreRefundDO;
import cn.iocoder.yudao.module.member.dal.mysql.storerefund.StoreRefundMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.STORE_REFUND_NOT_EXISTS;

/**
 * 超市订单退款审核 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class StoreRefundServiceImpl implements StoreRefundService {

    @Resource
    private StoreRefundMapper storeRefundMapper;

    @Override
    public Long createStoreRefund(StoreRefundSaveReqVO createReqVO) {
        // 插入
        StoreRefundDO storeRefund = BeanUtils.toBean(createReqVO, StoreRefundDO.class);
        storeRefundMapper.insert(storeRefund);
        // 返回
        return storeRefund.getId();
    }

    @Override
    public void updateStoreRefund(StoreRefundSaveReqVO updateReqVO) {
        // 校验存在
        validateStoreRefundExists(updateReqVO.getId());
        // 更新
        StoreRefundDO updateObj = BeanUtils.toBean(updateReqVO, StoreRefundDO.class);

        storeRefundMapper.updateById(updateObj);
    }

    @Override
    public void deleteStoreRefund(Long id) {
        // 校验存在
        validateStoreRefundExists(id);
        // 删除
        storeRefundMapper.deleteById(id);
    }

    private void validateStoreRefundExists(Long id) {
        if (storeRefundMapper.selectById(id) == null) {
            throw exception(STORE_REFUND_NOT_EXISTS);
        }
    }

    @Override
    public StoreRefundDO getStoreRefund(Long id) {
        return storeRefundMapper.selectById(id);
    }

    @Override
    public PageResult<StoreRefundDO> getStoreRefundPage(StoreRefundPageReqVO pageReqVO) {
        return storeRefundMapper.selectPage(pageReqVO);
    }

}