package com.ruoyi.zhushi.job;

import com.ruoyi.zhushi.entity.Inventory;
import com.ruoyi.zhushi.entity.InventoryBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

@Component
public class InventoryJob {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Scheduled(cron = "0 0 19 * * ?")
    public void exportDataToFile() {
        List<Inventory> data = jdbcTemplate.query("SELECT * FROM inventory", (rs, rowNum) -> {
            Inventory inventory = new Inventory();
            inventory.setId(rs.getString("id"));
            inventory.setTitle(rs.getString("title"));
            inventory.setUrl(rs.getString("url"));
            inventory.setImage(rs.getString("image"));
            inventory.setShopName(rs.getString("shop_name"));
            inventory.setEnableQuantity(rs.getString("enable_quantity"));
            inventory.setPrice(rs.getString("price"));
            inventory.setRemark(rs.getString("remark"));
            inventory.setCreateDate(rs.getString("update_date"));
            inventory.setUpdateDate(rs.getString("update_date"));
            return inventory;
        });

        try (FileWriter writer = new FileWriter("inventory.sql")) {
            for (Inventory inventory : data) {
                writer.write("INSERT INTO inventory (id, title, url, image, " +
                        "shop_name, enable_quantity, price, remark, create_date, update_date) VALUES ('" +
                        inventory.getId() + "', '" +inventory.getTitle() + "', '" +
                        inventory.getUrl() + "', '" + inventory.getImage() + "', '" +
                        inventory.getShopName() + "', '" +inventory.getEnableQuantity() +
                        "', '" + inventory.getPrice() + "', '" + inventory.getRemark() + "', '" +
                        inventory.getCreateDate()+ inventory.getUpdateDate() + "');\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Scheduled(cron = "0 10 19 * * ?")
    public void exportDataToFile2() {
        List<InventoryBase> data = jdbcTemplate.query("SELECT * FROM inventorybase", (rs, rowNum) -> {
            InventoryBase inventory = new InventoryBase();
            inventory.setId(rs.getString("id"));
            inventory.setUrl(rs.getString("url"));
            inventory.setType(rs.getString("type"));
            inventory.setRemark(rs.getString("remark"));
            return inventory;
        });

        try (FileWriter writer = new FileWriter("inventorybase.sql")) {
            for (InventoryBase inventoryBase : data) {
                writer.write("INSERT INTO inventorybase (id, url, type, remark) VALUES ('" +
                        inventoryBase.getId() + "', '" +
                        inventoryBase.getUrl() + "', '" +
                        inventoryBase.getType() + "', '" +
                        inventoryBase.getRemark() + "', '" +
                        inventoryBase.getRemark() + "');\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
