package com.ruoyi.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.demo.entity.Goods;
import com.ruoyi.demo.entity.bo.GoodsBo;
import com.ruoyi.demo.entity.bo.GoodsDeleteBo;
import com.ruoyi.demo.entity.vo.GoodsDetailVo;
import com.ruoyi.demo.entity.vo.GoodsListVo;
import com.ruoyi.demo.mapper.GoodsMapper;
import com.ruoyi.demo.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public int addGoods(Goods goods) {
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        goodsQueryWrapper.eq("appkey",goods.getAppkey());
        goodsQueryWrapper.eq("goodsId",goods.getGoodsId());
        Goods one = goodsMapper.selectOne(goodsQueryWrapper);
        if (one != null)
            return 2;
        goods.setCreateTime(LocalDateTime.now());
        return goodsMapper.insert(goods);
    }

    @Override
    public int deleteGoods(GoodsDeleteBo bo) {
        int i = 0;
        for (GoodsBo item : bo.getDeleteItems()) {
            QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
            goodsQueryWrapper.eq("appkey",item.getAppkey());
            goodsQueryWrapper.eq("goodsId",item.getGoodsId());
            i += goodsMapper.delete(goodsQueryWrapper);
        }
        return i;
    }

    @Override
    public List<GoodsListVo> getGoodsList(GoodsBo bo) {
        return null;
    }

    @Override
    public GoodsDetailVo getGoodsDetail(GoodsBo bo) {
        return null;
    }
}
