using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using MES.Service.Dto.service;
using MES.Service.Modes;
using MES.Service.service;
using MES.Service.service.QC;
using MES.Service.util;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace MESApplication.Controllers.QC;
///
/// SPI/AOI检测数据控制器
///
[Route("api/[controller]")]
[ApiController]
public class SpiAoiController : ControllerBase
{
private readonly MessageCenterManager _manager = new();
private readonly SpiAoiService _service = new();
private const string METHOD = "POST";
private const string HeaderTableName = "MES_SPI_AOI_HEADER";
private const string DetailTableName = "MES_SPI_AOI_DETAIL";
private const string BaseUrl = "http://localhost:10054/api/QC/SpiAoi/";
///
/// Upload AOI header data.
///
/// AOI header payload.
/// Upload result.
[HttpPost("UploadAoiHeader")]
public ResponseResult UploadAoiHeader(
[FromBody] SpiAoiHeaderDto header)
{
var entity = new MessageCenter
{
TableName = HeaderTableName,
Url = BaseUrl + "UploadAoiHeader",
Method = METHOD,
Data = JsonConvert.SerializeObject(header),
Status = 1,
CreateBy = "SPI_AOI_SYSTEM"
};
try
{
var response = _service.UploadAoiHeader(header);
dynamic resultInfos = new ExpandoObject();
resultInfos.headerId = response.HeaderId;
resultInfos.message = "AOI header upload succeeded";
entity.Result = 1;
entity.DealWith = 1;
entity.ResultData = JsonConvert.SerializeObject(response);
_manager.save(entity);
return new ResponseResult
{
status = 0,
message = "OK",
data = resultInfos
};
}
catch (Exception ex)
{
entity.Result = 0;
entity.DealWith = 0;
entity.ResultData = ex.Message;
_manager.save(entity);
return ResponseResult.ResponseError(ex);
}
}
///
/// Batch upload AOI header data.
///
/// AOI header payload collection.
/// Upload result.
[HttpPost("UploadAoiHeaderBatch")]
public ResponseResult UploadAoiHeaderBatch(
[FromBody] List headers)
{
var entity = new MessageCenter
{
TableName = HeaderTableName,
Url = BaseUrl + "UploadAoiHeaderBatch",
Method = METHOD,
Data = JsonConvert.SerializeObject(headers),
Status = 1,
CreateBy = "SPI_AOI_SYSTEM"
};
try
{
var responses = _service.UploadAoiHeaderBatch(headers);
dynamic resultInfos = new ExpandoObject();
resultInfos.headerIds = responses.Select(r => r.HeaderId).ToList();
resultInfos.message = "AOI header batch upload succeeded";
entity.Result = 1;
entity.DealWith = 1;
entity.ResultData = JsonConvert.SerializeObject(responses);
_manager.save(entity);
return new ResponseResult
{
status = 0,
message = "OK",
data = resultInfos
};
}
catch (Exception ex)
{
entity.Result = 0;
entity.DealWith = 0;
entity.ResultData = ex.Message;
_manager.save(entity);
return ResponseResult.ResponseError(ex);
}
}
///
/// Upload SPI detail data.
///
/// SPI detail payload.
/// Upload result.
[HttpPost("UploadSpiDetails")]
public ResponseResult UploadSpiDetails(
[FromBody] SpiAoiDetailDto request)
{
var entity = new MessageCenter
{
TableName = DetailTableName,
Url = BaseUrl + "UploadSpiDetails",
Method = METHOD,
Data = JsonConvert.SerializeObject(request),
Status = 1,
CreateBy = "SPI_AOI_SYSTEM"
};
try
{
var response = _service.UploadSpiDetails(request);
dynamic resultInfos = new ExpandoObject();
resultInfos.detailCount = response.DetailCount;
resultInfos.message = "SPI detail upload succeeded";
entity.Result = 1;
entity.DealWith = 1;
entity.ResultData = JsonConvert.SerializeObject(response);
_manager.save(entity);
return new ResponseResult
{
status = 0,
message = "OK",
data = resultInfos
};
}
catch (Exception ex)
{
entity.Result = 0;
entity.DealWith = 0;
entity.ResultData = ex.Message;
_manager.save(entity);
return ResponseResult.ResponseError(ex);
}
}
///
/// Batch upload SPI detail data.
///
/// SPI detail payload collection.
/// Upload result.
[HttpPost("UploadSpiDetailsBatch")]
public ResponseResult UploadSpiDetailsBatch(
[FromBody] List requests)
{
var entity = new MessageCenter
{
TableName = DetailTableName,
Url = BaseUrl + "UploadSpiDetailsBatch",
Method = METHOD,
Data = JsonConvert.SerializeObject(requests),
Status = 1,
CreateBy = "SPI_AOI_SYSTEM"
};
try
{
var response = _service.UploadSpiDetailsBatch(requests);
dynamic resultInfos = new ExpandoObject();
resultInfos.detailCount = response.DetailCount;
resultInfos.message = "SPI detail batch upload succeeded";
entity.Result = 1;
entity.DealWith = 1;
entity.ResultData = JsonConvert.SerializeObject(response);
_manager.save(entity);
return new ResponseResult
{
status = 0,
message = "OK",
data = resultInfos
};
}
catch (Exception ex)
{
entity.Result = 0;
entity.DealWith = 0;
entity.ResultData = ex.Message;
_manager.save(entity);
return ResponseResult.ResponseError(ex);
}
}
///
/// 根据条码查询SPI/AOI检测数据
///
/// 请求参数
/// 检测数据
[HttpPost("GetByBarcode")]
public ResponseResult GetByBarcode([FromBody] JObject request)
{
try
{
var boardBarcode = request["boardBarcode"]?.ToString();
if (StringUtil.IsNullOrEmpty(boardBarcode))
{
return new ResponseResult
{
status = 1,
message = "boardBarcode 不能为空",
data = null
};
}
var (header, details) = _service.GetByBarcode(boardBarcode);
if (header == null)
{
return new ResponseResult
{
status = 1,
message = $"未找到条码 {boardBarcode} 的检测数据",
data = null
};
}
dynamic resultInfos = new ExpandoObject();
resultInfos.header = header;
resultInfos.details = details;
return new ResponseResult
{
status = 0,
message = "OK",
data = resultInfos
};
}
catch (Exception ex)
{
return ResponseResult.ResponseError(ex);
}
}
///
/// 根据ID查询SPI/AOI检测数据
///
/// 请求参数
/// 检测数据
[HttpPost("GetById")]
public ResponseResult GetById([FromBody] JObject request)
{
try
{
var headerId = request["headerId"]?.ToObject();
if (!headerId.HasValue)
{
return new ResponseResult
{
status = 1,
message = "headerId 不能为空",
data = null
};
}
var (header, details) = _service.GetById(headerId.Value);
if (header == null)
{
return new ResponseResult
{
status = 1,
message = $"未找到ID {headerId} 的检测数据",
data = null
};
}
dynamic resultInfos = new ExpandoObject();
resultInfos.header = header;
resultInfos.details = details;
return new ResponseResult
{
status = 0,
message = "OK",
data = resultInfos
};
}
catch (Exception ex)
{
return ResponseResult.ResponseError(ex);
}
}
///
/// 分页查询SPI/AOI检测数据
///
/// 查询请求
/// 分页数据
[HttpPost("GetPage")]
public ResponseResult GetPage([FromBody] JObject request)
{
try
{
var boardBarcode = request["boardBarcode"]?.ToString();
var workOrder = request["workOrder"]?.ToString();
var surface = request["surface"]?.ToString();
var startDate = request["startDate"]?.ToString();
var endDate = request["endDate"]?.ToString();
var pageIndex = request["pageIndex"]?.ToObject() ?? 1;
var pageSize = request["pageSize"]?.ToObject() ?? 20;
DateTime? startDateTime = null;
DateTime? endDateTime = null;
if (StringUtil.IsNotNullOrEmpty(startDate) &&
DateTime.TryParse(startDate, out var start))
{
startDateTime = start;
}
if (StringUtil.IsNotNullOrEmpty(endDate) &&
DateTime.TryParse(endDate, out var end))
{
endDateTime = end;
}
var (items, totalCount) = _service.GetPage(
boardBarcode, workOrder, surface, startDateTime, endDateTime,
pageIndex, pageSize);
dynamic resultInfos = new ExpandoObject();
resultInfos.items = items;
return new ResponseResult
{
status = 0,
message = "OK",
data = resultInfos,
TotalCount = totalCount
};
}
catch (Exception ex)
{
return ResponseResult.ResponseError(ex);
}
}
}