package co.yixiang.yshop.module.member.service.userlevelgrowthvalueconfig;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import co.yixiang.yshop.module.member.controller.admin.userlevelgrowthvalueconfig.vo.*;
import co.yixiang.yshop.module.member.dal.dataobject.userlevelgrowthvalueconfig.UserLevelGrowthValueConfigDO;
import co.yixiang.yshop.framework.common.pojo.PageResult;

import co.yixiang.yshop.module.member.convert.userlevelgrowthvalueconfig.UserLevelGrowthValueConfigConvert;
import co.yixiang.yshop.module.member.dal.mysql.userlevelgrowthvalueconfig.UserLevelGrowthValueConfigMapper;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.member.enums.ErrorCodeConstants.*;

/**
 * 会员成长任务配置 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class UserLevelGrowthValueConfigServiceImpl implements UserLevelGrowthValueConfigService {

    @Resource
    private UserLevelGrowthValueConfigMapper userLevelGrowthValueConfigMapper;

    @Override
    public Integer createUserLevelGrowthValueConfig(UserLevelGrowthValueConfigCreateReqVO createReqVO) {
        // 插入
        UserLevelGrowthValueConfigDO userLevelGrowthValueConfig = UserLevelGrowthValueConfigConvert.INSTANCE.convert(createReqVO);
        userLevelGrowthValueConfigMapper.insert(userLevelGrowthValueConfig);
        // 返回
        return userLevelGrowthValueConfig.getId();
    }

    @Override
    public void updateUserLevelGrowthValueConfig(UserLevelGrowthValueConfigUpdateReqVO updateReqVO) {
        // 校验存在
        validateUserLevelGrowthValueConfigExists(updateReqVO.getId());
        // 更新
        UserLevelGrowthValueConfigDO updateObj = UserLevelGrowthValueConfigConvert.INSTANCE.convert(updateReqVO);
        userLevelGrowthValueConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserLevelGrowthValueConfig(Integer id) {
        // 校验存在
        validateUserLevelGrowthValueConfigExists(id);
        // 删除
        userLevelGrowthValueConfigMapper.deleteById(id);
    }

    private void validateUserLevelGrowthValueConfigExists(Integer id) {
        if (userLevelGrowthValueConfigMapper.selectById(id) == null) {
            throw exception(USER_LEVEL_GROWTH_VALUE_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public UserLevelGrowthValueConfigDO getUserLevelGrowthValueConfig(Integer id) {
        return userLevelGrowthValueConfigMapper.selectById(id);
    }

    @Override
    public List<UserLevelGrowthValueConfigDO> getUserLevelGrowthValueConfigList(Collection<Integer> ids) {
        return userLevelGrowthValueConfigMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<UserLevelGrowthValueConfigDO> getUserLevelGrowthValueConfigPage(UserLevelGrowthValueConfigPageReqVO pageReqVO) {
        return userLevelGrowthValueConfigMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserLevelGrowthValueConfigDO> getUserLevelGrowthValueConfigList(UserLevelGrowthValueConfigExportReqVO exportReqVO) {
        return userLevelGrowthValueConfigMapper.selectList(exportReqVO);
    }

    @Override
    public UserLevelGrowthValueConfigDO getByType(String type) {
        return userLevelGrowthValueConfigMapper.selectOne(UserLevelGrowthValueConfigDO::getType, type);
    }

}
