package com.seer.teach.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.seer.teach.common.enums.UserRelationEnum;
import com.seer.teach.user.entity.UserRelationEntity;
import com.seer.teach.user.mapper.UserFamilyMapper;
import com.seer.teach.user.mapper.UserRelationMapper;
import com.seer.teach.user.service.IUserFamilyService;
import com.seer.teach.user.service.IUserRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Wang
 * @since 2025-07-16 17:01:56
 */
@Service
@RequiredArgsConstructor
public class UserRelationServiceImpl extends ServiceImpl<UserRelationMapper, UserRelationEntity> implements IUserRelationService {

    private final UserFamilyMapper userFamilyMapper;

    @Override
    public Long getChildrenUserCountByUserId(Integer id) {
        return super.count(new LambdaQueryWrapper<>(UserRelationEntity.class).eq(UserRelationEntity::getUserId, id));
    }

    @Override
    public UserRelationEntity getUserRelationByRelationId(Integer relationId) {
        return super.getOne(new LambdaQueryWrapper<>(UserRelationEntity.class).eq(UserRelationEntity::getRelationId, relationId));
    }

    @Override
    public List<UserRelationEntity> getUserRelationByUserIdAndType(Integer userId, UserRelationEnum userRelationEnum) {
        return super.list(new LambdaQueryWrapper<>(UserRelationEntity.class).eq(UserRelationEntity::getUserId, userId)
                .eq(UserRelationEntity::getType, userRelationEnum.getCode()));
    }

    @Override
    public UserRelationEntity getUserRelationByUserIdAndRelationId(Integer userId, Integer relationId, UserRelationEnum userRelationEnum) {
        return super.getOne(new LambdaQueryWrapper<>(UserRelationEntity.class)
                .eq(UserRelationEntity::getRelationId, relationId)
                .eq(UserRelationEntity::getUserId, userId)
                .eq(UserRelationEntity::getType, userRelationEnum.getCode()));
    }

    @Override
    public List<UserRelationEntity> getUserRelationListByUserIdsAndRelation(Collection<Integer> ids, UserRelationEnum userRelationEnum) {
        return super.list(new LambdaQueryWrapper<>(UserRelationEntity.class)
                .in(UserRelationEntity::getUserId, ids)
                .eq(UserRelationEntity::getType, userRelationEnum.getCode()));
    }

    @Override
    public List<UserRelationEntity> getUserRelationsByUserId(Integer userId) {
        return super.list(new LambdaQueryWrapper<>(UserRelationEntity.class).eq(UserRelationEntity::getUserId, userId));
    }

    @Override
    public Integer getParentIdByChildrenId(Integer childrenId) {
        List<UserRelationEntity> list = list(new LambdaQueryWrapper<>(UserRelationEntity.class).eq(UserRelationEntity::getRelationId, childrenId)
                .eq(UserRelationEntity::getType, UserRelationEnum.PARENT_CHILD.getCode()));
        Integer familyId = list.get(0).getFamilyId();
        return userFamilyMapper.selectById(familyId).getPrimaryGuardianUserId();
    }

    @Override
    public UserRelationEntity getDefaultUserRelationByUserId(Integer userId) {
        return super.getOne(new LambdaQueryWrapper<>(UserRelationEntity.class).eq(UserRelationEntity::getUserId, userId).eq(UserRelationEntity::getIsDefault, UserRelationEntity.IS_DEFAULT));
    }

    @Override
    public boolean updateDefaultStatusByUserId(Integer userId, int isDefault) {
        return super.update(new LambdaUpdateWrapper<>(UserRelationEntity.class).set(UserRelationEntity::getIsDefault, isDefault).eq(UserRelationEntity::getUserId, userId));
    }

    @Override
    public boolean updateDefaultStatusByUserIdAndRelationId(Integer userId, Integer relationId, int isDefault) {
        return super.update(new LambdaUpdateWrapper<>(UserRelationEntity.class).set(UserRelationEntity::getIsDefault, isDefault)
                .eq(UserRelationEntity::getUserId, userId)
                .eq(UserRelationEntity::getRelationId, relationId));
    }

    @Override
    public List<UserRelationEntity> getUserRelationsByFamilyIdAndType(Integer familyId, UserRelationEnum userRelationEnum) {
        return super.list(new LambdaQueryWrapper<>(UserRelationEntity.class)
                .eq(UserRelationEntity::getFamilyId, familyId)
                .eq(UserRelationEntity::getType, userRelationEnum.getCode()));
    }

    @Override
    public List<UserRelationEntity> getUserRelationByUserIdAndFamilyId(Integer userId, Integer familyId) {
        return super.list(new LambdaQueryWrapper<>(UserRelationEntity.class)
                .eq(UserRelationEntity::getFamilyId, familyId)
                .eq(UserRelationEntity::getUserId, userId));
    }

    @Override
    public boolean deleteUserRelation(UserRelationEntity userRelation) {
        return super.remove(new LambdaQueryWrapper<>(UserRelationEntity.class)
                .eq(UserRelationEntity::getUserId, userRelation.getUserId())
                .eq(UserRelationEntity::getRelationId, userRelation.getRelationId())
                .eq(Objects.nonNull(userRelation.getType()),UserRelationEntity::getType, userRelation.getType()));
    }
}
