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.Foot;
import com.fzm.mall.server.front.user.mapper.FootMapper;
import com.fzm.mall.server.front.user.model.vo.FootGoodsVo;
import com.fzm.mall.server.front.user.service.IFootService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 用户浏览记录表 服务实现类
 * </p>
 *
 * @author fzm
 * @since 2021-05-24
 */
@Service
public class FootServiceImpl extends ServiceImpl<FootMapper, Foot> implements IFootService {

    @Autowired
    private FootMapper footMapper;

    @Override
    public void addRecord(String uid, String goodsId) {
        QueryWrapper<Foot> q = new QueryWrapper<>();
        q.eq("uid",uid);
        q.eq("goods_id",goodsId);
        Foot foot = getOne(q);
        if(foot == null){
            foot = new Foot();
            foot.setGoodsId(goodsId);
            foot.setUid(uid);
            save(foot);
        }else{
            foot.setUpdateTime(System.currentTimeMillis());
            foot.setIsDelete(0);
            updateById(foot);
        }
    }

    @Override
    public Integer getCount(String uid) {
        QueryWrapper<Foot> q = new QueryWrapper<>();
        q.eq("uid",uid);
        q.eq("is_delete",0);
        return count(q);
    }

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

    @Override
    public void delFoot(String uid, String[] split) {
        UpdateWrapper<Foot> q = new UpdateWrapper<>();
        q.eq("uid",uid);
        List<String> list = Arrays.asList(split);
        q.in("goods_id",list);
        q.set("is_delete",1);
        update(q);
    }
}
