package com.arrebol.taxiservicios.controller.sp.report;

import com.arrebol.taxiservicios.controller.connection.ConnectionManager;
import com.arrebol.taxiservicios.controller.util.HibernateUtil;
import com.arrebol.taxiservicios.model.sp.report.ReportGoogleDrive;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Oscar Vargas
 */
public class ReportGoogleDriveController extends ConnectionManager implements Serializable {

    /**
     *
     * @param starDate
     * @return
     */
    public List<ReportGoogleDrive> callReportGoogleDriveQueryMySQL(String starDate, String idLocation) {
        LOG.info("callReportGoogleDrive");

        List<ReportGoogleDrive> list = new ArrayList<>();
        Transaction transaction = null;

        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();

            List<Object> rows = session.createNativeQuery(queryMySQL)
                    .setParameter("starDate", starDate)
                    .setParameter("idLocation", idLocation)
                    .getResultList();

            transaction.commit();

            rows.forEach((row) -> {
                Object[] results = (Object[]) row;

                ReportGoogleDrive pay = new ReportGoogleDrive(
                        null == results[0] ? null : results[0].toString(),
                        null == results[1] ? null : results[1].toString(),
                        null == results[2] ? null : results[2].toString(),
                        null == results[3] ? null : results[3].toString(),
                        Float.valueOf(null == results[4] ? "0" : results[4].toString()),
                        Float.valueOf(null == results[5] ? "0" : results[5].toString()),
                        Float.valueOf(null == results[6] ? "0" : results[6].toString()),
                        Float.valueOf(null == results[7] ? "0" : results[7].toString()),
                        Float.valueOf(null == results[8] ? "0" : results[8].toString()),
                        Float.valueOf(null == results[9] ? "0" : results[9].toString()),
                        Float.valueOf(null == results[10] ? "0" : results[10].toString()),
                        Float.valueOf(null == results[11] ? "0" : results[11].toString()),
                        Float.valueOf(null == results[12] ? "0" : results[12].toString()),
                        Float.valueOf(null == results[13] ? "0" : results[13].toString()),
                        Float.valueOf(null == results[14] ? "0" : results[14].toString()),
                        Float.valueOf(null == results[15] ? "0" : results[15].toString()),
                        Float.valueOf(null == results[16] ? "0" : results[16].toString()),
                        Float.valueOf(null == results[17] ? "0" : results[17].toString()),
                        Integer.valueOf(null == results[18] ? "0" : results[18].toString()),
                        Integer.valueOf(null == results[19] ? "0" : results[19].toString()),
                        Integer.valueOf(null == results[20] ? "0" : results[20].toString()),
                        Integer.valueOf(null == results[21] ? "0" : results[21].toString()),
                        Integer.valueOf(null == results[22] ? "0" : results[22].toString()),
                        Integer.valueOf(null == results[23] ? "0" : results[23].toString()),
                        
                        Integer.valueOf(null == results[24] ? "0" : results[24].toString()),
                        Integer.valueOf(null == results[25] ? "0" : results[25].toString()),
                        Integer.valueOf(null == results[26] ? "0" : results[26].toString()),
                        Integer.valueOf(null == results[27] ? "0" : results[27].toString()),
                        Integer.valueOf(null == results[28] ? "0" : results[28].toString()),
                        Integer.valueOf(null == results[29] ? "0" : results[29].toString()),
                        Integer.valueOf(null == results[30] ? "0" : results[30].toString()),
                        Integer.valueOf(null == results[31] ? "0" : results[31].toString()),
                        Integer.valueOf(null == results[32] ? "0" : results[32].toString()),
                        Integer.valueOf(null == results[33] ? "0" : results[33].toString()),
                        Integer.valueOf(null == results[34] ? "0" : results[34].toString()),
                        Integer.valueOf(null == results[35] ? "0" : results[35].toString()),
                        Integer.valueOf(null == results[36] ? "0" : results[36].toString()),
                        Integer.valueOf(null == results[37] ? "0" : results[37].toString())
                );

                list.add(pay);
            });

            LOG.info("Success callReportGoogleDrive");
        } catch (HibernateException e) {
            LOG.error("Report google drive", e);
            rollback(transaction);
        } catch (Exception e) {
            LOG.error("Method callReportGoogleDrive(" + starDate + ") ", e);
            rollback(transaction);
        }
        return list;
    }

    private static final long serialVersionUID = 3405798902357776578L;

    final Logger LOG = LogManager.getLogger(ReportGoogleDriveController.class);

    private final String queryMySQL = "SELECT  "
            + "CONCAT(p.first_name, ' ', p.last_name) driver_name, "
            + "IF(p.active = 1, 'Activo', IF(p.black_list = 'ENABLED', 'Lista negra' , 'Banca')) estatus_driver, "
            + "IF(ISNULL(t.id), 'Sin vehiculo', CONCAT(t.economic_number)) vehicle_name, "
            + "CONCAT(p2.first_name, ' ', p2.last_name) customer_name, "
            + "(SELECT IF(ISNULL(SUM(total)) , 0 , SUM(total)) FROM TO_COLLECT WHERE driver_id = p.id) total_adeudo, "
            + "IF(ISNULL(t.pay_day), 0, t.pay_day) as payment_expected_week,  "
            + "IF(ISNULL(t.pay_saving), 0, t.pay_saving) as saving_expected_week,  "
            + "IF(ISNULL(t.pay_accident), 0, t.pay_accident) as accident_expected_week,  "
            + "(SELECT IF(ISNULL(SUM(pay_day + pay_saving + accident )), 0 , SUM(pay_day + pay_saving + accident )) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'monday'  AND person_id = p.id ) monday_pay, "
            + "(SELECT IF(ISNULL(SUM(pay_day + pay_saving + accident )), 0 , SUM(pay_day + pay_saving + accident )) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'tuesday'  AND person_id = p.id ) tuesday_pay, "
            + "(SELECT IF(ISNULL(SUM(pay_day + pay_saving + accident )), 0 , SUM(pay_day + pay_saving + accident )) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'wednesday'  AND person_id = p.id ) wednesday_pay, "
            + "(SELECT IF(ISNULL(SUM(pay_day + pay_saving + accident )), 0 , SUM(pay_day + pay_saving + accident )) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'thursday'  AND person_id = p.id ) thursday_pay, "
            + "(SELECT IF(ISNULL(SUM(pay_day + pay_saving + accident )), 0 , SUM(pay_day + pay_saving + accident )) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'friday'  AND person_id = p.id ) friday_pay, "
            + "(SELECT IF(ISNULL(SUM(pay_day + pay_saving + accident )), 0 , SUM(pay_day + pay_saving + accident )) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'saturday'  AND person_id = p.id ) saturday_pay, "
            + "(SELECT IF(ISNULL(SUM(pay_day + pay_saving + accident )), 0 , SUM(pay_day + pay_saving + accident )) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'sunday'  AND person_id = p.id ) sunday_pay, "
            + "(SELECT IF(ISNULL(SUM(pay_day)), 0 , SUM(pay_day)) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND person_id = p.id ) total_pay_week, "
            + "(SELECT IF(ISNULL(SUM(pay_saving)), 0 , SUM(pay_saving)) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND person_id = p.id ) total_pay_saving_week, "
            + "(SELECT IF(ISNULL(SUM(accident)), 0 , SUM(accident)) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND person_id = p.id ) total_accident_week, "
            + "IF(t.day_monday = 'ENEBLED', 1 , 0) as isMonday, "
            + "IF(t.day_tuesday = 'ENEBLED', 1 , 0) as isTuesday, "
            + "IF(t.day_wednesday = 'ENEBLED', 1 , 0) as isWednesday, "
            + "IF(t.day_thursday = 'ENEBLED', 1 , 0) as isThursday, "
            + "IF(t.day_friday = 'ENEBLED', 1 , 0) as isFriday, "
            + "IF(t.day_saturday = 'ENEBLED', 1 , 0) as isSaturday, "
            + "(SELECT IF(ISNULL(garage), 0 , garage ) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'monday'  AND person_id = p.id LIMIT 1  ) monday_garage, "
            + "(SELECT IF(ISNULL(garage), 0 , garage ) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'tuesday'  AND person_id = p.id LIMIT 1  ) tuesday_garage, "
            + "(SELECT IF(ISNULL(garage), 0 , garage ) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'wednesday'  AND person_id = p.id LIMIT 1  ) wednesday_garage, "
            + "(SELECT IF(ISNULL(garage), 0 , garage ) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'thursday'  AND person_id = p.id LIMIT 1  ) thursday_garage, "
            + "(SELECT IF(ISNULL(garage), 0 , garage ) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'friday'  AND person_id = p.id LIMIT 1  ) friday_garage, "
            + "(SELECT IF(ISNULL(garage), 0 , garage ) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'saturday'  AND person_id = p.id LIMIT 1  ) saturday_garage, "
            + "(SELECT IF(ISNULL(garage), 0 , garage ) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'sunday'  AND person_id = p.id LIMIT 1  ) sunday_garage, "
            + "(SELECT IF(ISNULL(banca), 0 , banca ) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'monday'  AND person_id = p.id LIMIT 1  ) monday_banca, "
            + "(SELECT IF(ISNULL(banca), 0 , banca ) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'tuesday'  AND person_id = p.id LIMIT 1  ) tuesday_banca, "
            + "(SELECT IF(ISNULL(banca), 0 , banca ) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'wednesday'  AND person_id = p.id LIMIT 1  ) wednesday_banca, "
            + "(SELECT IF(ISNULL(banca), 0 , banca ) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'thursday'  AND person_id = p.id LIMIT 1  ) thursday_banca, "
            + "(SELECT IF(ISNULL(banca), 0 , banca ) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'friday'  AND person_id = p.id LIMIT 1  ) friday_banca, "
            + "(SELECT IF(ISNULL(banca), 0 , banca ) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'saturday'  AND person_id = p.id LIMIT 1  ) saturday_banca, "
            + "(SELECT IF(ISNULL(banca), 0 , banca ) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(:starDate, '%Y-%m-%d'), 1) AND  "
            + "YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(:starDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'sunday'  AND person_id = p.id LIMIT 1  ) sunday_banca "
            + "FROM PERSON p "
            + "JOIN TAXI t ON t.driver_id = p.id "
            + "JOIN PERSON p2 ON p2.id = t.customer_id "
            + "WHERE p.person_type = 'DRIVER' AND p.active = 1 AND p.id_location = :idLocation AND t.apc = 'DISABLED'";

}
