Query_LoadMan.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using System;
  2. using System.Data;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using UAS_MES_NEW.DataOperate;
  6. using UAS_MES_NEW.Entity;
  7. using UAS_MES_NEW.PublicForm;
  8. using UAS_MES_NEW.PublicMethod;
  9. namespace UAS_MES_NEW.Query
  10. {
  11. public partial class Query_LoadMan : Form
  12. {
  13. AutoSizeFormClass asc = new AutoSizeFormClass();
  14. DataHelper dh = SystemInf.dh;
  15. DataTable Dbfind;
  16. public Query_LoadMan()
  17. {
  18. InitializeComponent();
  19. }
  20. private void Query_LoadMan_Load(object sender, EventArgs e)
  21. {
  22. asc.controllInitializeSize(this);
  23. //线别放大镜
  24. li_code.TableName = "line";
  25. li_code.SelectField = "li_code # 线别编号,li_name # 线别名称";
  26. li_code.FormName = Name;
  27. li_code.SetValueField = new string[] { "li_code" };
  28. li_code.Condition = "li_statuscode='AUDITED'";
  29. li_code.DbChange += Li_code_DbChange;
  30. //初始提示
  31. OperateResult.AppendText(">>请选择线别,输入人员编号后回车执行" + (UpLoadMan.Checked ? "上线" : "下线") + ";下线也可勾选列表后点\"确认下线\"批量处理\n", Color.Black);
  32. LoadGridData();
  33. em_code.Focus();
  34. }
  35. private void Query_LoadMan_SizeChanged(object sender, EventArgs e)
  36. {
  37. asc.controlAutoSize(this);
  38. }
  39. private void Li_code_DbChange(object sender, EventArgs e)
  40. {
  41. Dbfind = li_code.ReturnData;
  42. BaseUtil.SetFormValue(this.Controls, Dbfind);
  43. //切换线别后刷新当前线别的在线人员
  44. LoadGridData();
  45. em_code.Focus();
  46. }
  47. /// <summary>
  48. /// 人员编号输入框,回车后按选中的模式执行上线/下线
  49. /// 兼容两种输入格式:①只输入工号(名称从 SOPEMPLOYEEDETAIL 查) ②扫描/输入「工号/人员姓名」(直接解析,不再查表)
  50. /// </summary>
  51. private void em_code_KeyDown(object sender, KeyEventArgs e)
  52. {
  53. if (e.KeyCode != Keys.Enter)
  54. return;
  55. //阻止回车继续冒泡到 SnCollectionBox / EnterTextBox 的回车逻辑
  56. //(SnCollectionBox 的 KeyDown 里有一句 this.SelectAll(),会受消息顺序影响;
  57. // 另配 Em_code_KeyPress 拦掉字符级回车,双保险,不让内置逻辑有机会执行)
  58. e.SuppressKeyPress = true;
  59. e.Handled = true;
  60. HandleEmCode();
  61. }
  62. /// <summary>
  63. /// 字符级拦截:SnCollectionBox/EnterTextBox 内部的回车处理挂在 KeyDown 上,不监听 KeyPress,
  64. /// 而扫描枪以 Tab 结尾或手工回车都会先产生字符级 \r。
  65. /// 这里直接把 \r 吞掉并调用统一处理,SnCollectionBox 的回车逻辑(含 SelectAll)就完全不会触发,
  66. /// 从而不必改动该公共控件。
  67. /// </summary>
  68. private void Em_code_KeyPress(object sender, KeyPressEventArgs e)
  69. {
  70. if (e.KeyChar == '\r' || e.KeyChar == '\n')
  71. {
  72. e.Handled = true;
  73. HandleEmCode();
  74. }
  75. }
  76. /// <summary>
  77. /// 上线/下线的统一处理入口(由回车、字符级 \r 两处触发,故用 _emBusy 防止同一次回车重复执行)
  78. /// </summary>
  79. private bool _emBusy = false;
  80. private void HandleEmCode()
  81. {
  82. if (_emBusy)
  83. return;
  84. _emBusy = true;
  85. try
  86. {
  87. DoEmCode();
  88. }
  89. finally
  90. {
  91. _emBusy = false;
  92. }
  93. }
  94. private void DoEmCode()
  95. {
  96. string linecode = li_code.Text.Trim();
  97. string emcode = "";
  98. string emname = "";
  99. //解析输入内容:扫描枪输出「工号/人员姓名」两段格式,手工输入则只有工号
  100. if (!SplitEmInfo(em_code.Text, out emcode, out emname))
  101. {
  102. OperateResult.AppendText("<<扫描内容格式有误,应为「工号/人员姓名」\n", Color.Red, em_code);
  103. return;
  104. }
  105. if (emcode == "")
  106. {
  107. OperateResult.AppendText("<<人员编号不能为空\n", Color.Red, em_code);
  108. return;
  109. }
  110. if (UpLoadMan.Checked)
  111. {
  112. //上线:必须选择线别
  113. if (linecode == "")
  114. {
  115. OperateResult.AppendText("<<请先选择线别\n", Color.Red, li_code);
  116. return;
  117. }
  118. //上线:扫描格式已带姓名则直接用;只输入工号时才去 SOPEMPLOYEEDETAIL 取名称
  119. if (emname == "")
  120. {
  121. //同一 sed_emcode 可能多条,取最大值聚合
  122. emname = dh.getFieldDataByCondition("SOPEMPLOYEEDETAIL", "max(sed_emname)", "sed_emcode='" + emcode + "'").ToString().Trim();
  123. if (emname == "")
  124. {
  125. OperateResult.AppendText("<<" + emcode + "人员编号不存在\n", Color.Red, em_code);
  126. em_code.Clear();
  127. em_code.Focus();
  128. return;
  129. }
  130. }
  131. //一个人只能同时一条线上线:若已在别的线体上线,先把原线体自动下线,再在新线体上线
  132. DataTable dtOn = (DataTable)dh.ExecuteSql("select lm_linecode from loadman where lm_emcode='" + emcode + "' and lm_downtime is null", "select");
  133. if (dtOn.Rows.Count > 0)
  134. {
  135. string oldLineCode = dtOn.Rows[0]["lm_linecode"].ToString();
  136. //当前所在线体与新线体相同,提示已上线,不再重复插入
  137. if (oldLineCode == linecode)
  138. {
  139. OperateResult.AppendText("<<" + emcode + "已在" + linecode + "上线,不允许重复上线\n", Color.Red, em_code);
  140. em_code.Clear();
  141. em_code.Focus();
  142. return;
  143. }
  144. //自动下线原线体
  145. dh.ExecuteSql("update loadman set lm_downtime=sysdate where lm_emcode='" + emcode + "' and lm_downtime is null", "update");
  146. OperateResult.AppendText(">>" + emcode + "已从" + oldLineCode + "自动下线\n", Color.Black);
  147. }
  148. dh.ExecuteSql("insert into loadman(lm_id,lm_linecode,lm_emcode,lm_emname,lm_uptime,lm_inman)" +
  149. "values(loadman_seq.nextval,'" + linecode + "','" + emcode + "','" + emname + "',sysdate,'" + User.UserName + "')", "insert");
  150. OperateResult.AppendText("<<" + emcode + " " + emname + " 在" + linecode + "上线成功\n", Color.Green, em_code);
  151. }
  152. else
  153. {
  154. //下线:一个人同一时间只有一条在线记录,直接按人员下线
  155. DataTable dtOn = (DataTable)dh.ExecuteSql("select lm_linecode from loadman where lm_emcode='" + emcode + "' and lm_downtime is null", "select");
  156. if (dtOn.Rows.Count == 0)
  157. {
  158. OperateResult.AppendText("<<" + emcode + "当前不在任何线上,无法下线\n", Color.Red, em_code);
  159. em_code.Clear();
  160. em_code.Focus();
  161. return;
  162. }
  163. dh.ExecuteSql("update loadman set lm_downtime=sysdate where lm_emcode='" + emcode + "' and lm_downtime is null", "update");
  164. //姓名优先取扫描解析出的,否则用 loadman 里记录的名称
  165. string downName = emname == "" ? dh.getFieldDataByCondition("loadman", "max(lm_emname)",
  166. "lm_emcode='" + emcode + "' and lm_downtime is null").ToString().Trim() : emname;
  167. OperateResult.AppendText("<<" + emcode + (downName == "" ? "" : " " + downName) + " 已从"
  168. + dtOn.Rows[0]["lm_linecode"].ToString() + "下线\n", Color.Green, em_code);
  169. }
  170. //数据变更后刷新
  171. LoadGridData();
  172. em_code.Clear();
  173. em_code.Focus();
  174. }
  175. /// <summary>
  176. /// 解析人员输入内容,兼容两种格式:
  177. /// ①「工号/人员姓名」(扫描枪常用格式,分隔符支持 / \ 全角/ 竖线 | ,也兼容空格和中文顿号) → 工号与姓名都带出
  178. /// ②只有工号(手工输入) → 姓名返回空串,由调用方决定是否去 SOPEMPLOYEEDETAIL 查
  179. /// 分隔符在首尾或出现多次时视为格式错误,返回 false
  180. /// </summary>
  181. private bool SplitEmInfo(string input, out string emcode, out string emname)
  182. {
  183. emcode = "";
  184. emname = "";
  185. if (input == null)
  186. return true;
  187. string text = input.Trim();
  188. if (text == "")
  189. return true;
  190. string[] sep = new string[] { "/", "\\", "/", "|", "|", " ", " ", "、" };
  191. string[] parts = text.Split(sep, StringSplitOptions.RemoveEmptyEntries);
  192. if (parts.Length == 0)
  193. return true;
  194. emcode = parts[0].Trim();
  195. if (parts.Length > 1)
  196. emname = parts[1].Trim();
  197. //带分隔符却没解析出两段,说明格式不对(如以分隔符开头或多余分隔符)
  198. if (text.IndexOfAny(new char[] { '/', '\\', '/', '|', '|', ' ', ' ', '、' }) >= 0 && (emcode == "" || emname == ""))
  199. return false;
  200. return true;
  201. }
  202. /// <summary>
  203. /// 判断DGV中是否有勾选的行
  204. /// </summary>
  205. private bool IfCheckRow()
  206. {
  207. for (int i = 0; i < DGV.Rows.Count; i++)
  208. {
  209. if (DGV.Rows[i].Cells["choose"].Value != null && DGV.Rows[i].Cells["choose"].FormattedValue.ToString() == "True")
  210. {
  211. return true;
  212. }
  213. }
  214. return false;
  215. }
  216. /// <summary>
  217. /// 批量下线:把DGV中勾选的行按 lm_id 逐条置 lm_downtime=sysdate
  218. /// (DGV只加载未下线记录,勾选行必然在线,直接按主键更新即可)
  219. /// </summary>
  220. private void ConfirmDown_Click(object sender, EventArgs e)
  221. {
  222. if (!IfCheckRow())
  223. {
  224. OperateResult.AppendText("<<请先勾选需要下线的人员\n", Color.Red, em_code);
  225. return;
  226. }
  227. DataGridViewRow row;
  228. string lmId;
  229. string emcode;
  230. string emname;
  231. string linecode;
  232. int sucCount = 0;
  233. int failCount = 0;
  234. for (int i = 0; i < DGV.Rows.Count; i++)
  235. {
  236. row = DGV.Rows[i];
  237. //只处理勾选且有主键的行
  238. if (row.Cells["choose"].FormattedValue == null || row.Cells["choose"].FormattedValue.ToString() != "True")
  239. continue;
  240. if (row.Cells["lm_id"].Value == null || row.Cells["lm_id"].Value.ToString() == "")
  241. continue;
  242. lmId = row.Cells["lm_id"].Value.ToString();
  243. emcode = row.Cells["lm_emcode"].Value == null ? "" : row.Cells["lm_emcode"].Value.ToString();
  244. emname = row.Cells["lm_emname"].Value == null ? "" : row.Cells["lm_emname"].Value.ToString();
  245. linecode = row.Cells["lm_linecode"].Value == null ? "" : row.Cells["lm_linecode"].Value.ToString();
  246. try
  247. {
  248. dh.ExecuteSql("update loadman set lm_downtime=sysdate where lm_id='" + lmId + "' and lm_downtime is null", "update");
  249. sucCount++;
  250. OperateResult.AppendText("<<" + emcode + " " + emname + " 已从" + linecode + "下线\n", Color.Green);
  251. }
  252. catch (Exception ex)
  253. {
  254. failCount++;
  255. OperateResult.AppendText("<<" + emcode + " " + emname + " 下线失败:" + ex.Message + "\n", Color.Red);
  256. }
  257. }
  258. OperateResult.AppendText(">>本次下线成功 " + sucCount + " 人" + (failCount > 0 ? ",失败 " + failCount + " 人" : "") + "\n", Color.Black);
  259. //刷新后勾选状态会因重新绑定被清空,属预期
  260. LoadGridData();
  261. em_code.Focus();
  262. }
  263. private void UpLoadMan_CheckedChanged(object sender, EventArgs e)
  264. {
  265. if (UpLoadMan.Checked)
  266. {
  267. OperateResult.AppendText(">>已切换到上线模式,输入人员编号回车上线\n", Color.Black);
  268. em_code.Focus();
  269. }
  270. }
  271. private void DownLoadMan_CheckedChanged(object sender, EventArgs e)
  272. {
  273. if (DownLoadMan.Checked)
  274. {
  275. OperateResult.AppendText(">>已切换到下线模式,输入人员编号回车下线,或勾选列表后点\"确认下线\"批量下线\n", Color.Black);
  276. em_code.Focus();
  277. }
  278. }
  279. /// <summary>
  280. /// 刷新DGV:只取未下线的人员,已选线别时只显示当前线别
  281. /// 人员名称直接取 loadman.lm_emname(上线时已写入),不再关联 SOPEMPLOYEEDETAIL;
  282. /// 兼容历史数据:lm_emname 为空时才回退到 SOPEMPLOYEEDETAIL 取名称
  283. /// (SOPEMPLOYEEDETAIL 同一 sed_emcode 可能有多条,先按其聚合去重;上线时间 to_char 到秒)
  284. /// </summary>
  285. private void LoadGridData()
  286. {
  287. string sql = "select lm.lm_id,lm.lm_linecode,lm.lm_emcode,lm.lm_emname,"
  288. + "nvl(lm.lm_emname,se.sed_emname) emname,"
  289. + "to_char(lm.lm_uptime,'yyyy-mm-dd hh24:mi:ss') lm_uptime,lm.lm_inman from loadman lm"
  290. + " left join (select sed_emcode,max(sed_emname) sed_emname from SOPEMPLOYEEDETAIL group by sed_emcode) se on lm.lm_emcode=se.sed_emcode"
  291. + " where lm.lm_downtime is null";
  292. if (li_code.Text != "")
  293. sql += " and lm.lm_linecode='" + li_code.Text + "'";
  294. sql += " order by lm.lm_uptime desc";
  295. DataTable dt = (DataTable)dh.ExecuteSql(sql, "select");
  296. BaseUtil.FillDgvWithDataTable(DGV, dt);
  297. }
  298. }
  299. }