ホテル管理システム
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
@using System.ComponentModel.DataAnnotations
@using System.Text.RegularExpressions
@using System.Reflection
@using HotelPms.Client.Blazor.ViewModel
@using HotelPms.Data.Common
@using HotelPms.Data.Common.Interface.Master
@using HotelPms.DataAccessGrpc.Client
@using HotelPms.Share.IO
@using HotelPms.Share.Util
@using ViewModel = HotelPms.Client.Blazor.ViewModel
@using static HotelPms.Client.Blazor.Util.SystemEnum
@inject IJSRuntime JSRuntime
 
<MudDialog Style="width: 800px;">
    <TitleContent>
        <MudText Typo="Typo.h6">
            <MudIcon Icon="@Icons.Material.Filled.Dvr" Class="mr-3 mb-n1"/>
            館マスタ詳細
        </MudText>
    </TitleContent>
    <DialogContent>
        <MudGrid Justify="Justify.FlexStart" Class="align-center">
            @if (errorAll.Length > 0)
            {
                <MudItem xs="12">
                    <MudText Color="@Color.Error">@errorAll</MudText>
                </MudItem>
            }
 
            @foreach(var field in Data.Fields)
            {
                <MudItem xs="@field.WidthUnit">
                    <MudTextField @bind-Value="field.Text" Error="@field.Error" ErrorText="@field.ErrorText" @ref="field.Ref" T="string" Label="@field.Caption" 
                        AutoFocus = "@((field.Index == 0 && Data.EditMode == (int)EMasterEditStatus.Create) || (field.Index == 1 && Data.EditMode == (int)EMasterEditStatus.Update))" 
                        OnKeyDown="@(e => Data.KeyDown(field.Index, e))" KeyDownPreventDefault="@Data.KeyDownPreventDefault" 
                        OnKeyPress="@(e => Data.KeyPress(field.Index, e))" KeyPressPreventDefault="@Data.KeyPressPreventDefault" 
                        OnBlur="@(e => Data.Leave(field.Index, e))"
                              @onfocus="(e => Data.Enter(field.Index, e))" Margin="Margin.Dense" MaxLength="@field.MaxLenth"
                              Adornment="@(field.ShowStyle == EShowStyle.ShowList ? Adornment.End : Adornment.None)" 
                        AdornmentIcon="@(field.ShowStyle == EShowStyle.ShowList ? Icons.Material.Filled.ListAlt : string.Empty)" 
                        OnAdornmentClick="@(() => ShowListAction(field))" Disabled="@field.Disabled" />
                </MudItem>
 
                if (field.NewLine)
                {
                     <MudItem xs="@(12 - field.WidthUnit)"></MudItem>
                }
            }
        </MudGrid>
    </DialogContent>
    <DialogActions>
        <MudGrid Spacing="2" Justify="Justify.Center" Class="my-3">
            <MudItem>
                <MudButton @ref="btnSave" Variant="Variant.Filled" Color="MudBlazor.Color.Primary" OnClick="Save" StartIcon="@Icons.Filled.Save" Class="focus-button" Style="width: 120px; height: 40px;">保存</MudButton>
            </MudItem>
            <MudItem>
                <MudButton Variant="Variant.Filled" Color="MudBlazor.Color.Primary" OnClick="Close" StartIcon="@Icons.Filled.Close" Class="focus-button" Style="width: 120px; height: 40px;">閉じる</MudButton>
            </MudItem>
        </MudGrid>                
    </DialogActions>
</MudDialog>
 
@*<style>
    /* 伪类选择器 :focus-within */
    /* 它表示一个元素获得焦点,或,该元素的后代元素获得焦点。划重点,它或它的后代获得焦点。 */
    /* 这也就意味着,它或它的后代获得焦点,都可以触发 :focus-within。 */
    .focus-button:focus-within {
        border: 1px solid;
        border-color: #00FF00;
        -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(0, 255, 0, 0.6);
        -moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(0, 255, 0, 0.6);
        box-shadow: inset 0px 1px 1px rgba(0, 0, 0, 0.075), 0px 0px 8px rgba(0, 255, 0, 0.6);
    }
</style>
*@
 
@code {
    [Inject] private IDialogService DialogService { get; set; }
    [Inject] private GrpcChannel Channel { get; set; }
    [CascadingParameter] MudDialogInstance MudDialog { get; set; }
    [Parameter] public ViewModel.Building Data { get; set; }
    private string errorAll = string.Empty;
    private MudButton btnSave;
 
    protected override void OnInitialized()
    {
        Data.Refresh = new Action(StateHasChanged);  //メイン画面の更新
        Data.BeforeAutoNextFocus += BeforeAutoNextFocus;
        Data.BusinessValid += BusinessValid;
        Data.ShowList += ShowList;
    }
 
    private async void ShowList(ValidField sender, ValidEventArgs e)
    {
        await ShowListAction(sender);
    }
 
    /// <summary>
    /// 業務チェックを行う
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <returns></returns>
    private async Task<bool> BusinessValid(ValidField sender, ValidEventArgs e)
    {
        if (sender.Name == "ID")
        {
            using (BuildingAccess access = new BuildingAccess(Channel))
            {
                bool ret = await access.ExistsAsync(CConvert.ToInt(e.Text));
                EnvironmentSetting.Debug($"access.Exists:{ret}");
                if(Data.EditMode == (int)EMasterEditStatus.Create && ret)
                {
                    if (e.EnterPush)
                    {
                        sender.Error = true;
                        sender.ErrorText = "当該データが既に存在します。";                        
                    }
                    else
                    {
                        sender.Error = false;
                        sender.ErrorText = string.Empty;
                    } 
                    //StateHasChanged();
                    return false;
                }
                else if(Data.EditMode == (int)EMasterEditStatus.Update && !ret)
                {
                    if (e.EnterPush)
                    {
                        sender.Error = true;
                        sender.ErrorText = "当該データが存在しません。";
                    }
                    else
                    {
                        sender.Error = false;
                        sender.ErrorText = string.Empty;
                    } 
                    //StateHasChanged();
                    return false;
                }
            }
        }
 
        sender.Error = false;
        sender.ErrorText = string.Empty;
        //StateHasChanged();
        return true;
    }
 
    /// <summary>
    /// フォーカスを移動
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <returns></returns>
    private async Task<bool> BeforeAutoNextFocus(ValidField sender, ValidEventArgs e)
    {
        if (sender.Index == Data.Fields.Count - 1 && e.EnterPush)
        {
            await btnSave.FocusAsync();   //ここから非同期処理
            return false;
        }
        return true;
    }
 
    /// <summary>
    /// Endキー
    /// </summary>
    /// <param name="item"></param>
    private async Task ShowListAction(ViewModel.ValidField item)
    {
        if (item.Name == "ZipCode")
        {
            if(item.Text.Length == 0) { return; }
            using (DataTable data = await HotelPms.DataAccessGrpc.Client.AddressAccess.GetPostNoSearch(Channel, item.Text))
            {
                if (data.Rows.Count == 0)
                {
                    await DialogService.ShowMessageBox($"{item.Caption}検索","該当データがありません。",yesText: "OK");
                    return;
                }
 
                if (data.Rows.Count > 1000)
                {
                    await DialogService.ShowMessageBox($"{item.Caption}検索","対象データが1000件以上超えていますので、更に条件を調整してください。",yesText: "OK");
                    return;
                }
 
                var parameters = new DialogParameters { ["Data"] = data };
                var dialog = DialogService.Show<SelectList>($"{item.Caption}検索", parameters);
                var ret = await dialog.Result;
                if (!ret.Cancelled)
                {
                    var row = ret.Data as DataRow;
                    item.Text = row[0].ToString();
                    Data.GetField("Address2").Text = row[1].ToString();
                    StateHasChanged();
                }
            }
        }
        else if (item.Name == "Address2")
        {
            if(item.Text.Length == 0) { return; }
            using (DataTable data = await HotelPms.DataAccessGrpc.Client.AddressAccess.GetAddressSearch(Channel, item.Text))
            {
                if (data.Rows.Count == 0)
                {
                    await DialogService.ShowMessageBox($"{item.Caption}検索","該当データがありません。",yesText: "OK");
                    return;
                }
 
                if (data.Rows.Count > 1000)
                {
                    await DialogService.ShowMessageBox($"{item.Caption}検索","対象データが1000件以上超えていますので、更に条件を調整してください。",yesText: "OK");
                    return;
                }
 
                var parameters = new DialogParameters { ["Data"] = data };
                var dialog = DialogService.Show<SelectList>($"{item.Caption}検索", parameters);
                var ret = await dialog.Result;
                if (!ret.Cancelled)
                {
                    var row = ret.Data as DataRow;
                    Data.GetField("ZipCode").Text = row[0].ToString();
                    item.Text = row[1].ToString();                    
                    StateHasChanged();
                }
            }
        }
        else
        {
            await DialogService.ShowMessageBox(
                "Warning",
                "未実装!",
                yesText: "OK", cancelText: "Cancel");
        }
    }
 
    /// <summary>
    /// 画面閉じる
    /// </summary>
    private void Close()
    {
        MudDialog.Cancel();
    }
 
    /// <summary>
    /// 保存
    /// </summary>
    private async void Save()
    {
        if (!await Data.IsValidAll())
        {
            //errorAll = Data.ErrText;
            return;
        }
 
        //データ保存
        using (BuildingAccess access = new BuildingAccess(Channel))
        {
            HotelPms.Data.Master.Building item = new HotelPms.Data.Master.Building()
            {
                ID = CConvert.ToInt(Data.GetField("ID").Text),
                Name = Data.GetField("Name").Text,
                ShortName = Data.GetField("ShortName").Text,
                ZipCode = Data.GetField("ZipCode").Text,
                Tel = Data.GetField("Tel").Text,
                Fax = Data.GetField("Fax").Text,
                Address2 = Data.GetField("Address2").Text,
            };
            //IMaster im = item as IMaster;
 
            var result = Data.EditMode == (int)EMasterEditStatus.Create ?  await access.AddAsync(item) : await access.UpdateAsync(item);
            if(result.ErrNo != 0)
            {
                errorAll = $"更新に失敗しました。";
                OperationLog.Instance.WriteLog($"Building.Save:{result.ErrNo}.{result.ErrData}");
                return; 
            }
        }
 
        MudDialog.Close(DialogResult.Ok(Data));
    }
}