Calculate difference between dates and change status

0

I need to create some routine to calculate the difference between two dates directly in the MySql database and change the status of another field according to that difference. The database fields are:

  • Date Calibration,
  • Expiration Calibration,
  • Status Calibration: On day,                     Calibrate,                     Overdue.
  • How do I create the routine that takes the two dates, calculate the difference and when the date is more than 30 days, the status is "in day", when it is less than 30 days, the status is "Calibrate" and when the date is greater than the "calibration expiration" field, the status is "Overdue"?

        
    asked by anonymous 22.06.2016 / 11:50

    2 answers

    3

    I will not answer you how to do this in the Database, I will propose a way that (in my opinion) is more appropriate for calculated values. Where these calculated fields would not be fields saved in the database but calculated based on their indicators.

    Advantage:

    • The field will be recalculated at each new request, based on the current state of the object;
    • You do not need Procedures, Functions, or Triggers for this task linked to the database;
    • You do not run the risk of the calculated field being out of date based on your indicators;
    • Among other numerous small advantages that you can easily point out;

    Disadvantages:

    • If the bank is used by more than one system, all those involved will have to enter the rule to calculate the calculated fields;
    • If you do not use a Class template (Assign the query cursor directly to the DataSource (particularly I do not recommend));

    Implementation

    It would be something similar to this in C # (which seems to be the language you're using):

    public class Calibragem{
        public long Id { get; set; }
        public DateTime Data { get; set; }
        public DateTime Vencimento { get; set; }
        public StatusCalibragem Status { 
            get{
                // aqui você faz a conta da diferença em mémoria
                /** 
                 * DUVIDA: aqui você não deveria calcula a Data da ultima calibragem com a 
                 * data atual(DateTime.Now) ou o Vencimento com a data atual(DateTime.Now),
                 * ao invés de fazer o calculo entre as duas Datas?
                **/    
                TimeSpan diff = Data - Vencimento;
    
                // verifica a diferença em dias para ver qual o status atual
                if(diff.Days > 30){
                    return StatusCalibragem.EmDia;
                }
                if(diff.Days < 30){
                    return StatusCalibragem.Calibrar;
                }
                // não entendi quando será Vencida, com sua explicação
            } 
            private set; 
        }
    }
    
    public enum StatusCalibragem
    {
        EmDia,
        Calibrar,
        Vencida
    }
    

    In the database your table would only have the columns:

    ID | DATA_CALIBRAGEM | VENCIMENTO_CALIBRAGEM
    
        
    22.06.2016 / 13:56
    1

    To update the column according to the date conditions you can use something like this:

    UPDATE suatabela
       SET status = CASE WHEN CURDATE > data_vencimento THEN
                       status1
                    ELSE
                       CASE WHEN DATEDIFF(CURDATE(), data_calibragem) > 30 THEN
                          status2
                       ELSE
                          status3
                       END   
                    END;
    

    Remembering that with this statement all records in the table will be affected. Because at some point, each one will be in one of the three situations you mentioned.

    Comments:

    • CURDATE returns the current day ( link ).
    • DATEDIFF returns the difference between two dates ( link ).

    For this statement to be run daily you must create an event in the database ( link ).

    An example:

    delimiter |
    
    CREATE EVENT e_upd_calibragem
        ON SCHEDULE
          EVERY 1 DAY
        COMMENT 'Atualiza status de calibragem'
        DO
          BEGIN
    
            UPDATE suatabela
               SET status = CASE WHEN CURDATE > data_vencimento THEN
                               status1
                            ELSE
                               CASE WHEN DATEDIFF(CURDATE(), data_calibragem) > 30 THEN
                                  status2
                               ELSE
                                  status3
                               END   
                            END;
    
          END |
    
    delimiter ;
    
        
    22.06.2016 / 12:51