|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556 |
- 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<string> lstConditionValueType = new List<string>() { "等于", "连接", "若...则...", "截取", "除以", "乘以", "小数位数", "数值相加(减)", "截断开头(结尾)" };
- private List<string> lstConditionValueType = new List<string>() { "等于" };
-
- 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();
- }
-
-
- /// <summary>
- /// 初始化控件combobox
- /// </summary>
- 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;
- }
- /// <summary>
- /// 加载数据
- /// </summary>
- 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<CnasConditionMapValue>();
- 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<CnasConditionMapValue>();
- 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<CnasConditionMapValue>();
- 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<CnasConditionMapValue>();
- // 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<CnasConditionMapValue>();
- // 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<CnasConditionMapValue>();
- // 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<CnasConditionMapValue>();
- // 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<CnasConditionMapValue>();
- // 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<CnasConditionMapValue>();
- 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;
- }
- }
- }
|