using ICSharpCode.Core;
using Mesnac.Action.Base;
using Mesnac.Action.ChemicalWeighing.Entity;
using Mesnac.Codd.Session;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Mesnac.Action.ChemicalWeighing.Technical.XlRecipe
{
public class DelMaterialAction : ChemicalWeighingAction, IAction
{
#region 事件定义
///
/// 删除配方事件定义
///
public static event EventHandler OnDelMaterial;
#endregion
#region 字段定义
private RuntimeParameter _runtime;
private DbMCControl _clientGridControl = null;
private DbMCControl _weighGridControl = null;
#endregion
#region IAction接口实现
public void Run(RuntimeParameter runtime)
{
base.RunIni(runtime);
this._runtime = runtime;
ICSharpCode.Core.LoggingService.Debug("配方管理—删除配方物料业务...");
DbMCControl clientGridControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "xl_recipe").FirstOrDefault();
if (clientGridControl == null || !(clientGridControl.BaseControl is DataGridView))
{
ICSharpCode.Core.LoggingService.Error("{配方管理—删除配方物料}缺少配方管理控件...");
return;
}
this._clientGridControl = clientGridControl;
DbMCControl weighGridControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "xl_weigh").FirstOrDefault();
if (weighGridControl == null || !(weighGridControl.BaseControl is DataGridView))
{
ICSharpCode.Core.LoggingService.Error("{配方管理—删除配方物料}缺少配方管理控件...");
return;
}
this._weighGridControl = weighGridControl;
this.DoWork();
}
#endregion
#region 方法定义
///
/// 删除计划
///
protected void DoWork()
{
this._runtime.BaseControl.MCEnabled = false;
try
{
DataGridView clientGridView = this._clientGridControl.BaseControl as DataGridView;
DataGridView weighGridView = this._weighGridControl.BaseControl as DataGridView;
#region 1 变量定义
string selectRecipeName = null;
string selectRecipeID = string.Empty;
xl_recipe pmt_Recipe = null;
#endregion
#region 2 判断是否选择了要修改的配方
if (clientGridView.SelectedRows.Count != 1)
{
MessageBox.Show("请选择一条要删除的配方!", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
this._runtime.IsReturn = true;
return;
}
#endregion
#region 3 判断该配方是否可进行删除(如果配方正在RT_Plan表中,查不允许删除)
selectRecipeName = clientGridView.SelectedRows[0].Cells["Recipe_Name"].Value as string;
selectRecipeID = clientGridView.SelectedRows[0].Cells["ID"].Value as string;
string selectMaterName = weighGridView.SelectedRows[0].Cells["Material_Name"].Value as string;
if (Product.XlPlan.PlanHelper.PlanExistsRecipeName(selectRecipeID))
{
MessageBox.Show("该配方正在计划列表中,不能进行删除!", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
this._runtime.IsReturn = true;
return;
}
#endregion
//删除配方前询问
string msg1 = "确认删除当前物料(物料名:{0})吗?"; //确认删除当前配方(配方名:{0})吗?
msg1 = String.Format(msg1, selectMaterName);
DialogResult result = MessageBox.Show(msg1, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
#region 4 获取配方实体
pmt_Recipe = RecipeHelper.GetRecipeByName(selectRecipeID);
#endregion
#region 5 执行删除操作
if (pmt_Recipe != null)
{
#region 5.1 删除配方信息
var Id = weighGridView.SelectedRows[0].Cells["ID"].Value;
if (!string.IsNullOrEmpty(Id.ToString()))
{
RecipeHelper.DeleteRecipematerial(Id.ToString());
var wList = Product.XlPlan.PlanHelper.Getxl_weighList(pmt_Recipe.ID);
decimal totalError = wList.Sum(d => d.Set_Error);
decimal totalWeigh = wList.Sum(d => d.Set_Weight);
UpdateError(pmt_Recipe.ID, totalWeigh, totalError);
}
#endregion
#region 5.2 删除配方对应物料信息
//RecipeHelper.DelWeighByRecipeID(pmt_Recipe.ID);
#endregion
#region 5.3 触发事件
if (OnDelMaterial != null)
{
OnDelMaterial(this._runtime.BaseControl.MCRoot, System.EventArgs.Empty);
}
#endregion
MessageBox.Show("配方物料删除成功!");
}
#endregion
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("配方物料异常:" + ex.Message, ex);
#region 记录操作日志
base.DBLog(ex.Message);
#endregion
MessageBox.Show(ex.Message, Mesnac.Basic.LanguageHelper.WarnCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
finally
{
this._runtime.BaseControl.MCEnabled = true;
}
}
#endregion
#region 更新配方误差
///
/// 删除配方的物料信息
///
/// 配方ID
public static void UpdateError(string Id, decimal totalWeigh, decimal Set_Error)
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
dbHelper.CommandType = CommandType.Text;
string sqlstr = String.Empty;
//添加新配方
sqlstr = @" update xl_recipe set Total_Weight=@TotalWeight,Total_Error=@Error where ID=@Id";
dbHelper.ClearParameter();
dbHelper.CommandText = sqlstr;
dbHelper.AddParameter("@TotalWeight", totalWeigh);
dbHelper.AddParameter("@Error", Set_Error);
dbHelper.AddParameter("@Id", Id);
dbHelper.ExecuteNonQuery();
}
#endregion
}
}