FrmTqnqc14.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Collections;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. using com.steering.mes.mcp.entity;
  13. using Core.Mes.Client.Comm.Control;
  14. using Core.Mes.Client.Comm.Format;
  15. using Core.Mes.Client.Comm.Tool;
  16. using CoreFS.CA06;
  17. using Infragistics.Win;
  18. using Infragistics.Win.UltraWinGrid;
  19. using NPOI.SS.UserModel;
  20. using Core.StlMes.Client.ZGMil.Entity;
  21. namespace Core.StlMes.Client.ZGMil.Cost
  22. {
  23. public partial class FrmTqnqc14 : FrmBase
  24. {
  25. public FrmTqnqc14()
  26. {
  27. InitializeComponent();
  28. }
  29. private void FrmTqnqc14_Load(object sender, EventArgs e)
  30. {
  31. EntityHelper.ShowGridCaption<Tqnqc14Entity>(ultraGrid1.DisplayLayout.Bands[0]);
  32. Query();
  33. }
  34. public override void ToolBar_Click(object sender, string ToolbarKey)
  35. {
  36. switch (ToolbarKey)
  37. {
  38. case "DoQuery":
  39. Query();
  40. break;
  41. case "doImport":
  42. Import();
  43. break;
  44. case "doSave":
  45. break;
  46. case "doUpdate":
  47. Update();
  48. break;
  49. case "doDelete":
  50. delete();
  51. break;
  52. case "Close":
  53. Close();
  54. break;
  55. }
  56. }
  57. //导入
  58. private void Import()
  59. {
  60. List<Tqnqc14Entity> list = null;
  61. var openFileDialog = new OpenFileDialog
  62. {
  63. Filter = "精整工序基础信息|*.xls;*.xlsx"
  64. };
  65. if (openFileDialog.ShowDialog() == DialogResult.OK)
  66. {
  67. list = ReadExcelToRtdTqnqc14Entity(openFileDialog.FileName);
  68. }
  69. if (list == null) return;
  70. if (list.Count <= 0)
  71. {
  72. MessageBox.Show("Excel无数据");
  73. return;
  74. }
  75. string userName = UserInfo.GetUserName();
  76. var ccp = new CoreClientParam();
  77. ccp.ServerName = "com.steering.mes.cost.FrmTqnqc14";
  78. ccp.MethodName = "DoAddTqnqc14";
  79. ccp.ServerParams = new object[]
  80. {
  81. list.Select(JSONFormat.Format).ToList(),
  82. userName
  83. };
  84. ccp = ExecuteNonQuery(ccp, CoreInvokeType.Internal);
  85. if (ccp.ReturnCode != -1)
  86. {
  87. MessageUtil.ShowTips(ccp.ReturnInfo);
  88. if (ccp.ReturnInfo.Equals("导入成功"))
  89. {
  90. // Query();
  91. }
  92. }
  93. }
  94. private void delete()
  95. {
  96. this.ultraGrid1.UpdateData();
  97. IQueryable<UltraGridRow> checkMagRows = this.ultraGrid1.Rows.AsQueryable().Where(" CHK = 'True'");
  98. if (checkMagRows.Count() == 0)
  99. {
  100. MessageUtil.ShowTips("请选择需要删除的数据!");
  101. return;
  102. }
  103. ArrayList parm = new ArrayList();
  104. foreach (UltraGridRow uRow in checkMagRows)
  105. {
  106. Tqnqc14Entity OfflineResult = (Tqnqc14Entity)uRow.ListObject;
  107. string OfflineResultTity = JSONFormat.Format(OfflineResult);
  108. parm.Add(OfflineResultTity);
  109. }
  110. CoreClientParam ccp = new CoreClientParam();
  111. ccp.ServerName = "com.steering.mes.cost.FrmTqnqc14";
  112. ccp.MethodName = "doDelete";
  113. ccp.ServerParams = new object[] { parm };
  114. ccp = ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal);
  115. if (ccp.ReturnCode != -1)
  116. {
  117. if (ccp.ReturnInfo.Equals("操作成功"))
  118. {
  119. // DoQueryInList();
  120. Query();
  121. MessageUtil.ShowTips(ccp.ReturnInfo);
  122. }
  123. else
  124. {
  125. MessageUtil.ShowTips(ccp.ReturnInfo);
  126. }
  127. }
  128. }
  129. private void Update()
  130. {
  131. this.ultraGrid1.UpdateData();
  132. IQueryable<UltraGridRow> checkMagRows = this.ultraGrid1.Rows.AsQueryable().Where(" CHK = 'True'");
  133. if (checkMagRows.Count() == 0)
  134. {
  135. MessageUtil.ShowTips("请选择需要修改的数据!");
  136. return;
  137. }
  138. ArrayList parm = new ArrayList();
  139. foreach (UltraGridRow uRow in checkMagRows)
  140. {
  141. Tqnqc14Entity entity = (Tqnqc14Entity)uRow.ListObject;
  142. entity.DuMaker = UserInfo.GetUserName();
  143. string OfflineResultTity = JSONFormat.Format(entity);
  144. parm.Add(OfflineResultTity);
  145. }
  146. CoreClientParam ccp = new CoreClientParam();
  147. ccp.ServerName = "com.steering.mes.cost.FrmTqnqc14";
  148. ccp.MethodName = "doSave";
  149. ccp.ServerParams = new object[] { parm };
  150. ccp = ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal);
  151. if (ccp.ReturnCode != -1)
  152. {
  153. if (ccp.ReturnInfo.Equals("操作成功"))
  154. {
  155. // DoQueryInList();
  156. Query();
  157. MessageUtil.ShowTips(ccp.ReturnInfo);
  158. }
  159. else
  160. {
  161. MessageUtil.ShowTips(ccp.ReturnInfo);
  162. }
  163. }
  164. }
  165. private void Query()
  166. {
  167. string backlog = "";
  168. if (chkJugeHeatNo.Checked == true)
  169. {
  170. backlog = txtJugeNo.Text;
  171. }
  172. List<Tqnqc14Entity> listSource = EntityHelper.GetData<Tqnqc14Entity>(
  173. "com.steering.mes.cost.FrmTqnqc14.doQuery", new object[] { backlog }, this.ob);
  174. tqnqc14EntityBindingSource.DataSource = listSource;
  175. //GridHelper.RefreshAndAutoSize(ultraGrid1);
  176. }
  177. public List<Tqnqc14Entity> ReadExcelToRtdTqnqc14Entity(string fileName)
  178. {
  179. //定义要返回的datatable对象
  180. List<Tqnqc14Entity> Tqnqc14Entitys = new List<Tqnqc14Entity>();
  181. try
  182. {
  183. if (!File.Exists(fileName))
  184. {
  185. return null;
  186. }
  187. //根据指定路径读取文件
  188. using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
  189. {
  190. //根据文件流创建excel数据结构
  191. NPOI.SS.UserModel.IWorkbook workbook = NPOI.SS.UserModel.WorkbookFactory.Create(fs);
  192. //IWorkbook workbook = new HSSFWorkbook(fs);
  193. for (int k = 0; k < workbook.NumberOfSheets; k++)
  194. {
  195. NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(k);
  196. if (sheet != null)
  197. {
  198. //最后一列的标号
  199. int rowCount = sheet.LastRowNum;
  200. for (int i = 1; i <= rowCount; ++i)
  201. {
  202. Tqnqc14Entity entity = new Tqnqc14Entity();
  203. NPOI.SS.UserModel.IRow row = sheet.GetRow(i);
  204. if (row == null) continue; //没有数据的行默认是null       
  205. if (row.Cells.Count < 4)
  206. {
  207. throw new Exception("无足够列");
  208. }
  209. string jzCode = GetCellValue(row.Cells[0]);
  210. string subcod = GetCellValue(row.Cells[1]);
  211. string costCenterAc = GetCellValue(row.Cells[2]);
  212. string wholeBacklog = GetCellValue(row.Cells[3]);
  213. string zjWholeBacklog = "精整"+ GetCellValue(row.Cells[3]);
  214. string subBacklog = GetCellValue(row.Cells[4]);
  215. //string jzName =
  216. //string backlog = GetCellValue(row.Cells[2]);
  217. //string subbacklog = GetCellValue(row.Cells[3]);
  218. if (costCenterAc == "" || costCenterAc == null)
  219. {
  220. throw new Exception("成本中心为空");
  221. }
  222. if (jzCode == "" || jzCode == null)
  223. {
  224. throw new Exception("工序代码为空");
  225. }
  226. if (wholeBacklog == "" || wholeBacklog == null)
  227. {
  228. throw new Exception("工序名称为空");
  229. }
  230. if (zjWholeBacklog == "" || zjWholeBacklog == null)
  231. {
  232. throw new Exception("机组为空");
  233. }
  234. if (subBacklog == "" || subBacklog == null)
  235. {
  236. throw new Exception("标准成本工序");
  237. }
  238. decimal unitPrice;
  239. decimal timeNum;
  240. decimal gdPrice;
  241. decimal.TryParse(GetCellValue(row.Cells[5]), out gdPrice);
  242. decimal.TryParse(GetCellValue(row .Cells[6]), out unitPrice);
  243. decimal.TryParse(GetCellValue(row.Cells[7]), out timeNum);
  244. if (gdPrice == null || gdPrice < 0)
  245. {
  246. throw new Exception("gd价格异常");
  247. }
  248. if (unitPrice == null || unitPrice < 0)
  249. {
  250. throw new Exception("价格异常");
  251. }
  252. if (timeNum == null || timeNum < 0)
  253. {
  254. throw new Exception("次数异常");
  255. }
  256. entity.RpCode = jzCode;
  257. entity.CostCenterAc = costCenterAc;
  258. entity.WholeBacklog = wholeBacklog;
  259. entity.SubBacklog = subBacklog;
  260. entity.TimeNum = timeNum;
  261. entity.UnitPrice = unitPrice;
  262. entity.JzWholeBacklog = zjWholeBacklog;
  263. entity.SubCode = subcod;
  264. entity.GdPrice = gdPrice;
  265. Tqnqc14Entitys.Add(entity);
  266. }
  267. }
  268. }
  269. }
  270. return Tqnqc14Entitys;
  271. }
  272. catch (Exception ex)
  273. {
  274. MessageBox.Show("EXCEL格式错误:" + ex.Message);
  275. return null;
  276. }
  277. }
  278. private string GetCellValue(ICell cell)
  279. {
  280. string value = "";
  281. switch (cell.CellType)
  282. {
  283. case CellType.Blank:
  284. value = "";
  285. break;
  286. case CellType.Numeric:
  287. short format = cell.CellStyle.DataFormat;
  288. value = cell.NumericCellValue.ToString2();
  289. break;
  290. case CellType.String:
  291. value = cell.StringCellValue;
  292. break;
  293. }
  294. return value;
  295. }
  296. }
  297. }