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

import cn.newfeifan.mall.module.distri.controller.app.consumptiontopuplog.vo.ConsumptionTopUpLogPageReqVO;
import cn.newfeifan.mall.module.distri.controller.app.consumptiontopuplog.vo.ConsumptionTopUpLogSaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.newfeifan.mall.module.distri.dal.dataobject.consumptiontopuplog.ConsumptionTopUpLogDO;
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.consumptiontopuplog.ConsumptionTopUpLogMapper;

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 ConsumptionTopUpLogServiceImpl implements ConsumptionTopUpLogService {

    @Resource
    private ConsumptionTopUpLogMapper consumptionTopUpLogMapper;

    @Override
    public Long createConsumptionTopUpLog(ConsumptionTopUpLogSaveReqVO createReqVO) {
        // 插入
        ConsumptionTopUpLogDO consumptionTopUpLog = BeanUtils.toBean(createReqVO, ConsumptionTopUpLogDO.class);
        consumptionTopUpLogMapper.insert(consumptionTopUpLog);
        // 返回
        return consumptionTopUpLog.getId();
    }

    @Override
    public void updateConsumptionTopUpLog(ConsumptionTopUpLogSaveReqVO updateReqVO) {
        // 校验存在
        validateConsumptionTopUpLogExists(updateReqVO.getId());
        // 更新
        ConsumptionTopUpLogDO updateObj = BeanUtils.toBean(updateReqVO, ConsumptionTopUpLogDO.class);
        consumptionTopUpLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteConsumptionTopUpLog(Long id) {
        // 校验存在
        validateConsumptionTopUpLogExists(id);
        // 删除
        consumptionTopUpLogMapper.deleteById(id);
    }

    private void validateConsumptionTopUpLogExists(Long id) {
        if (consumptionTopUpLogMapper.selectById(id) == null) {
            throw exception(CONSUMPTION_TOP_UP_LOG_NOT_EXISTS);
        }
    }

    @Override
    public ConsumptionTopUpLogDO getConsumptionTopUpLog(Long id) {
        return consumptionTopUpLogMapper.selectById(id);
    }

    @Override
    public PageResult<ConsumptionTopUpLogDO> getConsumptionTopUpLogPage(ConsumptionTopUpLogPageReqVO pageReqVO) {
        return consumptionTopUpLogMapper.selectPage(pageReqVO);
    }

}