package com.qnfhq.modules.c2c.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qnfhq.common.constant.Constant;
import com.qnfhq.common.page.PageData;
import com.qnfhq.common.service.impl.BaseServiceImpl;
import com.qnfhq.modules.c2c.dao.UserFollowsDao;
import com.qnfhq.modules.c2c.entity.UserFollowsEntity;
import com.qnfhq.modules.c2c.service.UserFollowsService;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

/**
 * c2c用户和商家关注关系
 *
 * @author yelz 30262728@qq.com
 * @since 1.0.0 2025-11-29
 */
@Service
public class UserFollowsServiceImpl extends BaseServiceImpl<UserFollowsDao, UserFollowsEntity> implements UserFollowsService {

    @Override
    public UserFollowsEntity selectByFollowerIdFollowingId(Long followerId,Long followingId) {
        return baseDao.selectOne(new LambdaQueryWrapper<UserFollowsEntity>()
                .eq(UserFollowsEntity::getFollowerId, followerId)
                .eq(UserFollowsEntity::getFollowingId, followingId));
    }

    @Override
    public void deleteByFollowerIdFollowingId(Long followerId,Long followingId) {
        baseDao.delete(new LambdaQueryWrapper<UserFollowsEntity>()
                .eq(UserFollowsEntity::getFollowerId, StpUtil.getLoginIdAsLong())
                .eq(UserFollowsEntity::getFollowingId, followingId));
    }

    @Override
    public PageData<UserFollowsEntity> selectPageByFollowerId(Map<String, Object> params) {
        Long followerId = (Long)params.get("followerId");

        LambdaQueryWrapper<UserFollowsEntity> wrapper = new LambdaQueryWrapper<UserFollowsEntity>()
                .eq(Objects.nonNull(followerId),UserFollowsEntity::getFollowerId, followerId);

        IPage<UserFollowsEntity> page = baseDao.selectPage(
                getPage(params, (String)params.get(Constant.ORDER_FIELD), "asc".equals(params.get(Constant.ORDER)) ? true : false),
                wrapper
        );
        return getPageData(page, UserFollowsEntity.class);
    }

    @Override
    public PageData<UserFollowsEntity> selectPageByFollowingId(Map<String, Object> params) {
        Long followingId = (Long)params.get("followingId");

        LambdaQueryWrapper<UserFollowsEntity> wrapper = new LambdaQueryWrapper<UserFollowsEntity>()
                .eq(Objects.nonNull(followingId),UserFollowsEntity::getFollowingId, followingId);

        IPage<UserFollowsEntity> page = baseDao.selectPage(
                getPage(params, (String)params.get(Constant.ORDER_FIELD), "asc".equals(params.get(Constant.ORDER)) ? true : false),
                wrapper
        );
        return getPageData(page, UserFollowsEntity.class);
    }


}