package cn.iocoder.yudao.module.member.service.groupcount;

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

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.groupcount.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.groupcount.GroupCountDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.groupcount.GroupCountMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 用户分组充值统计 Service 实现类
 *
 * @author 秦俊旗
 */
@Service
@Validated
public class GroupCountServiceImpl implements GroupCountService {

    @Resource
    private GroupCountMapper groupCountMapper;

    @Override
    public Long createGroupCount(GroupCountSaveReqVO createReqVO) {
        // 插入
        GroupCountDO groupCount = BeanUtils.toBean(createReqVO, GroupCountDO.class);
        groupCountMapper.insert(groupCount);
        // 返回
        return groupCount.getId();
    }

    @Override
    public void updateGroupCount(GroupCountSaveReqVO updateReqVO) {
        // 校验存在
        validateGroupCountExists(updateReqVO.getId());
        // 更新
        GroupCountDO updateObj = BeanUtils.toBean(updateReqVO, GroupCountDO.class);
        groupCountMapper.updateById(updateObj);
    }

    @Override
    public void deleteGroupCount(Long id) {
        // 校验存在
        validateGroupCountExists(id);
        // 删除
        groupCountMapper.deleteById(id);
    }

    private void validateGroupCountExists(Long id) {
        if (groupCountMapper.selectById(id) == null) {
            throw exception(GROUP_COUNT_NOT_EXISTS);
        }
    }

    @Override
    public GroupCountDO getGroupCount(Long id) {
        return groupCountMapper.selectById(id);
    }

    @Override
    public PageResult<GroupCountDO> getGroupCountPage(GroupCountPageReqVO pageReqVO) {
        return groupCountMapper.selectPage(pageReqVO);
    }

}