NBEquipmentDebugController.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. using LeaRun.Application.Busines.NBManage;
  2. using LeaRun.Util;
  3. using LeaRun.Util.WebControl;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. using NPOI.HSSF.UserModel;
  7. using NPOI.XSSF.UserModel;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Data;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Web;
  14. using System.Web.Mvc;
  15. using System.Web.Script.Serialization;
  16. namespace LeaRun.Application.Web.Areas.NBManage.Controllers
  17. {
  18. public class NBEquipmentDebugController : MvcControllerBase
  19. {
  20. NBEquipmentDebugBLL bll = new NBEquipmentDebugBLL();
  21. DataTable dtexcel = new DataTable();
  22. LeaRun.Util.Log.Log log = Util.Log.LogFactory.GetLogger("NBEquipmentDebug");
  23. public ActionResult BatchRegistr()
  24. {
  25. return View();
  26. }
  27. public ActionResult DetailForm()
  28. {
  29. return View();
  30. }
  31. public ActionResult NetworkTest()
  32. {
  33. return View();
  34. }
  35. public ActionResult SyncData()
  36. {
  37. return View();
  38. }
  39. /// <summary>
  40. /// 获得数据
  41. /// </summary>
  42. /// <returns></returns>
  43. public ActionResult GetRecordJson()
  44. {
  45. string RecordJson = JsonConvert.SerializeObject(dtexcel);
  46. //string RecordJson = bll.GetRecordJson();
  47. return Content(RecordJson);
  48. }
  49. /// <summary>
  50. /// 联网测试
  51. /// </summary>
  52. /// <returns></returns>
  53. public ActionResult GetRecord(Pagination pagination, string imei = "", string thisDate = "", string lastDate = "", string address = "")
  54. {
  55. string RecordJson = bll.GetRecordJson(pagination, imei, thisDate, lastDate,address);
  56. return Content(RecordJson);
  57. }
  58. public ActionResult GetAjaxData(string deviceId)
  59. {
  60. //log.Debug("Method:GetAjaxData" + deviceId + "Tiem:" + DateTime.Now.ToString());
  61. JObject jobj = NBCommon.GetDeviceHistoryData(deviceId, Time.GetDate(-10), Time.GetDate(1));
  62. //log.Debug("4--" + jobj.ToString() + DateTime.Now.ToString() + Environment.NewLine);
  63. DataTable meterHistory = NBCommon.FormatJObjectHistoryData(jobj, "");
  64. //log.Debug("5--"+ meterHistory.ToJson() + DateTime.Now.ToString() + Environment.NewLine);
  65. return Content(meterHistory.ToJson());
  66. }
  67. #region 导入Excel 批量注册
  68. /// <summary>
  69. /// 导入excle
  70. /// </summary>
  71. /// <returns></returns>
  72. public ActionResult ImportExcel()
  73. {
  74. //1.接收客户端传过来的数据
  75. HttpPostedFileBase file = Request.Files["file"];
  76. if (file == null || file.ContentLength <= 0)
  77. {
  78. return Json("请选择要上传的Excel文件", JsonRequestBehavior.AllowGet);
  79. }
  80. //获取一个streamfile对象,该对象指向一个上传文件,
  81. Stream streamfile = file.InputStream;
  82. string FinName = Path.GetExtension(file.FileName);
  83. if (FinName != ".xls" && FinName != ".xlsx")
  84. {
  85. return Json("只能上传Excel文档", JsonRequestBehavior.AllowGet);
  86. }
  87. else
  88. {
  89. try
  90. {
  91. this.ImExport(FinName, streamfile);
  92. string jsonTable = JsonConvert.SerializeObject(dtexcel);
  93. return Content(jsonTable);
  94. }
  95. catch (Exception ex)
  96. {
  97. return Json("导入失败 !" + ex.Message, JsonRequestBehavior.AllowGet);
  98. }
  99. }
  100. }
  101. /// <summary>
  102. /// EXCEL 导入
  103. /// </summary>
  104. /// <param name="FinName"></param>
  105. /// <param name="streamfile"></param>
  106. public void ImExport(string FinName, Stream streamfile)
  107. {
  108. try
  109. {
  110. NPOI.SS.UserModel.ISheet sheet;
  111. if (FinName == ".xls")
  112. {
  113. //创建一个webbook,对应一个Excel文件(用于xls文件导入类)
  114. HSSFWorkbook hssfworkbook = new HSSFWorkbook(streamfile);
  115. // 在webbook中添加一个sheet,对应Excel文件中的sheet,取出第一个工作表,索引是0
  116. sheet = hssfworkbook.GetSheetAt(0);
  117. }
  118. else
  119. {
  120. XSSFWorkbook xssfworkbook = new XSSFWorkbook(streamfile);
  121. // 在webbook中添加一个sheet,对应Excel文件中的sheet,取出第一个工作表,索引是0
  122. sheet = xssfworkbook.GetSheetAt(0);
  123. }
  124. System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
  125. for (int j = 0; j < (sheet.GetRow(0).LastCellNum); j++)
  126. {
  127. dtexcel.Columns.Add(sheet.GetRow(0).Cells[j].ToString());
  128. }
  129. while (rows.MoveNext())
  130. {
  131. DataRow dr = dtexcel.NewRow();
  132. if (FinName == ".xls")
  133. {
  134. HSSFRow row = (HSSFRow)rows.Current;
  135. for (int i = 0; i < row.LastCellNum; i++)
  136. {
  137. NPOI.SS.UserModel.ICell cell = row.GetCell(i);
  138. if (cell == null)
  139. {
  140. dr[i] = null;
  141. }
  142. else
  143. {
  144. dr[i] = cell.ToString();
  145. }
  146. }
  147. }
  148. else
  149. {
  150. XSSFRow row = (XSSFRow)rows.Current;
  151. for (int i = 0; i < row.LastCellNum; i++)
  152. {
  153. NPOI.SS.UserModel.ICell cell = row.GetCell(i);
  154. if (cell == null)
  155. {
  156. dr[i] = null;
  157. }
  158. else
  159. {
  160. dr[i] = cell.ToString();
  161. }
  162. }
  163. }
  164. dtexcel.Rows.Add(dr);
  165. }
  166. dtexcel.Columns.Add("registerState", typeof(string));
  167. if (dtexcel != null && dtexcel.Rows.Count != 0)
  168. {
  169. for (int i = dtexcel.Rows.Count - 1; i >= 0; i--)
  170. {
  171. string IMEI = dtexcel.Rows[i]["IMEI"].ToString();
  172. string SIM = dtexcel.Rows[i]["SIM"].ToString();
  173. string MeterAddr = dtexcel.Rows[i]["MeterAddr"].ToString();
  174. if (string.IsNullOrEmpty(IMEI) || "IMEI".Equals(IMEI))
  175. {
  176. dtexcel.Rows[i].Delete();
  177. continue;
  178. }
  179. //查看是否已经注册过
  180. DataTable meterByIMEI = bll.GetBatchMeterByIMEI(IMEI);
  181. if (meterByIMEI.Rows.Count > 0)
  182. {
  183. dtexcel.Rows[i]["registerState"] = "注册失败,原因:此IMEI号已经注册过。";
  184. continue;
  185. }
  186. string resultCode = bll.SendCommand(IMEI, SIM, MeterAddr);//服务器返回消息
  187. JObject jObj = JsonConvert.DeserializeObject<JObject>(resultCode);
  188. if (null != jObj)
  189. {
  190. if (null != jObj["error_code"])
  191. {
  192. dtexcel.Rows[i]["registerState"] = "注册失败,原因:" + jObj["error_code"].ToString();
  193. }
  194. else if (null != jObj["deviceId"])
  195. {
  196. dtexcel.Rows[i]["registerState"] = "注册成功";
  197. }
  198. }
  199. else //(null==jObj1)
  200. {
  201. dtexcel.Rows[i]["registerState"] = "注册失败,原因未知";
  202. }
  203. }
  204. }
  205. }
  206. catch (Exception ex)
  207. {
  208. throw ex;
  209. }
  210. }
  211. #endregion
  212. #region 导入Excel 批量删除
  213. /// <summary>
  214. /// 导入excle
  215. /// </summary>
  216. /// <returns></returns>
  217. public ActionResult DeleteExcel()
  218. {
  219. //1.接收客户端传过来的数据
  220. HttpPostedFileBase file = Request.Files["file"];
  221. if (file == null || file.ContentLength <= 0)
  222. {
  223. return Json("请选择要上传的Excel文件", JsonRequestBehavior.AllowGet);
  224. }
  225. //获取一个streamfile对象,该对象指向一个上传文件,
  226. Stream streamfile = file.InputStream;
  227. string FinName = Path.GetExtension(file.FileName);
  228. if (FinName != ".xls" && FinName != ".xlsx")
  229. {
  230. return Json("只能上传Excel文档", JsonRequestBehavior.AllowGet);
  231. }
  232. else
  233. {
  234. try
  235. {
  236. this.DelExport(FinName, streamfile);
  237. string jsonTable = JsonConvert.SerializeObject(dtexcel);
  238. return Content(jsonTable);
  239. }
  240. catch (Exception ex)
  241. {
  242. return Json("导入失败 !" + ex.Message, JsonRequestBehavior.AllowGet);
  243. }
  244. }
  245. }
  246. /// <summary>
  247. /// EXCEL 导入
  248. /// </summary>
  249. /// <param name="FinName"></param>
  250. /// <param name="streamfile"></param>
  251. public void DelExport(string FinName, Stream streamfile)
  252. {
  253. try
  254. {
  255. NPOI.SS.UserModel.ISheet sheet;
  256. if (FinName == ".xls")
  257. {
  258. //创建一个webbook,对应一个Excel文件(用于xls文件导入类)
  259. HSSFWorkbook hssfworkbook = new HSSFWorkbook(streamfile);
  260. // 在webbook中添加一个sheet,对应Excel文件中的sheet,取出第一个工作表,索引是0
  261. sheet = hssfworkbook.GetSheetAt(0);
  262. }
  263. else
  264. {
  265. XSSFWorkbook xssfworkbook = new XSSFWorkbook(streamfile);
  266. // 在webbook中添加一个sheet,对应Excel文件中的sheet,取出第一个工作表,索引是0
  267. sheet = xssfworkbook.GetSheetAt(0);
  268. }
  269. System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
  270. for (int j = 0; j < (sheet.GetRow(0).LastCellNum); j++)
  271. {
  272. dtexcel.Columns.Add(sheet.GetRow(0).Cells[j].ToString());
  273. }
  274. while (rows.MoveNext())
  275. {
  276. DataRow dr = dtexcel.NewRow();
  277. if (FinName == ".xls")
  278. {
  279. HSSFRow row = (HSSFRow)rows.Current;
  280. for (int i = 0; i < row.LastCellNum; i++)
  281. {
  282. NPOI.SS.UserModel.ICell cell = row.GetCell(i);
  283. if (cell == null)
  284. {
  285. dr[i] = null;
  286. }
  287. else
  288. {
  289. dr[i] = cell.ToString();
  290. }
  291. }
  292. }
  293. else
  294. {
  295. XSSFRow row = (XSSFRow)rows.Current;
  296. for (int i = 0; i < row.LastCellNum; i++)
  297. {
  298. NPOI.SS.UserModel.ICell cell = row.GetCell(i);
  299. if (cell == null)
  300. {
  301. dr[i] = null;
  302. }
  303. else
  304. {
  305. dr[i] = cell.ToString();
  306. }
  307. }
  308. }
  309. dtexcel.Rows.Add(dr);
  310. }
  311. dtexcel.Columns.Add("registerState", typeof(string));
  312. if (dtexcel != null && dtexcel.Rows.Count != 0)
  313. {
  314. for (int i = dtexcel.Rows.Count - 1; i >= 0; i--)
  315. {
  316. string IMEI = dtexcel.Rows[i]["IMEI"].ToString();
  317. string SIM = dtexcel.Rows[i]["ICCID"].ToString();
  318. string DeviceID = dtexcel.Rows[i]["DeviceID"].ToString();
  319. string MeterAddr = dtexcel.Rows[i]["表地址"].ToString();
  320. if (string.IsNullOrEmpty(IMEI) || "IMEI".Equals(IMEI))
  321. {
  322. dtexcel.Rows[i].Delete();
  323. continue;
  324. }
  325. //查看是否已删除过
  326. DataTable meterByIMEI = bll.GetBatchMeterByDeviceID(DeviceID);
  327. if (meterByIMEI.Rows.Count > 0)
  328. {
  329. dtexcel.Rows[i]["registerState"] = "删除失败,原因:此水表已经删除过。";
  330. continue;
  331. }
  332. Int32 resultCode = bll.DelMeter(IMEI, SIM, MeterAddr, DeviceID);//服务器返回消息
  333. if (resultCode==1)
  334. {
  335. dtexcel.Rows[i]["registerState"] = "删除成功";
  336. }
  337. else //(null==jObj1)
  338. {
  339. dtexcel.Rows[i]["registerState"] = "删除失败";
  340. }
  341. }
  342. }
  343. }
  344. catch (Exception ex)
  345. {
  346. throw ex;
  347. }
  348. }
  349. #endregion
  350. /// <summary>
  351. /// 导出excle
  352. /// </summary>
  353. /// <returns></returns>
  354. public FileResult ExportExcel(string datavalue)
  355. {
  356. return null;
  357. }
  358. #region 删除表具信息
  359. /// <summary>
  360. /// 删除表具信息
  361. /// </summary>
  362. /// <param name="id"></param>
  363. /// <returns></returns>
  364. public ActionResult DeleteMeterForm(string id)
  365. {
  366. switch (bll.DeleteMeterForm(id))
  367. {
  368. case 1:
  369. return Success("删除成功。");
  370. case 0:
  371. return Error("删除失败。");
  372. default:
  373. return Error("删除失败。");
  374. }
  375. }
  376. #endregion
  377. /// <summary>
  378. /// 表具 Form
  379. /// </summary>
  380. /// <returns></returns>
  381. public ActionResult MeterForm()
  382. {
  383. return View();
  384. }
  385. #region 获取表具信息
  386. /// <summary>
  387. /// 获取表具信息
  388. /// </summary>
  389. /// <param name="data"></param>
  390. /// <returns></returns>
  391. public ActionResult GetMeterByMeterId(string keyValue)
  392. {
  393. DataTable dtMeter = bll.GetMeterByMeterId(keyValue);
  394. return Content(LeaRun.Util.Json.ToJson(dtMeter));
  395. }
  396. #endregion
  397. #region 保存表具数据
  398. /// <summary>
  399. /// 保存表具数据
  400. /// </summary>
  401. /// <param name="keyValue"></param>
  402. /// <param name="data"></param>
  403. /// <returns></returns>
  404. [HttpPost]
  405. [ValidateAntiForgeryToken]
  406. [AjaxOnly]
  407. public ActionResult SaveMeterForm(string keyValue, string meterAddr, string imei, string sim)
  408. {
  409. switch (bll.EditMeterForm(keyValue, meterAddr, imei, sim))
  410. {
  411. case 0:
  412. return Success("保存成功。");
  413. case 1:
  414. return Error("修改失败。");
  415. case 2:
  416. return Error("重复数据。");
  417. default:
  418. return Error("修改失败。");
  419. }
  420. }
  421. #endregion
  422. #region 导入Excel 数据同步
  423. /// <summary>
  424. /// 导入excle
  425. /// </summary>
  426. /// <returns></returns>
  427. public ActionResult ImportExcelForSync()
  428. {
  429. //1.接收客户端传过来的数据
  430. HttpPostedFileBase file = Request.Files["file"];
  431. if (file == null || file.ContentLength <= 0)
  432. {
  433. return Json("请选择要上传的Excel文件", JsonRequestBehavior.AllowGet);
  434. }
  435. //获取一个streamfile对象,该对象指向一个上传文件,
  436. Stream streamfile = file.InputStream;
  437. string fileName = Path.GetExtension(file.FileName);
  438. if (fileName != ".xls" && fileName != ".xlsx")
  439. {
  440. return Json("只能上传Excel文档", JsonRequestBehavior.AllowGet);
  441. }
  442. else
  443. {
  444. try
  445. {
  446. ImExportForSync(fileName, streamfile);
  447. string jsonTable = JsonConvert.SerializeObject(dtexcel);
  448. string json1 = jsonTable;
  449. return Content(json1);
  450. }
  451. catch (Exception ex)
  452. {
  453. return Json("导入失败 !" + ex.Message, JsonRequestBehavior.AllowGet);
  454. }
  455. }
  456. }
  457. ///<summary>
  458. /// 两种不同版本的操作excel
  459. /// 扩展名*.xls
  460. /// </summary>
  461. public void ImExportForSync(string fileName, Stream streamfile)
  462. {
  463. NPOI.SS.UserModel.ISheet sheet;
  464. if (fileName == ".xls")
  465. {
  466. //创建一个webbook,对应一个Excel文件(用于xls文件导入类)
  467. HSSFWorkbook hssfworkbook = new HSSFWorkbook(streamfile);
  468. sheet = hssfworkbook.GetSheetAt(0);
  469. }
  470. else
  471. {
  472. XSSFWorkbook xssfworkbook = new XSSFWorkbook(streamfile);
  473. sheet = xssfworkbook.GetSheetAt(0);
  474. }
  475. sheet.ShiftRows(1, sheet.LastRowNum, -1);
  476. System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
  477. for (int j = 0; j < (sheet.GetRow(1).LastCellNum); j++)
  478. {
  479. dtexcel.Columns.Add(sheet.GetRow(0).Cells[j].ToString());
  480. }
  481. while (rows.MoveNext())
  482. {
  483. DataRow dr = dtexcel.NewRow();
  484. if (fileName == ".xls")
  485. {
  486. HSSFRow row = (HSSFRow)rows.Current;
  487. for (int i = 0; i < row.LastCellNum; i++)
  488. {
  489. NPOI.SS.UserModel.ICell cell = row.GetCell(i);
  490. if (cell == null)
  491. {
  492. dr[i] = null;
  493. }
  494. else
  495. {
  496. dr[i] = cell.ToString();
  497. }
  498. }
  499. }
  500. else
  501. {
  502. XSSFRow row = (XSSFRow)rows.Current;
  503. for (int i = 0; i < row.LastCellNum; i++)
  504. {
  505. NPOI.SS.UserModel.ICell cell = row.GetCell(i);
  506. if (cell == null)
  507. {
  508. dr[i] = null;
  509. }
  510. else
  511. {
  512. dr[i] = cell.ToString();
  513. }
  514. }
  515. }
  516. dtexcel.Rows.Add(dr);
  517. }
  518. dtexcel.Columns.Add("syncState", typeof(string));
  519. if (dtexcel != null && dtexcel.Rows.Count != 0)
  520. {
  521. for (int i = dtexcel.Rows.Count - 1; i >= 0; i--)
  522. {
  523. if (string.IsNullOrEmpty(dtexcel.Rows[i]["IMEI"].ToString()) || "IMEI".Equals(dtexcel.Rows[i]["IMEI"].ToString().Trim()))
  524. {
  525. dtexcel.Rows[i].Delete();
  526. continue;
  527. }
  528. string area = dtexcel.Rows[i]["小区"].ToString();
  529. string bldg = dtexcel.Rows[i]["楼宇"].ToString();
  530. string apmt = dtexcel.Rows[i]["单元"].ToString();
  531. string room = dtexcel.Rows[i]["户号"].ToString();
  532. string IMEI = dtexcel.Rows[i]["IMEI"].ToString();
  533. string SIM = dtexcel.Rows[i]["ICCID"].ToString();
  534. string MeterAddr = dtexcel.Rows[i]["表地址"].ToString();
  535. string DeviceID = dtexcel.Rows[i]["DeviceID"].ToString();
  536. int resultCode = 0;
  537. if (!string.IsNullOrEmpty(IMEI))
  538. {
  539. resultCode = bll.SendCommandForSync(area, bldg, apmt, room, IMEI, SIM, MeterAddr, DeviceID);//服务器返回消息
  540. if (resultCode == 1)
  541. {
  542. dtexcel.Rows[i]["syncState"] = "同步成功";
  543. }
  544. else
  545. {
  546. dtexcel.Rows[i]["syncState"] = "同步失败";
  547. }
  548. }
  549. }
  550. }
  551. }
  552. #endregion
  553. /// <summary>
  554. /// 表具 Form
  555. /// </summary>
  556. /// <returns></returns>
  557. public ActionResult NetworkBDelete()
  558. {
  559. return View();
  560. }
  561. }
  562. }