package com.sdut.labex.common;

import com.sdut.labex.entity.LogEntity;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.concurrent.Callable;

/**
 * File: LogThread
 * Created: 2023/6/21
 * Author: springforest
 * Description:
 */

public class LogThread implements Callable<String> {
    private LogEntity log;
    private DataSource dataSource;

    public LogThread(LogEntity log, DataSource dataSource) {
        this.log = log;
        this.dataSource = dataSource;
    }

    @Override
    public String call() throws Exception {
        String sql = "insert into log(content,date,cost) values(?,?,?)";
        try (Connection conn = dataSource.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {

            ps.setString(1, log.getContent());
            ps.setString(2, log.getDate());
            ps.setLong(3, log.getCost());
            int count = ps.executeUpdate();
            return count > 0 ? "success" : "fail";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
