ホテル管理システム
ogi
yesterday 1a1c8e71fcd14858f595029f089b2d4a00202b32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
using Grpc.Core;
using HotelPms.Client.Blazor.Dialog;
using HotelPms.Client.Blazor.Models;
using HotelPms.Client.Blazor.Util;
using HotelPms.Data.Common;
using HotelPms.Share.IO;
using HotelPms.Share.Util;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using MudBlazor;
using System.Data;
using static HotelPms.Client.Blazor.Util.SystemEnum;
using static System.Net.Mime.MediaTypeNames;
 
namespace HotelPms.Client.Blazor.Pages.UseDetail;
 
public partial class RoomTypeInput : ComponentBase
{
    #region ★★★★★ Parameter ★★★★★
 
    [Parameter] public ViewModel.UseInput? Data { get; set; }
 
    /// <summary>
    /// 親Pageの再表示
    /// </summary>
    [Parameter] public Action OnRefresh { get; set; }
 
    #endregion 
 
    #region ★★★★★ Declare ★★★★★ 
    /// <summary>
    /// 异步锁
    ///     ■ロック追加前
    ///     Leave⇒Start
    ///     Leave⇒Row:1 Col:0 enterPush:False, orgText:, text:1
    ///     【10:07:06 046】SendAsync begin:10:07:06 046
    ///     【10:07:06 047】SendAsync Url⇒/HotelPms.Data.GrpcTableCore/GetData
    ///     Enter⇒Row:0 Col:0 Text:1
    ///     【10:07:06 095】SendAsync End:10:07:06 095
    ///     該当部屋タイプが既に存在します。
    ///     Leave⇒End
    ///     
    ///     ■追加後
    ///     Leave⇒Start
    ///     Leave⇒Row:1 Col:0 enterPush:False, text:1
    ///     【10:26:02 811】SendAsync begin:10:26:02 811
    ///     【10:26:02 811】SendAsync Url⇒/HotelPms.Data.GrpcTableCore/GetData
    ///     【10:26:02 860】SendAsync End:10:26:02 860
    ///     該当部屋タイプが既に存在します。
    ///     Leave⇒End
    ///     Enter⇒Row:0 Col:0
    ///     
    ///     Semaphore(int initialCount, int maximumCount);
    ///     initialCount代表还分配几个线程,比如是1,那就是还能允许一个线程继续跑锁起来的代码
    ///     maximumCount代表最大允许数, 比如是1, 那就是进去1个线程, 就会锁起来
    /// </summary>
    private System.Threading.SemaphoreSlim slimlock = new SemaphoreSlim(1, 1);
 
    private int selRow = 0;
    private int selectedRowNumber = -1;
    private MudTable<RoomTypeInputRow> mudRoomTypeInputTable;
 
    private bool keyDownPreventDefault = false;
    private bool keyPressPreventDefault = false;
    private bool enterPush = false;
    private bool arrowUpPush = false;
 
    private EditGridFocusEventArgs m_CurrentFocusData = new EditGridFocusEventArgs();
 
    #endregion
 
    #region ★★★★★ Property ★★★★★ 
 
    private RoomTypeInputRow m_SelRoomTypeInputRow = null;
 
    /// <summary>
    /// 選択されている行
    /// </summary>
    public RoomTypeInputRow SelRoomTypeInputRow
    {
        get { return m_SelRoomTypeInputRow; }
        set
        {
            m_SelRoomTypeInputRow = value;
            EnvironmentSetting.Debug($"Change:{m_SelRoomTypeInputRow.ID}");
        }
    }
 
    #endregion
 
    #region  ★★★★★ Class Event ★★★★★
 
    protected override void OnInitialized()
    {
        //EnvironmentSetting.Debug($"【{DateTime.Now.ToString("HH:mm:ss fff")}】RoomTypeInput.OnInitialized開始:");
        base.OnInitialized();
    }
 
    protected override void OnAfterRender(bool firstRender)
    {
        //EnvironmentSetting.Debug($"【{DateTime.Now.ToString("HH:mm:ss fff")}】RoomTypeInput.OnAfterRender開始:{firstRender}");
        base.OnAfterRender(firstRender);
    }
 
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await slimlock.WaitAsync();
        EnvironmentSetting.Debug($"RoomTypeInput.OnAfterRenderAsync開始:{firstRender}");
        await ShowFocus();
        EnvironmentSetting.Debug($"RoomTypeInput.OnAfterRenderAsync終了:{firstRender}");
        slimlock.Release();
    }
 
    protected override void OnParametersSet()
    {
        //EnvironmentSetting.Debug($"【{DateTime.Now.ToString("HH:mm:ss fff")}】RoomTypeInput.OnParametersSet開始:");
        base.OnParametersSet();
    }
 
    protected override bool ShouldRender()
    {
        EnvironmentSetting.Debug($"RoomTypeInput.ShouldRender開始:");
        return base.ShouldRender();
    }
 
    #endregion
 
    #region ★★★★★ Control Event ★★★★★ 
 
    private async Task Enter(int index, RoomTypeInputRow data, FocusEventArgs e)
    {
        await slimlock.WaitAsync();
        int rowIdx = GetRowIndex(data);
        Console.WriteLine($"Enter⇒Row:{rowIdx} Col:{index}");
        slimlock.Release();
    }
 
    private async Task Leave(int index, RoomTypeInputRow data, FocusEventArgs e)
    {
        string text = string.Empty;
        try
        {
            await slimlock.WaitAsync();
            Console.WriteLine($"Leave⇒Start");
            int rowIdx = GetRowIndex(data);
            text = data.GetCellText(index);
            Console.WriteLine($"Leave⇒Row:{rowIdx} Col:{index} enterPush:{enterPush}, text:{text}");
 
            if (enterPush)
            {
                //Enterキーを押した時にも既にチェック済
                enterPush = false;
            }
            else
            {
                //値変更したら、イベント発生
                if (!data.IsValueChanged(index, text))
                {
                    //必須判断:初期表示時:OrgTextは空白のため、値変更しないまま
                    return;
                }
 
                //通常チェックを行う
                if (!await IsValid(index, text, data, false))
                {
                    return;
                }
            }
        }
        catch (Exception ex)
        {
            OperationLog.Instance.WriteLog(ex.Message);
        }
        finally
        {
            //Refresh();    //UI更新(binding値変更通知)
            Console.WriteLine($"Leave⇒End");
            slimlock.Release();
        }
    }
 
    /// <summary>
    /// ※Backspaceここに来る
    /// </summary>
    /// <param name="index"></param>
    /// <param name="data"></param>
    /// <param name="e"></param>
    /// <returns></returns>
    private async Task KeyDown(int index, RoomTypeInputRow data, KeyboardEventArgs e)
    {
        if (e.Key == "ArrowUp")
        {
            try
            {
                EnvironmentSetting.Debug($"ArrowUp:{index}");
                if (SetAutoNextFocus(index, false, data))
                {
                    //BlazorのBugでEnterのパラメータ変数できない!
                    //Uncaught Error: System.ArgumentException: There is no event handler associated with this event. EventId: '290'. (Parameter 'eventHandlerId')
                    //mkArtakMSFT modified the milestones: Next sprint planning, 6.0-preview4 on 20 Mar ← Net6.0-preview4対応だそう
                    keyDownPreventDefault = true;
                    arrowUpPush = true;
                }
                else
                {
                    keyDownPreventDefault = false;
                    arrowUpPush = false;
                }
            }
            catch (Exception ex)
            {
                OperationLog.Instance.WriteLog($"KeyDown:{ex.Message}");
            }
            finally
            {
                Refresh("KeyDown⇒↑");    //UI更新(binding値変更通知)
            }
        }
        else if (e.Key == "End")
        {
            if (data.Cells[index].ShowStyle == EShowStyle.ShowList)
            {
                data.ActiveCol = index;
                if (index == (int)RoomTypeInputRow.ColType.ID)
                {
                    using DataTable dt = await  MasterCore.GetRoomTypeList();
                    var parameters = new DialogParameters { ["Data"] = dt };
                    var dialog = DialogService.Show<SelectList>($"部屋タイプ一覧", parameters);
                    var ret = await dialog.Result;
                    if (!ret.Cancelled)
                    {
                        var row = ret.Data as DataRow;
                        EnvironmentSetting.Debug(row[0].ToString());
                        data.Cells[index].Text = row[0].ToString();
                        data.Cells[(int)RoomTypeInputRow.ColType.Name].Text = row[1].ToString();
                        await KeyPress(index, data, new KeyboardEventArgs { Key = "Enter" });
                        //Refresh("KeyDown⇒End");
                    }
                }
            }
        }
        else if (e.Key == "Delete")
        {
            if (index == (int)RoomTypeInputRow.ColType.ID)
            {
                int rowIdx = GetRowIndex(data);
                List<RoomTypeInputRow> list = (Data.RoomTypeList as List<RoomTypeInputRow>);
                bool isLastRow = rowIdx == (list.Count - 1);
                if (isLastRow) { return; }
                SetFocus(rowIdx, (int)RoomTypeInputRow.ColType.ID);
                EnvironmentSetting.Debug($"削除前件数:{list.Count}");
                list.Remove(data);
                EnvironmentSetting.Debug($"削除後件数:{list.Count}");
                keyDownPreventDefault = false;
                arrowUpPush = false;
            }
        }
        else
        {
            keyDownPreventDefault = false;
            arrowUpPush = false;
        }
    }
 
    /// <summary>
    /// ※Backspaceここに来ない
    /// </summary>
    /// <param name="index"></param>
    /// <param name="data"></param>
    /// <param name="e"></param>
    /// <returns></returns>
    private async Task KeyPress(int index, RoomTypeInputRow data, KeyboardEventArgs e)
    {
        if (e.Key == "Enter")
        {
            try
            {
                int rowIdx = GetRowIndex(data);
                Console.WriteLine($"Row:{rowIdx} Col:{index}");
                Console.WriteLine("Data:" + CConvert.ToJsonText<RoomTypeInputRow>(data));
 
                //※その時、Fields[index].Textの値まだ変っていない!!!!!
                string text = await data.GetInputValue(index, JSRuntime);
                EnvironmentSetting.Debug($"Return:{index}⇒{text}");
                keyPressPreventDefault = true;   //イベント中止
                enterPush = true;   //Enterを押した知らせ
 
                //値変更したら、イベント発生
                if (data.IsValueChanged(index, text))
                {
                    //通常チェックを行う
                    if (!await IsValid(index, text, data, true)) { return; }
                }
                else
                {
                    //必須判断:初期表示時:OrgTextは空白のため、値変更しないまま
                    if (text.Trim().Length == 0)
                    {
                        //最終行となるはず
                    }
                }
 
                if (!SetAutoNextFocus(index, true, data)) { return; }
            }
            catch (Exception ex)
            {
                EnvironmentSetting.Debug(ex.Message);
            }
            finally
            {
                Refresh("KeyPress⇒Enter");    //UI更新(binding値変更通知)
            }
        }
        else
        {
            if (CConvert.IsNumPress(e.Key))
            {
                keyPressPreventDefault = false;
            }
            else
            {
                keyPressPreventDefault = true; //入力禁止(※:IMEモードで入力したものをここでは通らないため、防げない)
            }
            enterPush = false;
        }
    }
 
    #endregion
 
    #region ★★★★★ Private Method ★★★★★ 
 
    private RenderFragment RenderWidget(string msg) => builder =>
    {
        builder.OpenComponent(0, typeof(MessageContext));
        builder.AddAttribute(1, "Text", msg);
        builder.CloseComponent();
    };
 
    /// <summary>
    /// フォーカス移動準備
    /// </summary>
    /// <param name="row"></param>
    /// <param name="col"></param>
    /// <param name="action">EditModeの場合、自動で次のフォーカス移動する</param>
    private void SetFocus(int row, int col, EditGridFocusEventArgs.ActionType action = EditGridFocusEventArgs.ActionType.Focus)
    {
        EnvironmentSetting.Debug($"フォーカス移動準備:開始⇒Row:{row},Col:{col},Action:{action},Enabled:{m_CurrentFocusData.Enabled}");
        m_CurrentFocusData.Row = row;
        m_CurrentFocusData.Col = col;
        m_CurrentFocusData.Action = action;
        m_CurrentFocusData.Enabled = true;
        EnvironmentSetting.Debug($"フォーカス移動準備:完了⇒Row:{row},Col:{col},Action:{action},Enabled:{m_CurrentFocusData.Enabled}");
    }
 
    /// <summary>
    /// OnAfterRenderAsync時フォーカスを設定
    /// </summary>
    private async Task<bool> ShowFocus()
    {
        try
        {
            EnvironmentSetting.Debug($"SaleInput.FocusAsync行Before:Row={m_CurrentFocusData.Row},Col={m_CurrentFocusData.Col},Action={m_CurrentFocusData.Action},Enabled={m_CurrentFocusData.Enabled}");
            if (!m_CurrentFocusData.Enabled)
            {
                EnvironmentSetting.Debug($"OnAfterRenderAsync⇒return false");
                return false;
            }
            m_CurrentFocusData.Enabled = false;
 
            EnvironmentSetting.Debug($"ShowFocus処理:Row={m_CurrentFocusData.Row},Col={m_CurrentFocusData.Col},Action={m_CurrentFocusData.Action},Enabled={m_CurrentFocusData.Enabled}");
            if (m_CurrentFocusData.Action == EditGridFocusEventArgs.ActionType.Focus && m_CurrentFocusData.Row != -1 && m_CurrentFocusData.Col != -1)
            {
                //この時点Grid編集状態ですので、フォーカス可能になる
                EnvironmentSetting.Debug($"フォーカスへ開始:Row = {m_CurrentFocusData.Row}, Col = {m_CurrentFocusData.Col}");
                await (Data.RoomTypeList as List<RoomTypeInputRow>)[m_CurrentFocusData.Row].Cells[m_CurrentFocusData.Col].Ref.FocusAsync();  //ここに再度OnAfterRenderAsyncに入るが、EnabledはFalseのため、ShowFocusの動作がしない
                EnvironmentSetting.Debug($"フォーカスへ完了:Row = {m_CurrentFocusData.Row}, Col = {m_CurrentFocusData.Col}");
            }
            return true;
        }
        catch (Exception ex)
        {
            EnvironmentSetting.Debug(ex.Message);
            return false;
        }
    }
 
 
 
    private int GetRowIndex(RoomTypeInputRow data)
    {
        return (Data.RoomTypeList as List<RoomTypeInputRow>).IndexOf(data);
    }
 
    /// <summary>
    /// フォーカス移動
    /// </summary>
    /// <param name="index">列Index</param>
    /// <param name="isEnter"></param>
    /// <returns></returns>
    private bool SetAutoNextFocus(int index, bool isEnter, RoomTypeInputRow data)
    {
        try
        {
            int rowIdx = GetRowIndex(data);
            List<RoomTypeInputRow> list = (Data.RoomTypeList as List<RoomTypeInputRow>);
            if (isEnter)
            {
                if (index == (int)RoomTypeInputRow.ColType.ID)
                {
                    SetFocus(rowIdx, (int)RoomTypeInputRow.ColType.Count);
                }
                else
                {
                    if (rowIdx == -1)
                    {
                        //削除された場合
                        //await list[list.Count - 1].RefID.FocusAsync();
                    }
                    else
                    {
                        if (rowIdx < (list.Count - 1))
                        {
                            SetFocus(rowIdx + 1, (int)RoomTypeInputRow.ColType.ID);
                        }
                    }
                }
            }
            else
            {
                if (index == (int)RoomTypeInputRow.ColType.ID)
                {
                    if (rowIdx > 0) { SetFocus(rowIdx - 1, (int)RoomTypeInputRow.ColType.Count); }
                }
                else
                {
                    SetFocus(rowIdx, (int)RoomTypeInputRow.ColType.ID);
                }
            }
            return true;
        }
        catch
        {
            return false;
        }
    }
   
    /// <summary>
    /// 入力されたIDが存在したかどうか
    /// </summary>
    /// <param name="rowIdx"></param>
    /// <param name="id"></param>
    /// <returns></returns>
    private bool IsExistsID(int rowIdx, int id)
    {
        List<RoomTypeInputRow> list = (Data.RoomTypeList as List<RoomTypeInputRow>);
        int i = 0;
        foreach (RoomTypeInputRow row in list)
        {
            if (i == rowIdx) { continue; }
            if (CConvert.ToInt(row.ID) == id)
            {
                return true;
            }
            i++;
        }
        return false;
    }
 
    /// <summary>
    /// 業務チェック
    /// </summary>
    /// <param name="index"></param>
    /// <param name="inputText"></param>
    /// <param name="data"></param>
    /// <param name="isEnter"></param>
    /// <returns></returns>
    private async Task<bool> IsValid(int index, string inputText, RoomTypeInputRow data, bool isEnter)
    {
        int rowIdx = GetRowIndex(data);
        //最終行判断
        List<RoomTypeInputRow> list = (Data.RoomTypeList as List<RoomTypeInputRow>);
        bool isLastRow = rowIdx == (list.Count - 1);
 
        if (index == (int)RoomTypeInputRow.ColType.ID)
        {
            int id = CConvert.ToInt(inputText);
            RoomTypeBase typeBase = await MasterCore.GetRoomTypeBase(id);
            if (typeBase == null)
            {
                if (isEnter)
                {
                    Message.Show(RenderMessage("該当データが存在しません。"));
                }
                else
                {
                    Console.WriteLine("該当データが存在しません。");
                }
                data.RestoreText(RoomTypeInputRow.ColType.ID);
                return false;
            }
 
            if (IsExistsID(rowIdx, id))
            {
                if (isEnter)
                {
                    Message.Show(RenderMessage("該当部屋タイプが既に存在します。"));
                }
                else
                {
                    Console.WriteLine("該当部屋タイプが既に存在します。");
                }
                data.RestoreText(RoomTypeInputRow.ColType.ID);
                return false;
            }
            data.SetCellText((int)RoomTypeInputRow.ColType.Name, typeBase.Name);
 
            if (isLastRow)
            {
                //新規
                data.SetCellText((int)RoomTypeInputRow.ColType.Count, "1");
 
                //UseRoomを追加:当日
                var useRoom = await Data.CreateUseRoom(id, typeBase.Kind);
                data.DataList.Add(useRoom);
 
                //一番したの新規行表示
                list.Add(new RoomTypeInputRow());
            }
            else
            {
                //部屋タイプ変更(データ更新)
                foreach (var item in data.DataList)
                {
                    item.RoomTypeID = id;
                    item.RoomKind = typeBase.Kind;
                }
 
                //画面表示更新
                data.SetCellText((int)RoomTypeInputRow.ColType.ID, id.ToString());
            }
        }
        else if (index == (int)RoomTypeInputRow.ColType.Count)
        {
            if (isLastRow) { return false; }
            //0の場合、完全に削除する
            int orgCount = data.DataList.Count;
            int newCount = CConvert.ToInt(inputText);
 
            //UseRoomの±
            int count = newCount - orgCount;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    var useRoom = await Data.CreateUseRoom(data.DataList[0].RoomTypeID, data.DataList[0].RoomKind);
                    data.DataList.Add(useRoom);
                }
            }
            else
            {
                Data.RemoveUseRoom(data.DataList, -count);
            }
 
            if (newCount == 0)
            {
                list.Remove(data);
                if (rowIdx >= list.Count)
                {
                    SetFocus(list.Count - 1, (int)RoomTypeInputRow.ColType.ID);
                }
                else
                {
                    SetFocus(rowIdx, (int)RoomTypeInputRow.ColType.ID);
                }
                return false;
            }
 
            data.SetCellText(index, CConvert.ToInt(inputText).ToString());
        }
        return true;
    }
 
    #endregion
 
    #region ★★★★★ Public Method ★★★★★ 
 
    /// <summary>
    /// コントロール再表示
    /// </summary>
    public void Refresh(string type)
    {
        EnvironmentSetting.Debug($"再表示へ開始:{type}");
        StateHasChanged();
        EnvironmentSetting.Debug($"再表示へ終了:{type}");
        if(OnRefresh != null) { OnRefresh(); }
    }
 
    #endregion
}