package com.fzm.mall.server.front.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fzm.mall.server.front.goods.model.Shop;
import com.fzm.mall.server.front.goods.model.vo.PageVo;
import com.fzm.mall.server.front.user.model.Follow;
import com.fzm.mall.server.front.user.mapper.FollowMapper;
import com.fzm.mall.server.front.user.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fzm
 * @since 2021-04-01
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {


    @Autowired
    private FollowMapper followMapper;

    @Override
    public Follow getByUidAndMerchantId(String uid, String merchantId) {
        QueryWrapper<Follow> query = new QueryWrapper<>();
        query.eq("uid",uid);
        query.eq("merchant_id",merchantId);
        return getOne(query);
    }

    @Override
    public Boolean isFollow(String uid, String merchantId) {
        QueryWrapper<Follow> query = new QueryWrapper<>();
        query.eq("uid",uid);
        query.eq("merchant_id",merchantId);
        query.eq("status",1);
        Follow follow = getOne(query);
        if(follow == null){
            return false;
        }else{
            return  true;
        }
    }

    @Override
    public void delFollows(String uid, String[] split) {
        UpdateWrapper<Follow> query = new UpdateWrapper<>();
        query.eq("uid",uid);
        List<String> list = Arrays.asList(split);
        query.in("merchant_id",list);
        query.set("status",2);
        update(query);
    }

    @Override
    public List<Shop> getFollow(String uid, PageVo pageVo) {
        int start = (pageVo.getPage() - 1) * pageVo.getSize();
        return followMapper.getFollow(uid ,start ,pageVo.getSize());
    }

    @Override
    public Integer getCount(String uid) {
        QueryWrapper<Follow> query = new QueryWrapper<>();
        query.eq("uid",uid);
        query.eq("status",1);
        return count(query);
    }

    @Override
    public Integer getCountBymerchantId(String merchantId) {
        QueryWrapper<Follow> query = new QueryWrapper<>();
        query.eq("merchant_id",merchantId);
        query.eq("status",1);
        return count(query);
    }
}
