프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / ConvertService / ServiceBase / Markus.Service.StationController / ViewModel / DataBaseByStatusViewModel.cs @ b92f142f

이력 | 보기 | 이력해설 | 다운로드 (5.52 KB)

1

2
using System;
3
using System.Collections.Generic;
4
using System.ComponentModel;
5
using System.Linq;
6
using System.Text;
7
using System.Threading.Tasks;
8
using Markus.Service.Extensions;
9
using Markus.Message;
10
using Markus.Service.StationController.Extensions;
11
using Markus.Service.Interface;
12

    
13
namespace Markus.Service.StationController.ViewModel
14
{
15
    public class DataBaseByStatusViewModel : Mvvm.ToolKit.ViewModelBase
16
    {
17
        BackgroundWorker backgroundWorker;
18

    
19
        private System.Collections.ObjectModel.ObservableCollection<ConvertItem> items;
20
        private ConvertItem selectItem;
21

    
22
        private StatusCodeType statusType;
23
        private bool isLoading;
24

    
25
        public System.Collections.ObjectModel.ObservableCollection<ConvertItem> Items
26
        {
27
            get => items; set
28
            {
29
                items = value;
30
                OnPropertyChanged(() => Items);
31
            }
32
        }
33

    
34
        public ConvertItem SelectItem
35
        {
36
            get => selectItem; set
37
            {
38
                selectItem = value;
39
                OnPropertyChanged(() => SelectItem);
40
            }
41
        }
42

    
43

    
44
        public StatusCodeType StatusType
45
        {
46
            get => statusType; set
47
            {
48
                statusType = value;
49
                OnPropertyChanged(() => StatusType);
50
            }
51
        }
52

    
53
        public bool IsLoading
54
        {
55
            get => isLoading; set
56
            {
57
                if (isLoading != value)
58
                {
59
                    isLoading = value;
60
                    OnPropertyChanged(() => IsLoading);
61
                }
62
            }
63
        }
64

    
65
       
66

    
67
        public DataBaseByStatusViewModel()
68
        {
69
        }
70

    
71
        // 진행률에 변화가 있을때 이벤트가 발생
72
        private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
73
        {
74
        }
75

    
76
        // 일이 모두 마쳤을때 수행되어야할 코드
77
        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
78
        {
79
        }
80

    
81
        // BackgroundWorker에서 수행할 일을 정의.
82
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
83
        {
84
            while (IsAcitve)
85
            {
86
                System.Threading.Thread.Sleep(1000);
87

    
88
                try
89
                {
90
                    IsLoading = true;
91

    
92
                    List<ConvertItem> newitems = new List<ConvertItem>();
93

    
94
                    using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(App.MarkusDataBaseConnecitonString))
95
                    {
96
                        newitems = database.GetConvertItems(StatusType, x=>x.CREATE_DATETIME > DateTime.Today,10).ToList();
97
                    }
98

    
99
                    if (Items == null)
100
                    {
101
                        Items = new System.Collections.ObjectModel.ObservableCollection<ConvertItem>();
102

    
103
                        foreach (var item in newitems)
104
                        {
105
                            Items.Add(item);
106
                        }
107
                    }
108
                    else
109
                    {
110
                        /// 데이터 업데이트
111
                        newitems.ForEach(newitem =>
112
                        {
113
                            Items.UpdateWhere(changeitem => ConvertItemEx.ChangeValues(changeitem, newitem), x => x.ProjectNumber == newitem.ProjectNumber && x.ConvertID == newitem.ConvertID);
114
                        });
115

    
116
                        // 추가
117
                        foreach (var item in newitems)
118
                        {
119
                            if (Items.Count(x => x.ConvertID == item.ConvertID && x.ProjectNumber == item.ProjectNumber) == 0)
120
                            {
121
                                Items.Add(item);
122
                            }
123
                        }
124

    
125
                        /// 삭제
126

    
127
                        for (int i = Items.Count() - 1; i > -1; --i)
128
                        {
129
                            var item = Items[i];
130

    
131
                            if (newitems.Count(x => x.ConvertID == item.ConvertID && x.ProjectNumber == item.ProjectNumber) == 0)
132
                            {
133
                                Items.RemoveAt(i);
134
                            }
135
                        }
136
                    }
137
                }
138
                catch (Exception ex)
139
                {
140
                }
141
            }
142
            
143
        }
144

    
145
        public override void Loaded()
146
        {
147
            base.Loaded();
148

    
149
            if (!IsDesignMode)
150
            {
151
                backgroundWorker = new BackgroundWorker();
152
                backgroundWorker.DoWork += backgroundWorker_DoWork;
153
                backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
154
                backgroundWorker.WorkerReportsProgress = false;
155
                backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
156

    
157
                backgroundWorker.RunWorkerAsync();
158
            }
159
        }
160

    
161
        public override void Closed()
162
        {
163
            if (!IsDesignMode)
164
            {
165
                backgroundWorker.DoWork -= backgroundWorker_DoWork;
166
                backgroundWorker.RunWorkerCompleted -= backgroundWorker_RunWorkerCompleted;
167
                backgroundWorker.WorkerReportsProgress = false;
168
                backgroundWorker.ProgressChanged -= new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
169

    
170
                backgroundWorker.Dispose();
171
            }
172

    
173
            base.Closed();
174
        }
175
    }
176
}
클립보드 이미지 추가 (최대 크기: 500 MB)