alielie
2025-10-13 e3868d4d61ded0e76fc8fe3fdacdd9e6d9113cf1
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
using Panuon.WPF.UI;
using Plugin.SNGenerateTool.Entity;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
 
namespace Plugin.SNGenerateTool
{
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    public partial class UserControl1 : UserControl, INotifyPropertyChanged
    {
        #region ...
        public virtual event PropertyChangedEventHandler PropertyChanged;
 
        protected virtual void Set(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
 
        protected void Set<T>(ref T old, T @new, [CallerMemberName] string propertyName = null)
        {
            old = @new;
            if (propertyName != null)
            {
                Set(propertyName);
            }
        }
        #endregion
 
        private string iniFile = Path.Combine(Environment.CurrentDirectory, "Config", "UserSettings.ini");
 
        private string[] years = new string[30]
    {
        "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
        "B", "C", "D", "E", "F", "G", "H", "J", "K", "L",
        "M", "N", "P", "R", "S", "T", "V", "W", "X", "Y"
    };
 
        private string[] months = new string[12]
        {
        "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
        "B", "C"
        };
 
        private string[] days = new string[31]
        {
        "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
        "B", "C", "D", "E", "F", "G", "H", "J", "K", "L",
        "M", "N", "P", "R", "S", "T", "V", "W", "X", "Y",
        "Z"
        };
 
        private string[] F18CY = new string[10] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
 
        private ObservableCollection<DataInfo> datasource = new ObservableCollection<DataInfo>();
        public ObservableCollection<DataInfo> Datasource
        {
            get => datasource;
            set
            {
                Set(ref datasource, value);
            }
        }
 
 
        public UserControl1()
        {
            this.DataContext = this;
            InitializeComponent();
        }
 
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
 
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
 
 
        private void SaveSettings(string section, string file)
        {
            if (!File.Exists(file))
            {
                MessageBox.Show("配置文件不存在,保存失败", "错误提示", MessageBoxButton.OK, MessageBoxImage.Hand);
                return;
            }
            WritePrivateProfileString(section, "OrderId", tbx_OrderID.Text, file);
            WritePrivateProfileString(section, "SNPrefix", txt_SNPrefix.Text, file);
            WritePrivateProfileString(section, "MESStartSN", txt_StartSN.Text, file);
            WritePrivateProfileString(section, "MESEndSN", txt_EndSN.Text, file);
            WritePrivateProfileString(section, "XDSNDate", txt_Date1.Text, file);
            WritePrivateProfileString(section, "XDSNDaySN", txt_StartDaySN.Text, file);
            WritePrivateProfileString(section, "XDSNSpecialSN", txt_MinSpecialSN.Text, file);
            WritePrivateProfileString(section, "PartNo", txt_PN.Text, file);
            WritePrivateProfileString(section, "HW", txt_HW.Text, file);
            WritePrivateProfileString(section, "SW", txt_SW.Text, file);
            WritePrivateProfileString(section, "GeelyDU", txt_GeelyDU.Text, file);
            WritePrivateProfileString(section, "GeelyHWSD", txt_GeelyHWSD.Text, file);
            WritePrivateProfileString(section, "SXBL", txt_SXBL.Text, file);
            WritePrivateProfileString(section, "SXDISXBL", txt_SXDISXBL.Text, file);
            WritePrivateProfileString(section, "VolvoDU", txt_VolvoDU.Text, file);
            WritePrivateProfileString(section, "VolvoHWSD", txt_VolvoHWSD.Text, file);
        }
 
        public void CreateSN()
        {
            Datasource.Clear();
 
            try
            {
                int num = Convert.ToInt32(txt_StartSN.Text);
                int num2 = Convert.ToInt32(txt_EndSN.Text);
                int num3 = Convert.ToInt32(txt_StartDaySN.Text);
                int num4 = Convert.ToInt32(txt_MinSpecialSN.Text);
                int num5 = Convert.ToInt32(txt_StartDHUIDSN.Text);
                int num6 = Convert.ToInt32(txt_Date1.Text.Substring(0, 4)) % 2001;
                int num7 = Convert.ToInt32(txt_Date1.Text.Substring(4, 2)) - 1;
                int num8 = Convert.ToInt32(txt_Date1.Text.Substring(6, 2)) - 1;
                if (Convert.ToInt32(txt_Date1.Text.Substring(0, 4)) < 2020)
                {
                    MessageBox.Show("生产日期中的年份早于当前年份!", "错误提示", MessageBoxButton.OK, MessageBoxImage.Hand);
                    return;
                }
                int num9 = Convert.ToInt32(txt_Date1.Text.Substring(0, 4)) % 2020;
                GregorianCalendar gregorianCalendar = new GregorianCalendar();
                DateTime time = DateTime.ParseExact(txt_Date1.Text, "yyyyMMdd", CultureInfo.CurrentCulture);
                int weekOfYear = gregorianCalendar.GetWeekOfYear(time, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);
                int num10 = 0;
                switch (time.DayOfWeek.ToString())
                {
                    case "Monday":
                        num10 = 1;
                        break;
                    case "Tuesday":
                        num10 = 2;
                        break;
                    case "Wednesday":
                        num10 = 3;
                        break;
                    case "Thursday":
                        num10 = 4;
                        break;
                    case "Friday":
                        num10 = 5;
                        break;
                    case "Saturday":
                        num10 = 6;
                        break;
                    case "Sunday":
                        num10 = 7;
                        break;
                }
                if (txt_StartSN.Text != null && txt_EndSN.Text != null && num <= num2 && txt_EndSN.Text.Length < 5)
                {
                    int num11 = num;
                    int num12 = num3;
                    int num13 = num4;
                    int num14 = num5;
                    while (num11 <= num2)
                    {
                        DataInfo di = new DataInfo();
                        if (num12.ToString().Length < 5 && num13.ToString().Length < 8 && num14.ToString().Length < 5)
                        {
                            string text = txt_PCODE.Text.Trim() + years[num6] + num13.ToString().PadLeft(7, '0');
                            string text2 = HashPlus.GetHashMD5(text).ToUpper();
                            text2 = text2.Substring(0, 8) + "-" + text2.Substring(8, 4) + "-" + text2.Substring(12, 4) + "-" + text2.Substring(16, 4) + "-" + text2.Substring(20, 12);
                            string text3 = txt_SNPrefix.Text + num11.ToString().PadLeft(4, '0');
                            string text4 = txt_PCODE.Text + txt_MachineCode.Text + txt_FactoryCode.Text + txt_LineNo1.Text + years[num6] + months[num7] + days[num8] + num12.ToString().PadLeft(4, '0') + num13.ToString().PadLeft(7, '0');
                            string text5 = txt_VendorCode.Text + txt_PN.Text + years[num6] + months[num7] + days[num8] + txt_ShiftsNo.Text + num14.ToString().PadLeft(4, '0');
                            string empty = string.Empty;
                            empty = F18CY[num9] + weekOfYear.ToString().PadLeft(2, '0') + num10 + num12.ToString().PadLeft(4, '0');
                            if (text3.Length != 14 && text4.Length != 24 && text5.Length != 24)
                            {
                                MessageBox.Show("MESSN / XDSN / DHUID长度有误,请检查输入的数据是否有误!", "Error", MessageBoxButton.OK, MessageBoxImage.Hand);
                                return;
                            }
                            //dataRow["GUID"] = text2;
                            //dataRow["Orderid"] = tbx_OrderID.Text.Trim();
                            //dataRow["Model"] = cbox_Model.Text.Trim();
                            //dataRow["MESSN"] = text3.Trim();
                            //dataRow["XDSN"] = text4.Trim();
                            //dataRow["DHUID"] = text5.Trim();
                            //dataRow["ProductSN"] = empty.Trim();
                            //dataRow["HW"] = txt_HW.Text.PadRight(16, '0').Trim();
                            //dataRow["SW"] = txt_SW.Text.Trim();
                            //dataRow["SXBL"] = txt_SXBL.Text.Trim();
                            //dataRow["SXDI_SXBL"] = txt_SXDISXBL.Text.Trim();
                            //dataRow["GeelyDU"] = txt_GeelyDU.Text.Trim();
                            //dataRow["GeelyHWSD"] = txt_GeelyHWSD.Text.Trim();
                            //dataRow["ECUDAPN"] = txt_VolvoDU.Text.Trim();
                            //dataRow["ECUCAPN"] = txt_VolvoHWSD.Text.Trim();
                            //dataRow["Status"] = 0;
                            //dataRow["CreateTime"] = Convert.ToDateTime(DateTime.Now.ToString("G"));
 
                            di.GUID = text2;
                            di.OrderID = tbx_OrderID.Text.Trim();
                            di.Model = cbox_Model.Text.Trim();
                            di.MESSN = text3.Trim();
                            di.XDSN = text4.Trim();
                            di.DHUID = text5.Trim();
                            di.ProductSN = empty.Trim();
                            di.HW = txt_HW.Text.PadRight(16, '0').Trim();
                            di.SW = txt_SW.Text.Trim();
                            di.SXBL = txt_SXBL.Text.Trim();
                            di.SXDI_SXBL = txt_SXDISXBL.Text.Trim();
                            di.GeelyDU = txt_GeelyDU.Text.Trim();
                            di.GeelyHWSD = txt_GeelyHWSD.Text.Trim();
                            di.ECUDAPN = txt_VolvoDU.Text.Trim();
                            di.ECUCAPN = txt_VolvoHWSD.Text.Trim();
                            di.Status = 0;
                            di.CreateTime = Convert.ToDateTime(DateTime.Now.ToString("G"));
                            Datasource.Add(di);
                            num11++;
                            num12++;
                            num13++;
                            num14++;
                            continue;
                        }
                        MessageBox.Show("日流水号或连续号位数超出范围", "错误提示", MessageBoxButton.OK, MessageBoxImage.Hand);
                        break;
                    }
                    //toolStripStatusLabel8.Text = $"{table.Rows.Count}行";
                }
                else
                {
                    MessageBox.Show("请检查所有文本框是否有填写,并确认SN的流水号及位数是否有误", "错误提示", MessageBoxButton.OK, MessageBoxImage.Hand);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示", MessageBoxButton.OK, MessageBoxImage.Hand);
            }
        }
 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            SaveSettings(cbox_Model.Text, iniFile);
            CreateSN();
        }
    }
}