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

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.wxprofitsharingreturn.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.wxprofitsharingreturn.WxProfitsharingReturnDO;
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.wxprofitsharingreturn.WxProfitsharingReturnMapper;

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 WxProfitsharingReturnServiceImpl implements WxProfitsharingReturnService {

    @Resource
    private WxProfitsharingReturnMapper wxProfitsharingReturnMapper;

    @Override
    public Long createWxProfitsharingReturn(WxProfitsharingReturnSaveReqVO createReqVO) {
        // 插入
        WxProfitsharingReturnDO wxProfitsharingReturn = BeanUtils.toBean(createReqVO, WxProfitsharingReturnDO.class);
        wxProfitsharingReturnMapper.insert(wxProfitsharingReturn);
        // 返回
        return wxProfitsharingReturn.getId();
    }

    @Override
    public void updateWxProfitsharingReturn(WxProfitsharingReturnSaveReqVO updateReqVO) {
        // 校验存在
        validateWxProfitsharingReturnExists(updateReqVO.getId());
        // 更新
        WxProfitsharingReturnDO updateObj = BeanUtils.toBean(updateReqVO, WxProfitsharingReturnDO.class);
        wxProfitsharingReturnMapper.updateById(updateObj);
    }

    @Override
    public void deleteWxProfitsharingReturn(Long id) {
        // 校验存在
        validateWxProfitsharingReturnExists(id);
        // 删除
        wxProfitsharingReturnMapper.deleteById(id);
    }

    private void validateWxProfitsharingReturnExists(Long id) {
        if (wxProfitsharingReturnMapper.selectById(id) == null) {
            throw exception(WX_PROFITSHARING_RETURN_NOT_EXISTS);
        }
    }

    @Override
    public WxProfitsharingReturnDO getWxProfitsharingReturn(Long id) {
        return wxProfitsharingReturnMapper.selectById(id);
    }

    @Override
    public PageResult<WxProfitsharingReturnDO> getWxProfitsharingReturnPage(WxProfitsharingReturnPageReqVO pageReqVO) {
        return wxProfitsharingReturnMapper.selectPage(pageReqVO);
    }

}