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

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.consumptiontransferlog.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.consumptiontransferlog.ConsumptionTransferLogDO;
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.consumptiontransferlog.ConsumptionTransferLogMapper;

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 ConsumptionTransferLogServiceImpl implements ConsumptionTransferLogService {

    @Resource
    private ConsumptionTransferLogMapper consumptionTransferLogMapper;

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

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

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

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

    @Override
    public ConsumptionTransferLogDO getConsumptionTransferLog(Long id) {
        return consumptionTransferLogMapper.selectById(id);
    }

    @Override
    public PageResult<ConsumptionTransferLogDO> getConsumptionTransferLogPage(ConsumptionTransferLogPageReqVO pageReqVO) {
        return consumptionTransferLogMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ConsumptionTransferLogDO> selectList() {
        return consumptionTransferLogMapper.selectList();
    }

}