package com.example.logistics.manager;

import android.util.Log;

import com.example.logistics.tools.DBOpenHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class operationManager {
    private static final String TAG = "operationDao";

    public boolean add(String pickupcode, String phonenum){
        HashMap<String, Object> map = new HashMap<>();

        Connection connection = DBOpenHelper.getConn();
        try{
            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");

            Date date = new Date();

            String time = format1.format(date);

            String sql = "insert into operations(pickupcode, phonenum, time) values (?,?,?)";

            if(connection != null){
                PreparedStatement ps = connection.prepareStatement(sql);

                if(ps != null){
                    ps.setString(1, pickupcode);
                    ps.setString(2, phonenum);
                    ps.setString(3, time);

                    int rs = ps.executeUpdate();
                    if(rs > 0){
                        return true;
                    }else{
                        return false;
                    }
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }catch(Exception e){
            e.printStackTrace();
            Log.e(TAG, "异常 add operation");
            return false;
        }
    }

    public List<Map<String, Object>> history(String phonenum){

        Connection connection = DBOpenHelper.getConn();

        List<Map<String, Object>> datalist;
        datalist = new ArrayList<Map<String, Object>>();
        try{
            String sql = "select * from operations where phonenum = ?";

            if(connection != null){
                PreparedStatement ps = connection.prepareStatement(sql);
                if(ps != null){
                    Log.e(TAG, "phonenum: " + phonenum);
                    ps.setString(1,phonenum);
                    ResultSet rs = ps.executeQuery();

                    int count = rs.getMetaData().getColumnCount();
                    while(rs.next()){
                        Map<String, Object> map = new HashMap<String, Object>();
                        for(int i = 1; i <= count; i++){
                            String field = rs.getMetaData().getColumnName(i);
                            if(field.equals("pickupcode")){
                                map.put(field, rs.getString(field));
                            }
                            if(field.equals("time")) {
                                map.put(field, rs.getString(field));
                            }
                        }
                        datalist.add(map);
                    }
                    connection.close();
                    ps.close();
                }else{
                    Log.e(TAG, "ps is null");
                }
            }else{
                Log.e(TAG, "connection is null");
            }
        }catch (Exception e){
            e.printStackTrace();
            Log.d(TAG, "search bug: " + e.getMessage());
        }
        return datalist;
    }
}
