Scheduler.java 5.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
package com.roshka.service;

import com.roshka.modelo.Beneficio;
import com.roshka.modelo.Birthday;
import com.roshka.repositorio.BeneficioRepository;
import com.roshka.repositorio.BirthdayRepository;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Configuration
@EnableScheduling
public class Scheduler {
    @Autowired
    BirthdayRepository birthdayRepository;
    @Autowired
    BeneficioRepository beneficioRepository;
    @PersistenceContext
    private EntityManager entityManager;

38 39
        @Scheduled(cron = "0 0 8 * * MON-FRI")
//    @Scheduled(cron = "0 * * * * *")
40 41
    public void cumples() {
        String url = "https://hooks.slack.com/services/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";
42 43 44
        // todo verificar que no sea feriado
        // mandar los cumpleaños que fueron sabado y domingo
            // si hoy es lunes buscar si hubo cumpleaños sabado y domingo
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        // Verificar el cumpleaños de quien es
        List<Birthday> cumples = birthdayRepository.findAllByFecha(new Date());
        for (Birthday cumple : cumples) {
            String json = "{\n" +
                    "\"blocks\": [\n" +
                    "{\n" +
                    "\"type\": \"section\",\n" +
                    "\"text\": {\n" +
                    "\"type\": \"mrkdwn\",\n" +
                    "\"text\": \"Buenos días <!channel> \n Hoy está de cumpleaños *" + cumple.getNombreCompleto() + "* <@" + cumple.getIdSlack() + ">. 🥳 ¡Que los cumplas muy feliz te desea la Roshka! Que en la vida siempre encuentres razones para sonreír 🥳\",\n" +
                    "}" +
                    "}," +
                    "{" +
                    "\"type\": \"divider\"" +
                    "}," +
                    "{\n" +
                    "\"type\": \"image\",\n" +
                    "\"title\": {\n" +
                    "\"type\": \"plain_text\",\n" +
                    "\"text\": \"" + cumple.getNombreCompleto() + "\"\n" +
                    "},\n" +
                    "\"block_id\": \"image4\",\n" +
                    "\"image_url\": \"http://placekitten.com/500/500\",\n" +
                    "\"alt_text\": \"An incredibly cute kitten.\"\n" +
                    "}\n" +
                    "]\n" +
                    "}" +
                    "";
            String result = null;
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));

            try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
                try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
                    result = EntityUtils.toString(response.getEntity());
                }
            } catch (IOException | ParseException e) {
                e.printStackTrace();
            }
        }
    }

87 88
        @Scheduled(cron = "0 0 15 * * FRI")
//    @Scheduled(cron = "0 * * * * *")
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    public void beneficios() {
        String url = "https://hooks.slack.com/services/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";
        // Traer random
        Beneficio beneficio = getRandomBeneficio();
        if (beneficio != null) {
            String json = "{" +
                    "\"blocks\": [" +
                    "{" +
                    "\"type\": \"section\"," +
                    "\"text\": {" +
                    "\"type\": \"mrkdwn\"," +
                    "\"text\": \"Te recordamos uno de nuestros beneficios <!channel>\"" +
                    "}" +
                    "}," +
                    "{" +
                    "\"type\": \"header\"," +
                    "\"text\": {" +
                    "\"type\": \"plain_text\"," +
                    "\"text\": \"" + beneficio.getTitulo() + "\"," +
                    "\"emoji\": true" +
                    "}" +
                    "}," +
                    "{" +
                    "\"type\": \"section\"," +
                    "\"text\": {" +
                    "\"type\": \"mrkdwn\"," +
                    "\"text\": \"" + beneficio.getDescripcion() + "\"" +
                    "}" +
                    "}" +
                    "]" +
                    "}";
            String result = null;
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));

            try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
                try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
                    result = EntityUtils.toString(response.getEntity());
                }
            } catch (IOException | ParseException e) {
                e.printStackTrace();
            }
        }
    }

    public Beneficio  getRandomBeneficio() {
        String query = "SELECT o.id FROM Beneficio o ORDER BY random()";
        Query q = entityManager.createQuery(query);
        q.setMaxResults(1);
        Optional<Beneficio> beneficio = beneficioRepository.findById((Long) q.getSingleResult());
        if (beneficio.isPresent()) {
            return beneficio.get();
        }
        return null;
    }

}