SteelHelper.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using Core.Mes.Client.Comm.Server;
  7. using CoreFS.CA06;
  8. using Infragistics.Win;
  9. using Infragistics.Win.UltraWinEditors;
  10. using Infragistics.Win.UltraWinGrid;
  11. using System.Text.RegularExpressions;
  12. using Core.Mes.Client.Comm.Tool;
  13. using System.Reflection;
  14. using System.Windows.Forms;
  15. namespace Core.StlMes.Client.PlnSaleOrd.炼钢计划
  16. {
  17. public class SteelHelper
  18. {
  19. /// <summary>
  20. /// 初始化产线下拉框
  21. /// </summary>
  22. /// <param name="uce">下拉框</param>
  23. /// <param name="processCode">工序</param>
  24. /// <param name="ob">OpeBase</param>
  25. public static void InitPline(UltraComboEditor uce, string processCode, OpeBase ob)
  26. {
  27. DataTable dt = ServerHelper.GetData("com.steering.pss.plnsaleord.steelMarkingPlan.SteelHelper.initPline",
  28. new object[] {processCode}, ob);
  29. uce.DropDownListWidth = -1;
  30. //uce.DropDownStyle = DropDownStyle.DropDownList;
  31. uce.DataSource = dt;
  32. uce.DisplayMember = "PLINE_NAME";
  33. uce.ValueMember = "PLINE_CODE";
  34. uce.SelectedIndex = 0;
  35. }
  36. /// <summary>
  37. /// 初始化产线下拉框
  38. /// </summary>
  39. /// <param name="uce">下拉框</param>
  40. /// <param name="processCode">工序</param>
  41. /// <param name="ob">OpeBase</param>
  42. public static void InitPline2(UltraComboEditor uce, string processCode, OpeBase ob)
  43. {
  44. DataTable dt = ServerHelper.GetData("com.steering.pss.plnsaleord.steelMarkingPlan.SteelHelper.initPline", new object[] { processCode }, ob);
  45. DataRow dr = dt.NewRow();
  46. dr[0] = "";
  47. dr[1] = "离线库";
  48. dt.Rows.Add(dr);
  49. uce.DropDownListWidth = -1;
  50. //uce.DropDownStyle = DropDownStyle.DropDownList;
  51. uce.DataSource = dt;
  52. uce.DisplayMember = "PLINE_NAME";
  53. uce.ValueMember = "PLINE_CODE";
  54. uce.SelectedIndex = 0;
  55. }
  56. /// <summary>
  57. /// 初始化工序下拉框
  58. /// </summary>
  59. /// <param name="uce">下拉框</param>
  60. /// <param name="processCode">工序</param>
  61. /// <param name="ob">OpeBase</param>
  62. public static void InitProcess(UltraComboEditor uce, string processCode,OpeBase ob)
  63. {
  64. DataTable dt = ServerHelper.GetData("com.steering.pss.plnsaleord.steelMarkingPlan.SteelHelper.initProcess", new object[] { processCode }, ob);
  65. uce.DropDownListWidth = -1;
  66. uce.DropDownStyle = DropDownStyle.DropDownList;
  67. uce.DataSource = dt;
  68. uce.DisplayMember = "PROCESS_DESC";
  69. uce.ValueMember = "PROCESS_CODE";
  70. uce.SelectedIndex = 0;
  71. }
  72. /// <summary>
  73. /// 初始化余材类型
  74. /// </summary>
  75. /// <param name="uce"></param>
  76. public static void InitSurplusType(UltraComboEditor uce)
  77. {
  78. //余材类型(1生产超量2开浇加量3尾炉加量45)
  79. DataTable dt = new DataTable();
  80. dt.Columns.Add("SURPLUS_TYPE_NAME");
  81. dt.Columns.Add("SURPLUS_TYPE_CODE");
  82. DataRow dr1 = dt.NewRow();
  83. dr1["SURPLUS_TYPE_NAME"] = "生产超量";
  84. dr1["SURPLUS_TYPE_CODE"] = "1";
  85. dt.Rows.Add(dr1);
  86. DataRow dr2 = dt.NewRow();
  87. dr2["SURPLUS_TYPE_NAME"] = "开浇加量";
  88. dr2["SURPLUS_TYPE_CODE"] = "2";
  89. dt.Rows.Add(dr2);
  90. DataRow dr3 = dt.NewRow();
  91. dr3["SURPLUS_TYPE_NAME"] = "尾炉加量";
  92. dr3["SURPLUS_TYPE_CODE"] = "3";
  93. dt.Rows.Add(dr3);
  94. DataRow dr4 = dt.NewRow();
  95. dr4["SURPLUS_TYPE_NAME"] = "连浇前炉加量";
  96. dr4["SURPLUS_TYPE_CODE"] = "4";
  97. dt.Rows.Add(dr4);
  98. DataRow dr5 = dt.NewRow();
  99. dr5["SURPLUS_TYPE_NAME"] = "连浇后炉加量";
  100. dr5["SURPLUS_TYPE_CODE"] = "5";
  101. dt.Rows.Add(dr5);
  102. uce.DataSource = dt;
  103. uce.DisplayMember = "SURPLUS_TYPE_NAME";
  104. uce.ValueMember = "SURPLUS_TYPE_CODE";
  105. }
  106. /// <summary>
  107. /// (删除修改新增)涉及事务处理方法
  108. /// </summary>
  109. /// <param name="methodId">方法ID</param>
  110. /// <param name="param">参数</param>
  111. /// <param name="ob">OpeBase</param>
  112. /// <returns>string[] 第一个参数 true/false 第二个参数 返回提示信息</returns>
  113. public static string[] SetData(string methodId, object[] param, OpeBase ob)
  114. {
  115. CoreClientParam ccp = new CoreClientParam();
  116. ccp.ServerName = methodId.Substring(0, methodId.LastIndexOf("."));
  117. ccp.MethodName = methodId.Substring(methodId.LastIndexOf(".") + 1);
  118. ccp.ServerParams = param;
  119. ccp = ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal);
  120. if (ccp.ReturnCode == -1)
  121. {
  122. return null;
  123. }
  124. else
  125. {
  126. string[] strArray = new string[2];
  127. strArray[0] = ccp.ReturnObject.ToString();
  128. strArray[1] = ccp.ReturnInfo;
  129. return strArray;
  130. }
  131. }
  132. /// <summary>
  133. /// (删除修改新增)涉及事务处理方法
  134. /// </summary>
  135. /// <param name="methodId">方法ID</param>
  136. /// <param name="param">参数</param>
  137. /// <param name="ob">OpeBase</param>
  138. /// <returns>string[] 第一个参数 true/false 第二个参数 返回提示信息</returns>
  139. public static object[] SetDataReturnData(string methodId, object[] param, OpeBase ob)
  140. {
  141. CoreClientParam ccp = new CoreClientParam();
  142. ccp.ServerName = methodId.Substring(0, methodId.LastIndexOf("."));
  143. ccp.MethodName = methodId.Substring(methodId.LastIndexOf(".") + 1);
  144. ccp.ServerParams = param;
  145. ccp = ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal);
  146. if (ccp.ReturnCode == -1)
  147. {
  148. return null;
  149. }
  150. else
  151. {
  152. object[] strArray = new object[2];
  153. strArray[0] = ccp.ReturnObject;
  154. strArray[1] = ccp.ReturnInfo;
  155. return strArray;
  156. }
  157. }
  158. /// <summary>
  159. /// 验证字符串是否是数字
  160. /// </summary>
  161. /// <param name="str">字符串</param>
  162. /// <returns>bool</returns>
  163. public static bool IsNum(string str)
  164. {
  165. try
  166. {
  167. double a = Convert.ToDouble(str);
  168. }
  169. catch (Exception e)
  170. {
  171. return false;
  172. }
  173. return true;
  174. }
  175. /// 初始化钢种
  176. /// </summary>
  177. /// <param name="uce">下拉框</param>
  178. /// <param name="ob">OpeBase</param>
  179. public static void InitGrade(UltraComboEditor uce, OpeBase ob)
  180. {
  181. DataTable dt = ServerHelper.GetData("com.steering.pss.plnsaleord.steelMarkingPlan.SteelHelper.initGrade", new object[] { }, ob);
  182. uce.DropDownListWidth = -1;
  183. uce.DataSource = dt;
  184. uce.DisplayMember = "GRADENAME";
  185. uce.ValueMember = "GRADECODE";
  186. }
  187. /// <summary>
  188. /// 从服务端查询数据(query)
  189. /// </summary>
  190. /// <param name="methodId">服务端MethodId</param>
  191. /// <param name="param">参数数组,需与服务端方法参数一致</param>
  192. /// <param name="ob">界面OB对象</param>
  193. /// <exception cref="MESException">自定义异常</exception>
  194. /// <returns>查询数据集</returns>
  195. public static object GetData(string methodId, object[] param, OpeBase ob)
  196. {
  197. CoreClientParam ccp = new CoreClientParam();
  198. ccp.ServerName = methodId.Substring(0, methodId.LastIndexOf("."));
  199. ccp.MethodName = methodId.Substring(methodId.LastIndexOf(".") + 1);
  200. ccp.ServerParams = param;
  201. ccp = ob.ExecuteSortResultByQueryToDataTable(ccp, CoreInvokeType.Internal);
  202. if (ccp == null)
  203. {
  204. throw null;
  205. }
  206. return ccp.ReturnObject;
  207. }
  208. /// <summary>
  209. /// 初始化状态
  210. /// </summary>
  211. /// <param name="uce"></param>
  212. public static void InitStatus(UltraComboEditor uce)
  213. {
  214. //状态(0=组炉,1=组浇,2=下发,3生产中,4生产完成)
  215. DataTable dt = new DataTable();
  216. dt.Columns.Add("STATUS");
  217. dt.Columns.Add("STATUS_CODE");
  218. DataRow dr1 = dt.NewRow();
  219. dr1["STATUS"] = "组炉";
  220. dr1["STATUS_CODE"] = "0";
  221. dt.Rows.Add(dr1);
  222. DataRow dr2 = dt.NewRow();
  223. dr2["STATUS"] = "组浇";
  224. dr2["STATUS_CODE"] = "1";
  225. dt.Rows.Add(dr2);
  226. DataRow dr3 = dt.NewRow();
  227. dr3["STATUS"] = "下发";
  228. dr3["STATUS_CODE"] = "2";
  229. dt.Rows.Add(dr3);
  230. DataRow dr4 = dt.NewRow();
  231. dr4["STATUS"] = "生产中";
  232. dr4["STATUS_CODE"] = "3";
  233. dt.Rows.Add(dr4);
  234. DataRow dr5 = dt.NewRow();
  235. dr5["STATUS"] = "生产完成";
  236. dr5["STATUS_CODE"] = "4";
  237. dt.Rows.Add(dr5);
  238. uce.DataSource = dt;
  239. uce.DisplayMember = "STATUS";
  240. uce.ValueMember = "STATUS_CODE";
  241. }
  242. /// <summary>
  243. /// 初始化下拉框
  244. /// </summary>
  245. /// <param name="uce">下拉框</param>
  246. /// <param name="methodId">请求的服务</param>
  247. /// <param name="valueMember">值成员</param>
  248. /// <param name="ob">ob对象</param>
  249. /// <param name="isEmpty">是否有空行</param>
  250. public static void InitComboEditor(UltraComboEditor uce, string methodId, string valueMember, OpeBase ob, bool isEmpty)
  251. {
  252. DataTable dt = ServerHelper.GetData(methodId, new object[]{""}, ob);
  253. if (dt != null && dt.Rows.Count > 0)
  254. {
  255. if (isEmpty)
  256. {
  257. Object[] obj = new Object[] { "", "" };
  258. DataRow dr = dt.NewRow();
  259. dr.ItemArray = obj;
  260. dt.Rows.InsertAt(dr, 0);
  261. }
  262. uce.DataSource = dt;
  263. uce.ValueMember = valueMember;
  264. SetComboItemHeight(uce);
  265. }
  266. }
  267. /// <summary>
  268. /// 将下拉框绑定到GRID列
  269. /// </summary>
  270. /// <param name="uce">下拉框(已经初始化完成)</param>
  271. /// <param name="ColumnName">列名</param>
  272. /// <param name="con">空间集合(每次只需填入this.Controls)</param>
  273. /// <param name="ug">GRID</param>
  274. /// <param name="i">GRID的第几层结构</param>
  275. public static void BindColumn(UltraComboEditor uce, string ColumnName, System.Windows.Forms.Control.ControlCollection con, UltraGrid ug, int i)
  276. {
  277. con.Add(uce);
  278. uce.Visible = false;
  279. ug.DisplayLayout.Bands[i].Columns[ColumnName].EditorComponent = uce;
  280. ug.DisplayLayout.Bands[i].Columns[ColumnName].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.DropDown;
  281. SetComboItemHeight(uce);
  282. }
  283. /// <summary>
  284. /// 设置UltraComboEditor中的中文和非中文统一高度。
  285. /// </summary>
  286. /// <param name="cmb"></param>
  287. public static void SetComboItemHeight(UltraComboEditor cmb)
  288. {
  289. foreach (ValueListItem item in cmb.Items)
  290. {
  291. if (Regex.IsMatch(item.DisplayText, @"[\u4e00-\u9fa5]+"))
  292. {
  293. item.Appearance.FontData.SizeInPoints = 9.0F;
  294. }
  295. else
  296. {
  297. item.Appearance.FontData.SizeInPoints = 10.5F;
  298. }
  299. }
  300. }
  301. public static DataSourceList<T> GetModelByDt<T>(DataTable dt) where T : new()
  302. {
  303. var modelList = new DataSourceList<T>();
  304. //List<string> columns = dt.Columns.Cast<DataColumn>().Select(p => p.ColumnName.Replace("_", "").ToUpper()).ToList();
  305. foreach (DataRow dr in dt.Rows)
  306. {
  307. T model = new T();
  308. foreach (var propertyInfo in typeof(T).GetProperties().AsParallel())
  309. {
  310. if (dt.Columns.Contains(propertyInfo.Name.ToUpper()) &&
  311. (dr[propertyInfo.Name] != DBNull.Value))
  312. //SetPropertyValue(propertyInfo, model, dr[propertyInfo.Name]);
  313. propertyInfo.SetValue(model,
  314. ConvertDataType(propertyInfo.PropertyType, dr[propertyInfo.Name]), null);
  315. }
  316. modelList.Add(model);
  317. }
  318. return modelList;
  319. }
  320. private static object ConvertDataType(Type type, object objValue)
  321. {
  322. if (type == typeof(bool) || type == typeof(bool?))
  323. {
  324. return bool.Parse(objValue == null ? "False" : objValue.ToString());
  325. }
  326. else if (type == typeof(int?))
  327. {
  328. if (objValue == null)
  329. {
  330. return null;
  331. }
  332. else
  333. {
  334. return int.Parse(objValue.ToString());
  335. }
  336. }
  337. else if (type == typeof(long?))
  338. {
  339. if (objValue == null)
  340. {
  341. return null;
  342. }
  343. else
  344. {
  345. return long.Parse(objValue.ToString());
  346. }
  347. }
  348. else if (type == typeof(short?))
  349. {
  350. if (objValue == null)
  351. {
  352. return null;
  353. }
  354. else
  355. {
  356. return short.Parse(objValue.ToString());
  357. }
  358. }
  359. else if (type == typeof(float?))
  360. {
  361. if (objValue == null)
  362. {
  363. return null;
  364. }
  365. else
  366. {
  367. return float.Parse(objValue.ToString());
  368. }
  369. }
  370. else if (type == typeof(double?))
  371. {
  372. if (objValue == null)
  373. {
  374. return null;
  375. }
  376. else
  377. {
  378. return double.Parse(objValue.ToString());
  379. }
  380. }
  381. else if (type == typeof(decimal?))
  382. {
  383. if (objValue == null)
  384. {
  385. return null;
  386. }
  387. else
  388. {
  389. return decimal.Parse(objValue.ToString());
  390. }
  391. }
  392. else if (type == typeof(DateTime?))
  393. {
  394. if (objValue == null)
  395. {
  396. return null;
  397. }
  398. else
  399. {
  400. return DateTime.Parse(objValue.ToString());
  401. }
  402. }
  403. else
  404. {
  405. return objValue == null ? "" : objValue.ToString();
  406. }
  407. }
  408. private static void SetPropertyValue<T>(PropertyInfo propertyInfo, T model, object data)
  409. {
  410. try
  411. {
  412. if (data == null) return;
  413. propertyInfo.SetValue(model,
  414. propertyInfo.PropertyType == data.GetType()
  415. ? data
  416. : Convert.ChangeType(data, propertyInfo.PropertyType), null);
  417. }
  418. catch (Exception)
  419. {
  420. // ignored
  421. }
  422. }
  423. public static DataSourceList<T> TableToEntity<T>(DataTable dt) where T : class,new()
  424. {
  425. Type type = typeof(T);
  426. DataSourceList<T> list = new DataSourceList<T>();
  427. foreach (DataRow row in dt.Rows)
  428. {
  429. PropertyInfo[] pArray = type.GetProperties();
  430. T entity = new T();
  431. foreach (PropertyInfo p in pArray)
  432. {
  433. if (row[p.Name] is Int64)
  434. {
  435. p.SetValue(entity, Convert.ToInt32(row[p.Name]), null);
  436. continue;
  437. }
  438. p.SetValue(entity, row[p.Name], null);
  439. }
  440. list.Add(entity);
  441. }
  442. return list;
  443. }
  444. /// 执行DataTable中的查询返回新的DataTable
  445. /// </summary>
  446. /// <param name="dt">源数据DataTable</param>
  447. /// <param name="condition">查询条件</param>
  448. /// <returns></returns>
  449. public static DataTable GetNewDataTable(DataTable dt, string condition, string sortstr)
  450. {
  451. DataTable newdt = new DataTable();
  452. newdt = dt.Clone();
  453. DataRow[] dr = dt.Select(condition, sortstr);
  454. for (int i = 0; i < dr.Length; i++)
  455. {
  456. newdt.ImportRow((DataRow)dr[i]);
  457. }
  458. return newdt;//返回的查询结果
  459. }
  460. /// datatable去重
  461. /// </summary>
  462. /// <param name="dtSource">需要去重的datatable</param>
  463. /// <param name="columnNames">依据哪些列去重</param>
  464. /// <returns></returns>
  465. public static DataTable GetDistinctTable(DataTable dtSource, params string[] columnNames)
  466. {
  467. DataTable distinctTable = dtSource.Clone();
  468. try
  469. {
  470. if (dtSource != null && dtSource.Rows.Count > 0)
  471. {
  472. DataView dv = new DataView(dtSource);
  473. distinctTable = dv.ToTable(true, columnNames);
  474. }
  475. }
  476. catch (Exception ee)
  477. {
  478. MessageBox.Show(ee.ToString());
  479. }
  480. return distinctTable;
  481. }
  482. /// <summary>
  483. /// 主从表禁止排序
  484. /// </summary>
  485. /// <param name="ugr"></param>
  486. public static void ForbidSort(UltraGrid ugr)
  487. {
  488. foreach (UltraGridColumn ugc in ugr.DisplayLayout.Bands[0].Columns)
  489. {
  490. ugc.SortIndicator = SortIndicator.Disabled;
  491. }
  492. foreach (UltraGridColumn ug1 in ugr.DisplayLayout.Bands[1].Columns)
  493. {
  494. ug1.SortIndicator = SortIndicator.Disabled;
  495. }
  496. }
  497. }
  498. }