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.vo.PageVo;
import com.fzm.mall.server.front.goods.model.vo.SaleHotVo;
import com.fzm.mall.server.front.user.model.Collections;
import com.fzm.mall.server.front.user.mapper.CollectionsMapper;
import com.fzm.mall.server.front.user.service.ICollectionsService;
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-05-24
 */
@Service
public class CollectionsServiceImpl extends ServiceImpl<CollectionsMapper, Collections> implements ICollectionsService {


    @Autowired
    private CollectionsMapper collectionsMapper;
    @Override
    public Collections getByUidAndGoodsId(String uid, String goodsId) {
        QueryWrapper<Collections> query = new QueryWrapper<>();
        query.eq("uid",uid);
        query.eq("goods_id",goodsId);
        return getOne(query);
    }

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

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

    @Override
    public Boolean isCollections(String uid, String goodsId) {
        QueryWrapper<Collections> query = new QueryWrapper<>();
        query.eq("uid",uid);
        query.eq("status",1);
        query.eq("goods_id",goodsId);
        int i = count(query);
        if(i == 0){
            return  false;
        }else{
            return  true;
        }
    }

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