Make_UpdateCollectCode.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using UAS_MES.DataOperate;
  11. using UAS_MES.Entity;
  12. using UAS_MES.PublicMethod;
  13. namespace UAS_MES.Make
  14. {
  15. public partial class Make_UpdateCollectCode : Form
  16. {
  17. AutoSizeFormClass asc = new AutoSizeFormClass();
  18. LogStringBuilder sql = new LogStringBuilder();
  19. DataHelper dh;
  20. DataTable dt;
  21. DataTable info;
  22. int step = 0;
  23. List<CheckBox> list = new List<CheckBox>();//记录勾选的控件
  24. string ms_id = "";
  25. StringBuilder sb;//存更新makeserial表的数据
  26. StringBuilder toSi;//存储更新sninfo的数据
  27. StringBuilder insertSninfoFields;
  28. StringBuilder insertSninfoValues;
  29. string sncode = "";
  30. StringBuilder oldData;
  31. //记录新数据
  32. StringBuilder newData;
  33. public Make_UpdateCollectCode()
  34. {
  35. InitializeComponent();
  36. }
  37. private void Make_UpdateCollectCode_Load(object sender, EventArgs e)
  38. {
  39. asc.controllInitializeSize(this);
  40. dh = new DataHelper();
  41. OperateResult.AppendText(">>请先勾选需要更新的项,通过采集栏输入SN,更新项具体数据\n", Color.Black);
  42. inputValue.Focus();
  43. }
  44. private void Make_UpdateCollectCode_FormClosing(object sender, FormClosingEventArgs e)
  45. {
  46. }
  47. private void Make_UpdateCollectCode_SizeChanged(object sender, EventArgs e)
  48. {
  49. asc.controlAutoSize(this);
  50. }
  51. private void inputValue_KeyDown(object sender, KeyEventArgs e)
  52. {
  53. if (e.KeyCode == Keys.Enter)
  54. {
  55. //输入不能为空
  56. if (inputValue.Text == "")
  57. {
  58. OperateResult.AppendText("<<输入不能为空\n", Color.Red);
  59. return;
  60. }
  61. OperateResult.AppendText(">>" + inputValue.Text + "\n", Color.Black);
  62. if (step == 0)
  63. {
  64. //避免第一步出错的时候添加不需要的数据
  65. if (list.Count!=0)
  66. {
  67. list.Clear();
  68. }
  69. //判断是否有已勾选的项次
  70. foreach (Control ctl in collects.Controls)
  71. {
  72. if ((ctl as CheckBox).Checked)
  73. {
  74. list.Add((ctl as CheckBox));
  75. }
  76. }
  77. if (list.Count == 0)
  78. {
  79. //无勾选的内容
  80. OperateResult.AppendText("<<请先勾选需要更新的信息\n", Color.Red, inputValue);
  81. return;
  82. }
  83. //判断TSN或者SN是否存在
  84. ms_id = dh.getFieldDataByCondition("makeserial", "max(ms_id)", "ms_sncode ='" + inputValue.Text + "' or exists (select 1 from makesnrelation where beforesn='" + inputValue.Text + "' and ms_makecode=makecode)").ToString();
  85. sncode = inputValue.Text;
  86. if (ms_id == "")
  87. {
  88. OperateResult.AppendText("<<SN错误,不存在\n", Color.Red, inputValue);
  89. return;
  90. }
  91. //查询序列号状态
  92. dt = (DataTable)dh.ExecuteSql("select ms_status,ms_makecode from makeserial where ms_id='" + ms_id + "'", "select");
  93. //序列号状态码必须是3
  94. if (dt.Rows[0]["ms_status"].ToString() != "3")
  95. {
  96. OperateResult.AppendText("<<序列号必须是待维修状态\n", Color.Red, inputValue);
  97. return;
  98. }
  99. sql.Clear();
  100. sql.Append("select * from (select rownum rn, ms_prodcode,ms_makecode,ms_salecode,");
  101. sql.Append("ms_mac,ms_imei1,ms_bt,ms_netcode,ms_imei2,ms_imei3 from makeserial ");
  102. sql.Append("where ms_firstsn=(select ms_firstsn from makeserial where ms_id='" + ms_id + "')");
  103. sql.Append(" and (nvl(ms_mac,' ')<>' ' or nvl(ms_bt,' ')<>' ' or nvl(ms_imei1,' ')<>' ' ) order by ms_id asc)where rn=1");
  104. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  105. //无值则提示没有需要更新的信息
  106. if (dt.Rows.Count == 0)
  107. {
  108. OperateResult.AppendText("<<序列号" + inputValue.Text + "没有需要更新的信息\n", Color.Red, inputValue);
  109. return;
  110. }
  111. else
  112. {
  113. info = (DataTable)dh.ExecuteSql("select psr_type,psr_prefix,psr_length from productsnrelation where psr_prodcode='" + dt.Rows[0]["ms_prodcode"].ToString() + "'", "select");
  114. //查询规则显示
  115. collectCodeRules.DataSource = info;
  116. }
  117. step = 1;
  118. OperateResult.AppendText("<<请输入" + ((CheckBox)list[0]).Text + "\n", Color.Green, inputValue);
  119. sb = new StringBuilder();
  120. toSi = new StringBuilder();//初始化以备更新sninfo表
  121. oldData = new StringBuilder();
  122. newData = new StringBuilder();
  123. insertSninfoFields = new StringBuilder();
  124. insertSninfoValues = new StringBuilder();
  125. }
  126. else
  127. //采集的关联采集信息
  128. {
  129. //从第一个开始
  130. switch (((CheckBox)list[step - 1]).Text)
  131. {
  132. case "MAC/WIFI":
  133. case "BT":
  134. if (dh.CheckExist("makeserial", "ms_" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + " ='" + inputValue.Text + "' and ms_id='" + ms_id + "'"))
  135. {
  136. //如果一样提示用户“MAC 不允许与原来的值一致”,“请采集MAC”;
  137. OperateResult.AppendText("<<" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + "不允许与原来的值一致\n", Color.Red);
  138. OperateResult.AppendText(">>请采集" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + "\n", Color.Black, inputValue);
  139. return;
  140. }
  141. else
  142. {
  143. //判断是否有合同号MAC范围
  144. if (dh.CheckExist("SaleMacBTRange", "sr_sacode='" + dt.Rows[0]["ms_salecode"].ToString() + "' and sr_type='" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + "'"))
  145. {
  146. //有范围的情况下看是否在范围内
  147. if (!dh.CheckExist("SaleMacBTRange", "sr_sacode='" + dt.Rows[0]["ms_salecode"].ToString() + "' and sr_type='" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + "' AND '" + inputValue.Text + "' between sr_startcode and sr_endcode"))
  148. {
  149. OperateResult.AppendText("<<" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + ":" + inputValue.Text + "错误,不在合同防呆范围内\n", Color.Red, inputValue);
  150. OperateResult.AppendText(">>请采集" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + "\n", Color.Black);
  151. return;
  152. }
  153. }
  154. //验证长度前缀
  155. if (!checkPrefixAndLength())
  156. {
  157. return;
  158. }
  159. //验证长度前缀通过,验证数据库
  160. if (dh.CheckExist("makeserial", "ms_status in (1,2,3) and nvl(ms_nextmacode,' ')=' ' and ms_" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + " ='" + inputValue.Text + "'"))
  161. {
  162. OperateResult.AppendText("<<" + ((CheckBox)list[step - 1]).Text + ":" + inputValue.Text + "错误,已被使用\n", Color.Red, inputValue);
  163. return;
  164. }
  165. //通过校验
  166. sb.Append("ms_" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + " = '" + inputValue.Text + "',");
  167. toSi.Append("si_" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + " = '" + inputValue.Text + "',");
  168. insertSninfoFields.Append("si_" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + ",");
  169. insertSninfoValues.Append("'"+inputValue.Text+"',");
  170. oldData.Append("ms_" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + ":" + dt.Rows[0]["ms_" + ((CheckBox)list[step - 1]).Text.Split('/')[0]].ToString() + ",");
  171. newData.Append("ms_" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + ":" + inputValue.Text + ",");
  172. }
  173. break;
  174. case "网标":
  175. case "IMEI1":
  176. case "IMEI2":
  177. case "IMEI3":
  178. //验证长度前缀
  179. if (!checkPrefixAndLength())
  180. {
  181. return;
  182. }
  183. //验证长度前缀通过,验证数据库
  184. if (dh.CheckExist("makeserial", "ms_status in (1,2,3) and nvl(ms_nextmacode,' ')=' ' and ms_" + (((CheckBox)list[step - 1]).Text == "网标" ? "NETCODE" : ((CheckBox)list[step - 1]).Text) + " ='" + inputValue.Text + "'"))
  185. {
  186. OperateResult.AppendText("<<" + ((CheckBox)list[step - 1]).Text + ":" + inputValue.Text + "错误,已被使用\n", Color.Red, inputValue);
  187. return;
  188. }
  189. //通过校验
  190. sb.Append("ms_" + (((CheckBox)list[step - 1]).Text == "网标" ? "NETCODE" : ((CheckBox)list[step - 1]).Text) + " = '" + inputValue.Text + "',");
  191. toSi.Append("si_" + (((CheckBox)list[step - 1]).Text == "网标" ? "NETCODE" : ((CheckBox)list[step - 1]).Text) + " = '" + inputValue.Text + "',");
  192. insertSninfoFields.Append("si_" + (((CheckBox)list[step - 1]).Text == "网标" ? "NETCODE" : ((CheckBox)list[step - 1]).Text) + ",");
  193. insertSninfoValues.Append("'"+inputValue.Text+"',");
  194. 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() + ",");
  195. newData.Append("ms_" + (((CheckBox)list[step - 1]).Text == "网标" ? "NETCODE" : ((CheckBox)list[step - 1]).Text) + ":" + inputValue.Text + ",");
  196. break;
  197. }
  198. //判断是否是最后一个
  199. if (step == list.Count)
  200. {
  201. //更新数据库
  202. sql.Clear();
  203. sql.Append("update makeserial set ");
  204. sql.Append(sb.ToString().Substring(0, sb.ToString().Length - 1));
  205. sql.Append(" where ms_id='" + ms_id + "'");
  206. dh.ExecuteSql(sql.GetString(), "update");
  207. //提示烧录成功
  208. OperateResult.AppendText(">>烧录成功\n", Color.Green);
  209. //更新sninfo表
  210. insert2Sninfo(sncode);
  211. //记录日志
  212. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, dt.Rows[0]["ms_makecode"].ToString(), User.UserLineCode, User.UserSourceCode, "更新烧录","新数据:" + newData.ToString(), sncode, "");
  213. step = 0;
  214. //清除规则
  215. BaseUtil.CleanDataTableData(info);
  216. //聚焦,提示
  217. inputValue.Focus();
  218. OperateResult.AppendText("<<请采集SN\n", Color.Black, inputValue);
  219. //清除LIST
  220. list.Clear();
  221. }
  222. else
  223. {
  224. step++;
  225. //请输入下一个
  226. OperateResult.AppendText(">>请输入" + ((CheckBox)list[step - 1]).Text.Split('/')[0] + "\n", Color.Green, inputValue);
  227. }
  228. }
  229. }
  230. }
  231. private bool checkPrefixAndLength()
  232. {
  233. //找到对应的规则
  234. for (int i = 0; i < info.Rows.Count; i++)
  235. {
  236. if ((((CheckBox)list[step - 1]).Text.Split('/')[0] == "网标" ? "NETCODE" : ((CheckBox)list[step - 1]).Text.Split('/')[0]) == info.Rows[i]["psr_type"].ToString())
  237. {
  238. //校验长度
  239. if ((info.Rows[i]["psr_length"].ToString() == "" ? true : inputValue.Text.Length != int.Parse(info.Rows[i]["psr_length"].ToString())))
  240. {
  241. OperateResult.AppendText(">>" + info.Rows[i]["psr_type"] + "长度校验不通过,请重新输入\n", Color.Red);
  242. OperateResult.AppendText(">>请重新输入" + info.Rows[i]["psr_type"] + "\n", Color.Black, inputValue);
  243. return false;
  244. }
  245. //校验前缀
  246. //以|符号分割前缀
  247. string[] preFixs = info.Rows[i]["psr_prefix"].ToString().Split('|');
  248. //只要满足其中一个前缀条件即可
  249. bool isfit = false;
  250. for (int j = 0; j < preFixs.Length; j++)
  251. {
  252. if (preFixs[j] == "" ? true : inputValue.Text.StartsWith(preFixs[j]))
  253. {
  254. //满足其中一条即可
  255. isfit = true;
  256. break;
  257. }
  258. }
  259. if (!isfit)
  260. {
  261. OperateResult.AppendText(">>" + info.Rows[i]["psr_type"] + "前缀校验不通过,请重新输入\n", Color.Red);
  262. OperateResult.AppendText(">>请重新输入" + info.Rows[i]["psr_type"] + "\n", Color.Black, inputValue);
  263. return false;
  264. }
  265. return true;
  266. }
  267. }
  268. return true;
  269. }
  270. private void Mac_CheckedChanged(object sender, EventArgs e)
  271. {
  272. //当有checkBox状态改变的时候
  273. if (step != 0)
  274. {
  275. //步骤码归零
  276. step = 0;
  277. //清除list,信息栏
  278. list.Clear();
  279. BaseUtil.CleanDataTableData(info);
  280. //聚焦,提示
  281. inputValue.Focus();
  282. OperateResult.AppendText(">>请采集SN\n", Color.Black);
  283. }
  284. }
  285. private void insert2Sninfo(string sn)
  286. {
  287. //查询是否有记录
  288. string siid = dh.getFieldDataByCondition("sninfo", "max(si_id) si_id", "si_sn='"+sn+"' or si_sn in(select sn from makesnrelation where sn='"+sn+"' or beforesn = '"+sn+"' )").ToString();
  289. //是否为空
  290. if (siid == "")
  291. {
  292. //插入新的数据
  293. sql.Clear();
  294. sql.Append("insert into sninfo(si_id,si_sn,"+ insertSninfoFields.ToString().Substring(0, insertSninfoFields.ToString().Length - 1) + ") values ");
  295. sql.Append("(sninfo_seq.nextval,'"+sn+"',"+ insertSninfoValues.ToString().Substring(0, insertSninfoValues.ToString().Length - 1) + ")");
  296. dh.ExecuteSql(sql.GetString(),"insert");
  297. }
  298. else
  299. {
  300. //更新sninfo
  301. sql.Clear();
  302. sql.Append("update sninfo set ");
  303. sql.Append(toSi.ToString().Substring(0, toSi.ToString().Length - 1));
  304. sql.Append(" where si_id='" + siid + "'");
  305. dh.ExecuteSql(sql.GetString(), "update");
  306. }
  307. }
  308. }
  309. }