duheng
2024-06-25 43e88ef24c6b3a7988c495d1740e68e33ce52f84
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
using HStation.Dto;
using HStation.WinFrmUI.Basic;
using Mapster;
using Newtonsoft.Json;
using Yw.Dto;
 
namespace HStation.WinFrmUI
{
    public partial class EditProjectDlg : DevExpress.XtraEditors.XtraForm
    {
        public EditProjectDlg()
        {
            InitializeComponent();
        }
 
        private UpdateXhsProjectInput _updatePrj = null;
 
        private UpdateXhsProjectItemInput _UpdateItem = null;
 
        public event Func<UpdateXhsProjectInput, UpdateXhsProjectItemInput, Task<bool>> ReloadEvent = null;
 
        public async void SetBindingData(long ID)
        {
            var model = await new BLL.XhsProject().GetByID(ID);
            _updatePrj = model.Adapt<XhsProjectDto, UpdateXhsProjectInput>();
            this.NameTextEdit.Text = _updatePrj.Name;
            this.TagNameText.Text = _updatePrj.TagName;
            this.CustomerNameTextEdit.Text = _updatePrj.CustomerName;
            this.DescriptionTextEdit.Text = _updatePrj.Description;
            if (string.IsNullOrEmpty(_updatePrj.Address))
            {
                this.dxErrorProvider1.SetError(this.AddressTextEdit, "地址为空");
            }
            this.AddressTextEdit.Text = _updatePrj.Address;
        }
 
        //验证
        private bool Valid()
        {
            this.dxErrorProvider1.ClearErrors();
            if (string.IsNullOrEmpty(this.NameTextEdit.Text.Trim()))
            {
                this.dxErrorProvider1.SetError(this.NameTextEdit, "必填项");
                return false;
            }
            return true;
        }
 
        //确定
        private async void btnOk_Click(object sender, EventArgs e)
        {
            if (!Valid())
                return;
            _updatePrj.Name = this.NameTextEdit.Text.Trim();
            _updatePrj.TagName = this.TagNameText.Text.Trim();
            _updatePrj.CustomerName = this.CustomerNameTextEdit.Text.Trim();
            _updatePrj.Description = this.DescriptionTextEdit.Text.Trim();
            _updatePrj.Address = this.AddressTextEdit.Text.Trim();
            var itemDto = await new BLL.XhsProjectItem().GetByPrjID(_updatePrj.ID);
            _UpdateItem = itemDto.Adapt<XhsProjectItemDto, UpdateXhsProjectItemInput>();
            _UpdateItem.Name = _updatePrj.Name;
            _UpdateItem.TagName = _updatePrj.TagName;
            _UpdateItem.UseStatus = _updatePrj.UseStatus;
            if (await this.ReloadEvent.Invoke(_updatePrj, _UpdateItem))
            {
                MessageBoxHelper.ShowSuccess("编辑成功!");
            }
            else
            {
                MessageBoxHelper.ShowError("编辑失败!");
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
 
        private void EditProjectDlg_Load(object sender, EventArgs e)
        {
            this.webView21.Source = new Uri(AppDomain.CurrentDomain.BaseDirectory + "data/web/html/map_select.html");
        }
 
        private void webView21_WebMessageReceived(object sender, Microsoft.Web.WebView2.Core.CoreWebView2WebMessageReceivedEventArgs e)
        {
            var j = JsonConvert.DeserializeObject<FixationReceivedViewModel>(e.WebMessageAsJson);
            switch (j.Oper)
            {
                case "getLocation":
                    var o = JsonConvert.DeserializeObject<AmapLocationViewModel>(j.Json);
                    if (o == null)
                        break;
                    this.AddressTextEdit.Text = o.address;
                    _updatePrj.Address = o.address;
                    _updatePrj.MapPosition = o.location.lng + "," + o.location.lat;
                    break;
            }
        }
 
        private void SetMaker()
        {
            try
            {
                var message = new
                {
                    oper = "setMarker",
                    data = JsonConvert.SerializeObject(new
                    {
                        loc = _updatePrj.MapPosition,
                        name = _updatePrj.Address
                    })
                };
                var json = JsonConvert.SerializeObject(message);
                webView21.CoreWebView2.PostWebMessageAsJson(json);
            }
            catch (Exception error)
            {
                throw error;
            }
        }
 
        private void webView21_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)
        {
            SetMaker();
        }
    }
}