package dao.specification;

import dao.indentAbstractDAO;
import entities.Indent;
import org.jetbrains.annotations.NotNull;
import view.IndentItemView;

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

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

public class indentDAO implements indentAbstractDAO {

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

    @Override
    public int insert(@NotNull Indent indent) {
        int flag;
        //FIXME
        String insert_sql = "insert into Indent(userID, merchantsID,allPrice, message, createDate) " +
                "values(?,?,0,?,getdate()) ";
        String SetAllPrice = "update Indent " +
                "set allPrice = (select SUM(finalPrice) " +
                "from indentItem where Indent.id = indentItem.indentID group by indentID)" +
                "from indentItem where indentID = Indent.id and Indent.id = ?";
        String selectID = "select MAX(Indent.id) from Indent";

        try {
            IndentItemView iiv = new IndentItemView();
            connection = getConnection();
            preparedStatement = connection.prepareStatement(insert_sql);
            //用户ID
            preparedStatement.setLong(1, indent.getUserID().getId());
            //商家ID
            preparedStatement.setLong(2, indent.getMerchantsID().getId());
            //备注
            if (indent.getMessage() != null && !Objects.equals(indent.getMessage(), " ")) {
                preparedStatement.setString(3, indent.getMessage());
            } else {
                preparedStatement.setNull(3, Types.NVARCHAR);
            }

            flag = preparedStatement.executeUpdate();

            //创建订单中的菜品
            preparedStatement = connection.prepareStatement(selectID);
            resultSet = preparedStatement.executeQuery();
            //新的订单对象，获取当前订单ID
            indent = new Indent();
            while (resultSet.next()) {
                indent.setId(resultSet.getLong(1));
                iiv.CreateIItem(indent.getId());
            }
            //更新总价
            preparedStatement = connection.prepareStatement(SetAllPrice);
            preparedStatement.setLong(1, indent.getId());
            preparedStatement.executeUpdate();

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

        return flag;
    }

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

    @Override
    public int update(@NotNull Indent indent) {
        int flag;
        String update_sql = "update Indent " +
                "set userID = ?, merchantsID = ?, allPrice = ?, message = ?, " +
                "createDate = getdate() where id = ?";
        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(update_sql);

            preparedStatement.setObject(1, indent.getUserID());
            preparedStatement.setObject(2, indent.getMerchantsID());
            preparedStatement.setFloat(3, indent.getAllPrice());
            preparedStatement.setString(4, indent.getMessage());
            preparedStatement.setLong(5, indent.getId());

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

        return flag;
    }

    @Override
    public List<Indent> search(Indent indent) {
        List<Indent> list = new ArrayList<>();
        String selectAll_sql = "select * from Indent";

        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(selectAll_sql);
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                indent = new Indent();
                SetAttribute(indent, resultSet);
                list.add(indent);
            }

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

        return list;
    }

    @Override
    public Indent searchID(Long id) {
        Indent indent = null;
        String selectID_sql = "select * from Indent where id = ?";

        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(selectID_sql);
            preparedStatement.setLong(1, id);
            resultSet = preparedStatement.executeQuery();

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

        return indent;
    }

    private void SetAttribute(@NotNull Indent indent, @NotNull ResultSet rs) throws SQLException {
        indent.setId(rs.getLong("id"));

        //结果集是 Long 型数据
        userDAO user = new userDAO();
        indent.setUserID(user.searchID(rs.getLong("userID")));
        merchantsDAO merchant = new merchantsDAO();
        indent.setMerchantsID(merchant.searchID(rs.getLong("merchantsID")));

        indent.setAllPrice(rs.getFloat("allPrice"));
        indent.setMessage(rs.getString("message"));
        indent.setCreatedDate(rs.getDate("createDate"));
    }
}
