chr
7 天以前 43a0207d207390abdeeb3ab9155eebf03edd7b1a
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
using PdmSwPlugin.Common.Constants;
using PdmSwPlugin.PriceCheck.Model;
using PdmSwPlugin.PriceCheck.Param;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Controls;
using ExpressionEvaluator;
 
namespace PdmSwPlugin.PriceCheck
{
    /// <summary>
    /// JgjControl.xaml 的交互逻辑
    /// </summary>
    public partial class JgjControl : UserControl, PriceCounter
    {
        public JgjModel Model { get; set; }
        public ModelProperty ModelProperty
        {
            get => Model.ModelProperty;
            set => Model.ModelProperty = value;
        }
 
        // 缓存服务器返回的工艺数据
        private Technology tech { get; set; }
 
        public JgjControl(ModelProperty modelProperty)
        {
            Model = new JgjModel(modelProperty);
            DataContext = Model;
            InitializeComponent();
        }
 
        public double GetPrice(out string message)
        {
            return Model.CountTotal(out message);
        }
 
        public double DoCount(Technology tech, out string message)
        {
            message = null;
            // 傻逼WPF瞎几把触发事件,真尼玛弱智,垃圾,垃圾垃圾,傻逼
            if (tech == null)
            {
                return 0;
            }
            this.tech = tech;
            TechProp densityProp = tech.props.FirstOrDefault(e => e.name == PriceCheckName.DENSITY);
            TechProp unitPriceProp = tech.props.FirstOrDefault(e => e.name == PriceCheckName.UNIT_PRICE);
            TechCraft surfaceCraft = tech.surfaceProcesses.FirstOrDefault(e => e.parameter == PriceCheckName.SURFACE_UP_W);
            TechCraft heatCraft = tech.heatProcesses.FirstOrDefault(e => e.parameter == PriceCheckName.UNIT_PRICE_CN);
 
            ModelProperty.density = densityProp == null ? 0 : double.Parse(densityProp.value);
 
            // 表达式解析
            TypeRegistry reg = new TypeRegistry();
            reg.RegisterSymbol("prop", ModelProperty);
            CompiledExpression exp;
 
            if (ModelProperty.round)
            {
                string express = "3.14 * (prop.length / 2) * (prop.length / 2) * prop.height";
                exp = new CompiledExpression(express)
                {
                    TypeRegistry = reg
                };
                ModelProperty.currentVolume = double.Parse(exp.Eval().ToString());
 
                // ModelProperty.currentVolume = 3.14 * (ModelProperty.length / 2) * (ModelProperty.length / 2) * ModelProperty.height;
            }
            else
            {
                string express = "prop.length * prop.width * prop.height";
                exp = new CompiledExpression(express)
                {
                    TypeRegistry = reg
                };
                ModelProperty.currentVolume = double.Parse(exp.Eval().ToString());
 
                // ModelProperty.currentVolume = ModelProperty.length * ModelProperty.width * ModelProperty.height;
            }
            // 体积是立方mm,转成立方cm,计算出来的单位是g
            ModelProperty.weight = ModelProperty.currentVolume / 1000000 * ModelProperty.density;
            // 保存材料单价
            ModelProperty.stuffUnitPrice = unitPriceProp == null ? 0 : double.Parse(unitPriceProp.value);
            // 计算材料费
            double cache = 0;
            if (ModelProperty.round)
            {
                string express = "3.14 * (prop.length / 2 + 2) * (prop.length / 2 + 2) * (prop.height + 10) * prop.density * prop.stuffUnitPrice / 1000000";
                exp = new CompiledExpression(express)
                {
                    TypeRegistry = reg
                };
                cache = double.Parse(exp.Eval().ToString());
                // cache = 3.14 * (ModelProperty.length / 2 + 2) * (ModelProperty.length / 2 + 2) * (ModelProperty.height + 10) * ModelProperty.density * ModelProperty.stuffUnitPrice / 1000000;
            }
            else
            {
                string express = "(prop.length + 10) * (prop.width + 10) * (prop.height + 5) * prop.density * prop.stuffUnitPrice / 1000000";
                exp = new CompiledExpression(express)
                {
                    TypeRegistry = reg
                };
                cache = double.Parse(exp.Eval().ToString());
                // cache = (ModelProperty.length + 10) * (ModelProperty.width + 10) * (ModelProperty.height + 5) * ModelProperty.density * ModelProperty.stuffUnitPrice / 1000000;
            }
            cache = cache < 5 ? 5 : cache;
            ModelProperty.stuffPrice = cache; // ModelProperty.stuffUnitPrice * ModelProperty.weight;
            Model.stuffFee = ModelProperty.stuffPrice.ToString();
 
            // 表处理单价
            ModelProperty.surfaceUnitPrice = surfaceCraft == null ? 0 : double.Parse(surfaceCraft.value);
            if (ModelProperty.surfaceType == "镀硬铬")
            {
                ModelProperty.surfacePrice = ModelProperty.surfaceUnitPrice * (ModelProperty.length + ModelProperty.height) * (ModelProperty.width + ModelProperty.height) * 2;
            }
            else
            {
                ModelProperty.surfacePrice = ModelProperty.surfaceUnitPrice * ModelProperty.weight;
            }
            Model.surfaceFee = ModelProperty.surfacePrice.ToString();
 
            ModelProperty.heatUnitPrice = heatCraft == null ? 0 : double.Parse(heatCraft.value);
            ModelProperty.heatPrice = ModelProperty.heatUnitPrice * ModelProperty.weight;
            Model.heatFee = ModelProperty.heatPrice.ToString();
 
 
            Model.ProcessGrid = new ObservableCollection<TechProcess>(tech.techProcesses);
            if (Model.ProcessGrid.Count > 0)
            {
                Model.workFee = Model.ProcessGrid.Select(e => e.price).Aggregate((i, j) => i + j).ToString();
            }
            else
            {
                Model.workFee = "0";
            }
 
            double totalPrice = Model.CountTotal(out message);
            if (message != null)
            {
                Model.price = null;
                return 0;
            }
            Model.price = totalPrice.ToString();
            return totalPrice;
        }
 
        private void workFeeTable_CellEditEnding(object sender, DataGridCellEditEndingEventArgs eve)
        {
            if (Model.ProcessGrid.Count > 0)
            {
                Model.workFee = Model.ProcessGrid.Select(e => e.price).Aggregate((i, j) => i + j).ToString();
            }
            else
            {
                Model.workFee = "0";
            }
            double totalPrice = Model.CountTotal(out string message);
            if (message != null)
            {
                Model.price = null;
            }
            else
            {
                Model.price = totalPrice.ToString();
            }
        }
 
        private void CheckBox_Changed(object sender, System.Windows.RoutedEventArgs e)
        {
            DoCount(tech, out string message);
        }
    }
}