tx
2025-04-09 fa7510e1ed63df0366787fa4ed1b3db6426d2b46
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
using DevExpress.XtraEditors;
using System;
using System.Windows.Forms;
 
 
namespace TProduct.WinFrmUI.Data4Factory
{
    public partial class AddProductPumpDlg : DevExpress.XtraEditors.XtraForm
    {
        public AddProductPumpDlg()
        {
            InitializeComponent();
            this.Text = "添加泵信息";
 
            this.ImgPhaseNumMotor.Properties.Items.Clear();
            this.ImgPhaseNumMotor.Properties.AddEnum(typeof(Model.eSupplyCurrentPhase), false);
            this.ImgPhaseNumMotor.SelectedIndex = 0;
            this.simpleBtnOK.SetConfirmButtonColor();
            this.simpleBtnCancel.SetCancelButtonColor();
 
 
            selMotorSeriesCtrl.SetProductType(Model.eProductType.Motor);
            selPumpSeriesCtrl.SetProductType(Model.eProductType.Pump);
 
            selPumpSeriesCtrl.FocusedDataChangedEvent += (s) =>
            {
                if (s.IsInnerMotor == 1)
                {
                    layoutCtrlMotorGourp.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                }
                else
                {
                    layoutCtrlMotorGourp.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                }
                if (s.Paras.SupplyCurrentType != null)
                {
                    setPumpRatedParasCtrl1.SetBindingData(s);
                    selMotorSeriesCtrl.SetSupplyCurrentType(s.Paras.SupplyCurrentType.Value);
                    imageComboBox供电方式Motor.EditValue = (int)s.Paras.SupplyCurrentType.Value;
                }
 
            };
        }
 
        //回调
        public event Action<Model.ProductMainExPump,
            Model.ProductMainExMotor> ReloadDataEvent;
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="SeriesID"></param>
        public void SetBindingData(TProduct.Model.ProductSeries Series , int SortCode)
        {
            if (Series == null)
                return;
 
            this.ckCode.Checked = true;
 
            if (Series.Paras.SupplyCurrentType != null)
                this.selMotorSeriesCtrl.SetBindingData( Series.Paras.SupplyCurrentType.Value, 0);
            else
                this.selMotorSeriesCtrl.SetBindingData(0);
 
            this.selPumpSeriesCtrl.SetBindingData(Series.ID);
            this.setPumpRatedParasCtrl1.SetBindingData(Series);
            this.partBaseCtrl1.SetBindingData(0);
            this.spinEditSortCode.EditValue = SortCode;
        }
 
        /// <summary>
        /// 复制
        /// </summary>
        /// <param name="SeriesID"></param>
        public void SetBindingData(TProduct.Model.ProductSeries pumpSeries, TProduct.Model.ProductMainExPump pump)
        {
            this.ckCode.Checked = true;
            this.selPumpSeriesCtrl.SetBindingData( pumpSeries.ID);
            //this.partBaseCtrl1.SetBindingData(pump);
 
 
            this.spinEditSortCode.EditValue = pump.SortCode;
            this.TextEditName.Text = pump.Name + "(复制)";
            //如果已经绑定电机则初始化电机信息
            if (pump.MotorMainID > 0)
            {
                var motor_id = Convert.ToInt64(pump.MotorMainID);
                var motor_series_id = new BLL.ProductMain().GetByID(motor_id).SeriesID;
                if (pumpSeries.Paras.SupplyCurrentType != null)
                    this.selMotorSeriesCtrl.SetBindingData(pumpSeries.Paras.SupplyCurrentType.Value, motor_series_id);
                else
                    this.selMotorSeriesCtrl.SetBindingData( motor_series_id);
 
                //this.selectMotorCtrl.SetBindingData(motor_series_id, motor_id);
            }
            else
            {
                if (pumpSeries.Paras.SupplyCurrentType != null)
                    this.selMotorSeriesCtrl.SetBindingData( pumpSeries.Paras.SupplyCurrentType.Value,0);
                else
                    this.selMotorSeriesCtrl.SetBindingData(0);
            }
            ///初始化额定参数列表
            this.setPumpRatedParasCtrl1.SetBindingData(pump);
        }
 
        /// <summary>
        /// 验证
        /// </summary> 
        private bool Valid()
        {
            this.dxErrorProvider1.ClearErrors();
            if (string.IsNullOrEmpty(this.TextEditName.Text.Trim()))
            {
                this.dxErrorProvider1.SetError(this.TextEditName, "必填项");
                return false;
            }
 
            if (this.selPumpSeriesCtrl.SelectProductSeries == null)
            {
                XtraMessageBox.Show("您必须选择一个系列才能继续此项操作");
                return false;
            }
 
            return this.setPumpRatedParasCtrl1.Valid();
        }
        private void simpleBtnOK_Click(object sender, EventArgs e)
        {
            if (!Valid())
                return;
            Model.ProductMainExPump model_pump = new Model.ProductMainExPump();
            if (this.TextEditCode.EditValue != null)
                model_pump.Code = this.TextEditCode.Text.Trim();
            var pump_series = this.selPumpSeriesCtrl.SelectProductSeries;
            if (pump_series != null)
                model_pump.SeriesID = pump_series.ID;
            else
                return;
            Model.ProductMainExMotor motor_model = null;
            if (pump_series.IsInnerMotor == 1)
            {
                model_pump.MotorMainID = -1;
            }
            else
            {
                motor_model = this.selectProductMotorCtrl1.SelectMotor;
                if (motor_model != null)
                    model_pump.MotorMainID = motor_model.ID;
                else
                    model_pump.MotorMainID = -1;
            }
 
 
            model_pump.Name = this.TextEditName.Text.Trim();
 
            if (this.TextEditCode.Text != "")
                model_pump.Code = this.TextEditCode.Text.Trim();
 
            this.setPumpRatedParasCtrl1.GetBindingData(ref model_pump);
 
            var pumpParas = TProduct.Model.RatedParas4Pump.ToModel(model_pump.RatedParas);
            if (pumpParas.CurrentType == Model.eSupplyCurrentType.直流 &&
                pump_series.IsInnerMotor != 1)
            {
                if (string.IsNullOrEmpty(this.TextEditRatedUMotor.Text.Trim()))
                {
                    this.dxErrorProvider1.SetError(this.TextEditRatedUMotor, "必填项");
                    return;
                }
                if (motor_model != null)
                {
                    var motorParas = TProduct.Model.RatedParas4Motor.ToModel(motor_model.RatedParas);
                    if (motorParas.CurrentType != pumpParas.CurrentType)
                    {
                        MessageBox.Show("泵供电方式和电机不一致");
                        return;
                    }
                    if (motor_model.RatedU.HasValue && pumpParas.Voltage != motor_model.RatedU)
                    {
                        pumpParas.Voltage = motor_model.RatedU.Value;
                        motor_model.RatedParas = pumpParas.ToJson();
                    }
                }
 
            }
 
            model_pump.CreateUserID = TProduct.WinFrmUI.GlobeParas.CurrentLoginUser.ID;
            model_pump.CreateTime = DateTime.Now;
            model_pump.UpdateUserID = TProduct.WinFrmUI.GlobeParas.CurrentLoginUser.ID;
            model_pump.UpdateTime = DateTime.Now;
            var bll = new BLL.ProductPump();
            var pump_id = bll.InsertEx(model_pump);
            if (pump_id <= 0)
            {
                XtraMessageBox.Show("添加失败");
                return;
            }
 
            var partlist = this.partBaseCtrl1.GetAllPartList();
            if (partlist != null && partlist.Count > 0)
            {
                foreach (var item in partlist)
                {
                    item.ProductMainID = pump_id;
                }
 
                var partbll = new BLL.PartBase();
                partbll.Inserts(partlist);
            }
 
 
            //修改电机 
            if (motor_model != null)
            {
                motor_model.Code = this.TextEditCodeMotor.Text.Trim();
                motor_model.Name = this.TextEditNameMontor.Text.Trim();
                if (!string.IsNullOrEmpty(this.TextEditRatedPowerMotor.Text.Trim()))
                {
                    var RatedPower = Convert.ToDouble(this.TextEditRatedPowerMotor.EditValue);
                    motor_model.RatedPower = RatedPower == 0 ? -1 : RatedPower;
                }
                if (!string.IsNullOrEmpty(this.TextEditPowerFactorMotor.Text.Trim()))
                {
                    var PowerFactor = Convert.ToDouble(this.TextEditPowerFactorMotor.EditValue);
                    motor_model.PowerFactor = PowerFactor == 0 ? -1 : PowerFactor;
                }
                if (!string.IsNullOrEmpty(this.TextEditRatedIMotor.Text.Trim()))
                {
                    var RatedI = Convert.ToDouble(this.TextEditRatedIMotor.EditValue);
                    motor_model.RatedI = RatedI == 0 ? -1 : RatedI;
                }
                if (!string.IsNullOrEmpty(this.TextEditRatedUMotor.Text.Trim()))
                {
                    var RatedU = Convert.ToDouble(this.TextEditRatedUMotor.EditValue);
                    motor_model.RatedU = RatedU == 0 ? -1 : RatedU;
                }
                if (!string.IsNullOrEmpty(this.TextEditRatednMotor.Text.Trim()))
                {
                    var speed = Convert.ToDouble(this.TextEditRatednMotor.EditValue);
                    motor_model.Ratedn = speed == 0 ? -1 : speed;
                }
 
                motor_model.PhaseNum = (Model.eSupplyCurrentPhase)this.ImgPhaseNumMotor.EditValue;
 
                Model.RatedParas4Motor ratedparas_motor = new Model.RatedParas4Motor();
                ratedparas_motor.E_Self = Convert.ToDouble(this.TextEditE_SelfMotor.EditValue);
                ratedparas_motor.E_PLC = Convert.ToDouble(this.TextEditE_PLCMotor.EditValue);
                ratedparas_motor.IsFrequency = this.cekIsFrequency.Checked;
                ratedparas_motor.CurrentType = (Model.eSupplyCurrentType)Convert.ToInt32
                    (this.imageComboBox供电方式Motor.EditValue);
 
                motor_model.RatedParas = ratedparas_motor.ToJson();
                motor_model.UpdateUserID = TProduct.WinFrmUI.GlobeParas.CurrentLoginUser.ID;
                motor_model.UpdateTime = DateTime.Now;
 
                if (pump_series.IsInnerMotor != 1 && motor_model.Ratedn != null && model_pump.Ratedn != null)
                {
                    if (Math.Abs(motor_model.Ratedn.Value - model_pump.Ratedn.Value) / model_pump.Ratedn > 0.1)
                    {
                        XtraMessageBox.Show("请确认电机转速和泵转速是否匹配!");
                    };
                }
                var motorbll = new BLL.ProductMotor();
                if (!motorbll.UpdateEx(motor_model))
                {
                    XtraMessageBox.Show("电机修改失败!");
                    return;
                }
            }
 
 
 
            model_pump = new BLL.ProductPump().GetExByProductID(pump_id);
 
            this.ReloadDataEvent?.Invoke(model_pump, motor_model);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
 
        private void ckCode_EditValueChanged(object sender, EventArgs e)
        {
            if (ckCode.Checked == true)
                layoutCtrlCode.Enabled = false;
            else
                layoutCtrlCode.Enabled = true;
        }
 
 
        private void selectMotorSeriesCtrl_FocusedDataChangedEvent(Model.ProductSeries obj)
        {
            if (obj == null)
                return;
            this.selectProductMotorCtrl1.SetBindingData(obj.ID, 0);
        }
 
        private void selMotorCtrl_FocusedDataChangedEvent(Model.ProductMainExMotor motor)
        {
            if (motor == null)
                return;
 
            this.TextEditCodeMotor.EditValue = motor.Code;
            this.TextEditNameMontor.EditValue = motor.Name;
            this.TextEditRatedPowerMotor.EditValue = motor.RatedPower < 0 ? null : motor.RatedPower;
            this.TextEditPowerFactorMotor.EditValue = motor.PowerFactor < 0 ? null : motor.PowerFactor;
            this.TextEditRatedIMotor.EditValue = motor.RatedI < 0 ? null : motor.RatedI;
            this.TextEditRatedUMotor.EditValue = motor.RatedU < 0 ? null : motor.RatedU;
            this.TextEditRatednMotor.EditValue = motor.Ratedn < 0 ? null : motor.Ratedn;
            this.ImgPhaseNumMotor.SelectedIndex = (int)motor.PhaseNum == 1 ? 0 : 1;
            if (!string.IsNullOrEmpty(motor.RatedParas))
            {
                var rateParas_motor = Model.RatedParas4Motor.ToModel(motor.RatedParas);
 
                this.imageComboBox供电方式Motor.EditValue = (int)rateParas_motor.CurrentType;
                this.TextEditE_SelfMotor.EditValue = rateParas_motor.E_Self == -1 ? null : rateParas_motor?.E_Self;
                this.TextEditE_PLCMotor.EditValue = rateParas_motor.E_PLC == -1 ? null : rateParas_motor?.E_PLC;
                this.cekIsFrequency.EditValue = rateParas_motor.IsFrequency;
            }
        }
 
        private void AddProductPumpDlg_Activated(object sender, EventArgs e)
        {
            this.TextEditName.Focus();
        }
 
        private void AddProductPumpDlg_Load(object sender, EventArgs e)
        {
 
        }
 
        private void simpleBtnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}