MachineQuery.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. using DllPubInfo;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using Cksoft.Unity;
  13. using DllEapEntity;
  14. using System.Collections;
  15. using Newtonsoft.Json;
  16. using DllHsms;
  17. namespace DllXqManager
  18. {
  19. public partial class MachineQuery : Form
  20. {
  21. private IList<Machine02> CurrDs = null;
  22. private string CurrTableName = nameof(Machine02);
  23. private int IsSel = 0;//1为选择模式
  24. private Form CurrForm = null;
  25. private string CurrCondition = "";
  26. private int CurrType = 1;
  27. public delegate void SelRow(List<Machine02> rows);
  28. public event SelRow eventSelRow = null;
  29. public delegate void CloseForm();
  30. public event CloseForm eventCloseForm = null;
  31. private List<LayoutDetail> AlreadySelDs = null;
  32. public MachineQuery(List<LayoutDetail> sels)
  33. {
  34. InitializeComponent();
  35. InitTypeItem();
  36. AlreadySelDs = sels;
  37. }
  38. //public Machine02Query(int statusid)
  39. //{
  40. // InitializeComponent();
  41. // InitTypeItem();
  42. // PubInfo.FillComBoBox(edit状态, 0);
  43. // Filldgvmain(GetCondition());
  44. // this.IsSel = 1;
  45. //}
  46. //public Machine02Query(string condition)
  47. //{
  48. // InitializeComponent();
  49. // CurrCondition = condition;
  50. // SetForm(2);
  51. // InitTypeItem();
  52. // //PubInfo.FillComBoBox(edit状态, 0);
  53. // Filldgvmain(GetCondition());
  54. // this.IsSel = 1;
  55. //}
  56. private void SetForm(int ftype)
  57. {
  58. CurrType = ftype;
  59. if (ftype == 2)
  60. {
  61. button3.Visible = true;
  62. button4.Visible = false;
  63. }
  64. }
  65. private void InitTypeItem()
  66. {
  67. //string sqlstr = $"select * from constitem where preid=8";
  68. //string errorinfo = "";
  69. //IList<Constitem> items = PubInfo.Select<Constitem>(sqlstr,nameof(Constitem), ref errorinfo);
  70. //PubInfo.InitComboBox(edit状态, items, 8);
  71. //PubInfo.FillComBoBox(edit状态, 0);
  72. //dateTimePicker2.Value = DateTime.Now;
  73. //dateTimePicker1.Value = DateTime.Now.AddDays(-3);
  74. }
  75. private string GetCondition()
  76. {
  77. string sqlstr ="";
  78. string colname = EntityAttribute.GetPropertyCondition<Machine02>(nameof(Machine02.MModeCode));
  79. if (edit机型代码.Text.Trim() != ""&&!string.IsNullOrEmpty(colname))
  80. {
  81. sqlstr += string.Format($" and {colname} like '%{edit机型代码.Text.Trim()}%'");
  82. }
  83. colname = EntityAttribute.GetPropertyCondition<Machine02>(nameof(Machine02.MModeName));
  84. if (edit机型名称.Text.Trim() != "" && !string.IsNullOrEmpty(colname))
  85. {
  86. sqlstr += string.Format($" and {colname} like '%{edit机型名称.Text.Trim()}%'");
  87. }
  88. colname = EntityAttribute.GetPropertyCondition<Machine02>(nameof(Machine02.FCode));
  89. if (edit机台代码.Text.Trim() != "" && !string.IsNullOrEmpty(colname))
  90. {
  91. sqlstr += string.Format($" and {colname} like '%{edit机台代码.Text.Trim()}%'");
  92. }
  93. colname = EntityAttribute.GetPropertyCondition<Machine02>(nameof(Machine02.FName));
  94. if (edit机台名称.Text.Trim() != "" && !string.IsNullOrEmpty(colname))
  95. {
  96. sqlstr += string.Format($" and {colname} like '%{edit机台名称.Text.Trim()}%'");
  97. }
  98. colname = EntityAttribute.GetPropertyCondition<Machine02>(nameof(Machine02.SupplierFCode));
  99. if (edit厂商代码.Text.Trim() != "" && !string.IsNullOrEmpty(colname))
  100. {
  101. sqlstr += string.Format($" and {colname} like '%{edit厂商代码.Text.Trim()}%'");
  102. }
  103. colname = EntityAttribute.GetPropertyCondition<Machine02>(nameof(Machine02.FactoryName));
  104. if (edit厂区名称.Text.Trim() != "" && !string.IsNullOrEmpty(colname))
  105. {
  106. sqlstr += string.Format($" and {colname} like '%{edit厂区名称.Text.Trim()}%'");
  107. }
  108. colname = EntityAttribute.GetPropertyCondition<Machine02>(nameof(Machine02.StatusID));
  109. if (radioButton2.Checked)
  110. {
  111. sqlstr += string.Format($" and {colname} >1");
  112. }
  113. if (radioButton3.Checked)
  114. {
  115. sqlstr += string.Format($" and {colname} <=1");
  116. }
  117. if (CurrCondition != "")
  118. sqlstr += CurrCondition;
  119. return sqlstr;
  120. }
  121. private void Filldgvmain(string condition)
  122. {
  123. try
  124. {
  125. Cursor = Cursors.WaitCursor;
  126. string errorinfo = "";
  127. CurrDs = PubInfo.SelectForCondition<Machine02>(condition, ref errorinfo);
  128. if (CurrDs == null)
  129. {
  130. MessageBox.Show(errorinfo);
  131. return;
  132. }
  133. Binddgvmain();
  134. }
  135. catch (Exception ex)
  136. {
  137. MessageBox.Show("操作发生错误,错误信息为:" + ex.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  138. }
  139. finally
  140. {
  141. Cursor = Cursors.Default;
  142. }
  143. }
  144. private BindingSource currbs = null;
  145. public void Binddgvmain()
  146. {
  147. currbs = new BindingSource();
  148. List<Machine02> cols = new List<Machine02>();// CurrDs.Where(t => t.EntityStatusID >= 0).ToList();
  149. List<LayoutDetail> templist = new List<LayoutDetail>();
  150. foreach(var item in CurrDs)
  151. {
  152. item.IsSelected = 0;//清空所有选择
  153. templist = AlreadySelDs.Where(t => t.EntityTypeID == EntityType.Machine&& t.EntityID == item.ID).ToList();
  154. if (templist.Count > 0)
  155. continue;
  156. cols.Add(item);
  157. }
  158. currbs.DataSource = cols;
  159. bindingNavigator2.BindingSource = currbs;
  160. dgvmain.DataSource = currbs;
  161. PubInfo.InitDataGridView<Machine02>(dgvmain);
  162. //Type type = typeof(Machine02);
  163. //PropertyInfo[] props = type.GetProperties();
  164. //foreach (PropertyInfo prop in props)
  165. //{
  166. // if (dgvmain.Columns.Contains(prop.Name))
  167. // {
  168. // dgvmain.Columns[prop.Name].Visible = false;
  169. // }
  170. //}
  171. //dgvmain.Columns[prop.Name].Visible = true;
  172. //dgvmain.Columns[nameof(BaseEntity.IsSelected)].Visible = false;
  173. //dgvColDetail.Rows[0].Visible = false;
  174. }
  175. private void button1_Click(object sender, EventArgs e)
  176. {
  177. Filldgvmain(GetCondition());
  178. }
  179. private void button4_Click(object sender, EventArgs e)
  180. {
  181. ShowDetailForm(new Machine02());
  182. }
  183. private void JobbookingQueryFor_Load(object sender, EventArgs e)
  184. {
  185. this.Location = new Point(this.Owner.Right-this.Width, 0);
  186. Filldgvmain(GetCondition());
  187. }
  188. private void ShowDetailForm(Machine02 entity)
  189. {
  190. //if(CurrForm==null)
  191. //{
  192. // CurrForm = new Machine02Base(entity);
  193. // (CurrForm as Machine02Base).eventChangeDs += Machine02Query_eventChangeDs;
  194. // (CurrForm as Machine02Base).eventCloseForm += Machine02Query_eventCloseForm;
  195. // CurrForm.Owner = this;
  196. // CurrForm.Show();
  197. //}
  198. //else
  199. //{
  200. // (CurrForm as Machine02Base).InitForm(entity);
  201. //}
  202. }
  203. private void MachineQuery_eventCloseForm()
  204. {
  205. //throw new NotImplementedException();
  206. CurrForm = null;
  207. }
  208. private void MachineQuery_eventChangeDs(Machine02 entity)
  209. {
  210. //throw new NotImplementedException();
  211. var temp = CurrDs.Where(t => t.ID == entity.ID).ToList();
  212. if(temp.Count>0)
  213. {
  214. EntityHelper.CopyPropertyValue<Machine02>(entity, temp[0]);
  215. }
  216. else
  217. {
  218. CurrDs.Add(entity);
  219. }
  220. Binddgvmain();
  221. }
  222. private void dgvmain_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  223. {
  224. //if (dgvmain.CurrentRow == null)
  225. // return;
  226. //Machine02 job = dgvmain.CurrentRow.DataBoundItem as Machine02;
  227. //ShowDetailForm(job);
  228. }
  229. private void button3_Click(object sender, EventArgs e)
  230. {
  231. //Machine02Base temp = new Machine02Base();
  232. //CurrDs.Add(temp);
  233. dgvmain.EndEdit();
  234. edit机型代码.Focus();
  235. List<Machine02> rows = CurrDs.Where(t => t.IsSelected == 1).ToList();
  236. if (rows.Count <= 0)
  237. {
  238. MessageBox.Show("您没有选择数据,请选择!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  239. return;
  240. }
  241. eventSelRow?.Invoke(rows);
  242. this.Close();
  243. }
  244. private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
  245. {
  246. foreach (DataGridViewRow dgvr in dgvmain.Rows)
  247. {
  248. dgvr.Cells[nameof(BaseEntity.IsSelected)].Value = 1;
  249. }
  250. }
  251. private void 反全选ToolStripMenuItem_Click(object sender, EventArgs e)
  252. {
  253. foreach (DataGridViewRow dgvr in dgvmain.Rows)
  254. {
  255. dgvr.Cells[nameof(BaseEntity.IsSelected)].Value = 0;
  256. }
  257. }
  258. private void dgvmain_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
  259. {
  260. if (e.RowIndex < 0)
  261. return;
  262. DataGridView dgv = sender as DataGridView;
  263. dgv.CurrentCell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];
  264. if (e.Button != MouseButtons.Right)
  265. {
  266. if(dgv.Columns[e.ColumnIndex].Name== "IsSelected")
  267. {
  268. //if((dgv.Columns[e.ColumnIndex] as DataGridViewCheckBoxColumn).v)
  269. //Machine02 mac = dgv.Rows[e.RowIndex].DataBoundItem as Machine02;
  270. ////if(mac.IsSelected)
  271. //mac.IsSelected = mac.IsSelected == 0 ? 1 : 0;
  272. //dgv.Refresh();
  273. }
  274. else
  275. {
  276. dgvmain.EndEdit();
  277. edit机型代码.Focus();
  278. List<Machine02> macs = CurrDs.Where(t => t.IsSelected == 1).ToList();// new List<Machine02>();
  279. if(macs.Count<=0)
  280. {
  281. macs = new List<Machine02>();
  282. macs.Add(dgvmain.CurrentRow.DataBoundItem as Machine02);
  283. }
  284. List<LayoutDetail> details = new List<LayoutDetail>();
  285. foreach(var item in macs)
  286. {
  287. details.Add(AddDetailFor(item));
  288. }
  289. dgvmain.DoDragDrop(details, DragDropEffects.Move);
  290. }
  291. return;
  292. }
  293. if (e.RowIndex < 0 || e.ColumnIndex < 0)
  294. {
  295. return;
  296. }
  297. System.Drawing.Point pointglobal = System.Windows.Forms.Control.MousePosition;
  298. cms.Show(pointglobal);
  299. }
  300. private LayoutDetail AddDetailFor(Machine02 row)
  301. {
  302. LayoutDetail addrow = new LayoutDetail();
  303. addrow.ID = 0;
  304. addrow.EntityTypeID = EntityType.Machine;
  305. addrow.EntityID = row.ID;
  306. addrow.FCode = row.FCode;
  307. addrow.FName = row.FName;
  308. addrow.IPAddress = row.IPAddress;
  309. addrow.FontSize = 9;
  310. addrow.FColor = "";
  311. return addrow;
  312. }
  313. private LayoutDetail AddDetailFor(int entitytype)
  314. {
  315. LayoutDetail addrow = new LayoutDetail();
  316. addrow.ID = 0;
  317. addrow.EntityTypeID = entitytype;
  318. addrow.EntityID = 0;
  319. switch(entitytype)
  320. {
  321. case EntityType.Label:
  322. addrow.FCode = "Label";
  323. addrow.FName = "标签";
  324. break;
  325. case EntityType.Porwer:
  326. addrow.FCode = "Power";
  327. addrow.FName = "动力棒";
  328. break;
  329. }
  330. addrow.IPAddress = "";
  331. addrow.FontSize = 9;
  332. addrow.FColor = "";
  333. return addrow;
  334. }
  335. private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
  336. {
  337. List<Machine02> rows = CurrDs.Where(t => t.IsSelected == 1).ToList();// CurrDs.Tables[0].Select("选择=1");
  338. if (rows.Count <= 0)
  339. {
  340. MessageBox.Show("您没有选择数据,请选择!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  341. return;
  342. }
  343. if (MessageBox.Show("您确定要执行此操作吗?", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.Cancel)
  344. return;
  345. List<Machine02> tempdt = new List<Machine02>();
  346. foreach (var temprow in rows)
  347. {
  348. tempdt.Add(temprow);
  349. }
  350. int result = CallDelMachine(tempdt);
  351. if (result > 0)
  352. {
  353. MessageBox.Show("操作成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  354. foreach (var ttrow in rows)
  355. {
  356. CurrDs.Remove(ttrow);
  357. }
  358. Binddgvmain();
  359. }
  360. }
  361. private int CallDelMachine(List<Machine02> entitys)
  362. {
  363. try
  364. {
  365. Hashtable tempds = new Hashtable();
  366. string str = JsonConvert.SerializeObject(entitys);
  367. tempds.Add(nameof(Machine02), str);
  368. Hashtable reds = PubInfo.CallFunction("DllEapBll.EapBll", "DelMachine02", tempds);
  369. if (reds == null)
  370. return -1;
  371. int count = int.Parse(reds["result"].ToString());
  372. return 1;
  373. }
  374. catch (Exception e)
  375. {
  376. MessageBox.Show(e.Message);
  377. return -1;
  378. }
  379. }
  380. private void dgvmain_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  381. {
  382. if (dgvmain.Columns[e.ColumnIndex].Name == "IsSelected")
  383. return;
  384. PubInfo.DataGridViewSort<Machine02>(sender, e);
  385. }
  386. private void dgvmain_MouseDown(object sender, MouseEventArgs e)
  387. {
  388. //if (e.Button == MouseButtons.Left)
  389. //{
  390. // //if (dgvSelRole.SelectedCells.Count <= 0)
  391. // // return;
  392. // //Control tempcontrol = dgvmain.GetChildAtPoint(new Point(e.X, e.Y));
  393. // List<Machine02> macs = new List<Machine02>();
  394. // macs.Add(dgvmain.CurrentRow.DataBoundItem as Machine02);
  395. // dgvmain.DoDragDrop(macs, DragDropEffects.Move);
  396. //}
  397. }
  398. private void MachineQuery_FormClosing(object sender, FormClosingEventArgs e)
  399. {
  400. eventCloseForm?.Invoke();
  401. }
  402. private void label5_MouseDown(object sender, MouseEventArgs e)
  403. {
  404. List<LayoutDetail> details = new List<LayoutDetail>();
  405. details.Add(AddDetailFor(EntityType.Label));
  406. label5.DoDragDrop(details, DragDropEffects.Move);
  407. }
  408. private void label6_MouseDown(object sender, MouseEventArgs e)
  409. {
  410. List<LayoutDetail> details = new List<LayoutDetail>();
  411. details.Add(AddDetailFor(EntityType.Porwer));
  412. label6.DoDragDrop(details, DragDropEffects.Move);
  413. }
  414. }
  415. }