Lot3Controller.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using EInk.Dtos;
  2. using EInk.Models;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Newtonsoft.Json;
  5. using SqlSugar;
  6. using System;
  7. using System.ComponentModel;
  8. using System.Xml.Linq;
  9. using System;
  10. using System.Threading;
  11. using System.Text;
  12. namespace EInk.Controllers
  13. {
  14. [Route("api/[controller]")]
  15. [ApiController]
  16. public class Lot3Controller : ControllerBase
  17. {
  18. private readonly ISqlSugarClient _db;
  19. public Lot3Controller(ISqlSugarClient db)
  20. {
  21. _db = db;
  22. }
  23. // POST api/<Lot3Controller>
  24. [HttpPost("AccessLot3")]
  25. public IActionResult Lot3Data(string Lot3Data)
  26. {
  27. try
  28. {
  29. if (String.IsNullOrEmpty(Lot3Data))
  30. {
  31. return Ok(ResultDto.Fail("Components is null"));
  32. }
  33. Lot2Lot3Dto lot3Dto = new Lot2Lot3Dto();
  34. try
  35. {
  36. lot3Dto = JsonConvert.DeserializeObject<Lot2Lot3Dto>(Lot3Data);
  37. }
  38. catch (Exception)
  39. {
  40. return Ok(ResultDto.Fail("Incorrect Json format"));
  41. }
  42. try
  43. {
  44. lot3Dto.Valid();
  45. }
  46. catch (Exception ex)
  47. {
  48. return Ok(ResultDto.Fail(ex.Message));
  49. }
  50. //int id = _db.Ado.GetInt("select max(count) from lot2lot3_info");
  51. Lot2Lot3Model lot3model = new()
  52. {
  53. //id = id + 1,
  54. lot_id = lot3Dto.id,
  55. //判断数据是否重复插入,重复插入is_send标记为3
  56. is_send = _db.Ado.GetInt(string.Format("select count(*) from lot2lot3_info where lot_id='{0}'", lot3Dto.id)) == 0 ? 0 : 3,
  57. content = Lot3Data,
  58. create_time = DateTime.Now
  59. };
  60. _db.Insertable(lot3model).ExecuteCommand();
  61. return Ok(ResultDto.Success());
  62. }
  63. catch (Exception ex)
  64. {
  65. return Ok(ResultDto.Fail("error:" + ex.Message));
  66. }
  67. }
  68. // POST api/<Lot3Controller>
  69. [HttpPost("UpdateDefectInfo")]
  70. public IActionResult UpdateDefectInfo(IFormFile file)
  71. {
  72. if (file == null || file.Length == 0)
  73. {
  74. return Ok(ResultDto.Fail("No file selected"));
  75. }
  76. string line;
  77. try
  78. {
  79. _db.Ado.BeginTran();
  80. _db.Deleteable<DefectModel>().ExecuteCommand();
  81. using (var reader = new StreamReader(file.OpenReadStream(), Encoding.GetEncoding("gb2312")))
  82. {
  83. var header = reader.ReadLine();
  84. var headers = header.Split(',');
  85. while ((line = reader.ReadLine()) != null)
  86. {
  87. string[] linelist = line.Split(",");
  88. _db.Insertable(new DefectModel
  89. {
  90. id = Convert.ToInt32(linelist[0]),
  91. defect_code = linelist[1],
  92. defect_name = linelist[2],
  93. classification = linelist[3].Replace(';', ',')
  94. }).ExecuteCommand();
  95. }
  96. }
  97. _db.Ado.CommitTran();
  98. }
  99. catch(Exception ex)
  100. {
  101. _db.Ado.RollbackTran();
  102. return Ok(ResultDto.Fail(ex.Message));
  103. }
  104. return Ok(ResultDto.Success());
  105. }
  106. }
  107. }