Query_LoadMan.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. //ctcode 为 ALL 时不限线别,放大镜列出全部已审核线别;否则只列出登录人长线下的线别
  29. li_code.Condition = User.UserCtCode == "ALL" ? "li_statuscode='AUDITED'"
  30. : "li_statuscode='AUDITED' and instr('" + User.UserCtCode + "',li_longline)>0";
  31. li_code.DbChange += Li_code_DbChange;
  32. //初始提示
  33. OperateResult.AppendText(">>请选择线别,输入人员工号后回车执行" + (UpLoadMan.Checked ? "上线" : "下线") + ";下线也可勾选列表后点\"确认下线\"批量处理\n", Color.Black);
  34. em_code.Focus();
  35. }
  36. private void Query_LoadMan_SizeChanged(object sender, EventArgs e)
  37. {
  38. asc.controlAutoSize(this);
  39. }
  40. private void Li_code_DbChange(object sender, EventArgs e)
  41. {
  42. Dbfind = li_code.ReturnData;
  43. BaseUtil.SetFormValue(this.Controls, Dbfind);
  44. //切换线别后刷新当前线别的在线人员
  45. LoadGridData();
  46. em_code.Focus();
  47. }
  48. /// <summary>
  49. /// 人员工号输入框,回车后按选中的模式执行上线/下线
  50. /// 只接受工号,不解析姓名;上线时若工号存在于 SOPEMPLOYEEDETAIL 则顺带记录人员名称,不存在则只记工号,不做存在性卡控
  51. /// </summary>
  52. private void em_code_KeyDown(object sender, KeyEventArgs e)
  53. {
  54. if (e.KeyCode != Keys.Enter)
  55. return;
  56. //阻止回车继续冒泡到 SnCollectionBox / EnterTextBox 的回车逻辑
  57. //(SnCollectionBox 的 KeyDown 里有一句 this.SelectAll(),会受消息顺序影响;
  58. // 另配 Em_code_KeyPress 拦掉字符级回车,双保险,不让内置逻辑有机会执行)
  59. e.SuppressKeyPress = true;
  60. e.Handled = true;
  61. HandleEmCode();
  62. }
  63. /// <summary>
  64. /// 字符级拦截:SnCollectionBox/EnterTextBox 内部的回车处理挂在 KeyDown 上,不监听 KeyPress,
  65. /// 而扫描枪以 Tab 结尾或手工回车都会先产生字符级 \r。
  66. /// 这里直接把 \r 吞掉并调用统一处理,SnCollectionBox 的回车逻辑(含 SelectAll)就完全不会触发,
  67. /// 从而不必改动该公共控件。
  68. /// </summary>
  69. private void Em_code_KeyPress(object sender, KeyPressEventArgs e)
  70. {
  71. if (e.KeyChar == '\r' || e.KeyChar == '\n')
  72. {
  73. e.Handled = true;
  74. HandleEmCode();
  75. }
  76. }
  77. /// <summary>
  78. /// 上线/下线的统一处理入口(由回车、字符级 \r 两处触发,故用 _emBusy 防止同一次回车重复执行)
  79. /// </summary>
  80. private bool _emBusy = false;
  81. private void HandleEmCode()
  82. {
  83. if (_emBusy)
  84. return;
  85. _emBusy = true;
  86. try
  87. {
  88. DoEmCode();
  89. }
  90. finally
  91. {
  92. _emBusy = false;
  93. }
  94. }
  95. private void DoEmCode()
  96. {
  97. string linecode = li_code.Text.Trim();
  98. //只取工号:扫描枪或手工输入均只记工号,不做姓名解析
  99. string emcode = em_code.Text.Trim();
  100. if (emcode == "")
  101. {
  102. OperateResult.AppendText("<<人员工号不能为空\n", Color.Red, em_code);
  103. return;
  104. }
  105. //线别卡控(上/下线均适用):必须先选线别,且线别必须在登录人长线范围内
  106. if (linecode == "")
  107. {
  108. OperateResult.AppendText("<<请先选择线别\n", Color.Red, li_code);
  109. return;
  110. }
  111. //登录人资料未维护长线(em_ctcode为空)则不允许扫码
  112. if (User.UserCtCode == "")
  113. {
  114. OperateResult.AppendText("<<人员资料未维护线别,仅拉长可以扫码\n", Color.Red, em_code);
  115. return;
  116. }
  117. //所选线别必须属于登录人长线(line.li_longline)下的线别,否则上/下线都不允许
  118. //ctcode 为 ALL 时不限线别,任何线别都可操作
  119. if (User.UserCtCode != "ALL" && dh.getFieldDataByCondition("line", "count(1)",
  120. "instr('" + User.UserCtCode + "',li_longline)>0 and li_code='" + linecode + "' and li_statuscode='AUDITED'").ToString() == "0")
  121. {
  122. OperateResult.AppendText("<<线别" + linecode + "不在" + User.UserCtCode + "范围内,不允许扫码\n", Color.Red, li_code);
  123. return;
  124. }
  125. if (UpLoadMan.Checked)
  126. {
  127. //一个人只能同时一条线上线:若已在别的线体上线,先把原线体自动下线,再在新线体上线
  128. DataTable dtOn = (DataTable)dh.ExecuteSql("select lm_linecode from loadman where lm_emcode='" + emcode + "' and lm_downtime is null", "select");
  129. if (dtOn.Rows.Count > 0)
  130. {
  131. string oldLineCode = dtOn.Rows[0]["lm_linecode"].ToString();
  132. //当前所在线体与新线体相同,提示已上线,不再重复插入
  133. if (oldLineCode == linecode)
  134. {
  135. OperateResult.AppendText("<<" + emcode + "已在" + linecode + "上线,不允许重复上线\n", Color.Red, em_code);
  136. em_code.Clear();
  137. em_code.Focus();
  138. return;
  139. }
  140. //自动下线原线体
  141. dh.ExecuteSql("update loadman set lm_downtime=sysdate where lm_emcode='" + emcode + "' and lm_downtime is null", "update");
  142. OperateResult.AppendText(">>" + emcode + "已从" + oldLineCode + "自动下线\n", Color.Black);
  143. }
  144. //人员名称:存在于 SOPEMPLOYEEDETAIL 则带上,不存在只记工号,不做存在性卡控
  145. string emname = dh.getFieldDataByCondition("SOPEMPLOYEEDETAIL", "max(sed_emname)", "sed_emcode='" + emcode + "'").ToString().Trim();
  146. if (emname == "")
  147. {
  148. dh.ExecuteSql("insert into loadman(lm_id,lm_linecode,lm_emcode,lm_uptime,lm_inman)" +
  149. "values(loadman_seq.nextval,'" + linecode + "','" + emcode + "',sysdate,'" + User.UserName + "')", "insert");
  150. }
  151. else
  152. {
  153. dh.ExecuteSql("insert into loadman(lm_id,lm_linecode,lm_emcode,lm_emname,lm_uptime,lm_inman)" +
  154. "values(loadman_seq.nextval,'" + linecode + "','" + emcode + "','" + emname + "',sysdate,'" + User.UserName + "')", "insert");
  155. }
  156. OperateResult.AppendText("<<" + emcode + (emname == "" ? "" : " " + emname) + " 在" + linecode + "上线成功\n", Color.Green, em_code);
  157. }
  158. else
  159. {
  160. //下线:一个人同一时间只有一条在线记录,直接按人员下线
  161. DataTable dtOn = (DataTable)dh.ExecuteSql("select lm_linecode from loadman where lm_emcode='" + emcode + "' and lm_downtime is null", "select");
  162. if (dtOn.Rows.Count == 0)
  163. {
  164. OperateResult.AppendText("<<" + emcode + "当前不在任何线上,无法下线\n", Color.Red, em_code);
  165. em_code.Clear();
  166. em_code.Focus();
  167. return;
  168. }
  169. //人员当前在线线体也必须在登录人长线范围内,否则不允许下线;ctcode 为 ALL 时不限
  170. string onLineCode = dtOn.Rows[0]["lm_linecode"].ToString();
  171. if (User.UserCtCode != "ALL" && dh.getFieldDataByCondition("line", "count(1)",
  172. "instr('" + User.UserCtCode + "',li_longline)>0 and li_code='" + onLineCode + "' and li_statuscode='AUDITED'").ToString() == "0")
  173. {
  174. OperateResult.AppendText("<<" + emcode + "当前在" + onLineCode + "上线,该线别不在" + User.UserCtCode + "范围内,不允许下线\n", Color.Red, em_code);
  175. em_code.Clear();
  176. em_code.Focus();
  177. return;
  178. }
  179. dh.ExecuteSql("update loadman set lm_downtime=sysdate where lm_emcode='" + emcode + "' and lm_downtime is null", "update");
  180. OperateResult.AppendText("<<" + emcode + " 已从"
  181. + onLineCode + "下线\n", Color.Green, em_code);
  182. }
  183. //数据变更后刷新
  184. LoadGridData();
  185. em_code.Clear();
  186. em_code.Focus();
  187. }
  188. /// <summary>
  189. /// 全选/取消全选(切换式):点击时若已全部勾选则全部取消,否则全部勾选
  190. /// 只操作复选框单元格的值,不触发数据刷新
  191. /// </summary>
  192. private void SelectAll_Click(object sender, EventArgs e)
  193. {
  194. if (DGV.Rows.Count == 0)
  195. {
  196. OperateResult.AppendText("<<当前没有可勾选的人员\n", Color.Red, em_code);
  197. return;
  198. }
  199. //判断当前是否已全部勾选,以此决定本次是全勾还是全不勾
  200. bool allChecked = true;
  201. for (int i = 0; i < DGV.Rows.Count; i++)
  202. {
  203. if (DGV.Rows[i].Cells["choose"].FormattedValue == null || DGV.Rows[i].Cells["choose"].FormattedValue.ToString() != "True")
  204. {
  205. allChecked = false;
  206. break;
  207. }
  208. }
  209. bool setValue = !allChecked;
  210. for (int i = 0; i < DGV.Rows.Count; i++)
  211. {
  212. DGV.Rows[i].Cells["choose"].Value = setValue;
  213. }
  214. //刷新一下显示,让复选框立即反映最新状态
  215. DGV.RefreshEdit();
  216. SelectAll.Text = setValue ? "取消全选" : "全选";
  217. OperateResult.AppendText(setValue ? ">>已全选当前列表人员\n" : ">>已取消全选\n", Color.Black);
  218. }
  219. /// <summary>
  220. /// 判断DGV中是否有勾选的行
  221. /// </summary>
  222. private bool IfCheckRow()
  223. {
  224. for (int i = 0; i < DGV.Rows.Count; i++)
  225. {
  226. if (DGV.Rows[i].Cells["choose"].Value != null && DGV.Rows[i].Cells["choose"].FormattedValue.ToString() == "True")
  227. {
  228. return true;
  229. }
  230. }
  231. return false;
  232. }
  233. /// <summary>
  234. /// 批量下线:把DGV中勾选的行按 lm_id 逐条置 lm_downtime=sysdate
  235. /// (DGV只加载未下线记录,勾选行必然在线,直接按主键更新即可)
  236. /// </summary>
  237. private void ConfirmDown_Click(object sender, EventArgs e)
  238. {
  239. if (!IfCheckRow())
  240. {
  241. OperateResult.AppendText("<<请先勾选需要下线的人员\n", Color.Red, em_code);
  242. return;
  243. }
  244. DataGridViewRow row;
  245. string lmId;
  246. string emcode;
  247. string linecode;
  248. int sucCount = 0;
  249. int failCount = 0;
  250. for (int i = 0; i < DGV.Rows.Count; i++)
  251. {
  252. row = DGV.Rows[i];
  253. //只处理勾选且有主键的行
  254. if (row.Cells["choose"].FormattedValue == null || row.Cells["choose"].FormattedValue.ToString() != "True")
  255. continue;
  256. if (row.Cells["lm_id"].Value == null || row.Cells["lm_id"].Value.ToString() == "")
  257. continue;
  258. lmId = row.Cells["lm_id"].Value.ToString();
  259. emcode = row.Cells["lm_emcode"].Value == null ? "" : row.Cells["lm_emcode"].Value.ToString();
  260. linecode = row.Cells["lm_linecode"].Value == null ? "" : row.Cells["lm_linecode"].Value.ToString();
  261. try
  262. {
  263. dh.ExecuteSql("update loadman set lm_downtime=sysdate where lm_id='" + lmId + "' and lm_downtime is null", "update");
  264. sucCount++;
  265. OperateResult.AppendText("<<" + emcode + " 已从" + linecode + "下线\n", Color.Green);
  266. }
  267. catch (Exception ex)
  268. {
  269. failCount++;
  270. OperateResult.AppendText("<<" + emcode + " 下线失败:" + ex.Message + "\n", Color.Red);
  271. }
  272. }
  273. OperateResult.AppendText(">>本次下线成功 " + sucCount + " 人" + (failCount > 0 ? ",失败 " + failCount + " 人" : "") + "\n", Color.Black);
  274. //刷新后勾选状态会因重新绑定被清空,属预期
  275. LoadGridData();
  276. em_code.Focus();
  277. }
  278. private void UpLoadMan_CheckedChanged(object sender, EventArgs e)
  279. {
  280. if (UpLoadMan.Checked)
  281. {
  282. OperateResult.AppendText(">>已切换到上线模式,输入人员工号回车上线\n", Color.Black);
  283. em_code.Focus();
  284. }
  285. }
  286. private void DownLoadMan_CheckedChanged(object sender, EventArgs e)
  287. {
  288. if (DownLoadMan.Checked)
  289. {
  290. OperateResult.AppendText(">>已切换到下线模式,输入人员工号回车下线,或勾选列表后点\"确认下线\"批量下线\n", Color.Black);
  291. em_code.Focus();
  292. }
  293. }
  294. /// <summary>
  295. /// 刷新DGV:只取未下线的人员,已选线别时只显示当前线别
  296. /// 人员名称取 loadman.lm_emname(上线时查 SOPEMPLOYEEDETAIL 带出,查不到的记录为空)
  297. /// (上线时间 to_char 到秒)
  298. /// </summary>
  299. private void LoadGridData()
  300. {
  301. string sql = "select lm.lm_id,lm.lm_linecode,lm.lm_emcode,lm.lm_emname,"
  302. + "to_char(lm.lm_uptime,'yyyy-mm-dd hh24:mi:ss') lm_uptime,lm.lm_inman from loadman lm"
  303. + " where lm.lm_downtime is null";
  304. if (li_code.Text != "")
  305. sql += " and lm.lm_linecode='" + li_code.Text + "'";
  306. sql += " order by lm.lm_uptime desc";
  307. DataTable dt = (DataTable)dh.ExecuteSql(sql, "select");
  308. BaseUtil.FillDgvWithDataTable(DGV, dt);
  309. //序号列:绑定数据源后按当前行序手工赋号
  310. for (int i = 0; i < DGV.Rows.Count; i++)
  311. {
  312. DGV.Rows[i].Cells["serial"].Value = i + 1;
  313. }
  314. //重新绑定后复选框一律清空,全选按钮文案复位
  315. SelectAll.Text = "全选";
  316. //当前DGV行数总计
  317. rowcount_label.Text = "当前在线人数: " + DGV.Rows.Count;
  318. }
  319. }
  320. }