package dao.specification;

import dao.merchantsAbstractDAO;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import entities.Merchants;

import java.util.ArrayList;
import java.util.List;
import java.sql.*;

import static dao.specification.abstractDAO.Delete;
import static util.SQLDatabaseConnection.close;
import static util.SQLDatabaseConnection.getConnection;

public class merchantsDAO implements merchantsAbstractDAO {
    private Connection connection = null;
    private PreparedStatement preparedStatement = null;
    private ResultSet resultSet = null;

    //新加商家
    @Override
    public int insert(@NotNull Merchants merchants) {
        int flag;
        String insert_sql = "insert into Merchants(name,address,description,phoneNumber) " +
                "values(?,?,?,?)";
        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(insert_sql);

            preparedStatement.setString(1, merchants.getName());
            preparedStatement.setString(2, merchants.getAddress());

            //NULL值判断和设定
            if (merchants.getDescription().equals(" ")) {
                preparedStatement.setString(3, merchants.getDescription());
            } else {
                preparedStatement.setNull(3, Types.NVARCHAR);
            }

            if (merchants.getPhoneNumber().equals(" ")) {
                preparedStatement.setString(4, merchants.getPhoneNumber());
            } else {
                preparedStatement.setNull(4, Types.VARCHAR);
            }

            flag = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(connection, preparedStatement, resultSet);
        }
        return flag;
    }

    //删除商家
    @Override
    public int delete(@NotNull Merchants merchants) {
        String delete_sql = "delete from Merchants where id = ?";
        return Delete
                (connection, preparedStatement, resultSet, delete_sql, merchants.getId());
    }

    //更新信息
    //@TODO 只能更新所有信息，不能选择性更新
    @Override
    public int update(@NotNull Merchants merchants) {
        int flag;
        String update_sql = "update Merchants " +
                "set name = ?, address = ?, description = ?, phoneNumber= ? " +
                "where id = ?";
        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(update_sql);

            preparedStatement.setString(1, merchants.getName());
            preparedStatement.setString(2, merchants.getAddress());
            preparedStatement.setString(3, merchants.getDescription());
            preparedStatement.setString(4, merchants.getPhoneNumber());
            preparedStatement.setLong(5, merchants.getId());

            flag = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return flag;
    }

    //查询所有商家
    @Override
    public List<Merchants> search(Merchants merchants) {
        List<Merchants> list = new ArrayList<>();
        String selectAll_sql = "select * from Merchants";

        try {
            connection = getConnection();

            preparedStatement = connection.prepareStatement(selectAll_sql);
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                merchants = new Merchants();
                SetAttribute(merchants, resultSet);
                list.add(merchants);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(connection, preparedStatement, resultSet);
        }
        return list;
    }


    //通过id寻找商家
    @Override
    public Merchants searchID(Long id) {
        Merchants merchants = null;
        String selectID_sql = "select * from Merchants where id = ?";
        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(selectID_sql);
            preparedStatement.setLong(1, id);
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                merchants = new Merchants();
                SetAttribute(merchants, resultSet);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(connection, preparedStatement, resultSet);
        }
        return merchants;
    }

    //重构，合并方法
    private void SetAttribute(@NotNull Merchants merchants, @NotNull ResultSet rs) throws SQLException {
        merchants.setId(rs.getLong("id"));
        merchants.setName(rs.getString("name"));
        merchants.setPhoneNumber(rs.getString("phoneNumber"));
        merchants.setAddress(rs.getString("address"));
        merchants.setDescription(rs.getString("description"));
    }
}
