TEntityQuery.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. 
  2. using DllEapEntity;
  3. using DllPubInfo;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Diagnostics;
  11. using System.Drawing;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading;
  15. using System.Windows.Forms;
  16. namespace DllXqManager
  17. {
  18. public partial class TEntityQuery : Form
  19. {
  20. private List<TEntity> CurrDs = null;
  21. public delegate void SelRow(List<TEntity> rows,int seltype);
  22. public event SelRow eventSelRow = null;
  23. private int CurrType = 1;//1为维护机台,2为选择机台
  24. private int CurrSelType = 1;//选择模式,1为启动模式,2为标准模式
  25. private string CurrCondition = "";
  26. private List<LayoutDetail> CurrLayoutDetails = null;
  27. public TEntityQuery()
  28. {
  29. InitializeComponent();
  30. }
  31. public TEntityQuery(string condition)
  32. {
  33. InitializeComponent();
  34. CurrSelType = 1;
  35. CurrCondition = condition;
  36. SetForm(2);
  37. }
  38. public TEntityQuery(List<LayoutDetail> details)
  39. {
  40. InitializeComponent();
  41. CurrSelType = 1;
  42. CurrLayoutDetails = details;
  43. SetForm(2);
  44. }
  45. private void SetForm(int ftype)
  46. {
  47. CurrType = ftype;
  48. if (ftype == 2)
  49. {
  50. button3.Visible = true;
  51. button4.Visible = false;
  52. }
  53. }
  54. private string GetCondition()
  55. {
  56. StringBuilder sqlstr = new StringBuilder(100);
  57. if(edit代码.Text.Trim()!="")
  58. {
  59. sqlstr.AppendFormat(" and a.fcode like '%{0}%'", edit代码.Text.Trim());
  60. }
  61. if (edit名称.Text.Trim() != "")
  62. {
  63. sqlstr.AppendFormat(" and a.fname like '%{0}%'", edit名称.Text.Trim());
  64. }
  65. sqlstr.AppendFormat(CurrCondition);
  66. return sqlstr.ToString();
  67. }
  68. private void Filldgvmain(string condition)
  69. {
  70. try
  71. {
  72. Cursor = Cursors.WaitCursor;
  73. string errorinfo = "";
  74. CurrDs = PubInfo.SelectForCondition<TEntity>(condition, ref errorinfo).ToList();
  75. if (CurrDs == null)
  76. {
  77. MessageBox.Show(errorinfo);
  78. return;
  79. }
  80. if(CurrLayoutDetails!=null)
  81. {
  82. foreach(var item in CurrLayoutDetails)
  83. {
  84. List<TEntity> templist = CurrDs.Where(t => t.ID == item.EntityID).ToList();
  85. if(templist.Count>0)
  86. {
  87. CurrDs.Remove(templist[0]);
  88. }
  89. }
  90. }
  91. Binddgvmain();
  92. foreach (DataGridViewColumn dgvc in dgvmain.Columns)
  93. {
  94. dgvc.ReadOnly = true;
  95. }
  96. dgvmain.Columns["选择"].ReadOnly = false;
  97. if (CurrType == 1)
  98. {
  99. //dgvmain.Columns[nameof(OrderMst.MacFCode)].Visible = false;
  100. //dgvmain.Columns[nameof(OrderMst.MacFName)].Visible = false;
  101. //dgvmain.Columns["机台指令类别"].Visible = false;
  102. //dgvmain.Columns["机台指令类别ID"].Visible = false;
  103. }
  104. }
  105. catch (Exception ex)
  106. {
  107. MessageBox.Show("操作发生错误,错误信息为:" + ex.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  108. }
  109. finally
  110. {
  111. Cursor = Cursors.Default;
  112. }
  113. //dgvmain.Columns["选择"].ReadOnly = false;
  114. }
  115. private BindingSource currbs = null;
  116. private void Binddgvmain()
  117. {
  118. currbs = new BindingSource();
  119. var cols = CurrDs.Where(t => t.EntityStatusID >= 0).ToList();
  120. currbs.DataSource = cols;
  121. bindingNavigator2.BindingSource = currbs;
  122. dgvmain.DataSource = currbs;
  123. PubInfo.InitDataGridView<TEntity>(dgvmain);
  124. //dgvmain.Columns[nameof(BaseEntity.IsSelected)].Visible = false;
  125. //dgvColDetail.Rows[0].Visible = false;
  126. }
  127. private void button1_Click(object sender, EventArgs e)
  128. {
  129. Filldgvmain(GetCondition());
  130. }
  131. private Form CurrForm = null;
  132. public void CloseCurrForm()
  133. {
  134. CurrForm =null;
  135. }
  136. public void AddRow(TEntity id)
  137. {
  138. //查询是否存在,不存在则添加
  139. List<TEntity> rows = CurrDs.Where(t => t.ID == id.ID).ToList();
  140. if (rows.Count <= 0)
  141. {
  142. CurrDs.Add(id);
  143. }
  144. else
  145. {
  146. rows[0] = id;
  147. }
  148. //dgvmain.Refresh();
  149. Binddgvmain();
  150. }
  151. private void ShowBase(TEntity id)
  152. {
  153. if(CurrForm != null)
  154. {
  155. (CurrForm as TEntityBase).SetCurrID(id);
  156. }
  157. else
  158. {
  159. CurrForm = new TEntityBase(id);
  160. (CurrForm as TEntityBase).eventaddrow += AddRow;
  161. (CurrForm as TEntityBase).eventcloseform += CloseCurrForm;
  162. CurrForm.Owner = this;
  163. CurrForm.Show();
  164. }
  165. }
  166. private void button4_Click(object sender, EventArgs e)
  167. {
  168. ShowBase(null);
  169. }
  170. private void mmaquery_Load(object sender, EventArgs e)
  171. {
  172. Filldgvmain(GetCondition());
  173. }
  174. private void dgvmain_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  175. {
  176. if (dgvmain.CurrentRow == null)
  177. return;
  178. TEntity entity = dgvmain.CurrentRow.DataBoundItem as TEntity;
  179. ShowBase(entity);
  180. }
  181. private void dgvmain_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
  182. {
  183. if (e.Button == MouseButtons.Right)
  184. {
  185. if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
  186. {
  187. dgvmain.CurrentCell = dgvmain.Rows[e.RowIndex].Cells[e.ColumnIndex];
  188. System.Drawing.Point pointglobal = System.Windows.Forms.Control.MousePosition;
  189. cms.Show(pointglobal);
  190. }
  191. }
  192. }
  193. private void toolStripMenuItem3_Click(object sender, EventArgs e)
  194. {
  195. foreach(var temprow in CurrDs)
  196. {
  197. temprow.IsSelected = 1;
  198. }
  199. dgvmain.Refresh();
  200. }
  201. private void toolStripMenuItem4_Click(object sender, EventArgs e)
  202. {
  203. foreach (var temprow in CurrDs)
  204. {
  205. temprow.IsSelected = 0;
  206. }
  207. dgvmain.Refresh();
  208. }
  209. private void button2_Click(object sender, EventArgs e)
  210. {
  211. }
  212. private void button3_Click(object sender, EventArgs e)
  213. {
  214. dgvmain.EndEdit();
  215. edit代码.Focus();
  216. List<TEntity> rows = CurrDs.Where(t => t.IsSelected == 1).ToList();
  217. if(rows.Count<=0)
  218. {
  219. MessageBox.Show("您没有选择数据,请选择!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  220. return;
  221. }
  222. eventSelRow?.Invoke(rows,CurrSelType);
  223. this.Close();
  224. }
  225. private TEntity DelTEntity(List<TEntity> entitys)
  226. {
  227. try
  228. {
  229. Hashtable tempds = new Hashtable();
  230. string str = JsonConvert.SerializeObject(entitys);
  231. tempds.Add(nameof(TEntity), str);
  232. Hashtable reds = PubInfo.CallFunction("DllStatusShowBll.StatusShowBll", "DelTEntity", tempds);
  233. if (reds == null)
  234. return null;
  235. TEntity reentity = JsonConvert.DeserializeObject<TEntity>(reds[nameof(TEntity)].ToString());
  236. return reentity;
  237. }
  238. catch (Exception e)
  239. {
  240. MessageBox.Show("操作发生错误,错误信息为:" + e.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  241. return null;
  242. }
  243. }
  244. private void toolStripMenuItem2_Click(object sender, EventArgs e)
  245. {
  246. List<TEntity> rows = CurrDs.Where(t => t.IsSelected == 1).ToList();
  247. if (rows.Count <= 0)
  248. {
  249. MessageBox.Show("您没有选择数据,请选择。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  250. return;
  251. }
  252. if (MessageBox.Show("您确定要执行此操作吗?", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.Cancel)
  253. return;
  254. TEntity result = DelTEntity(rows);
  255. if (result != null)
  256. {
  257. MessageBox.Show("操作成功。");
  258. foreach (var temprow in rows)
  259. CurrDs.Remove(temprow);
  260. Binddgvmain();
  261. }
  262. }
  263. private void button5_Click(object sender, EventArgs e)
  264. {
  265. string errorinfo = "";
  266. }
  267. private void mcaquery_FormClosed(object sender, FormClosedEventArgs e)
  268. {
  269. }
  270. private void 批量重启ToolStripMenuItem_Click(object sender, EventArgs e)
  271. {
  272. }
  273. private void cms_Opening(object sender, CancelEventArgs e)
  274. {
  275. dgvmain.EndEdit();
  276. edit代码.Focus();
  277. }
  278. private void toolStripButton3_Click(object sender, EventArgs e)
  279. {
  280. if (MessageBox.Show("您确定要执行此操作吗?", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.Cancel)
  281. return;
  282. int result=CallSynchronousMac();
  283. if(result>0)
  284. {
  285. MessageBox.Show("操作成功。");
  286. }
  287. }
  288. private int CallSynchronousMac()
  289. {
  290. try
  291. {
  292. Hashtable tempds = new Hashtable();
  293. Hashtable reds = PubInfo.CallFunction("DllStatusShowBll.StatusShowBll", "SynchronousMac", tempds);
  294. if (reds == null)
  295. return -1;
  296. return 1;
  297. }
  298. catch (Exception e)
  299. {
  300. MessageBox.Show("操作发生错误,错误信息为:" + e.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  301. return -1;
  302. }
  303. }
  304. }
  305. }