using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using UAS_MES_NEW.DataOperate; using UAS_MES_NEW.Entity; using UAS_MES_NEW.PublicMethod; namespace UAS_MES_NEW.Make { public partial class Make_UpdateCollectCode : Form { AutoSizeFormClass asc = new AutoSizeFormClass(); LogStringBuilder sql = new LogStringBuilder(); DataHelper dh; DataTable dt; DataTable info; DataTable macRange; int step = 0; List list = new List();//记录勾选的控件 string ms_id = ""; StringBuilder sb;//存更新makeserial表的数据 StringBuilder toSi;//存储更新sninfo的数据 StringBuilder insertSninfoFields; StringBuilder insertSninfoValues; string sncode = ""; StringBuilder oldData; //记录新数据 StringBuilder newData; public Make_UpdateCollectCode() { InitializeComponent(); } private void Make_UpdateCollectCode_Load(object sender, EventArgs e) { asc.controllInitializeSize(this); dh = SystemInf.dh; OperateResult.AppendText(">>请先勾选需要更新的项,通过采集栏输入SN,更新项具体数据\n", Color.Black); inputValue.Focus(); } private void Make_UpdateCollectCode_FormClosing(object sender, FormClosingEventArgs e) { } private void Make_UpdateCollectCode_SizeChanged(object sender, EventArgs e) { asc.controlAutoSize(this); } private void inputValue_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { //输入不能为空 if (inputValue.Text == "") { OperateResult.AppendText("<<输入不能为空\n", Color.Red); return; } OperateResult.AppendText(">>" + inputValue.Text + "\n", Color.Black); if (step == 0) { //避免第一步出错的时候添加不需要的数据 if (list.Count != 0) { list.Clear(); } //判断是否有已勾选的项次 foreach (Control ctl in collects.Controls) { if ((ctl as CheckBox).Checked) { list.Add((ctl as CheckBox)); } } if (list.Count == 0) { //无勾选的内容 OperateResult.AppendText("<<请先勾选需要更新的信息\n", Color.Red, inputValue); return; } //判断TSN或者SN是否存在 ms_id = dh.getFieldDataByCondition("makeserial", "max(ms_id)", "ms_sncode ='" + inputValue.Text + "' or ms_imei1='" + inputValue.Text + "' or ms_imei2='" + inputValue.Text + "'").ToString(); sncode = inputValue.Text; if (ms_id == "") { OperateResult.AppendText("<' ' or nvl(ms_bt,' ')<>' ' or nvl(ms_imei1,' ')<>' ' ) order by ms_id asc)where rn=1"); dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); //无值则提示没有需要更新的信息 if (dt.Rows.Count == 0) { OperateResult.AppendText("<<序列号" + inputValue.Text + "没有需要更新的信息\n", Color.Red, inputValue); return; } else { info = (DataTable)dh.ExecuteSql("select psr_type,psr_prefix,psr_length from productsnrelation where psr_prodcode='" + dt.Rows[0]["ms_prodcode"].ToString() + "'", "select"); //查询规则显示 collectCodeRules.DataSource = info; } step = 1; OperateResult.AppendText("<<请输入" + ((CheckBox)list[0]).Text + "\n", Color.Green, inputValue); sb = new StringBuilder(); toSi = new StringBuilder();//初始化以备更新sninfo表 oldData = new StringBuilder(); newData = new StringBuilder(); insertSninfoFields = new StringBuilder(); insertSninfoValues = new StringBuilder(); } else //采集的关联采集信息 { //从第一个开始 switch (((CheckBox)list[step - 1]).Text) { case "MAC/WIFI": case "BT": if (dh.CheckExist("makeserial", "ms_" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + " ='" + inputValue.Text.Replace(":", "").Replace("-", "") + "' and ms_id='" + ms_id + "'")) { //如果一样提示用户“MAC 不允许与原来的值一致”,“请采集MAC”; OperateResult.AppendText("<<" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + "不允许与原来的值一致\n", Color.Red); OperateResult.AppendText(">>请采集" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + "\n", Color.Black, inputValue); return; } else { //验证长度前缀 if (!checkPrefixAndLength()) { return; } //验证MAC和BT合同合同防呆 if (!LogicHandler.checkMacOrBtRange(ms_id, inputValue.Text.Replace(":", "").Replace("-", ""), ((CheckBox)list[step - 1]).Text.Split('/')[0])) { OperateResult.AppendText("<<" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + ":" + inputValue.Text + "错误,不在合同防呆范围内\n", Color.Red, inputValue); OperateResult.AppendText(">>请采集" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + "\n", Color.Black); return; } //验证长度前缀通过,验证数据库 if (dh.CheckExist("makeserial", "ms_status in (0,1,2,3) and ms_" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + " ='" + inputValue.Text.Replace(":", "").Replace("-", "") + "'")) { OperateResult.AppendText("<<" + ((CheckBox)list[step - 1]).Text + ":" + inputValue.Text + "错误,已被使用\n", Color.Red, inputValue); return; } //通过校验 sb.Append("ms_" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + " = '" + inputValue.Text.Replace(":", "").Replace("-", "") + "',"); toSi.Append("si_" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + " = '" + inputValue.Text.Replace(":", "").Replace("-", "") + "',"); insertSninfoFields.Append("si_" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + ","); insertSninfoValues.Append("'" + inputValue.Text.Replace(":", "").Replace("-", "") + "',"); oldData.Append("ms_" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + ":" + dt.Rows[0]["ms_" + ((CheckBox)list[step - 1]).Text.Split('/')[0]].ToString() + ","); newData.Append("ms_" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + ":" + inputValue.Text.Replace(":", "").Replace("-", "") + ","); } break; case "网标": case "IMEI1": case "IMEI2": case "IMEI3": //验证长度前缀 if (!checkPrefixAndLength()) { return; } //验证长度前缀通过,验证数据库 if (dh.CheckExist("makeserial", "ms_status in (0,1,2,3) and ms_" + (((CheckBox)list[step - 1]).Text == "网标" ? "NETCODE" : ((CheckBox)list[step - 1]).Text) + " ='" + inputValue.Text + "' and ms_id <>'" + ms_id + "'")) { OperateResult.AppendText("<<" + ((CheckBox)list[step - 1]).Text + ":" + inputValue.Text + "错误,已被使用\n", Color.Red, inputValue); return; } //通过校验 sb.Append("ms_" + (((CheckBox)list[step - 1]).Text == "网标" ? "NETCODE" : ((CheckBox)list[step - 1]).Text) + " = '" + inputValue.Text + "',"); toSi.Append("si_" + (((CheckBox)list[step - 1]).Text == "网标" ? "NETCODE" : ((CheckBox)list[step - 1]).Text) + " = '" + inputValue.Text + "',"); insertSninfoFields.Append("si_" + (((CheckBox)list[step - 1]).Text == "网标" ? "NETCODE" : ((CheckBox)list[step - 1]).Text) + ","); insertSninfoValues.Append("'" + inputValue.Text + "',"); oldData.Append("ms_" + (((CheckBox)list[step - 1]).Text == "网标" ? "NETCODE" : ((CheckBox)list[step - 1]).Text) + ":" + dt.Rows[0]["ms_" + (((CheckBox)list[step - 1]).Text == "网标" ? "NETCODE" : ((CheckBox)list[step - 1]).Text)].ToString() + ","); newData.Append("ms_" + (((CheckBox)list[step - 1]).Text == "网标" ? "NETCODE" : ((CheckBox)list[step - 1]).Text) + ":" + inputValue.Text + ","); break; } //判断是否是最后一个 if (step == list.Count) { //更新数据库 sql.Clear(); sql.Append("update makeserial set "); sql.Append(sb.ToString().Substring(0, sb.ToString().Length - 1)); sql.Append(" where ms_id='" + ms_id + "'"); dh.ExecuteSql(sql.GetString(), "update"); //提示烧录成功 OperateResult.AppendText(">>烧录成功\n", Color.Green); //更新sninfo表 insert2Sninfo(sncode); //记录日志 LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, dt.Rows[0]["ms_makecode"].ToString(), User.UserLineCode, User.UserSourceCode, "更新烧录", "原:" + oldData.ToString() + ",新:" + newData.ToString(), sncode, ""); step = 0; //清除规则 BaseUtil.CleanDataTableData(info); //聚焦,提示 inputValue.Focus(); OperateResult.AppendText("<<请采集SN\n", Color.Black, inputValue); //清除LIST list.Clear(); } else { step++; //请输入下一个 OperateResult.AppendText(">>请输入" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + "\n", Color.Green, inputValue); } } } } private bool checkPrefixAndLength() { //找到对应的规则 for (int i = 0; i < info.Rows.Count; i++) { if ((((CheckBox)list[step - 1]).Text.Split('/')[0] == "网标" ? "NETCODE" : ((CheckBox)list[step - 1]).Text.Split('/')[0]) == info.Rows[i]["psr_type"].ToString()) { //校验长度 if ((info.Rows[i]["psr_length"].ToString() == "0" ? false : inputValue.Text.Length != int.Parse(info.Rows[i]["psr_length"].ToString()))) { OperateResult.AppendText(">>" + info.Rows[i]["psr_type"] + "长度校验不通过,请重新输入\n", Color.Red); OperateResult.AppendText(">>请重新输入" + info.Rows[i]["psr_type"] + "\n", Color.Black, inputValue); return false; } //校验前缀 //以|符号分割前缀 string[] preFixs = info.Rows[i]["psr_prefix"].ToString().Split('|'); //只要满足其中一个前缀条件即可 bool isfit = false; for (int j = 0; j < preFixs.Length; j++) { if (preFixs[j] == "" ? true : inputValue.Text.StartsWith(preFixs[j])) { //满足其中一条即可 isfit = true; break; } } if (!isfit) { OperateResult.AppendText(">>" + info.Rows[i]["psr_type"] + "前缀校验不通过,请重新输入\n", Color.Red); OperateResult.AppendText(">>请重新输入" + info.Rows[i]["psr_type"] + "\n", Color.Black, inputValue); return false; } return true; } } return true; } private void Mac_CheckedChanged(object sender, EventArgs e) { //当有checkBox状态改变的时候 if (step != 0) { //步骤码归零 step = 0; //清除list,信息栏 list.Clear(); BaseUtil.CleanDataTableData(info); //聚焦,提示 inputValue.Focus(); OperateResult.AppendText(">>请采集SN\n", Color.Black); } } private void insert2Sninfo(string sn) { //查询是否有记录 string siid = dh.getFieldDataByCondition("sninfo", "max(si_id) si_id", "si_sn='" + sn + "'").ToString(); //是否为空 if (siid == "") { //插入新的数据 sql.Clear(); sql.Append("insert into sninfo(si_id,si_sn," + insertSninfoFields.ToString().Substring(0, insertSninfoFields.ToString().Length - 1) + ") values "); sql.Append("(sninfo_seq.nextval,'" + sn + "'," + insertSninfoValues.ToString().Substring(0, insertSninfoValues.ToString().Length - 1) + ")"); dh.ExecuteSql(sql.GetString(), "insert"); } else { //更新sninfo sql.Clear(); sql.Append("update sninfo set "); sql.Append(toSi.ToString().Substring(0, toSi.ToString().Length - 1)); sql.Append(" where si_id='" + siid + "'"); dh.ExecuteSql(sql.GetString(), "update"); } } } }