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

import cn.hutool.core.util.StrUtil;
import co.yixiang.yshop.module.member.controller.admin.user.vo.UserPageReqVO;
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 java.util.stream.Collectors;

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

import co.yixiang.yshop.module.member.convert.usertagref.UserTagRefConvert;
import co.yixiang.yshop.module.member.dal.mysql.usertagref.UserTagRefMapper;

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 UserTagRefServiceImpl implements UserTagRefService {

    @Resource
    private UserTagRefMapper userTagRefMapper;

    @Override
    public Long createUserTagRef(UserTagRefCreateReqVO createReqVO) {
        // 插入
        UserTagRefDO userTagRef = UserTagRefConvert.INSTANCE.convert(createReqVO);
        userTagRefMapper.insert(userTagRef);
        // 返回
        return userTagRef.getId();
    }

    @Override
    public void updateUserTagRef(UserTagRefUpdateReqVO updateReqVO) {
        // 校验存在
        validateUserTagRefExists(updateReqVO.getId());
        // 更新
        UserTagRefDO updateObj = UserTagRefConvert.INSTANCE.convert(updateReqVO);
        userTagRefMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserTagRef(Long id) {
        // 校验存在
        validateUserTagRefExists(id);
        // 删除
        userTagRefMapper.deleteById(id);
    }

    private void validateUserTagRefExists(Long id) {
        if (userTagRefMapper.selectById(id) == null) {
            throw exception(USER_TAG_REF_NOT_EXISTS);
        }
    }

    @Override
    public UserTagRefDO getUserTagRef(Long id) {
        return userTagRefMapper.selectById(id);
    }

    @Override
    public List<UserTagRefDO> getUserTagRefList(Collection<Long> ids) {
        return userTagRefMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<UserTagRefDO> getUserTagRefPage(UserTagRefPageReqVO pageReqVO) {
        return userTagRefMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserTagRefDO> getUserTagRefList(UserTagRefExportReqVO exportReqVO) {
        return userTagRefMapper.selectList(exportReqVO);
    }

    @Override
    public List<Long> getUserIdByTagName(UserPageReqVO pageReqVO) {
        List<Long> userIds = null;
        if (StrUtil.isNotBlank(pageReqVO.getTagName())) {
            List<UserTagRefDO> userTagRefDOList = this.getUserTagRefList(UserTagRefExportReqVO.builder().tagName(pageReqVO.getTagName()).build());
            userIds = userTagRefDOList.stream().map(UserTagRefDO::getUserId).distinct().collect(Collectors.toList());
        }
        return userIds;
    }

    @Override
    public void insertBatch(List<UserTagRefDO> doList) {
        userTagRefMapper.insertBatch(doList);
    }

    @Override
    public void deleteByUserId(Long id) {
        userTagRefMapper.delete(Wrappers.<UserTagRefDO>lambdaUpdate().eq(UserTagRefDO::getUserId, id));
    }

    @Override
    public Long selectCountByTagId(Long id) {
        return userTagRefMapper.selectCount(UserTagRefDO::getTagId, id);
    }

}
