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

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.userlevelequity.vo.*;
import co.yixiang.yshop.module.member.dal.dataobject.userlevelequity.UserLevelEquityDO;
import co.yixiang.yshop.framework.common.pojo.PageResult;

import co.yixiang.yshop.module.member.convert.userlevelequity.UserLevelEquityConvert;
import co.yixiang.yshop.module.member.dal.mysql.userlevelequity.UserLevelEquityMapper;

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 UserLevelEquityServiceImpl implements UserLevelEquityService {

    @Resource
    private UserLevelEquityMapper userLevelEquityMapper;

    @Override
    public Long createUserLevelEquity(UserLevelEquityCreateReqVO createReqVO) {
        // 插入
        UserLevelEquityDO userLevelEquity = UserLevelEquityConvert.INSTANCE.convert(createReqVO);
        userLevelEquityMapper.insert(userLevelEquity);
        // 返回
        return userLevelEquity.getId();
    }

    @Override
    public void updateUserLevelEquity(UserLevelEquityUpdateReqVO updateReqVO) {
        // 校验存在
        validateUserLevelEquityExists(updateReqVO.getId());
        // 更新
        UserLevelEquityDO updateObj = UserLevelEquityConvert.INSTANCE.convert(updateReqVO);
        userLevelEquityMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserLevelEquity(Long id) {
        // 校验存在
        validateUserLevelEquityExists(id);
        // 删除
        userLevelEquityMapper.deleteById(id);
    }

    private void validateUserLevelEquityExists(Long id) {
        if (userLevelEquityMapper.selectById(id) == null) {
            throw exception(USER_LEVEL_EQUITY_NOT_EXISTS);
        }
    }

    @Override
    public UserLevelEquityDO getUserLevelEquity(Long id) {
        return userLevelEquityMapper.selectById(id);
    }

    @Override
    public List<UserLevelEquityDO> getUserLevelEquityList() {
        return userLevelEquityMapper.selectList();
    }

    @Override
    public PageResult<UserLevelEquityDO> getUserLevelEquityPage(UserLevelEquityPageReqVO pageReqVO) {
        return userLevelEquityMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserLevelEquityDO> getUserLevelEquityList(UserLevelEquityExportReqVO exportReqVO) {
        return userLevelEquityMapper.selectList(exportReqVO);
    }

}
