/*******************************************************************************
* Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
* 类的信息:
* 1.程序名称:F_TAT_0102.cs
* 2.功能描述:缺陷扣罚策略明细
* 编辑履历:
* 作者 日期 版本 修改内容
* 庄天威 2014/11/18 1.00 新建
*******************************************************************************/
using System;
using System.Data;
using System.Windows.Forms;
using Dongke.IBOSS.PRD.Basics.BaseControls;
using Dongke.IBOSS.PRD.Basics.BaseResources;
using Dongke.IBOSS.PRD.Client.CommonModule;
using Dongke.IBOSS.PRD.Client.Controls;
using Dongke.IBOSS.PRD.WCF.DataModels;
using Dongke.IBOSS.PRD.WCF.Proxys;
using Dongke.IBOSS.PRD.WCF.Proxys.SystemModuleService;
using Dongke.IBOSS.PRD.WCF.Proxys.TATModuleService;
namespace Dongke.IBOSS.PRD.Client.TATModule
{
///
/// 缺陷扣罚策略明细
///
public partial class F_TAT_0102 : FormBase
{
#region 成员变量
// 窗体模式ID
private Constant.FormMode _formType;
// 操作实体ID
private int? _entityId;
// 选择窗体弹出标识
private bool _showFlag = true;
// 数据源
private DataSet _dsSourse;
// 产品数据源
private DataTable _dtGoodsSourse;
// 窗体加载完毕标识
private bool _pageLoadOK = false;
#endregion
#region 构造函数
///
/// 窗体构造
///
/// 窗体开启模式
/// 操作实体ID
public F_TAT_0102(Constant.FormMode formType, int entityId)
{
InitializeComponent();
this._formType = formType;
this._entityId = entityId;
this.Text = FormTitles.F_TAT_0102;
this.tsbtnAdaptive.Text = ButtonText.TSBTN_ADAPTIVE;
this.tsbtnCreateJobs.Text = ButtonText.TSBTN_CREATEJOBS;
this.tsbtnCreateGoods.Text = ButtonText.TSBTN_CREATEGOODS;
this.btnEdit.Text = ButtonText.BTN_EDIT;
}
#endregion
#region 事件
///
/// 窗体加载
///
///
///
private void F_TAT_0102_Load(object sender, EventArgs e)
{
try
{
//绑定产品信息
this.GetGoods();
//绑定数据源与列表
this.BindData();
this.dgvJobs.AutoGenerateColumns = false;
this.dgvGoods.AutoGenerateColumns = false;
//页面加载完毕
this._pageLoadOK = true;
//如果是审核及观看模式
if (this._formType == Constant.FormMode.Display)
{
//列表设置为只读
this.dgvGoods.ReadOnly = true;
this.dgvJobs.ReadOnly = true;
this.dgvGoods.AllowUserToDeleteRows = false;
this.dgvJobs.AllowUserToDeleteRows = false;
//控件除关闭外全部不可用
this.tsbtnCreateGoods.Enabled = false;
this.tsbtnCreateJobs.Enabled = false;
this.btnEdit.Enabled = false;
this.txtMoney.Enabled = false;
this.btnSave.Enabled = false;
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 工种列表值变更
///
///
///
private void dgvJobs_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
try
{
//变更后重新加载
this._pageLoadOK = false;
if (this.dgvJobs.Rows.Count <= Constant.INT_IS_ONE || !this._showFlag)
{
return;
}
DataGridViewRow rowItem = this.dgvJobs.Rows[e.RowIndex];
DataGridViewColumn columnItem = this.dgvJobs.Columns[e.ColumnIndex];
//打开多选页面进行值绑定
this._showFlag = false;
FormUtility.BindJobsRowDataSource(this.dgvJobs,
e.RowIndex, columnItem.Name, string.Empty);
this._showFlag = true;
//根据工种信息绑定明细信息
this.BindDetail();
//加载OK
this._pageLoadOK = true;
// 设置可输入单元格的颜色
this.dgvJobs.IsSetInputColumnsColor = true;
this.dgvGoods.IsSetInputColumnsColor = true;
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 关闭窗体
///
///
///
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
///
/// 保存操作
///
///
///
private void btnSave_Click(object sender, EventArgs e)
{
try
{
//提交操作
ServiceResultEntity srEntity =
(ServiceResultEntity)DoAsync(() =>
{
return TATModuleProxy.Service.EditDefectFineDetail(Convert.ToInt32(this._entityId), this._dsSourse);
});
ServiceResultEntityManager.HandleServiceResultEntity(srEntity, this.Text);
if (srEntity.Status == Constant.ServiceResultStatus.Success)
{
this.DialogResult = DialogResult.OK;
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 创建全部工种信息,并同时创建各工种对应的明细信息
///
///
///
private void tsbtnCreateJobs_Click(object sender, EventArgs e)
{
try
{
//需重新加载
this._pageLoadOK = false;
//这句话,因为新建的时候,莫名其妙会多出一个数据源不存在的空行,这样设定就不会出现了。
this.dgvJobs.AllowUserToAddRows = false;
//获取全部工种
DataSet dsJobs = (DataSet)DoAsync(() =>
{
return SystemModuleProxy.Service.GetJobsData(1);
});
//如果获取到了工种
if (dsJobs != null && dsJobs.Tables.Count != Constant.INT_IS_ZERO)
{
//循环生成工种信息并加入到数据源中
foreach (DataRow drFor in dsJobs.Tables[0].Rows)
{
//首先查看该工种在现有数据源中是否存在
int JobIsHave = 0;
foreach (DataRow drSourse in this._dsSourse.Tables[0].Rows)
{
if (drSourse.RowState == DataRowState.Deleted)
{
continue;
}
int drForJobsId = Convert.ToInt32(drFor["JobsId"]);
int drSourseJobsId = Convert.ToInt32(drSourse["JobsId"]);
if (drForJobsId == drSourseJobsId)
{
JobIsHave++;
break;
}
}
//如果不存在才重新生成
if (JobIsHave == 0 && drFor["JobsId"] != DBNull.Value)
{
DataRow drJobs = _dsSourse.Tables[0].NewRow();
drJobs["JobsId"] = drFor["JobsId"];
drJobs["JobsCode"] = drFor["JobsCode"];
drJobs["JobsName"] = drFor["JobsName"];
this._dsSourse.Tables[0].Rows.Add(drJobs);
}
}
//绑定
this.dgvJobs.AutoGenerateColumns = false;
this.dgvJobs.DataSource = this._dsSourse.Tables[0];
//并根据工种生成明细
this.BindDetail();
this.dgvGoods.IsSetInputColumnsColor = true;
}
//加载完毕
this._pageLoadOK = true;
this.dgvJobs.AllowUserToAddRows = true;
this.dgvJobs.IsSetInputColumnsColor = true;
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 为某工种添加明细信息
///
///
///
private void tsbtnCreateGoods_Click(object sender, EventArgs e)
{
try
{
//如果选择了一个有效工种
if (this.dgvJobs.SelectedRows.Count != Constant.INT_IS_ZERO && this.dgvJobs.SelectedRows[0].IsNewRow == false)
{
//获取工种ID
DataGridViewRow gvrSelected = this.dgvJobs.SelectedRows[0];
int JobsId = Convert.ToInt32(gvrSelected.Cells["JobsId"].Value);
//遍历产品明细添加该工种的所有明细信息
foreach (DataRow drFor in this._dtGoodsSourse.Rows)
{
//首先查看该产品是否已存在
int GoodsIsHave = 0;
foreach (DataGridViewRow gvrFor in this.dgvGoods.Rows)
{
if (Convert.ToInt32(gvrFor.Cells["GoodsID"].Value)
== Convert.ToInt32(drFor["GoodsID"]))
{
GoodsIsHave++;
break;
}
}
//如果不存在才进行添加
if (GoodsIsHave == 0)
{
//向最终数据源添加扣罚明细信息
DataRow drDetail = this._dsSourse.Tables[1].NewRow();
drDetail["JobsId"] = gvrSelected.Cells["JobsId"].Value;
drDetail["GoodsID"] = drFor["GoodsId"];
drDetail["GoodsCode"] = drFor["GoodsCode"];
drDetail["GoodsName"] = drFor["GoodsName"];
drDetail["FineAmount"] = 0;
this._dsSourse.Tables[1].Rows.Add(drDetail);
}
}
this.dgvGoods.IsSetInputColumnsColor = true;
}
else
{
//提示用户先选中一行工种信息
MessageBox.Show(Messages.MSG_TAT_W001, this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 工种删除
///
///
///
private void dgvJobs_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
try
{
//获取删除的工种ID
int JobsId = Convert.ToInt32(e.Row.Cells["JobsId"].Value);
//将该工种对应的明细信息全部删除
this.DeleteGoods(JobsId);
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 批量更新扣罚金额
///
///
///
private void btnEdit_Click(object sender, EventArgs e)
{
try
{
if (!string.IsNullOrWhiteSpace(this.txtMoney.Text))
{
//批量更新现工种对应的明细信息
foreach (DataGridViewRow gvrFor in this.dgvGoods.Rows)
{
gvrFor.Cells["FineAmount"].Value = this.txtMoney.Text;
}
this.dgvGoods.Focus();
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 产品编号变更
///
///
///
private void dgvGoods_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
try
{
if (this.dgvGoods.Rows.Count <= Constant.INT_IS_ONE)
{
return;
}
DataGridViewRow rowItem = this.dgvGoods.Rows[e.RowIndex];
DataGridViewColumn columnItem = this.dgvGoods.Columns[e.ColumnIndex];
// 用编号获取产品信息
if ("GoodsCode".Equals(columnItem.Name))
{
FormUtility.BindGoodsRowDataSource(this.dgvGoods,
e.RowIndex, columnItem.Name, string.Empty);
// 设置可输入单元格的颜色
this.dgvGoods.IsSetInputColumnsColor = true;
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 工种选择变更
///
///
///
private void dgvJobs_SelectionChanged(object sender, EventArgs e)
{
try
{
this.dgvGoods.AutoGenerateColumns = false;
//根据选择的工种绑定对应的明细信息
if (this.dgvJobs.SelectedRows.Count != Constant.INT_IS_ZERO)
{
DataGridViewRow gvrNow = this.dgvJobs.SelectedRows[0];
if (gvrNow.Cells["JobsId"].Value != DBNull.Value && gvrNow.Cells["JobsId"].Value != null)
{
DataTable dtDetail = this._dsSourse.Tables[1];
dtDetail.DefaultView.RowFilter = "JobsId=" + gvrNow.Cells["JobsId"].Value.ToString();
this.dgvGoods.DataSource = dtDetail;
if (this._formType != Constant.FormMode.Display)
{
this.dgvGoods.IsSetInputColumnsColor = true;
}
}
else
{
this.dgvGoods.DataSource = null;
}
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 窗体自适应
///
///
///
private void tsbtnAdaptive_Click(object sender, EventArgs e)
{
this.dgvJobs.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
this.dgvGoods.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
}
#endregion
#region 私有方法
///
/// 绑定数据源
///
private void BindData()
{
try
{
if (this._entityId != 0 && this._entityId != null)
{
ServiceResultEntity srEntity = TATModuleProxy.Service.GetDefectFineDetailById(Convert.ToInt32(this._entityId));
if (srEntity.Data != null)
{
this._dsSourse = (DataSet)srEntity.Data;
if (this._dsSourse.Tables.Count != Constant.INT_IS_TWO)
{
this.DialogResult = DialogResult.Cancel;
}
this.dgvJobs.DataSource = this._dsSourse.Tables[0];
if (this._formType != Constant.FormMode.Display)
{
this.dgvJobs.IsSetInputColumnsColor = true;
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 批量生成明细信息并绑定
///
private void BindDetail()
{
try
{
//首先遍历选中的工种
foreach (DataGridViewRow gvrFor in this.dgvJobs.Rows)
{
//新行不进行该操作
if (gvrFor.IsNewRow)
{
return;
}
//已存在的工种不进行该操作
if (gvrFor.Cells["isHave"].Value != DBNull.Value)
{
continue;
}
//向符合条件的工种添加全部产品信息
foreach (DataRow drFor in this._dtGoodsSourse.Rows)
{
//向最终数据源添加扣罚明细信息
if (gvrFor.Cells["JobsId"].Value != DBNull.Value)
{
DataRow drDetail = this._dsSourse.Tables[1].NewRow();
drDetail["JobsId"] = gvrFor.Cells["JobsId"].Value;
drDetail["GoodsID"] = drFor["GoodsId"];
drDetail["GoodsCode"] = drFor["GoodsCode"];
drDetail["GoodsName"] = drFor["GoodsName"];
drDetail["FineAmount"] = 0;
this._dsSourse.Tables[1].Rows.Add(drDetail);
}
}
gvrFor.Cells["isHave"].Value = 1;
}
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 获取全部产品信息
///
private void GetGoods()
{
try
{
GoodsEntity goods = new GoodsEntity();
goods.ValueFlag = 1;
DataSet dsGoods = SystemModuleProxy.Service.SerachGoods(goods);
if (dsGoods != null && dsGoods.Tables.Count != Constant.INT_IS_ZERO)
{
this._dtGoodsSourse = dsGoods.Tables[0];
}
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 根据工种ID删除明细信息
///
///
private void DeleteGoods(int JobsId)
{
try
{
//遍历全部明细信息
for (int i = 0; i < this._dsSourse.Tables[1].Rows.Count; i++)
{
DataRow drFor = this._dsSourse.Tables[1].Rows[i];
//如果是已存在的才需要删除,新建的会直接消失
if (drFor.RowState != DataRowState.Deleted)
{
//如果是要删除的明细信息,进行移除并修改迭代变量
int drJobsId = Convert.ToInt32(drFor["JobsId"]);
if (drJobsId == JobsId)
{
this._dsSourse.Tables[1].Rows.Remove(drFor);
i--;
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}