Query_LoadMan.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. /// </summary>
  50. private void em_code_KeyDown(object sender, KeyEventArgs e)
  51. {
  52. if (e.KeyCode != Keys.Enter)
  53. return;
  54. string linecode = li_code.Text.Trim();
  55. string emcode = em_code.Text.Trim();
  56. //限制:人员编号不能为空
  57. if (emcode == "")
  58. {
  59. OperateResult.AppendText("<<人员编号不能为空\n", Color.Red, em_code);
  60. return;
  61. }
  62. if (UpLoadMan.Checked)
  63. {
  64. //上线:必须选择线别
  65. if (linecode == "")
  66. {
  67. OperateResult.AppendText("<<请先选择线别\n", Color.Red, li_code);
  68. return;
  69. }
  70. //上线:先校验人员编号存在于 SOPEMPLOYEEDETAIL,并取出人员名称(同一 sed_emcode 可能多条,取最大值聚合)
  71. string emname = dh.getFieldDataByCondition("SOPEMPLOYEEDETAIL", "max(sed_emname)", "sed_emcode='" + emcode + "'").ToString().Trim();
  72. if (emname == "")
  73. {
  74. OperateResult.AppendText("<<" + emcode + "人员编号不存在\n", Color.Red, em_code);
  75. em_code.Clear();
  76. em_code.Focus();
  77. return;
  78. }
  79. //一个人只能同时一条线上线:若已在别的线体上线,先把原线体自动下线,再在新线体上线
  80. DataTable dtOn = (DataTable)dh.ExecuteSql("select lm_linecode from loadman where lm_emcode='" + emcode + "' and lm_downtime is null", "select");
  81. if (dtOn.Rows.Count > 0)
  82. {
  83. string oldLineCode = dtOn.Rows[0]["lm_linecode"].ToString();
  84. //当前所在线体与新线体相同,提示已上线,不再重复插入
  85. if (oldLineCode == linecode)
  86. {
  87. OperateResult.AppendText("<<" + emcode + "已在" + linecode + "上线,不允许重复上线\n", Color.Red, em_code);
  88. em_code.Clear();
  89. em_code.Focus();
  90. return;
  91. }
  92. //自动下线原线体
  93. dh.ExecuteSql("update loadman set lm_downtime=sysdate where lm_emcode='" + emcode + "' and lm_downtime is null", "update");
  94. OperateResult.AppendText(">>" + emcode + "已从" + oldLineCode + "自动下线\n", Color.Black);
  95. }
  96. dh.ExecuteSql("insert into loadman(lm_id,lm_linecode,lm_emcode,lm_emname,lm_uptime,lm_inman)" +
  97. "values(loadman_seq.nextval,'" + linecode + "','" + emcode + "','" + emname + "',sysdate,'" + User.UserName + "')", "insert");
  98. OperateResult.AppendText("<<" + emcode + " " + emname + " 在" + linecode + "上线成功\n", Color.Green, em_code);
  99. }
  100. else
  101. {
  102. //下线:一个人同一时间只有一条在线记录,直接按人员下线
  103. DataTable dtOn = (DataTable)dh.ExecuteSql("select lm_linecode from loadman where lm_emcode='" + emcode + "' and lm_downtime is null", "select");
  104. if (dtOn.Rows.Count == 0)
  105. {
  106. OperateResult.AppendText("<<" + emcode + "当前不在任何线上,无法下线\n", Color.Red, em_code);
  107. em_code.Clear();
  108. em_code.Focus();
  109. return;
  110. }
  111. dh.ExecuteSql("update loadman set lm_downtime=sysdate where lm_emcode='" + emcode + "' and lm_downtime is null", "update");
  112. OperateResult.AppendText("<<" + emcode + "已从" + dtOn.Rows[0]["lm_linecode"].ToString() + "下线\n", Color.Green, em_code);
  113. }
  114. //数据变更后刷新
  115. LoadGridData();
  116. em_code.Clear();
  117. em_code.Focus();
  118. }
  119. /// <summary>
  120. /// 判断DGV中是否有勾选的行
  121. /// </summary>
  122. private bool IfCheckRow()
  123. {
  124. for (int i = 0; i < DGV.Rows.Count; i++)
  125. {
  126. if (DGV.Rows[i].Cells["choose"].Value != null && DGV.Rows[i].Cells["choose"].FormattedValue.ToString() == "True")
  127. {
  128. return true;
  129. }
  130. }
  131. return false;
  132. }
  133. /// <summary>
  134. /// 批量下线:把DGV中勾选的行按 lm_id 逐条置 lm_downtime=sysdate
  135. /// (DGV只加载未下线记录,勾选行必然在线,直接按主键更新即可)
  136. /// </summary>
  137. private void ConfirmDown_Click(object sender, EventArgs e)
  138. {
  139. if (!IfCheckRow())
  140. {
  141. OperateResult.AppendText("<<请先勾选需要下线的人员\n", Color.Red, em_code);
  142. return;
  143. }
  144. DataGridViewRow row;
  145. string lmId;
  146. string emcode;
  147. string emname;
  148. string linecode;
  149. int sucCount = 0;
  150. int failCount = 0;
  151. for (int i = 0; i < DGV.Rows.Count; i++)
  152. {
  153. row = DGV.Rows[i];
  154. //只处理勾选且有主键的行
  155. if (row.Cells["choose"].FormattedValue == null || row.Cells["choose"].FormattedValue.ToString() != "True")
  156. continue;
  157. if (row.Cells["lm_id"].Value == null || row.Cells["lm_id"].Value.ToString() == "")
  158. continue;
  159. lmId = row.Cells["lm_id"].Value.ToString();
  160. emcode = row.Cells["lm_emcode"].Value == null ? "" : row.Cells["lm_emcode"].Value.ToString();
  161. emname = row.Cells["sed_emname"].Value == null ? "" : row.Cells["sed_emname"].Value.ToString();
  162. linecode = row.Cells["lm_linecode"].Value == null ? "" : row.Cells["lm_linecode"].Value.ToString();
  163. try
  164. {
  165. dh.ExecuteSql("update loadman set lm_downtime=sysdate where lm_id='" + lmId + "' and lm_downtime is null", "update");
  166. sucCount++;
  167. OperateResult.AppendText("<<" + emcode + " " + emname + " 已从" + linecode + "下线\n", Color.Green);
  168. }
  169. catch (Exception ex)
  170. {
  171. failCount++;
  172. OperateResult.AppendText("<<" + emcode + " " + emname + " 下线失败:" + ex.Message + "\n", Color.Red);
  173. }
  174. }
  175. OperateResult.AppendText(">>本次下线成功 " + sucCount + " 人" + (failCount > 0 ? ",失败 " + failCount + " 人" : "") + "\n", Color.Black);
  176. //刷新后勾选状态会因重新绑定被清空,属预期
  177. LoadGridData();
  178. em_code.Focus();
  179. }
  180. private void UpLoadMan_CheckedChanged(object sender, EventArgs e)
  181. {
  182. if (UpLoadMan.Checked)
  183. {
  184. OperateResult.AppendText(">>已切换到上线模式,输入人员编号回车上线\n", Color.Black);
  185. em_code.Focus();
  186. }
  187. }
  188. private void DownLoadMan_CheckedChanged(object sender, EventArgs e)
  189. {
  190. if (DownLoadMan.Checked)
  191. {
  192. OperateResult.AppendText(">>已切换到下线模式,输入人员编号回车下线,或勾选列表后点\"确认下线\"批量下线\n", Color.Black);
  193. em_code.Focus();
  194. }
  195. }
  196. /// <summary>
  197. /// 刷新DGV:只取未下线的人员,已选线别时只显示当前线别;按 lm_emcode 关联 SOPEMPLOYEEDETAIL 取人员名称
  198. /// (SOPEMPLOYEEDETAIL 同一 sed_emcode 可能有多条,先按其聚合去重再关联;上线时间 to_char 到秒)
  199. /// </summary>
  200. private void LoadGridData()
  201. {
  202. string sql = "select lm.lm_id,lm.lm_linecode,lm.lm_emcode,se.sed_emname,to_char(lm.lm_uptime,'yyyy-mm-dd hh24:mi:ss') lm_uptime,lm.lm_inman from loadman lm"
  203. + " left join (select sed_emcode,max(sed_emname) sed_emname from SOPEMPLOYEEDETAIL group by sed_emcode) se on lm.lm_emcode=se.sed_emcode"
  204. + " where lm.lm_downtime is null";
  205. if (li_code.Text != "")
  206. sql += " and lm.lm_linecode='" + li_code.Text + "'";
  207. sql += " order by lm.lm_uptime desc";
  208. DataTable dt = (DataTable)dh.ExecuteSql(sql, "select");
  209. BaseUtil.FillDgvWithDataTable(DGV, dt);
  210. }
  211. }
  212. }