package cn.newfeifan.mall.module.distri.service.ptdailywithdrawal;

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

import cn.newfeifan.mall.module.distri.controller.admin.ptdailywithdrawal.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.ptdailywithdrawal.PtDailyWithdrawalDO;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;

import cn.newfeifan.mall.module.distri.dal.mysql.ptdailywithdrawal.PtDailyWithdrawalMapper;

import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.newfeifan.mall.module.distri.enums.ErrorCodeConstants.*;

/**
 * 平台每日提现记录 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class PtDailyWithdrawalServiceImpl implements PtDailyWithdrawalService {

    @Resource
    private PtDailyWithdrawalMapper ptDailyWithdrawalMapper;

    @Override
    public Long createPtDailyWithdrawal(PtDailyWithdrawalSaveReqVO createReqVO) {
        // 插入
        PtDailyWithdrawalDO ptDailyWithdrawal = BeanUtils.toBean(createReqVO, PtDailyWithdrawalDO.class);
        ptDailyWithdrawalMapper.insert(ptDailyWithdrawal);
        // 返回
        return ptDailyWithdrawal.getId();
    }

    @Override
    public void updatePtDailyWithdrawal(PtDailyWithdrawalSaveReqVO updateReqVO) {
        // 校验存在
        validatePtDailyWithdrawalExists(updateReqVO.getId());
        // 更新
        PtDailyWithdrawalDO updateObj = BeanUtils.toBean(updateReqVO, PtDailyWithdrawalDO.class);
        ptDailyWithdrawalMapper.updateById(updateObj);
    }

    @Override
    public void deletePtDailyWithdrawal(Long id) {
        // 校验存在
        validatePtDailyWithdrawalExists(id);
        // 删除
        ptDailyWithdrawalMapper.deleteById(id);
    }

    private void validatePtDailyWithdrawalExists(Long id) {
        if (ptDailyWithdrawalMapper.selectById(id) == null) {
            throw exception(PT_DAILY_WITHDRAWAL_NOT_EXISTS);
        }
    }

    @Override
    public PtDailyWithdrawalDO getPtDailyWithdrawal(Long id) {
        return ptDailyWithdrawalMapper.selectById(id);
    }

    @Override
    public PageResult<PtDailyWithdrawalDO> getPtDailyWithdrawalPage(PtDailyWithdrawalPageReqVO pageReqVO) {
        return ptDailyWithdrawalMapper.selectPage(pageReqVO);
    }

}