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

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import cn.newfeifan.mall.module.distri.controller.admin.consumptionerroruser.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.consumptionerroruser.ConsumptionErrorUserDO;
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.consumptionerroruser.ConsumptionErrorUserMapper;

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 ConsumptionErrorUserServiceImpl implements ConsumptionErrorUserService {

    @Resource
    private ConsumptionErrorUserMapper consumptionErrorUserMapper;

    @Override
    public Long createConsumptionErrorUser(ConsumptionErrorUserSaveReqVO createReqVO) {
        // 插入
        ConsumptionErrorUserDO consumptionErrorUser = BeanUtils.toBean(createReqVO, ConsumptionErrorUserDO.class);
        consumptionErrorUserMapper.insert(consumptionErrorUser);
        // 返回
        return consumptionErrorUser.getId();
    }

    @Override
    public void updateConsumptionErrorUser(ConsumptionErrorUserSaveReqVO updateReqVO) {
        // 校验存在
        validateConsumptionErrorUserExists(updateReqVO.getId());
        // 更新
        ConsumptionErrorUserDO updateObj = BeanUtils.toBean(updateReqVO, ConsumptionErrorUserDO.class);
        consumptionErrorUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteConsumptionErrorUser(Long id) {
        // 校验存在
        validateConsumptionErrorUserExists(id);
        // 删除
        consumptionErrorUserMapper.deleteById(id);
    }

    private void validateConsumptionErrorUserExists(Long id) {
        if (consumptionErrorUserMapper.selectById(id) == null) {
            throw exception(CONSUMPTION_ERROR_USER_NOT_EXISTS);
        }
    }

    @Override
    public ConsumptionErrorUserDO getConsumptionErrorUser(Long id) {
        return consumptionErrorUserMapper.selectById(id);
    }

    @Override
    public PageResult<ConsumptionErrorUserDO> getConsumptionErrorUserPage(ConsumptionErrorUserPageReqVO pageReqVO) {
        return consumptionErrorUserMapper.selectPage(pageReqVO);
    }

}