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

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

import java.util.*;
import cn.newfeifan.mall.module.distri.controller.admin.integraltransferlog.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.integraltransferlog.IntegralTransferLogDO;
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.integraltransferlog.IntegralTransferLogMapper;

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 IntegralTransferLogServiceImpl implements IntegralTransferLogService {

    @Resource
    private IntegralTransferLogMapper integralTransferLogMapper;

    @Override
    public Long createConsumptionTransferLog(IntegralTransferLogSaveReqVO createReqVO) {
        // 插入
        IntegralTransferLogDO consumptionTransferLog = BeanUtils.toBean(createReqVO, IntegralTransferLogDO.class);
        integralTransferLogMapper.insert(consumptionTransferLog);
        // 返回
        return consumptionTransferLog.getId();
    }

    @Override
    public void updateConsumptionTransferLog(IntegralTransferLogSaveReqVO updateReqVO) {
        // 校验存在
        validateConsumptionTransferLogExists(updateReqVO.getId());
        // 更新
        IntegralTransferLogDO updateObj = BeanUtils.toBean(updateReqVO, IntegralTransferLogDO.class);
        integralTransferLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteConsumptionTransferLog(Long id) {
        // 校验存在
        validateConsumptionTransferLogExists(id);
        // 删除
        integralTransferLogMapper.deleteById(id);
    }

    private void validateConsumptionTransferLogExists(Long id) {
        if (integralTransferLogMapper.selectById(id) == null) {
            throw exception(CONSUMPTION_TRANSFER_LOG_NOT_EXISTS);
        }
    }

    @Override
    public IntegralTransferLogDO getConsumptionTransferLog(Long id) {
        return integralTransferLogMapper.selectById(id);
    }

    @Override
    public PageResult<IntegralTransferLogDO> getConsumptionTransferLogPage(IntegralTransferLogPageReqVO pageReqVO) {
        return integralTransferLogMapper.selectPage(pageReqVO);
    }

    @Override
    public List<IntegralTransferLogDO> selectList() {
        return integralTransferLogMapper.selectList();
    }

}