예제 #1
0
        public static IList <SeatArrangement> GenSeatArrangements(this IEnumerable <Term> terms, ISimpleEmployee agent, Func <string, Seat> getSeat, Func <string, SeatArrangement, bool> add)
        {
            var prv = default(Term);

            return(terms.GetSeatArrangement((dateRange, term, isNeedSeat) =>
            {
                if (term == null || !term.IsNeedSeat)
                {
                    prv = term;
                    return default(SeatArrangement);
                }

                if (prv != null && !ReferenceEquals(prv, term) && prv.Level == 0 && term.Level == 0)
                {
                    prv = null;
                }

                var source = default(Term);
                TermExt.X(prv, term, ref source);

                var instance = default(SeatArrangement);

                var seatObj = source.If(o => o.SeatIsEmpty()) == true ? default(Seat) : getSeat(source.Seat);
                if (seatObj != null)
                {
                    instance = new SeatArrangement(agent, dateRange.Start, dateRange.End, seatObj)
                    {
                        Source = source
                    };
                    add(source.Seat, instance);
                }

                prv = term;
                return instance;
            }));
        }
        public void AssignSeat(Term shift, IWorkingAgent selectedAgent)
        {
            var affectedRows = new[] { SelectedSeat };

            if (!shift.SeatIsEmpty())
            {
                var removeDelegates = new Action(() => { });
                affectedRows = selectedAgent.Schedule.GetCoveredTermsWithAbsent(shift).Add(shift)
                               .Where(o => !o.SeatIsEmpty() && _seatBoxes.ContainsKey(o.Seat))
                               .Select(o =>
                {
                    var seatBox = (SeatBox)_seatBoxes[o.Seat];
                    foreach (var c in seatBox.Occupations.Where(c => c.If <SeatArrangement>(a => a.Source.Equals(o))))
                    {
                        var occupation   = c;
                        removeDelegates += () =>
                        {
                            seatBox.RemoveOccupation(occupation);
                            selectedAgent.Occupations.Remove(occupation);
                        };
                    }
                    return(seatBox);
                })
                               .ToArray();
                removeDelegates();

                //var originalSeat = (SeatBox)_seatBoxes[shift.Seat];
                //var seatArrangement = originalSeat.Occupations.OfType<SeatArrangement>().FirstOrDefault(o => o.Source == shift && o.TimeEquals(shift));
                //if (seatArrangement != null)
                //    originalSeat.RemoveOccupation(seatArrangement);
                //affectedRows = new[] { SelectedSeat, originalSeat };
            }

            var prv    = default(Term);
            var seatId = SelectedSeat.Id.ToString();

            shift.Seat = seatId;
            var coveredTerms = selectedAgent.Schedule.GetCoveredTermsWithAbsent(shift);

            coveredTerms.ForEach(o => o.Seat = null).Add(shift)
            .SliceOccupied((dateRange, term) =>
            {
                if (!term.IsNeedSeat)
                {
                    term.Seat = seatId;
                    prv       = term;
                    return(default(SeatArrangement));
                }

                if (prv != null && !ReferenceEquals(prv, term) && prv.Level == 0 && term.Level == 0)
                {
                    prv = null;
                }

                var source = default(Term);
                TermExt.X(prv, term, ref source);
                var instance = new SeatArrangement(selectedAgent.Profile, dateRange.Start, dateRange.End, SelectedSeat.Seat)
                {
                    Source = source
                };
                source.Seat = seatId;
                SelectedSeat.AddOccupation(instance);
                selectedAgent.Occupations.Add(instance);
                prv = term;
                return(instance);
            }, t => t.IsNeedSeat);
            //shift.SaftyInvoke<AssignmentBase>(o => { o.OccupyStatus = "C"; });
            Reload(affectedRows);
            OccupationsChanged(-1);
        }