package com.gxwebsoft.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.shop.entity.UserFollow;
import com.gxwebsoft.shop.mapper.UserFollowMapper;
import com.gxwebsoft.shop.param.UserFollowParam;
import com.gxwebsoft.shop.service.UserFollowService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 关注记录表Service实现
 *
 * @author 科技小王子
 * @since 2022-11-25 19:04:43
 */
@Service
public class UserFollowServiceImpl extends ServiceImpl<UserFollowMapper, UserFollow> implements UserFollowService {
    @Resource
    UserFollowService userFollowService;

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

    @Override
    public List<UserFollow> listRel(UserFollowParam param) {
        List<UserFollow> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<UserFollow, UserFollowParam> page = new PageParam<>();
        page.setDefaultOrder("create_time desc");
        // 我关注的人
        final List<UserFollow> follows = userFollowService.list(new LambdaQueryWrapper<UserFollow>().eq(UserFollow::getUserId, param.getLoginUserId()));
        final Set<Integer> collect = follows.stream().map(UserFollow::getShopId).collect(Collectors.toSet());
        System.out.println("collect = " + collect);
        list.forEach(d -> {
            d.setEachFollow(false);
            for (Integer shopId : collect) {
              if(Objects.equals(shopId, d.getUserId())){
                d.setEachFollow(true);
              }
            }
          });
        return page.sortRecords(list);
    }

    @Override
    public UserFollow getByIdRel(Integer logId) {
        UserFollowParam param = new UserFollowParam();
        param.setLogId(logId);
        return param.getOne(baseMapper.selectListRel(param));
    }

}
