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
{
///
/// JgjControl.xaml 的交互逻辑
///
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(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);
}
}
}