using CnasSynchronusClient; using CnasSynchrousModel; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace CNAS_BalanceClient { public partial class frmConditionMap : Form { DataBaseInfo targetDataBase; public SyncBalanceItem currentBalanceItem; private ComboBox cbxConditionValue; //private List lstConditionValueType = new List() { "等于", "连接", "若...则...", "截取", "除以", "乘以", "小数位数", "数值相加(减)", "截断开头(结尾)" }; private List lstConditionValueType = new List() { "等于" }; public frmConditionMap(SyncBalanceItem currentBalanceItem, DataBaseInfo targetDataBase) { InitializeComponent(); this.currentBalanceItem = currentBalanceItem; this.targetDataBase = targetDataBase; } private void frmConditionMap_Load(object sender, EventArgs e) { if (currentBalanceItem == null || currentBalanceItem.syncParamasInfos == null || currentBalanceItem.syncParamasInfos.Count == 0) { MessageBox.Show("请先指定至少一个映射字段。"); return; } //初始化ComBobox InnitalComboBox(); //加载数据 LoadShowData(); } /// /// 初始化控件combobox /// private void InnitalComboBox() { cbxConditionValue = new ComboBox(); cbxConditionValue.SelectedIndexChanged += new EventHandler(cbxConditionValue_SelectedIndexChanged); cbxConditionValue.SelectionChangeCommitted += new EventHandler(cbxConditionValue_SelectionChangedCommitted); cbxConditionValue.Visible = false; cbxConditionValue.DataSource = lstConditionValueType; } private void cbxConditionValue_SelectionChangedCommitted(object sender, EventArgs e) { cbxConditionValue.Visible = false; } private void cbxConditionValue_SelectedIndexChanged(object sender, EventArgs e) { //当前选中单元格的值将随之发生更改 if (dgvCnas.CurrentCell == null) return; dgvCnas.Rows[dgvCnas.CurrentCell.RowIndex].Cells["condition"].Value = cbxConditionValue.Text; } /// /// 加载数据 /// private void LoadShowData() { //1.根据映射表中的表名,获取该表所有字段,全部显示 string strTableName = currentBalanceItem.syncParamasInfos[0].TargetTable; DataTable dtTableStruct = CnasDataOperationFact.CnasDataOperation().GetCNASTablesStruct(strTableName, targetDataBase); if (dtTableStruct != null) { DataTable dtCnasShow = new DataTable(); dtCnasShow.Columns.Add("CnasTableName"); dtCnasShow.Columns.Add("CnasFieldName"); dtCnasShow.Columns.Add("CnasDataType"); foreach (DataColumn dc in dtTableStruct.Columns) { if (dc.ColumnName.ToUpper() == "ID") continue; dtCnasShow.Rows.Add(new object[] { strTableName, dc.ColumnName, dc.DataType }); } dgvCnas.DataSource = dtCnasShow; } //2.根据已存储数据,匹配后填充到datagridview中 if (currentBalanceItem.lstFixedValue == null) currentBalanceItem.lstFixedValue = new List(); if (currentBalanceItem.lstFixedValue.Count == 0) return; foreach (DataGridViewRow dgvRow in dgvCnas.Rows) { string strCurrentTable = dgvRow.Cells["TableName"].Value.ToString(); string strCurrentColumn = dgvRow.Cells["TableColumn"].Value.ToString(); if (strCurrentTable == "" || strCurrentColumn == "") continue; var query = currentBalanceItem.lstFixedValue.Where(s => s.TableName == strCurrentTable && s.ColumnName == strCurrentColumn).ToList(); if (query.Count == 1) { dgvRow.Cells["Value"].Value = query[0].Value; dgvRow.Cells["Condition"].Value = GiveShowByCondition(query[0]); } } } public string GiveShowByCondition(CnasConditionMapValue cnasFixed) { string strCurrentCondition = ""; switch (cnasFixed.Condition) { case MapCondition.Equal: strCurrentCondition = "等于"; break; case MapCondition.Sub: strCurrentCondition = "连接"; break; case MapCondition.IFThen: strCurrentCondition = "若...则..."; break; case MapCondition.Divided: strCurrentCondition = "除以"; break; case MapCondition.Multiplied: strCurrentCondition = "乘以"; break; case MapCondition.SubString: strCurrentCondition = "截取"; break; case MapCondition.DecimalDigits: strCurrentCondition = "小数位数"; break; case MapCondition.AddSubtract: strCurrentCondition = "数值相加(减)"; break; case MapCondition.SubstringStartEnd: strCurrentCondition = "截断开头(结尾)"; break; } return strCurrentCondition; } private void btnOK_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.OK; this.Close(); } private void dgvCnas_CellEndEdit(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 1) //此时是“值”列 { //如果没有指定条件,无法存储结果 if (dgvCnas.Rows[e.RowIndex].Cells["condition"].Value == null) return; //当前列名 string strCurrentTable = dgvCnas.Rows[e.RowIndex].Cells["TableName"].Value.ToString(); string strCurrentColumn = dgvCnas.Rows[e.RowIndex].Cells["TableColumn"].Value.ToString(); string strCurrentType = dgvCnas.Rows[e.RowIndex].Cells["DataType"].Value.ToString(); string strCurrentCondition = dgvCnas.Rows[e.RowIndex].Cells["condition"].Value.ToString(); if (dgvCnas.CurrentCell.Value == null) { currentBalanceItem.lstFixedValue.RemoveAll(s => s.TableName == strCurrentTable && s.ColumnName == strCurrentColumn); return; } string strInputValue = dgvCnas.CurrentCell.Value.ToString(); //检查合法性 if (strCurrentColumn == "ID") { MessageBox.Show("该列不能指定固定值。"); return; } bool bIfSuccess = true; switch (strCurrentType) { case "System.Decimal": decimal defaultdecimal = 0; if (!decimal.TryParse(strInputValue, out defaultdecimal)) bIfSuccess = false; break; case "System.Int": int defaultint = 0; if (!int.TryParse(strInputValue, out defaultint)) bIfSuccess = false; break; case "System.String": default: break; } if (!bIfSuccess) { MessageBox.Show("输入格式不正确,请重新输入。"); return; } if (GetBytesOfString(strInputValue) > 20) { MessageBox.Show("输入内容超出限制,请重新输入。"); dgvCnas.CurrentCell.Value = ""; return; } //将数据插入到数据源中 var query = currentBalanceItem.lstFixedValue.Where(s => s.TableName == strCurrentTable && s.ColumnName == strCurrentColumn).ToList(); if (query.Count >= 1) { GiveConditionByShow(query[0], strCurrentCondition); //query[0].Condition = strCurrentCondition == "Sub" ? MapCondition.Sub : (strCurrentCondition == "IFThen" ? MapCondition.IFThen: MapCondition.Equal); query[0].Value = strInputValue; } else { CnasConditionMapValue cnasFixed = new CnasConditionMapValue(); cnasFixed.TableName = strCurrentTable; cnasFixed.ColumnName = strCurrentColumn; cnasFixed.Value = strInputValue; //cnasFixed.Condition= strCurrentCondition == "Sub" ? MapCondition.Sub : (strCurrentCondition == "IFThen" ? MapCondition.IFThen : MapCondition.Equal); GiveConditionByShow(cnasFixed, strCurrentCondition); currentBalanceItem.lstFixedValue.Add(cnasFixed); } } } public int GetBytesOfString(string Text) { int nByte = 0; byte[] bytes = Encoding.Unicode.GetBytes(Text); for (int i = 0; i < bytes.GetLength(0); i++) { // 偶数位置,如0、2、4等,为UCS2编码中两个字节的第一个字节 if (i % 2 == 0) { nByte++; // 在UCS2第一个字节时n加1 } else { // 当UCS2编码的第二个字节大于0时,该UCS2字符为汉字,一个汉字算两个字节 if (bytes[i] > 0) { nByte++; } } } return nByte; } public void GiveConditionByShow(CnasConditionMapValue cnasFixed, string strCurrentCondition) { switch (strCurrentCondition) { case "连接": cnasFixed.Condition = MapCondition.Sub; break; case "若...则...": cnasFixed.Condition = MapCondition.IFThen; break; case "截取": cnasFixed.Condition = MapCondition.SubString; break; case "除以": cnasFixed.Condition = MapCondition.Divided; break; case "乘以": cnasFixed.Condition = MapCondition.Multiplied; break; case "小数位数": cnasFixed.Condition = MapCondition.DecimalDigits; break; case "等于": cnasFixed.Condition = MapCondition.Equal; break; case "数值相加(减)": cnasFixed.Condition = MapCondition.AddSubtract; break; case "截断开头(结尾)": cnasFixed.Condition = MapCondition.SubstringStartEnd; break; default: break; } } private void dgvCnas_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgvCnas.RowHeadersWidth - 4, e.RowBounds.Height); TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgvCnas.RowHeadersDefaultCellStyle.Font, rectangle, dgvCnas.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right); } private void dgvCnas_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 0) //如果此时是“条件”列 { ShowConditionValue(); } #region //if (e.ColumnIndex == 1)//如果此时是“值”列 //{ // if (dgvCnas.Rows[e.RowIndex].Cells["condition"].Value == null) return; // if (dgvCnas.Rows[e.RowIndex].Cells["condition"].Value.ToString() == "连接") // { // CnasConditionMapValue cnasFixed = new CnasConditionMapValue // { // TableName = dgvCnas.Rows[e.RowIndex].Cells["TableName"].Value.ToString(), // ColumnName = dgvCnas.Rows[e.RowIndex].Cells["TableColumn"].Value.ToString(), // Condition = MapCondition.Sub // }; // if (dgvCnas.Rows[e.RowIndex].Cells["Value"].Value != null) // cnasFixed.Value = dgvCnas.Rows[e.RowIndex].Cells["Value"].Value.ToString(); // frmConditionParam frmCondition = new frmConditionParam(syncInstrument, cnasFixed); // if (frmCondition.ShowDialog() == DialogResult.OK) // { // if (frmCondition.conditionvalue.Value != null && frmCondition.conditionvalue.Value.ToString() != "") // { // dgvCnas.Rows[e.RowIndex].Cells["Value"].Value = frmCondition.conditionvalue.Value; // //将数据插入到数据源中 // var query = syncInstrument.lstFixedValue.Where(s => s.TableName == cnasFixed.TableName && s.ColumnName == cnasFixed.ColumnName).ToList(); // if (query.Count >= 1) // { // query[0].Condition = cnasFixed.Condition; // query[0].Value = frmCondition.conditionvalue.Value; // } // else // { // cnasFixed.Value = frmCondition.conditionvalue.Value; // syncInstrument.lstFixedValue.Add(cnasFixed); // } // } // } // } // if (dgvCnas.Rows[e.RowIndex].Cells["condition"].Value.ToString() == "若...则...") // { // CnasConditionMapValue cnasFixed = new CnasConditionMapValue // { // TableName = dgvCnas.Rows[e.RowIndex].Cells["TableName"].Value.ToString(), // ColumnName = dgvCnas.Rows[e.RowIndex].Cells["TableColumn"].Value.ToString(), // Condition = MapCondition.IFThen // }; // if (dgvCnas.Rows[e.RowIndex].Cells["Value"].Value != null) // cnasFixed.Value = dgvCnas.Rows[e.RowIndex].Cells["Value"].Value.ToString(); // frmIfThenParams frmIfThen = new frmIfThenParams(syncInstrument, cnasFixed); // if (frmIfThen.ShowDialog() == DialogResult.OK) // { // if (frmIfThen.conditionvalue.Value != null && frmIfThen.conditionvalue.Value.ToString() != "") // { // dgvCnas.Rows[e.RowIndex].Cells["Value"].Value = frmIfThen.conditionvalue.Value; // //将数据插入到数据源中 // var query = syncInstrument.lstFixedValue.Where(s => s.TableName == cnasFixed.TableName && s.ColumnName == cnasFixed.ColumnName).ToList(); // if (query.Count >= 1) // { // query[0].Condition = cnasFixed.Condition; // query[0].Value = frmIfThen.conditionvalue.Value; // } // else // { // cnasFixed.Value = frmIfThen.conditionvalue.Value; // syncInstrument.lstFixedValue.Add(cnasFixed); // } // } // } // } // if (dgvCnas.Rows[e.RowIndex].Cells["condition"].Value.ToString() == "截取") // { // CnasConditionMapValue cnasFixed = new CnasConditionMapValue // { // TableName = dgvCnas.Rows[e.RowIndex].Cells["TableName"].Value.ToString(), // ColumnName = dgvCnas.Rows[e.RowIndex].Cells["TableColumn"].Value.ToString(), // Condition = MapCondition.SubString // }; // if (dgvCnas.Rows[e.RowIndex].Cells["Value"].Value != null) // cnasFixed.Value = dgvCnas.Rows[e.RowIndex].Cells["Value"].Value.ToString(); // frmSplitParam frmSplitParam = new frmSplitParam(cnasFixed); // if (frmSplitParam.ShowDialog() == DialogResult.OK) // { // if (frmSplitParam.conditionvalue.Value != null && frmSplitParam.conditionvalue.Value.ToString() != "") // { // dgvCnas.Rows[e.RowIndex].Cells["Value"].Value = frmSplitParam.conditionvalue.Value; // //将数据插入到数据源中 // var query = syncInstrument.lstFixedValue.Where(s => s.TableName == cnasFixed.TableName && s.ColumnName == cnasFixed.ColumnName).ToList(); // if (query.Count >= 1) // { // query[0].Condition = cnasFixed.Condition; // query[0].Value = frmSplitParam.conditionvalue.Value; // } // else // { // cnasFixed.Value = frmSplitParam.conditionvalue.Value; // syncInstrument.lstFixedValue.Add(cnasFixed); // } // } // } // } // if (dgvCnas.Rows[e.RowIndex].Cells["condition"].Value.ToString() == "截断开头(结尾)") // { // CnasConditionMapValue cnasFixed = new CnasConditionMapValue // { // TableName = dgvCnas.Rows[e.RowIndex].Cells["TableName"].Value.ToString(), // ColumnName = dgvCnas.Rows[e.RowIndex].Cells["TableColumn"].Value.ToString(), // Condition = MapCondition.SubstringStartEnd // }; // if (dgvCnas.Rows[e.RowIndex].Cells["Value"].Value != null) // cnasFixed.Value = dgvCnas.Rows[e.RowIndex].Cells["Value"].Value.ToString(); // frmStartEndSubstring frmStartEnd = new frmStartEndSubstring(cnasFixed); // if (frmStartEnd.ShowDialog() == DialogResult.OK) // { // if (frmStartEnd.conditionvalue.Value != null && frmStartEnd.conditionvalue.Value.ToString() != "") // { // dgvCnas.Rows[e.RowIndex].Cells["Value"].Value = frmStartEnd.conditionvalue.Value; // //将数据插入到数据源中 // var query = syncInstrument.lstFixedValue.Where(s => s.TableName == cnasFixed.TableName && s.ColumnName == cnasFixed.ColumnName).ToList(); // if (query.Count >= 1) // { // query[0].Condition = cnasFixed.Condition; // query[0].Value = frmStartEnd.conditionvalue.Value; // } // else // { // cnasFixed.Value = frmStartEnd.conditionvalue.Value; // syncInstrument.lstFixedValue.Add(cnasFixed); // } // } // } // } // if (dgvCnas.Rows[e.RowIndex].Cells["condition"].Value.ToString() == "数值相加(减)") // { // CnasConditionMapValue cnasFixed = new CnasConditionMapValue // { // TableName = dgvCnas.Rows[e.RowIndex].Cells["TableName"].Value.ToString(), // ColumnName = dgvCnas.Rows[e.RowIndex].Cells["TableColumn"].Value.ToString(), // Condition = MapCondition.AddSubtract // }; // if (dgvCnas.Rows[e.RowIndex].Cells["Value"].Value != null) // cnasFixed.Value = dgvCnas.Rows[e.RowIndex].Cells["Value"].Value.ToString(); // frmAddSubtract frmAddSubtract = new frmAddSubtract(syncInstrument, cnasFixed); // if (frmAddSubtract.ShowDialog() == DialogResult.OK) // { // if (frmAddSubtract.conditionvalue.Value != null && frmAddSubtract.conditionvalue.Value.ToString() != "") // { // dgvCnas.Rows[e.RowIndex].Cells["Value"].Value = frmAddSubtract.conditionvalue.Value; // //将数据插入到数据源中 // var query = syncInstrument.lstFixedValue.Where(s => s.TableName == cnasFixed.TableName && s.ColumnName == cnasFixed.ColumnName).ToList(); // if (query.Count >= 1) // { // query[0].Condition = cnasFixed.Condition; // query[0].Value = frmAddSubtract.conditionvalue.Value; // } // else // { // cnasFixed.Value = frmAddSubtract.conditionvalue.Value; // syncInstrument.lstFixedValue.Add(cnasFixed); // } // } // } // } // dgvCnas.EndEdit(); //} #endregion } private void ShowConditionValue() { Rectangle TmpRect = dgvCnas.GetCellDisplayRectangle(dgvCnas.CurrentCell.ColumnIndex, dgvCnas.CurrentCell.RowIndex, true); cbxConditionValue.Size = TmpRect.Size; cbxConditionValue.Top = TmpRect.Top; cbxConditionValue.Left = TmpRect.Left; cbxConditionValue.DropDownStyle = ComboBoxStyle.DropDownList; cbxConditionValue.FormattingEnabled = true; cbxConditionValue.Visible = true; if (!this.dgvCnas.Controls.Contains(cbxConditionValue)) this.dgvCnas.Controls.Add(cbxConditionValue); } private void btnDelete_Click(object sender, EventArgs e) { if (currentBalanceItem == null) return; if (currentBalanceItem.lstFixedValue == null) return; if (dgvCnas == null) return; if (dgvCnas.Rows.Count <= 0) return; foreach (DataGridViewRow dr in dgvCnas.Rows) { dr.Cells["condition"].Value = ""; dr.Cells["Value"].Value = ""; } currentBalanceItem.lstFixedValue.Clear(); } private void dgvCnas_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex <= 0) return; if (e.ColumnIndex == 0) //此时是“条件”列 { if (dgvCnas.Rows[e.RowIndex].Cells["TableName"].Value == null) return; if (dgvCnas.Rows[e.RowIndex].Cells["TableColumn"].Value == null) return; if (dgvCnas.Rows[e.RowIndex].Cells["DataType"].Value == null) return; if (dgvCnas.Rows[e.RowIndex].Cells["Condition"].Value == null) return; //当前列名 string strCurrentTable = dgvCnas.Rows[e.RowIndex].Cells["TableName"].Value.ToString(); string strCurrentColumn = dgvCnas.Rows[e.RowIndex].Cells["TableColumn"].Value.ToString(); string strCurrentType = dgvCnas.Rows[e.RowIndex].Cells["DataType"].Value.ToString(); string strCurrentCondition = dgvCnas.Rows[e.RowIndex].Cells["Condition"].Value.ToString(); string strCurrentValue = dgvCnas.Rows[e.RowIndex].Cells["Value"].Value == null ? "" : dgvCnas.Rows[e.RowIndex].Cells["Value"].Value.ToString(); //将数据插入到数据源中 var query = currentBalanceItem.lstFixedValue.Where(s => s.TableName == strCurrentTable && s.ColumnName == strCurrentColumn).ToList(); if (query.Count >= 1) { GiveConditionByShow(query[0], strCurrentCondition); } else { CnasConditionMapValue cnasFixed = new CnasConditionMapValue(); cnasFixed.TableName = strCurrentTable; cnasFixed.ColumnName = strCurrentColumn; cnasFixed.Value = strCurrentValue; GiveConditionByShow(cnasFixed, strCurrentCondition); currentBalanceItem.lstFixedValue.Add(cnasFixed); } } } private void dgvCnas_CellClick(object sender, DataGridViewCellEventArgs e) { cbxConditionValue.Visible = false; } } }