package com.yolo.keyboard.service.userinvitecodes;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.yolo.keyboard.controller.admin.userinvitecodes.vo.*;
import com.yolo.keyboard.dal.dataobject.userinvitecodes.KeyboardUserInviteCodesDO;
import com.yolo.keyboard.framework.common.pojo.PageResult;
import com.yolo.keyboard.framework.common.pojo.PageParam;
import com.yolo.keyboard.framework.common.util.object.BeanUtils;

import com.yolo.keyboard.dal.mysql.userinvitecodes.KeyboardUserInviteCodesMapper;

import static com.yolo.keyboard.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yolo.keyboard.framework.common.util.collection.CollectionUtils.convertList;
import static com.yolo.keyboard.framework.common.util.collection.CollectionUtils.diffList;
import static com.yolo.keyboard.module.infra.enums.ErrorCodeConstants.USER_INVITE_CODES_NOT_EXISTS;

/**
 * 用户生成的邀请码表，用于邀请新用户注册/安装并建立邀请关系 Service 实现类
 *
 * @author ziin
 */
@Service
@Validated
public class KeyboardUserInviteCodesServiceImpl implements KeyboardUserInviteCodesService {

    @Resource
    private KeyboardUserInviteCodesMapper userInviteCodesMapper;

    @Override
    public Long createUserInviteCodes(KeyboardUserInviteCodesSaveReqVO createReqVO) {
        // 插入
        KeyboardUserInviteCodesDO userInviteCodes = BeanUtils.toBean(createReqVO, KeyboardUserInviteCodesDO.class);
        userInviteCodesMapper.insert(userInviteCodes);

        // 返回
        return userInviteCodes.getId();
    }

    @Override
    public void updateUserInviteCodes(KeyboardUserInviteCodesSaveReqVO updateReqVO) {
        // 校验存在
        validateUserInviteCodesExists(updateReqVO.getId());
        // 更新
        KeyboardUserInviteCodesDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardUserInviteCodesDO.class);
        userInviteCodesMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserInviteCodes(Long id) {
        // 校验存在
        validateUserInviteCodesExists(id);
        // 删除
        userInviteCodesMapper.deleteById(id);
    }

    @Override
        public void deleteUserInviteCodesListByIds(List<Long> ids) {
        // 删除
        userInviteCodesMapper.deleteByIds(ids);
        }


    private void validateUserInviteCodesExists(Long id) {
        if (userInviteCodesMapper.selectById(id) == null) {
            throw exception(USER_INVITE_CODES_NOT_EXISTS);
        }
    }

    @Override
    public KeyboardUserInviteCodesDO getUserInviteCodes(Long id) {
        return userInviteCodesMapper.selectById(id);
    }

    @Override
    public PageResult<KeyboardUserInviteCodesDO> getUserInviteCodesPage(KeyboardUserInviteCodesPageReqVO pageReqVO) {
        return userInviteCodesMapper.selectPage(pageReqVO);
    }

}