package com.gxwebsoft.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.cms.mapper.FollowMapper;
import com.gxwebsoft.cms.service.FollowService;
import com.gxwebsoft.cms.entity.Follow;
import com.gxwebsoft.cms.param.FollowParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 关注Service实现
 *
 * @author 科技小王子
 * @since 2024-12-11 18:25:28
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements FollowService {

    @Override
    public PageResult<Follow> pageRel(FollowParam param) {
        PageParam<Follow, FollowParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, create_time desc");
        List<Follow> list = baseMapper.selectPageRel(page, param);
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<Follow> listRel(FollowParam param) {
        List<Follow> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<Follow, FollowParam> page = new PageParam<>();
        page.setDefaultOrder("sort_number asc, create_time desc");
        return page.sortRecords(list);
    }

    @Override
    public Follow getByIdRel(Integer id) {
        FollowParam param = new FollowParam();
        param.setId(id);
        return param.getOne(baseMapper.selectListRel(param));
    }

    @Override
    public Follow check(Integer userId, Integer followUserId) {
        FollowParam param = new FollowParam();
        param.setUserId(userId);
        param.setFollowUserId(followUserId);
        return param.getOne(baseMapper.selectListRel(param));
    }

    @Override
    public Integer countByFollowUser(Integer followUserId) {
        return count(
                new LambdaQueryWrapper<Follow>()
                        .eq(Follow::getFollowUserId, followUserId)
        );
    }

}
