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

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.newfeifan.mall.module.distri.controller.admin.socialstatuschangecategory.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.socialstatuschangecategory.SocialStatusChangeCategoryDO;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.pojo.PageParam;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;

import cn.newfeifan.mall.module.distri.dal.mysql.socialstatuschangecategory.SocialStatusChangeCategoryMapper;

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 SocialStatusChangeCategoryServiceImpl implements SocialStatusChangeCategoryService {

    @Resource
    private SocialStatusChangeCategoryMapper socialStatusChangeCategoryMapper;

    @Override
    public Long createSocialStatusChangeCategory(SocialStatusChangeCategorySaveReqVO createReqVO) {
        // 插入
        SocialStatusChangeCategoryDO socialStatusChangeCategory = BeanUtils.toBean(createReqVO, SocialStatusChangeCategoryDO.class);
        socialStatusChangeCategoryMapper.insert(socialStatusChangeCategory);
        // 返回
        return socialStatusChangeCategory.getId();
    }

    @Override
    public void updateSocialStatusChangeCategory(SocialStatusChangeCategorySaveReqVO updateReqVO) {
        // 校验存在
        validateSocialStatusChangeCategoryExists(updateReqVO.getId());
        // 更新
        SocialStatusChangeCategoryDO updateObj = BeanUtils.toBean(updateReqVO, SocialStatusChangeCategoryDO.class);
        socialStatusChangeCategoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteSocialStatusChangeCategory(Long id) {
        // 校验存在
        validateSocialStatusChangeCategoryExists(id);
        // 删除
        socialStatusChangeCategoryMapper.deleteById(id);
    }

    private void validateSocialStatusChangeCategoryExists(Long id) {
        if (socialStatusChangeCategoryMapper.selectById(id) == null) {
            throw exception(SOCIAL_STATUS_CHANGE_CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public SocialStatusChangeCategoryDO getSocialStatusChangeCategory(Long id) {
        return socialStatusChangeCategoryMapper.selectById(id);
    }

    @Override
    public PageResult<SocialStatusChangeCategoryDO> getSocialStatusChangeCategoryPage(SocialStatusChangeCategoryPageReqVO pageReqVO) {
        return socialStatusChangeCategoryMapper.selectPage(pageReqVO);
    }

}