package dao.specification;

import dao.indentItemAbstractDAO;
import entities.IndentItem;
import org.jetbrains.annotations.NotNull;

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

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

public class indentItemDAO implements indentItemAbstractDAO {

    private Connection connection = null;
    private PreparedStatement preparedStatement = null;
    private ResultSet resultSet = null;

    //TODO 这里逻辑不对，用户点菜的时候不能自己设置折扣，可能需要再多一个实体才可以
    @Override
    public int insert(@NotNull IndentItem IItem) {
        int flag;
        String insert_sql = "insert into indentItem(name, initialPrice, discount, " +
                "finalPrice, description, indentID) values (?,?,?,?,?,?)";
        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(insert_sql);

            preparedStatement.setString(1, IItem.getName());
            preparedStatement.setFloat(2, IItem.getInitialPrice());
            //折扣和折后价null值的判断
            if (IItem.getDiscount().compareTo(0F) > 0) {//折扣和0比较,返回1为合法值
                preparedStatement.setFloat(3, IItem.getDiscount());
                IItem.setFinalPrice(IItem.getInitialPrice() * IItem.getDiscount());//计算最终价格
                preparedStatement.setFloat(4, IItem.getFinalPrice());
            } else {
                preparedStatement.setNull(3, Types.FLOAT);
                //最终价格等于初始价格
                preparedStatement.setFloat(4, IItem.getInitialPrice());
            }
            //描述
            if (IItem.getDescription() != null && !IItem.getDescription().equals(" ")) {
                preparedStatement.setString(5, IItem.getDescription());
            } else {
                preparedStatement.setNull(5, Types.NVARCHAR);
            }
            //菜单ID
            preparedStatement.setObject(6, IItem.getIndentID().getId());

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


    @Override
    public int delete(@NotNull IndentItem IItem) {
        String delete_sql = "delete from indentItem where id = ?";
        return Delete
                (connection, preparedStatement, resultSet, delete_sql, IItem.getId());
    }


    @Override//TODO 这个要考虑的太多了，先跳过去
    public int update(IndentItem IItem) {
        return 0;
    }

    @Override
    public List<IndentItem> search(IndentItem IItem) {
        List<IndentItem> list = new ArrayList<>();
        String selectAll_sql = "select * from indentItem";
        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(selectAll_sql);
            preparedStatement.executeQuery();

            while (resultSet.next()) {
                IItem = new IndentItem();
                SetAttribute(IItem, resultSet);
                list.add(IItem);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(connection, preparedStatement, resultSet);
        }
        return list;
    }

    @Override
    public IndentItem searchID(Long id) {
        IndentItem IItem = null;
        String selectID_sql = "select * from indentItem where id = ?";
        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(selectID_sql);
            preparedStatement.setLong(1, id);
            preparedStatement.executeQuery();
            while (resultSet.next()) {
                IItem = new IndentItem();
                SetAttribute(IItem, resultSet);
            }

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

    private void SetAttribute(@NotNull IndentItem item, @NotNull ResultSet rs) throws SQLException {
        item.setId(rs.getLong("id"));
        item.setName(rs.getString("name"));
        item.setInitialPrice(rs.getFloat("initialPrice"));
        item.setDiscount(rs.getFloat("discount"));
        item.setFinalPrice(rs.getFloat("finalPrice"));
        item.setDescription(rs.getString("description"));
    }
}
