FrmTqnqc15.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 FrmTqnqc15 : FrmBase
  24. {
  25. public FrmTqnqc15()
  26. {
  27. InitializeComponent();
  28. }
  29. private void FrmTqnqc14_Load(object sender, EventArgs e)
  30. {
  31. EntityHelper.ShowGridCaption<Tqnqc15Entity>(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<Tqnqc15Entity> 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.FrmTqnqc15";
  78. ccp.MethodName = "DoAddTqnqc15";
  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. Tqnqc15Entity Tqnqc15Result = (Tqnqc15Entity)uRow.ListObject;
  107. string Tqnqc15Results = JSONFormat.Format(Tqnqc15Result);
  108. parm.Add(Tqnqc15Results);
  109. }
  110. CoreClientParam ccp = new CoreClientParam();
  111. ccp.ServerName = "com.steering.mes.cost.FrmTqnqc15";
  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. Tqnqc15Entity entity = (Tqnqc15Entity)uRow.ListObject;
  142. entity.RecRevisor = UserInfo.GetUserName();
  143. string Tqnqc15ResultTity = JSONFormat.Format(entity);
  144. parm.Add(Tqnqc15ResultTity);
  145. }
  146. CoreClientParam ccp = new CoreClientParam();
  147. ccp.ServerName = "com.steering.mes.cost.FrmTqnqc15";
  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 name = "";
  168. string code = "";
  169. string yearmonth = "";
  170. if (chk_name.Checked == true)
  171. {
  172. name = text_name.Text;
  173. }
  174. if (chk_code.Checked == true)
  175. {
  176. code = text_code.Text;
  177. }
  178. if (chkTim.Checked == true)
  179. {
  180. yearmonth = this.StartTime.Value.ToString("yyyyMM");
  181. }
  182. List<Tqnqc15Entity> listSource = EntityHelper.GetData<Tqnqc15Entity>(
  183. "com.steering.mes.cost.FrmTqnqc15.doQuery", new object[] { name, code, yearmonth }, this.ob);
  184. tqnqc15EntityBindingSource.DataSource = listSource;
  185. // GridHelper.RefreshAndAutoSize(ultraGrid1);
  186. }
  187. public List<Tqnqc15Entity> ReadExcelToRtdTqnqc14Entity(string fileName)
  188. {
  189. //定义要返回的datatable对象
  190. List<Tqnqc15Entity> Tqnqc15Entitys = new List<Tqnqc15Entity>();
  191. try
  192. {
  193. if (!File.Exists(fileName))
  194. {
  195. return null;
  196. }
  197. //根据指定路径读取文件
  198. using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
  199. {
  200. //根据文件流创建excel数据结构
  201. NPOI.SS.UserModel.IWorkbook workbook = NPOI.SS.UserModel.WorkbookFactory.Create(fs);
  202. //IWorkbook workbook = new HSSFWorkbook(fs);
  203. for (int k = 0; k < workbook.NumberOfSheets; k++)
  204. {
  205. NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(k);
  206. if (sheet != null)
  207. {
  208. //最后一列的标号
  209. int rowCount = sheet.LastRowNum;
  210. for (int i = 1; i <= rowCount; ++i)
  211. {
  212. Tqnqc15Entity entity = new Tqnqc15Entity();
  213. NPOI.SS.UserModel.IRow row = sheet.GetRow(i);
  214. if (row == null) continue; //没有数据的行默认是null       
  215. if (row.Cells.Count < 6)
  216. {
  217. throw new Exception("无足够列");
  218. }
  219. string yearMonth = GetCellValue(row.Cells[0]);
  220. string mName = GetCellValue(row.Cells[1]);
  221. string mCode = GetCellValue(row.Cells[2]);
  222. if (yearMonth == "" || yearMonth == null)
  223. {
  224. throw new Exception("月份不能为空");
  225. }
  226. if (mName == "" || mName == null)
  227. {
  228. throw new Exception("物料名称不能为空");
  229. }
  230. if (mCode == "" || mCode == null)
  231. {
  232. throw new Exception("物料编码不能为空");
  233. }
  234. decimal alloyPrice;
  235. decimal alloyPriceNext;
  236. decimal alloyPriceNow;
  237. decimal.TryParse(GetCellValue(row.Cells[3]), out alloyPrice);
  238. decimal.TryParse(GetCellValue(row.Cells[4]), out alloyPriceNext);
  239. decimal.TryParse(GetCellValue(row.Cells[4]), out alloyPriceNow);
  240. if (alloyPrice == null || alloyPrice <= 0)
  241. {
  242. throw new Exception("当前市场采购价异常");
  243. }
  244. if (alloyPriceNext == null || alloyPriceNext <= 0)
  245. {
  246. throw new Exception("次月市场采购价异常");
  247. }
  248. if (alloyPriceNow == null || alloyPriceNow <= 0)
  249. {
  250. throw new Exception("当前库存价异常");
  251. }
  252. entity.YearMonth = yearMonth;
  253. entity.MaterialName = mName;
  254. entity.MaterialCode = mCode;
  255. entity.AlloyPrice = alloyPrice;
  256. entity.AlloyPriceNext = alloyPriceNext;
  257. entity.AlloyPriceNow = alloyPriceNow;
  258. entity.RecCreator = UserInfo.GetUserName();
  259. Tqnqc15Entitys.Add(entity);
  260. }
  261. }
  262. }
  263. }
  264. return Tqnqc15Entitys;
  265. }
  266. catch (Exception ex)
  267. {
  268. MessageBox.Show("EXCEL格式错误:" + ex.Message);
  269. return null;
  270. }
  271. }
  272. private string GetCellValue(ICell cell)
  273. {
  274. string value = "";
  275. switch (cell.CellType)
  276. {
  277. case CellType.Blank:
  278. value = "";
  279. break;
  280. case CellType.Numeric:
  281. short format = cell.CellStyle.DataFormat;
  282. value = cell.NumericCellValue.ToString2();
  283. break;
  284. case CellType.String:
  285. value = cell.StringCellValue;
  286. break;
  287. }
  288. return value;
  289. }
  290. }
  291. }