Esempio n. 1
0
        public DataResult Data(DataCommand command)
        {
            if (command.Period == default)
            {
                throw new Exception("Missing parameter: Period");
            }

            if (command.Period.Day != 1)
            {
                throw new Exception("Period must be the first day of the month.");
            }

            var startedAt = DateTime.Now;

            using (var conn = NewConnection())
            {
                conn.Open();

                WriteToolDataResult  writeToolDataProcessResult  = null;
                WriteRoomDataResult  writeRoomDataProcessResult  = null;
                WriteStoreDataResult writeStoreDataProcessResult = null;

                if ((command.BillingCategory & BillingCategory.Tool) > 0)
                {
                    writeToolDataProcessResult = new WriteToolDataProcess(WriteToolDataConfig.Create(conn, "ProcessRepository.Data", command.Period, command.ClientID, command.Record, Cost.GetToolCosts(command.Period, command.Record))).Start();
                }

                if ((command.BillingCategory & BillingCategory.Room) > 0)
                {
                    writeRoomDataProcessResult = new WriteRoomDataProcess(new WriteRoomDataConfig {
                        Connection = conn, Context = "ProcessRepository.Data", Period = command.Period, ClientID = command.ClientID, RoomID = command.Record
                    }).Start();
                }

                if ((command.BillingCategory & BillingCategory.Store) > 0)
                {
                    writeStoreDataProcessResult = new WriteStoreDataProcess(new WriteStoreDataConfig {
                        Connection = conn, Context = "ProcessRepository.Data", Period = command.Period, ClientID = command.ClientID, ItemID = command.Record
                    }).Start();
                }

                conn.Close();

                DataResult result = new DataResult(startedAt)
                {
                    WriteToolDataProcessResult  = writeToolDataProcessResult,
                    WriteRoomDataProcessResult  = writeRoomDataProcessResult,
                    WriteStoreDataProcessResult = writeStoreDataProcessResult
                };

                return(result);
            }
        }
Esempio n. 2
0
        public UpdateResult Update(UpdateCommand command)
        {
            if (command.Period == default)
            {
                throw new Exception("Missing parameter: Period");
            }

            if (command.Period.Day != 1)
            {
                throw new Exception("Period must be the first day of the month.");
            }

            var startedAt = DateTime.Now;

            using (var conn = NewConnection())
            {
                conn.Open();

                var types = command.BillingTypes.ToString().Split(',').Select(x => x.Trim()).ToArray();
                // types should be {"Tool", "Room", "Store"} for example

                WriteToolDataCleanResult  writeToolDataCleanProcessResult  = null;
                WriteRoomDataCleanResult  writeRoomDataCleanProcessResult  = null;
                WriteStoreDataCleanResult writeStoreDataCleanProcessResult = null;
                WriteToolDataResult       writeToolDataProcessResult       = null;
                WriteRoomDataResult       writeRoomDataProcessResult       = null;
                WriteStoreDataResult      writeStoreDataProcessResult      = null;
                string error = null;

                foreach (string t in types)
                {
                    //Clean data always first, then final data
                    string[] dTypes = null;
                    switch (command.UpdateTypes)
                    {
                    case UpdateDataType.DataClean:
                        dTypes = new string[] { "Clean" };
                        break;

                    case UpdateDataType.Data:
                        dTypes = new string[] { "" };
                        break;

                    case UpdateDataType.DataClean | UpdateDataType.Data:
                        dTypes = new string[] { "Clean", "" };
                        break;
                    }

                    foreach (string dType in dTypes)
                    {
                        DateTime sd;
                        DateTime ed;

                        string dataType = $"{t}Data{dType}";  //e.g. StoreData, StoreDataClean OR RoomData, RoomDataClean
                        string funcName = $"Write{dataType}"; //e.g. WriteStoreData, WriteStoreDataClean OR WriteRoomData, WriteRoomDataClean

                        DateTime fom = command.Period.FirstOfMonth();

                        //always do clean first (called from within the select)
                        if (dType == "Clean")
                        {
                            //This stored procedure will delete all records that are of today (i.e. 00:00:00 of current day)
                            //and will return the max date after the deletion (so the return date should be yesterday)

                            string procName = $"dbo.{dataType}_Select";

                            var lastUpdate = GetLastUpdate(conn, procName, command.ClientID);

                            /*
                             * For tool: lastUpdate is the last BeginDateTime in ToolDataClean after deleteing all records in this way:
                             *
                             * SET @eDate = CONVERT(datetime, CONVERT(nvarchar(10), GETDATE(), 120)) -- 00:00:00 of current day
                             *
                             * DELETE dbo.ToolDataClean -- allows multiple queries in the same day
                             * WHERE BeginDateTime >= @eDate OR ActualBeginDateTime >= @eDate
                             */

                            if (lastUpdate == default)
                            {
                                throw new Exception($"Cannot get lastUpdate from {procName}");
                            }

                            // sd is which ever is earlier: previousDay or lastUpdate
                            sd = lastUpdate < fom ? lastUpdate : fom;
                            ed = fom.AddMonths(1).Date;
                        }
                        else
                        {
                            // For Data always import the whole period.
                            sd = fom;
                            ed = sd.AddMonths(1);
                        }

                        // At this point ed is always 1 month after period (the arg passed to this method). For Data,
                        // sd will always be period. For DataClean sd will either be period or the last date currently
                        // found in the Clean table, whichever is earliest.

                        // No looping is necessary because for Clean the methods take a start and end date, and for Data
                        // we always use period (the arg passed to this method). Assuming DataClean works, there will be
                        // something to import into Data.

                        try
                        {
                            switch (funcName)
                            {
                            case "WriteToolDataClean":
                                writeToolDataCleanProcessResult = new WriteToolDataCleanProcess(new WriteToolDataCleanConfig {
                                    Connection = conn, Context = "ProcessRepository.Update", StartDate = sd, EndDate = ed, ClientID = command.ClientID
                                }).Start();
                                break;

                            case "WriteToolData":
                                writeToolDataProcessResult = new WriteToolDataProcess(WriteToolDataConfig.Create(conn, "ProcessRepository.Update", sd, command.ClientID, 0, Cost.GetToolCosts(sd, 0))).Start();
                                break;

                            case "WriteRoomDataClean":
                                writeRoomDataCleanProcessResult = new WriteRoomDataCleanProcess(new WriteRoomDataCleanConfig {
                                    Connection = conn, Context = "ProcessRepository.Update", StartDate = sd, EndDate = ed, ClientID = command.ClientID
                                }).Start();
                                break;

                            case "WriteRoomData":
                                writeRoomDataProcessResult = new WriteRoomDataProcess(new WriteRoomDataConfig {
                                    Connection = conn, Context = "ProcessRepository.Update", Period = sd, ClientID = command.ClientID, RoomID = 0
                                }).Start();
                                break;

                            case "WriteStoreDataClean":
                                writeStoreDataCleanProcessResult = new WriteStoreDataCleanProcess(new WriteStoreDataCleanConfig {
                                    Connection = conn, Context = "ProcessRepository.Update", StartDate = sd, EndDate = ed, ClientID = command.ClientID
                                }).Start();
                                break;

                            case "WriteStoreData":
                                writeStoreDataProcessResult = new WriteStoreDataProcess(new WriteStoreDataConfig {
                                    Connection = conn, Context = "ProcessRepository.Update", Period = sd, ClientID = command.ClientID, ItemID = 0
                                }).Start();
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            // [2010-02-03] Test code to track who calls this function
                            // [2016-09-28 jg] Only call when there's an error
                            error = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] function = {funcName}, ClientID = {command.ClientID}, sd = {sd}, ed = {ed}"
                                    + Environment.NewLine + Environment.NewLine + ex.ToString();

                            string subj = $"Call from LNF.CommonTools.WriteData.UpdateTables [{t + dType}] [{DateTime.Now:yyyy-MM-dd HH:mm:ss}]";

                            SendEmail.SendDeveloperEmail("LNF.CommonTools.WriteData.UpdateTables", subj, error);
                        }
                    }
                }

                conn.Close();

                var result = new UpdateResult(startedAt)
                {
                    BillingTypes = command.BillingTypes,
                    UpdateTypes  = command.UpdateTypes,
                    Period       = command.Period,
                    ClientID     = command.ClientID,
                    WriteToolDataCleanProcessResult  = writeToolDataCleanProcessResult,
                    WriteRoomDataCleanProcessResult  = writeRoomDataCleanProcessResult,
                    WriteStoreDataCleanProcessResult = writeStoreDataCleanProcessResult,
                    WriteToolDataProcessResult       = writeToolDataProcessResult,
                    WriteRoomDataProcessResult       = writeRoomDataProcessResult,
                    WriteStoreDataProcessResult      = writeStoreDataProcessResult,
                    Error = error
                };

                return(result);
            }
        }