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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.usertaggroup.vo.*;
import co.yixiang.yshop.module.member.dal.dataobject.usertaggroup.UserTagGroupDO;
import co.yixiang.yshop.framework.common.pojo.PageResult;

import co.yixiang.yshop.module.member.convert.usertaggroup.UserTagGroupConvert;
import co.yixiang.yshop.module.member.dal.mysql.usertaggroup.UserTagGroupMapper;

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 UserTagGroupServiceImpl implements UserTagGroupService {

    @Resource
    private UserTagGroupMapper userTagGroupMapper;

    @Override
    public Long createUserTagGroup(UserTagGroupCreateReqVO createReqVO) {
        // 插入
        UserTagGroupDO userTagGroup = UserTagGroupConvert.INSTANCE.convert(createReqVO);
        userTagGroupMapper.insert(userTagGroup);
        // 返回
        return userTagGroup.getId();
    }

    @Override
    public void updateUserTagGroup(UserTagGroupUpdateReqVO updateReqVO) {
        // 校验存在
        validateUserTagGroupExists(updateReqVO.getId());
        // 更新
        UserTagGroupDO updateObj = UserTagGroupConvert.INSTANCE.convert(updateReqVO);
        userTagGroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserTagGroup(Long id) {
        // 校验存在
        validateUserTagGroupExists(id);
        // 删除
        userTagGroupMapper.deleteById(id);
    }

    private void validateUserTagGroupExists(Long id) {
        if (userTagGroupMapper.selectById(id) == null) {
            throw exception(USER_TAG_GROUP_NOT_EXISTS);
        }
    }

    @Override
    public UserTagGroupDO getUserTagGroup(Long id) {
        return userTagGroupMapper.selectById(id);
    }

    @Override
    public List<UserTagGroupDO> getUserTagGroupList() {
        return userTagGroupMapper.selectList(Wrappers.<UserTagGroupDO>lambdaQuery().orderByAsc(UserTagGroupDO::getSorted));
    }

    @Override
    public PageResult<UserTagGroupDO> getUserTagGroupPage(UserTagGroupPageReqVO pageReqVO) {
        return userTagGroupMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserTagGroupDO> getUserTagGroupList(UserTagGroupExportReqVO exportReqVO) {
        return userTagGroupMapper.selectList(exportReqVO);
    }

}
