Beispiel #1
0
    public static void Main(Comindware.Process.Api.Data.ScriptContext context, Comindware.Entities entities)
    {
        var sessioinNumeratorListAlias  = "sessionNumeratorList";
        var sessionsForTomorrowListName = "numeratorList";

        var session_start_dateProperty         = "op.12";
        var session_training_apparatusProperty = "op.7";
        var calculatedCompanyIdProperty        = "op.1010";
        var sessionNumberProperty = "op.220";

        var sessionStatusProperty = "op.116";

        var sessionRTId             = "oa.5";
        var sessionsForTomorrowRTId = "oa.43";

        var S7InvetsId = "72848";

        var systemCompanies = new List <string>();

        systemCompanies.Add("473");
        systemCompanies.Add("22967");
        systemCompanies.Add("5481");
        systemCompanies.Add("23488");

        var    businessObject = Api.TeamNetwork.ObjectService.Get(context.BusinessObjectId);
        object startPeriodObject;
        object endPeriodObject;

        businessObject.TryGetValue("op.663", out startPeriodObject);
        businessObject.TryGetValue("op.662", out endPeriodObject);

        var startPeriod = (DateTime)startPeriodObject;
        var endPeriod   = (DateTime)endPeriodObject;

        string sessionNumeratorDatasetConfigId;
        var    sessionLists = Api.TeamNetwork.DatasetConfigurationService.List(sessionRTId);

        if (sessionLists.Select(l => l.Name).Contains(sessioinNumeratorListAlias))
        {
            var sessionDatasetId = sessionLists.FirstOrDefault(l => l.Name == sessioinNumeratorListAlias).Id;
            sessionNumeratorDatasetConfigId = Api.TeamNetwork.DatasetConfigurationService.Get(sessionDatasetId)?.Id;
        }
        else
        {
            var columns = new List <Comindware.TeamNetwork.Api.Data.ColumnConfiguration>
            {
                new Comindware.TeamNetwork.Api.Data.ColumnConfiguration
                {
                    DataSourceInfo = new Comindware.TeamNetwork.Api.Data.DataSourceInfo
                    {
                        PropertyPath = new[] { session_start_dateProperty }
                    }
                },
                new Comindware.TeamNetwork.Api.Data.ColumnConfiguration
                {
                    DataSourceInfo = new Comindware.TeamNetwork.Api.Data.DataSourceInfo
                    {
                        PropertyPath = new[] { session_training_apparatusProperty }
                    }
                },
                new Comindware.TeamNetwork.Api.Data.ColumnConfiguration
                {
                    DataSourceInfo = new Comindware.TeamNetwork.Api.Data.DataSourceInfo
                    {
                        PropertyPath = new[] { calculatedCompanyIdProperty }
                    }
                },
                new Comindware.TeamNetwork.Api.Data.ColumnConfiguration
                {
                    DataSourceInfo = new Comindware.TeamNetwork.Api.Data.DataSourceInfo
                    {
                        PropertyPath = new[] { sessionStatusProperty }
                    }
                },
                new Comindware.TeamNetwork.Api.Data.ColumnConfiguration
                {
                    DataSourceInfo = new Comindware.TeamNetwork.Api.Data.DataSourceInfo
                    {
                        PropertyPath = new[] { "isDisabled" }
                    }
                }
            };

            sessionNumeratorDatasetConfigId = Api.TeamNetwork.DatasetConfigurationService.Create(new Comindware.TeamNetwork.Api.Data.DatasetConfiguration
            {
                Name        = sessioinNumeratorListAlias,
                ContainerId = sessionRTId,
                Columns     = columns,
                Paging      = new Comindware.TeamNetwork.Api.Data.PagingConfiguration
                {
                    Size = 1000000000
                },
                SystemFilterExpression = @"@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
                    @prefix cmw: <http://comindware.com/logics#>.
                    @prefix axis: <http://comindware.com/logics/axis#>.
                    @prefix log: <http://www.w3.org/2000/10/swap/log#>.
                    @prefix object: <http://comindware.com/ontology/object#>.
                    @prefix user: <http://comindware.com/ontology/user#>.
                    @prefix property: <http://comindware.com/ontology/user/op#>.
                    @prefix session: <http://comindware.com/ontology/session#>.
                    @prefix time: <http://comindware.com/logics/time#>.
                    @prefix math: <http://www.w3.org/2000/10/swap/math#>.
                    @prefix cmwtime: <http://comindware.com/logics/time#>.

                    {
                        (""session_statys""^^xsd:string ""session_statys_name""^^xsd:string) object:findProperty ?propertystat.
                        (""sessions""^^xsd:string ""session_statys""^^xsd:string) object:findProperty ?propertysesstat.
                        (""sessions""^^xsd:string ""session_start_date""^^xsd:string) object:findProperty ?propertyStartDate.
                        (""sessions""^^xsd:string ""calculatedCompanyId""^^xsd:string) object:findProperty ?propertycalculatedCompanyId.

                        # +(0h-3h) in seconds
                        ""-10800""^^xsd:double <http://comindware.com/logics/time#toDuration> ?startDayOffset.
                        # +(24h-3h) in seconds
                        ""75600""^^xsd:double <http://comindware.com/logics/time#toDuration> ?endDayOffset.

                        session:context session:requestTime ?nowUTC.
                        ?nowUTC <http://comindware.com/ontology/entity/nullable#startOfDay> ?startOfCurrentDayUTC.
                        (?startOfCurrentDayUTC ?startDayOffset) <http://comindware.com/logics/time#addDuration> ?startTime.
                        (?startOfCurrentDayUTC ?endDayOffset) <http://comindware.com/logics/time#addDuration> ?endTime.

                        ?statD ?propertystat ""Удалена"".
                        ?statR ?propertystat ""Отменена (неявка)"".
                        ?container object:alias ""sessions"".
                        ?item cmw:container ?container.
                        not{?item ?propertysesstat ?statD.}.
                        not{?item ?propertysesstat ?statR.}.

                        ?item ?propertyStartDate ?startDate.
                        ?startDate math:notLessThan ?startTime.
                        ?startDate math:lessThan ?endTime.
                    }"
            });
        }

        var query = new Comindware.TeamNetwork.Api.Data.DatasetQuery
        {
            DatasetId = sessionNumeratorDatasetConfigId
        };

        var sessionsData = Api.TeamNetwork.DatasetService.QueryData(query).Rows;
        var sessionList  = new List <List <SessionModel> >
        {
            new List <SessionModel>(),
            new List <SessionModel>(),
            new List <SessionModel>(),
            new List <SessionModel>(),
            new List <SessionModel>(),
        };

        foreach (var session in sessionsData)
        {
            switch ((session.Data[1] as Comindware.TeamNetwork.Api.Data.Reference)?.Id)
            {
            case ("87312"):
                //landingAreaSimulator
                FillSimulator(sessionList[0], session);
                break;

            case ("31976"):
                //mftdSimulator
                FillSimulator(sessionList[1], session);
                break;

            case ("64"):
                //a3201Simulator
                FillSimulator(sessionList[2], session);
                break;

            case ("63"):
                //a3202Simulator
                FillSimulator(sessionList[3], session);
                break;

            case ("62"):
                //b737Simulator
                FillSimulator(sessionList[4], session);
                break;
            }
        }

        //isSystem ? 11 : 1;
        foreach (var list in sessionList)
        {
            foreach (var session in list)
            {
                int number;
                if (session.CompanyId == S7InvetsId)
                {
                    number = 0;
                }
                else if (systemCompanies.Contains(session.CompanyId))
                {
                    number = list.Count(s => systemCompanies.Contains(s.CompanyId) && s.StartDate < session.StartDate) + 11;
                }
                else
                {
                    number = list.Count(s => !systemCompanies.Contains(s.CompanyId) && s.CompanyId != S7InvetsId && s.StartDate < session.StartDate) + 1;
                }
                Api.TeamNetwork.ObjectService.Edit(session.Id, new Dictionary <string, object> {
                    { sessionNumberProperty, number }
                });
            }
        }
    }
Beispiel #2
0
    public static void Main(Comindware.Process.Api.Data.ScriptContext context, Comindware.Entities entities)
    {
        string path = @"C:\report\CTPcopy.csv";
        string text;

        using (StreamReader sr = new System.IO.StreamReader(path))
        {
            text = sr.ReadToEnd();
        }
        List <Session> sessionFile = new List <Session>();

        DateTime startDate  = DateTime.MaxValue;
        DateTime finishDate = DateTime.MinValue;

        foreach (var line in text.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries))
        {
            var      column = line.Split(';');
            DateTime current_session_start_date;
            //DateTime.TryParse(column[0].Trim(), out current_session_start_date);

            //var provider = CultureInfo.InvariantCulture;
            var provider = new CultureInfo("ru-RU");
            //05.12.2017 23:45
            var format = "dd.MM.yyyy HH:mm:ss";

            current_session_start_date = DateTime.ParseExact(column[0], format, provider);
            current_session_start_date = TimeZoneInfo.ConvertTime(current_session_start_date, TimeZoneInfo.Utc, TimeZoneInfo.Local);
            DateTime current_session_finish_date;
            //DateTime.TryParse(column[1].Trim(), out current_session_finish_date);
            current_session_finish_date = DateTime.ParseExact(column[1].Trim(), format, provider);
            current_session_finish_date = TimeZoneInfo.ConvertTime(current_session_finish_date, TimeZoneInfo.Utc, TimeZoneInfo.Local);

            sessionFile.Add(new Session()
            {
                session_start_date             = current_session_start_date,
                session_finish_date            = current_session_finish_date,
                session_training_apparatus_str = column[2].Trim(),
                typeAIMS_str         = column[3].Trim(), //secondary parameter
                fbs_str              = column[4].Trim(), //secondary parameter
                leaseWet_str         = column[5].Trim(),
                session_company_str  = column[6].Trim(),
                instructor_str       = column[7].Trim(),  //secondary parameter
                pilot1_str           = column[8].Trim(),  //secondary parameter
                pilot2_str           = column[9].Trim(),  //secondary parameter
                code_str             = column[10].Trim(), //secondary parameter
                session_codeAIMS_str = column[11].Trim(), //secondary parameter
                observer_str         = column[12].Trim()
            });

            if (current_session_start_date < startDate.ToUniversalTime())
            {
                startDate = current_session_start_date.ToUniversalTime();
            }
            if (current_session_finish_date > finishDate.ToUniversalTime())
            {
                finishDate = current_session_finish_date.ToUniversalTime();
            }
        }

        //Выбор всех сессий, с которыми потенциально можно пересечься при создании новых сессий
        var allSessions =
            entities.sessions.Where(s => s.session_finish_date.HasValue && s.session_start_date.HasValue && (startDate < s.session_finish_date.Value) && (s.session_start_date.Value < finishDate) &&
                                    s._isDisabled != true && !s.session_name.Contains("ОЭТ (15)") && s.ticket != null)
            .Select(s => new
        {
            s.id,
            s.session_start_date,
            s.session_finish_date,
            s.session_training_apparatus,
            s.ticket,
            s.typeAIMS,
            s.fbs,
            s.instructor,
            s.first_pilot,
            s.second_pilot,
            s.codeAIMS
        })
            .ToList();

        //Выбор всех тренажёров
        var trainingApparatusDictionary = entities.training_apparatus.Where(x => x.codeAIMS != null).ToDictionary(x => x.codeAIMS, x => x.id);

        var ticketDictionary = new Dictionary <string, string>();
        var ticketList       = entities.tickets.Where(t => (t.company_ticket != null)).Select(t => new { t.id, t.company_ticket }).ToList();

        foreach (var ticket in ticketList)
        {
            if (String.IsNullOrEmpty(ticket.company_ticket) || ticketDictionary.ContainsKey(ticket.id))
            {
                continue;
            }
            else
            {
                ticketDictionary.Add(ticket.id, ticket.company_ticket);
            }
        }



        foreach (var line in sessionFile)
        {
            //Определение заявки
            string session_ticket = null;
            session_ticket = entities.company.Where(c => c.codeAIMS == line.session_company_str && c._isDisabled != true)
                             .Select(t => t.ticket_aims)
                             .FirstOrDefault();
            if (String.IsNullOrEmpty(session_ticket))
            {
                continue;
            }

            //Определение тренажёра
            string training_apparatus = null;
            if (line.session_training_apparatus_str != null)
            {
                trainingApparatusDictionary.TryGetValue(line.session_training_apparatus_str, out training_apparatus);
            }

            bool fbs = false;
            if (line.fbs_str == "FBS")
            {
                fbs = true;
            }

            bool leaseWET = !String.IsNullOrWhiteSpace(line.leaseWet_str);

            string sessionStatus = "423";    // Статус "Забронирована"

            //Определение инструктора
            string instructor = null;
            if (!String.IsNullOrEmpty(line.instructor_str))
            {
                var instructor_parts = line.instructor_str.Split('/');
                var code             = instructor_parts[1];
                instructor = entities.contacts.Where(i => i.codeAIMS == code).Select(i => i.id).FirstOrDefault();
                if (instructor == null)
                {
                    string companyId = "";
                    if (code.StartsWith("10"))
                    {
                        companyId = entities.company.Where(c => c.codeAIMS == "S7").Select(c => c.id).FirstOrDefault();
                    }
                    else if (code.StartsWith("11"))
                    {
                        companyId = entities.company.Where(c => c.codeAIMS == "GH").Select(c => c.id).FirstOrDefault();
                    }
                    var fullname       = instructor_parts[0].Split(',');
                    var instructorData = new Dictionary <string, object>()
                    {
                        { "name", fullname[1] },
                        { "name_eng", fullname[1] },
                        { "surname", fullname[0] },
                        { "surname_eng", fullname[0] },
                        { "contact_position", "Instructor" },
                        { "contact_company", companyId },
                        { "codeAIMS", code }
                    };
                    instructor = Api.TeamNetwork.ObjectService.CreateWithAlias("contacts", instructorData);
                }
            }

            //Определение пилота 1
            string pilot1 = null;
            if (!String.IsNullOrEmpty(line.pilot1_str))
            {
                var pilot1_parts = line.pilot1_str.Split('/');
                var code         = pilot1_parts[1];
                pilot1 = entities.contacts.Where(i => i.codeAIMS == code).Select(i => i.id).FirstOrDefault();
                if (pilot1 == null)
                {
                    string companyId = "";
                    if (code.StartsWith("10"))
                    {
                        companyId = entities.company.Where(c => c.codeAIMS == "S7").Select(c => c.id).FirstOrDefault();
                    }
                    else if (code.StartsWith("11"))
                    {
                        companyId = entities.company.Where(c => c.codeAIMS == "GH").Select(c => c.id).FirstOrDefault();
                    }
                    var fullname   = pilot1_parts[0].Split(',');
                    var pilot1Data = new Dictionary <string, object>()
                    {
                        { "name", fullname[1] },
                        { "name_eng", fullname[1] },
                        { "surname", fullname[0] },
                        { "surname_eng", fullname[0] },
                        { "contact_position", "Pilot" },
                        { "contact_company", companyId },
                        { "codeAIMS", code }
                    };
                    pilot1 = Api.TeamNetwork.ObjectService.CreateWithAlias("contacts", pilot1Data);
                }
            }

            //Определение пилота 2
            string pilot2 = null;
            if (!String.IsNullOrEmpty(line.pilot2_str))
            {
                var pilot2_parts = line.pilot2_str.Split('/');
                var code         = pilot2_parts[1];
                pilot2 = entities.contacts.Where(i => i.codeAIMS == code).Select(i => i.id).FirstOrDefault();
                if (pilot2 == null)
                {
                    string companyId = "";
                    if (code.StartsWith("10"))
                    {
                        companyId = entities.company.Where(c => c.codeAIMS == "S7").Select(c => c.id).FirstOrDefault();
                    }
                    else if (code.StartsWith("11"))
                    {
                        companyId = entities.company.Where(c => c.codeAIMS == "GH").Select(c => c.id).FirstOrDefault();
                    }
                    var fullname   = pilot2_parts[0].Split(',');
                    var pilot2Data = new Dictionary <string, object>()
                    {
                        { "name", fullname[1] },
                        { "name_eng", fullname[1] },
                        { "surname", fullname[0] },
                        { "surname_eng", fullname[0] },
                        { "contact_position", "Pilot" },
                        { "contact_company", companyId },
                        { "codeAIMS", code }
                    };
                    pilot2 = Api.TeamNetwork.ObjectService.CreateWithAlias("contacts", pilot2Data);
                }
            }
            //Определение Наблюдателя
            string observer = null;
            if (!String.IsNullOrEmpty(line.observer_str))
            {
                var observer_parts = line.observer_str.Split('/');
                var code           = observer_parts[1];
                observer = entities.contacts.Where(i => i.codeAIMS == code).Select(i => i.id).FirstOrDefault();
                if (observer == null)
                {
                    string companyId = "";
                    if (code.StartsWith("10"))
                    {
                        companyId = entities.company.Where(c => c.codeAIMS == "S7").Select(c => c.id).FirstOrDefault();
                    }
                    else if (code.StartsWith("11"))
                    {
                        companyId = entities.company.Where(c => c.codeAIMS == "GH").Select(c => c.id).FirstOrDefault();
                    }
                    var fullname     = observer_parts[0].Split(',');
                    var observerData = new Dictionary <string, object>()
                    {
                        { "name", fullname[1] },
                        { "name_eng", fullname[1] },
                        { "surname", fullname[0] },
                        { "surname_eng", fullname[0] },
                        { "contact_position", "Instructor" },
                        { "contact_company", companyId },
                        { "codeAIMS", code }
                    };
                    observer = Api.TeamNetwork.ObjectService.CreateWithAlias("contacts", observerData);
                }
            }

            //Подготовка блока данных для создания сессии
            var data = new Dictionary <string, object>()
            {
                { "session_start_date", line.session_start_date },
                { "session_finish_date", line.session_finish_date },
                { "session_training_apparatus", training_apparatus },
                { "ticket", session_ticket },
                { "fbs", fbs },
                { "typeAIMS", line.typeAIMS_str },
                { "leaseWET", leaseWET },
                { "session_statys", sessionStatus },
                { "instructor", instructor },
                { "first_pilot", pilot1 },
                { "second_pilot", pilot2 },
                { "codeAIMS", line.session_codeAIMS_str },
                { "observer", observer }
            };

            //Компания для текущей строки
            var currentCompanyForRow = entities.company.Where(c => c.codeAIMS == line.session_company_str && c._isDisabled != true)
                                       .Select(c => c.id).Single();

            //сессии у которых совпадают даты и компания
            var partiallySameSessions = allSessions.Where(s => s.session_training_apparatus == training_apparatus && s.session_start_date.Value.Subtract(DateTime.UtcNow).TotalDays <= 7 &&
                                                          s.session_start_date.Value.Subtract(DateTime.UtcNow).TotalDays > 0 &&
                                                          ((ticketDictionary[s.ticket] == currentCompanyForRow) &&
                                                           (line.session_start_date.ToUniversalTime() == s.session_start_date.Value) && (line.session_finish_date.ToUniversalTime()
                                                                                                                                         == s.session_finish_date.Value)));

            if (partiallySameSessions.Any())
            {
                //проверка других полей
                foreach (var session in partiallySameSessions)
                {
                    if (session.typeAIMS != line.typeAIMS_str || session.fbs != fbs ||
                        session.instructor != instructor || session.first_pilot != pilot1 ||
                        session.second_pilot != pilot2 ||
                        session.codeAIMS != line.session_codeAIMS_str)
                    {
                        Api.TeamNetwork.ObjectService.EditWithAlias("sessions", session.id,
                                                                    new Dictionary <string, object>
                        {
                            { "typeAIMS", line.typeAIMS_str },
                            { "fbs", fbs },
                            { "instructor", instructor },
                            { "first_pilot", pilot1 },
                            { "second_pilot", pilot2 },
                            { "codeAIMS", line.session_codeAIMS_str }
                        });
                    }
                }
            }
            else
            {
                //Выбор сессий, с которыми происходит пересечение
                var intersectedSessionIds =
                    allSessions.Where(
                        s => s.session_training_apparatus == training_apparatus &&
                        !((ticketDictionary[s.ticket] == currentCompanyForRow) && (line.session_start_date.ToUniversalTime() == s.session_start_date.Value) && (line.session_finish_date.ToUniversalTime() == s.session_finish_date.Value)) &&
                        ((line.session_start_date.ToUniversalTime() < s.session_finish_date.Value) && (s.session_start_date.Value < line.session_finish_date.ToUniversalTime()))).Select(s => s.id);

                //Сокрытие сессий
                foreach (var oldSessionId in intersectedSessionIds)
                {
                    Api.TeamNetwork.ObjectService.Disable(oldSessionId);
                }

                //Проверка необходимости создания сессии
                var mayBeSession = allSessions.Where(s => (ticketDictionary[s.ticket] == currentCompanyForRow) && s.session_training_apparatus == training_apparatus && s.session_start_date.Value == line.session_start_date.ToUniversalTime() && s.session_finish_date.Value == line.session_finish_date.ToUniversalTime()).FirstOrDefault();

                if (mayBeSession == null)
                {
                    Api.TeamNetwork.ObjectService.CreateWithAlias("sessions", data);
                }
            }
        }
    }