using EInk.Dtos; using EInk.Models; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using SqlSugar; using System; using System.ComponentModel; using System.Xml.Linq; using System; using System.Threading; using System.Text; namespace EInk.Controllers { [Route("api/[controller]")] [ApiController] public class Lot3Controller : ControllerBase { private readonly ISqlSugarClient _db; public Lot3Controller(ISqlSugarClient db) { _db = db; } // POST api/ [HttpPost("AccessLot3")] public IActionResult Lot3Data(string Lot3Data) { try { if (String.IsNullOrEmpty(Lot3Data)) { return Ok(ResultDto.Fail("Components is null")); } Lot2Lot3Dto lot3Dto = new Lot2Lot3Dto(); try { lot3Dto = JsonConvert.DeserializeObject(Lot3Data); } catch (Exception) { return Ok(ResultDto.Fail("Incorrect Json format")); } try { lot3Dto.Valid(); } catch (Exception ex) { return Ok(ResultDto.Fail(ex.Message)); } //int id = _db.Ado.GetInt("select max(count) from lot2lot3_info"); Lot2Lot3Model lot3model = new() { //id = id + 1, lot_id = lot3Dto.id, //判断数据是否重复插入,重复插入is_send标记为3 is_send = _db.Ado.GetInt(string.Format("select count(*) from lot2lot3_info where lot_id='{0}'", lot3Dto.id)) == 0 ? 0 : 3, content = Lot3Data, create_time = DateTime.Now }; _db.Insertable(lot3model).ExecuteCommand(); return Ok(ResultDto.Success()); } catch (Exception ex) { return Ok(ResultDto.Fail("error:" + ex.Message)); } } // POST api/ [HttpPost("UpdateDefectInfo")] public IActionResult UpdateDefectInfo(IFormFile file) { if (file == null || file.Length == 0) { return Ok(ResultDto.Fail("No file selected")); } string line; try { _db.Ado.BeginTran(); _db.Deleteable().ExecuteCommand(); using (var reader = new StreamReader(file.OpenReadStream(), Encoding.GetEncoding("gb2312"))) { var header = reader.ReadLine(); var headers = header.Split(','); while ((line = reader.ReadLine()) != null) { string[] linelist = line.Split(","); _db.Insertable(new DefectModel { id = Convert.ToInt32(linelist[0]), defect_code = linelist[1], defect_name = linelist[2], classification = linelist[3].Replace(';', ',') }).ExecuteCommand(); } } _db.Ado.CommitTran(); } catch(Exception ex) { _db.Ado.RollbackTran(); return Ok(ResultDto.Fail(ex.Message)); } return Ok(ResultDto.Success()); } } }