package com.ruoyi.zhushi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.zhushi.entity.Inventory;
import com.ruoyi.zhushi.mapper.InventoryMapper;
import com.ruoyi.zhushi.service.InventoryService;
import com.ruoyi.zhushi.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class InventoryServiceImpl implements InventoryService {

    @Autowired
    private InventoryMapper inventoryMapper;

    @Override
    public boolean saveInventory(List<Inventory> inventorys) {
        for (Inventory inventory:inventorys) {
            //先删除
            QueryWrapper<Inventory> delWrapper = new QueryWrapper();
            delWrapper.eq("url", inventory.getUrl());
            int i =  inventoryMapper.delete(delWrapper);
            //在插入
            String time = DateUtils.getTime();
            inventory.setCreateDate(time);
            inventory.setUpdateDate(time);
            inventoryMapper.insert(inventory);
        }
        return true;
    }

    @Override
    public boolean deleteInventoryById(String id) {
        return inventoryMapper.deleteById(id) > 0;
    }

    @Override
    public boolean updateInventory(Inventory inventory) {
        String time = DateUtils.getTime();
        inventory.setUpdateDate(time);
        return inventoryMapper.updateById(inventory) > 0;
    }

    @Override
    public Inventory getInventoryById(String id) {
        return inventoryMapper.selectById(id);
    }

    @Override
    public List<Inventory> getAllInventories() {
        return inventoryMapper.selectList(null);
    }

    @Override
    public boolean updateInventoryByUrl(List<Inventory> inventorys) {
        for (Inventory inventory:inventorys) {
            String url = inventory.getUrl();
            String time = DateUtils.getTime();
            inventory.setUpdateDate(time);
            UpdateWrapper<Inventory> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("url", url);
            inventoryMapper.update(inventory, updateWrapper);
        }
        return true;
    }

    @Override
    public List<Inventory> getInventoriesByParam(String queryWord, String queryDate, String type) {
        return inventoryMapper.getInventoriesByParam(queryWord,queryDate,type);
    }
}
