using AMESCoreStudio.CommonTools.Result; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System.Threading.Tasks; using System; using Microsoft.Extensions.Configuration; using System.Linq; using Dapper; using System.Data; using System.Collections.Generic; using System.Data.SqlClient; using AMESCoreStudio.WebApi.DTO.AVLE; using AMESCoreStudio.WebApi.Models.AMES; namespace AMESCoreStudio.WebApi.Controllers.AVLE { /// /// AMESinTime /// [Route("api/[controller]")] [ApiController] public class AMESInTimeTableController : Controller { private readonly AMESContext _context; private readonly IConfiguration _config; /// /// 建構式 /// /// public AMESInTimeTableController(AMESContext context, IConfiguration config) { _config = config; _context = context; } /// /// 寫入AMES_in_Time_Table /// /// /// [HttpPost] public async Task> AMESinTimeTable(AMESInTimeTable aMESInTimeTable) { ResultModel result = new ResultModel(); try { var query = from q1 in _context.BarcodeInfoes.Where(w => w.BarcodeNo == aMESInTimeTable.BarcodeNO) join q2 in _context.BarcodeStation.Where(w => w.StationID == aMESInTimeTable.StationID && w.LineId == aMESInTimeTable.Line && w.CreateUserID == aMESInTimeTable.UserID) on q1.BarcodeID equals q2.BarcodeID join q3 in _context.WipInfos.Where(w => w.WipNO == aMESInTimeTable.Wip) on q1.WipID equals q3.WipID select q2.CreateDate; if (query.Count() == 0 ) { result.Success = false; result.Msg = "Error: 查不到出站時間"; return result; } else { var maxCreateDate = query.DefaultIfEmpty().Max(); // 如果没有记录,返回默认值(null) // 如果没有找到匹配的记录,返回默认日期或者 null aMESInTimeTable.InTime = maxCreateDate; var context = _config.GetConnectionString("AVLEContext"); using (IDbConnection _AVLE_context = new SqlConnection(context)) { DynamicParameters p = new DynamicParameters(); string sql = @"Insert into [iFactory].[PE].[AMES_In_Time_Table] (Record_Time, WIP, SN, IN_TIME) VALUES (@RECODE_TIME, @WIP, @SN, @IN_TIME)"; p.Add("RECODE_TIME", System.DateTime.Now, DbType.DateTime); p.Add("WIP", aMESInTimeTable.Wip, DbType.String); p.Add("SN", aMESInTimeTable.SN, DbType.String); p.Add("IN_TIME", aMESInTimeTable.InTime, DbType.DateTime); var q = await _AVLE_context.ExecuteAsync(sql, p); result.Success = true; result.Msg = "OK"; return result; } } } catch (Exception ex) { result.Success = false; if (ex.InnerException != null) { result.Msg = $"Error: {ex.InnerException.Message}"; } else { result.Msg = $"Error: {ex.ToString()}"; } return result; } } } }