You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

104 lines
3.9 KiB

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
{
/// <summary>
/// AMESinTime
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class AMESInTimeTableController : Controller
{
private readonly AMESContext _context;
private readonly IConfiguration _config;
/// <summary>
/// 建構式
/// </summary>
/// <param name="context"></param>
public AMESInTimeTableController(AMESContext context, IConfiguration config)
{
_config = config;
_context = context;
}
/// <summary>
/// 寫入AMES_in_Time_Table
/// </summary>
/// <param name="aMESinTimeTable"></param>
/// <returns></returns>
[HttpPost]
public async Task<ResultModel<AMESInTimeTable>> AMESinTimeTable(AMESInTimeTable aMESInTimeTable)
{
ResultModel<AMESInTimeTable> result = new ResultModel<AMESInTimeTable>();
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;
}
}
}
}