package io.renren.modules.admin.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.admin.dao.MemberFavoriteDao;
import io.renren.modules.admin.entity.MemberFavoriteEntity;
import io.renren.modules.admin.service.MemberFavoriteService;


@Service("memberFavoriteService")
public class MemberFavoriteServiceImpl extends ServiceImpl<MemberFavoriteDao, MemberFavoriteEntity> implements MemberFavoriteService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberFavoriteEntity> page = this.page(
                new Query<MemberFavoriteEntity>().getPage(params),
                new QueryWrapper<MemberFavoriteEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryFavoritePage(Integer userId, Map<String, Object> params) {
        IPage<MemberFavoriteEntity> page = this.page(
                new Query<MemberFavoriteEntity>().getPage(params),
                new QueryWrapper<MemberFavoriteEntity>().eq("member_id", userId).eq("type", 1)
        );

        return new PageUtils(page);
    }

    @Override
    public List<MemberFavoriteEntity> getRecentList(Integer userId) {
        return this.list(new QueryWrapper<MemberFavoriteEntity>().eq("member_id", userId).eq("type", 2).orderByDesc("id").last("limit 6"));
    }

    @Override
    public MemberFavoriteEntity getOneFavorite(Long memberid, Long did) {
        MemberFavoriteEntity favoriteEntity = this.getOne(
            new QueryWrapper<MemberFavoriteEntity>().eq("member_id", memberid).eq("did", did).eq("type", 1)
        );
        return favoriteEntity;
    }

    @Override
    public List<String> getHotWord() {
        List<Map<String, Object>> list = baseMapper.getHotWord();
        List<String> hList = new ArrayList<>();
        for (Map<String,Object> map : list) {
            hList.add((String) map.get("code"));
        }
        return hList;
    }

    @Override
    public MemberFavoriteEntity getOneRecent(Long did, Long memberId) {
        MemberFavoriteEntity memberFavoriteEntity = this.getOne(new QueryWrapper<MemberFavoriteEntity>().eq("member_id", memberId).eq("did", did).eq("type", 2));
        return memberFavoriteEntity;
    }
}